2 * Copyright (c) 2000-2018 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 uint64_t vm_cs_defer_to_pmap_cs
= 0;
213 uint64_t vm_cs_defer_to_pmap_cs_not
= 0;
216 void vm_pre_fault(vm_map_offset_t
, vm_prot_t
);
218 extern char *kdp_compressor_decompressed_page
;
219 extern addr64_t kdp_compressor_decompressed_page_paddr
;
220 extern ppnum_t kdp_compressor_decompressed_page_ppnum
;
226 vm_rtfault_record_t
*vm_rtf_records
;
228 #define VMRTF_DEFAULT_BUFSIZE (4096)
229 #define VMRTF_NUM_RECORDS_DEFAULT (VMRTF_DEFAULT_BUFSIZE / sizeof(vm_rtfault_record_t))
230 int vmrtf_num_records
= VMRTF_NUM_RECORDS_DEFAULT
;
232 static void vm_rtfrecord_lock(void);
233 static void vm_rtfrecord_unlock(void);
234 static void vm_record_rtfault(thread_t
, uint64_t, vm_map_offset_t
, int);
236 lck_spin_t vm_rtfr_slock
;
237 extern lck_grp_t vm_page_lck_grp_bucket
;
238 extern lck_attr_t vm_page_lck_attr
;
241 * Routine: vm_fault_init
243 * Initialize our private data structures.
248 int i
, vm_compressor_temp
;
249 boolean_t need_default_val
= TRUE
;
251 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
252 * computed as a percentage of available memory, and the percentage used is scaled inversely with
253 * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems
254 * and reduce the value down to 10% for very large memory configurations. This helps give us a
255 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
256 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
259 vm_hard_throttle_threshold
= sane_size
* (35 - MIN((int)(sane_size
/ (1024 * 1024 * 1024)), 25)) / 100;
262 * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry.
265 if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp
, sizeof(vm_compressor_temp
))) {
266 for (i
= 0; i
< VM_PAGER_MAX_MODES
; i
++) {
267 if (vm_compressor_temp
> 0 &&
268 ((vm_compressor_temp
& (1 << i
)) == vm_compressor_temp
)) {
269 need_default_val
= FALSE
;
270 vm_compressor_mode
= vm_compressor_temp
;
274 if (need_default_val
) {
275 printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp
);
278 if (need_default_val
) {
279 /* If no boot arg or incorrect boot arg, try device tree. */
280 PE_get_default("kern.vm_compressor", &vm_compressor_mode
, sizeof(vm_compressor_mode
));
282 printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode
);
284 PE_parse_boot_argn("vm_protect_privileged_from_untrusted", &vm_protect_privileged_from_untrusted
, sizeof(vm_protect_privileged_from_untrusted
));
288 vm_rtfault_record_init(void)
290 PE_parse_boot_argn("vm_rtfault_records", &vmrtf_num_records
, sizeof(vmrtf_num_records
));
292 assert(vmrtf_num_records
>= 1);
293 vmrtf_num_records
= MAX(vmrtf_num_records
, 1);
294 size_t kallocsz
= vmrtf_num_records
* sizeof(vm_rtfault_record_t
);
295 vmrtfrs
.vm_rtf_records
= kalloc(kallocsz
);
296 bzero(vmrtfrs
.vm_rtf_records
, kallocsz
);
297 vmrtfrs
.vmrtfr_maxi
= vmrtf_num_records
- 1;
298 lck_spin_init(&vm_rtfr_slock
, &vm_page_lck_grp_bucket
, &vm_page_lck_attr
);
301 * Routine: vm_fault_cleanup
303 * Clean up the result of vm_fault_page.
305 * The paging reference for "object" is released.
306 * "object" is unlocked.
307 * If "top_page" is not null, "top_page" is
308 * freed and the paging reference for the object
309 * containing it is released.
312 * "object" must be locked.
319 vm_object_paging_end(object
);
320 vm_object_unlock(object
);
322 if (top_page
!= VM_PAGE_NULL
) {
323 object
= VM_PAGE_OBJECT(top_page
);
325 vm_object_lock(object
);
326 VM_PAGE_FREE(top_page
);
327 vm_object_paging_end(object
);
328 vm_object_unlock(object
);
332 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
335 boolean_t vm_page_deactivate_behind
= TRUE
;
337 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
339 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
340 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
341 /* we use it to size an array on the stack */
343 int vm_default_behind
= VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW
;
345 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
348 * vm_page_is_sequential
350 * Determine if sequential access is in progress
351 * in accordance with the behavior specified.
352 * Update state to indicate current access pattern.
354 * object must have at least the shared lock held
358 vm_fault_is_sequential(
360 vm_object_offset_t offset
,
361 vm_behavior_t behavior
)
363 vm_object_offset_t last_alloc
;
367 last_alloc
= object
->last_alloc
;
368 sequential
= object
->sequential
;
369 orig_sequential
= sequential
;
372 case VM_BEHAVIOR_RANDOM
:
374 * reset indicator of sequential behavior
379 case VM_BEHAVIOR_SEQUENTIAL
:
380 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
382 * advance indicator of sequential behavior
384 if (sequential
< MAX_SEQUENTIAL_RUN
) {
385 sequential
+= PAGE_SIZE
;
389 * reset indicator of sequential behavior
395 case VM_BEHAVIOR_RSEQNTL
:
396 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
398 * advance indicator of sequential behavior
400 if (sequential
> -MAX_SEQUENTIAL_RUN
) {
401 sequential
-= PAGE_SIZE
;
405 * reset indicator of sequential behavior
411 case VM_BEHAVIOR_DEFAULT
:
413 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
415 * advance indicator of sequential behavior
417 if (sequential
< 0) {
420 if (sequential
< MAX_SEQUENTIAL_RUN
) {
421 sequential
+= PAGE_SIZE
;
423 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
425 * advance indicator of sequential behavior
427 if (sequential
> 0) {
430 if (sequential
> -MAX_SEQUENTIAL_RUN
) {
431 sequential
-= PAGE_SIZE
;
435 * reset indicator of sequential behavior
441 if (sequential
!= orig_sequential
) {
442 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
444 * if someone else has already updated object->sequential
445 * don't bother trying to update it or object->last_alloc
451 * I'd like to do this with a OSCompareAndSwap64, but that
452 * doesn't exist for PPC... however, it shouldn't matter
453 * that much... last_alloc is maintained so that we can determine
454 * if a sequential access pattern is taking place... if only
455 * one thread is banging on this object, no problem with the unprotected
456 * update... if 2 or more threads are banging away, we run the risk of
457 * someone seeing a mangled update... however, in the face of multiple
458 * accesses, no sequential access pattern can develop anyway, so we
459 * haven't lost any real info.
461 object
->last_alloc
= offset
;
465 int vm_page_deactivate_behind_count
= 0;
468 * vm_page_deactivate_behind
470 * Determine if sequential access is in progress
471 * in accordance with the behavior specified. If
472 * so, compute a potential page to deactivate and
475 * object must be locked.
477 * return TRUE if we actually deactivate a page
481 vm_fault_deactivate_behind(
483 vm_object_offset_t offset
,
484 vm_behavior_t behavior
)
487 int pages_in_run
= 0;
488 int max_pages_in_run
= 0;
490 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
491 vm_object_offset_t run_offset
= 0;
492 vm_object_offset_t pg_offset
= 0;
494 vm_page_t page_run
[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
];
498 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
501 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
503 * Do not deactivate pages from the kernel object: they
504 * are not intended to become pageable.
505 * or we've disabled the deactivate behind mechanism
509 if ((sequential_run
= object
->sequential
)) {
510 if (sequential_run
< 0) {
511 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
512 sequential_run
= 0 - sequential_run
;
514 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
518 case VM_BEHAVIOR_RANDOM
:
520 case VM_BEHAVIOR_SEQUENTIAL
:
521 if (sequential_run
>= (int)PAGE_SIZE
) {
522 run_offset
= 0 - PAGE_SIZE_64
;
523 max_pages_in_run
= 1;
526 case VM_BEHAVIOR_RSEQNTL
:
527 if (sequential_run
>= (int)PAGE_SIZE
) {
528 run_offset
= PAGE_SIZE_64
;
529 max_pages_in_run
= 1;
532 case VM_BEHAVIOR_DEFAULT
:
534 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
537 * determine if the run of sequential accesss has been
538 * long enough on an object with default access behavior
539 * to consider it for deactivation
541 if ((uint64_t)sequential_run
>= behind
&& (sequential_run
% (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
* PAGE_SIZE
)) == 0) {
543 * the comparisons between offset and behind are done
544 * in this kind of odd fashion in order to prevent wrap around
547 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
548 if (offset
>= behind
) {
549 run_offset
= 0 - behind
;
550 pg_offset
= PAGE_SIZE_64
;
551 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
554 if (offset
< -behind
) {
556 pg_offset
= 0 - PAGE_SIZE_64
;
557 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
563 for (n
= 0; n
< max_pages_in_run
; n
++) {
564 m
= vm_page_lookup(object
, offset
+ run_offset
+ (n
* pg_offset
));
566 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
) {
567 page_run
[pages_in_run
++] = m
;
570 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
572 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
573 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
574 * new reference happens. If no futher references happen on the page after that remote TLB flushes
575 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
576 * by pageout_scan, which is just fine since the last reference would have happened quite far
577 * in the past (TLB caches don't hang around for very long), and of course could just as easily
578 * have happened before we did the deactivate_behind.
580 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m
), VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
584 vm_page_lockspin_queues();
586 for (n
= 0; n
< pages_in_run
; n
++) {
589 vm_page_deactivate_internal(m
, FALSE
);
591 vm_page_deactivate_behind_count
++;
593 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
596 vm_page_unlock_queues();
604 #if (DEVELOPMENT || DEBUG)
605 uint32_t vm_page_creation_throttled_hard
= 0;
606 uint32_t vm_page_creation_throttled_soft
= 0;
607 uint64_t vm_page_creation_throttle_avoided
= 0;
608 #endif /* DEVELOPMENT || DEBUG */
611 vm_page_throttled(boolean_t page_kept
)
613 clock_sec_t elapsed_sec
;
615 clock_usec_t tv_usec
;
617 thread_t thread
= current_thread();
619 if (thread
->options
& TH_OPT_VMPRIV
) {
623 if (thread
->t_page_creation_throttled
) {
624 thread
->t_page_creation_throttled
= 0;
626 if (page_kept
== FALSE
) {
630 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
631 #if (DEVELOPMENT || DEBUG)
632 thread
->t_page_creation_throttled_hard
++;
633 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
634 #endif /* DEVELOPMENT || DEBUG */
635 return HARD_THROTTLE_DELAY
;
638 if ((vm_page_free_count
< vm_page_throttle_limit
|| (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& SWAPPER_NEEDS_TO_UNTHROTTLE())) &&
639 thread
->t_page_creation_count
> (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
* VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
)) {
640 if (vm_page_free_wanted
== 0 && vm_page_free_wanted_privileged
== 0) {
641 #if (DEVELOPMENT || DEBUG)
642 OSAddAtomic64(1, &vm_page_creation_throttle_avoided
);
646 clock_get_system_microtime(&tv_sec
, &tv_usec
);
648 elapsed_sec
= tv_sec
- thread
->t_page_creation_time
;
650 if (elapsed_sec
<= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
||
651 (thread
->t_page_creation_count
/ elapsed_sec
) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
) {
652 if (elapsed_sec
>= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
)) {
654 * we'll reset our stats to give a well behaved app
655 * that was unlucky enough to accumulate a bunch of pages
656 * over a long period of time a chance to get out of
657 * the throttled state... we reset the counter and timestamp
658 * so that if it stays under the rate limit for the next second
659 * it will be back in our good graces... if it exceeds it, it
660 * will remain in the throttled state
662 thread
->t_page_creation_time
= tv_sec
;
663 thread
->t_page_creation_count
= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
* (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
- 1);
665 VM_PAGEOUT_DEBUG(vm_page_throttle_count
, 1);
667 thread
->t_page_creation_throttled
= 1;
669 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& HARD_THROTTLE_LIMIT_REACHED()) {
670 #if (DEVELOPMENT || DEBUG)
671 thread
->t_page_creation_throttled_hard
++;
672 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
673 #endif /* DEVELOPMENT || DEBUG */
674 return HARD_THROTTLE_DELAY
;
676 #if (DEVELOPMENT || DEBUG)
677 thread
->t_page_creation_throttled_soft
++;
678 OSAddAtomic(1, &vm_page_creation_throttled_soft
);
679 #endif /* DEVELOPMENT || DEBUG */
680 return SOFT_THROTTLE_DELAY
;
683 thread
->t_page_creation_time
= tv_sec
;
684 thread
->t_page_creation_count
= 0;
687 thread
->t_page_creation_count
++;
694 * check for various conditions that would
695 * prevent us from creating a ZF page...
696 * cleanup is based on being called from vm_fault_page
698 * object must be locked
699 * object == m->vmp_object
701 static vm_fault_return_t
702 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
)
706 if (object
->shadow_severed
||
707 VM_OBJECT_PURGEABLE_FAULT_ERROR(object
)) {
710 * 1. the shadow chain was severed,
711 * 2. the purgeable object is volatile or empty and is marked
712 * to fault on access while volatile.
713 * Just have to return an error at this point
715 if (m
!= VM_PAGE_NULL
) {
718 vm_fault_cleanup(object
, first_m
);
720 thread_interrupt_level(interruptible_state
);
722 return VM_FAULT_MEMORY_ERROR
;
724 if (page_throttle
== TRUE
) {
725 if ((throttle_delay
= vm_page_throttled(FALSE
))) {
727 * we're throttling zero-fills...
728 * treat this as if we couldn't grab a page
730 if (m
!= VM_PAGE_NULL
) {
733 vm_fault_cleanup(object
, first_m
);
735 VM_DEBUG_EVENT(vmf_check_zfdelay
, VMF_CHECK_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
737 delay(throttle_delay
);
739 if (current_thread_aborted()) {
740 thread_interrupt_level(interruptible_state
);
741 return VM_FAULT_INTERRUPTED
;
743 thread_interrupt_level(interruptible_state
);
745 return VM_FAULT_MEMORY_SHORTAGE
;
748 return VM_FAULT_SUCCESS
;
753 * do the work to zero fill a page and
754 * inject it into the correct paging queue
756 * m->vmp_object must be locked
757 * page queue lock must NOT be held
760 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
762 int my_fault
= DBG_ZERO_FILL_FAULT
;
765 object
= VM_PAGE_OBJECT(m
);
768 * This is is a zero-fill page fault...
770 * Checking the page lock is a waste of
771 * time; this page was absent, so
772 * it can't be page locked by a pager.
774 * we also consider it undefined
775 * with respect to instruction
776 * execution. i.e. it is the responsibility
777 * of higher layers to call for an instruction
778 * sync after changing the contents and before
779 * sending a program into this area. We
780 * choose this approach for performance
782 m
->vmp_pmapped
= TRUE
;
784 m
->vmp_cs_validated
= FALSE
;
785 m
->vmp_cs_tainted
= FALSE
;
786 m
->vmp_cs_nx
= FALSE
;
788 if (no_zero_fill
== TRUE
) {
789 my_fault
= DBG_NZF_PAGE_FAULT
;
791 if (m
->vmp_absent
&& m
->vmp_busy
) {
795 vm_page_zero_fill(m
);
797 VM_STAT_INCR(zero_fill_count
);
798 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
800 assert(!m
->vmp_laundry
);
801 assert(object
!= kernel_object
);
802 //assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0);
804 if (!VM_DYNAMIC_PAGING_ENABLED() &&
805 (object
->purgable
== VM_PURGABLE_DENY
||
806 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
807 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
808 vm_page_lockspin_queues();
810 if (!VM_DYNAMIC_PAGING_ENABLED()) {
811 assert(!VM_PAGE_WIRED(m
));
814 * can't be on the pageout queue since we don't
815 * have a pager to try and clean to
817 vm_page_queues_remove(m
, TRUE
);
818 vm_page_check_pageable_safe(m
);
819 vm_page_queue_enter(&vm_page_queue_throttled
, m
, vmp_pageq
);
820 m
->vmp_q_state
= VM_PAGE_ON_THROTTLED_Q
;
821 vm_page_throttled_count
++;
823 vm_page_unlock_queues();
830 * Routine: vm_fault_page
832 * Find the resident page for the virtual memory
833 * specified by the given virtual memory object
835 * Additional arguments:
836 * The required permissions for the page is given
837 * in "fault_type". Desired permissions are included
839 * fault_info is passed along to determine pagein cluster
840 * limits... it contains the expected reference pattern,
841 * cluster size if available, etc...
843 * If the desired page is known to be resident (for
844 * example, because it was previously wired down), asserting
845 * the "unwiring" parameter will speed the search.
847 * If the operation can be interrupted (by thread_abort
848 * or thread_terminate), then the "interruptible"
849 * parameter should be asserted.
852 * The page containing the proper data is returned
856 * The source object must be locked and referenced,
857 * and must donate one paging reference. The reference
858 * is not affected. The paging reference and lock are
861 * If the call succeeds, the object in which "result_page"
862 * resides is left locked and holding a paging reference.
863 * If this is not the original object, a busy page in the
864 * original object is returned in "top_page", to prevent other
865 * callers from pursuing this same data, along with a paging
866 * reference for the original object. The "top_page" should
867 * be destroyed when this guarantee is no longer required.
868 * The "result_page" is also left busy. It is not removed
869 * from the pageout queues.
871 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
872 * fault succeeded but there's no VM page (i.e. the VM object
873 * does not actually hold VM pages, but device memory or
874 * large pages). The object is still locked and we still hold a
875 * paging_in_progress reference.
877 unsigned int vm_fault_page_blocked_access
= 0;
878 unsigned int vm_fault_page_forced_retry
= 0;
883 vm_object_t first_object
, /* Object to begin search */
884 vm_object_offset_t first_offset
, /* Offset into object */
885 vm_prot_t fault_type
, /* What access is requested */
886 boolean_t must_be_resident
,/* Must page be resident? */
887 boolean_t caller_lookup
, /* caller looked up page */
888 /* Modifies in place: */
889 vm_prot_t
*protection
, /* Protection for mapping */
890 vm_page_t
*result_page
, /* Page found, if successful */
892 vm_page_t
*top_page
, /* Page in top object, if
893 * not result_page. */
894 int *type_of_fault
, /* if non-null, fill in with type of fault
895 * COW, zero-fill, etc... returned in trace point */
896 /* More arguments: */
897 kern_return_t
*error_code
, /* code if page is in error */
898 boolean_t no_zero_fill
, /* don't zero fill absent pages */
899 boolean_t data_supply
, /* treat as data_supply if
900 * it is a write fault and a full
901 * page is provided */
902 vm_object_fault_info_t fault_info
)
906 vm_object_offset_t offset
;
908 vm_object_t next_object
;
909 vm_object_t copy_object
;
910 boolean_t look_for_page
;
911 boolean_t force_fault_retry
= FALSE
;
912 vm_prot_t access_required
= fault_type
;
913 vm_prot_t wants_copy_flag
;
914 kern_return_t wait_result
;
915 wait_interrupt_t interruptible_state
;
916 boolean_t data_already_requested
= FALSE
;
917 vm_behavior_t orig_behavior
;
918 vm_size_t orig_cluster_size
;
919 vm_fault_return_t error
;
921 uint32_t try_failed_count
;
922 int interruptible
; /* how may fault be interrupted? */
923 int external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
924 memory_object_t pager
;
925 vm_fault_return_t retval
;
929 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
930 * marked as paged out in the compressor pager or the pager doesn't exist.
931 * Note also that if the pager for an internal object
932 * has not been created, the pager is not invoked regardless of the value
933 * of MUST_ASK_PAGER().
935 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
936 * is marked as paged out in the compressor pager.
937 * PAGED_OUT() is used to determine if a page has already been pushed
938 * into a copy object in order to avoid a redundant page out operation.
940 #define MUST_ASK_PAGER(o, f, s) \
941 ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
943 #define PAGED_OUT(o, f) \
944 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
949 #define RELEASE_PAGE(m) \
951 PAGE_WAKEUP_DONE(m); \
952 if ( !VM_PAGE_PAGEABLE(m)) { \
953 vm_page_lockspin_queues(); \
954 if ( !VM_PAGE_PAGEABLE(m)) { \
955 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE) \
956 vm_page_deactivate(m); \
958 vm_page_activate(m); \
960 vm_page_unlock_queues(); \
965 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
968 interruptible
= fault_info
->interruptible
;
969 interruptible_state
= thread_interrupt_level(interruptible
);
972 * INVARIANTS (through entire routine):
974 * 1) At all times, we must either have the object
975 * lock or a busy page in some object to prevent
976 * some other thread from trying to bring in
979 * Note that we cannot hold any locks during the
980 * pager access or when waiting for memory, so
981 * we use a busy page then.
983 * 2) To prevent another thread from racing us down the
984 * shadow chain and entering a new page in the top
985 * object before we do, we must keep a busy page in
986 * the top object while following the shadow chain.
988 * 3) We must increment paging_in_progress on any object
989 * for which we have a busy page before dropping
992 * 4) We leave busy pages on the pageout queues.
993 * If the pageout daemon comes across a busy page,
994 * it will remove the page from the pageout queues.
997 object
= first_object
;
998 offset
= first_offset
;
999 first_m
= VM_PAGE_NULL
;
1000 access_required
= fault_type
;
1003 * default type of fault
1005 my_fault
= DBG_CACHE_HIT_FAULT
;
1009 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1013 #if CONFIG_SECLUDED_MEMORY
1014 if (object
->can_grab_secluded
) {
1015 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
1017 #endif /* CONFIG_SECLUDED_MEMORY */
1019 if (!object
->alive
) {
1021 * object is no longer valid
1022 * clean up and return error
1024 vm_fault_cleanup(object
, first_m
);
1025 thread_interrupt_level(interruptible_state
);
1027 return VM_FAULT_MEMORY_ERROR
;
1030 if (!object
->pager_created
&& object
->phys_contiguous
) {
1032 * A physically-contiguous object without a pager:
1033 * must be a "large page" object. We do not deal
1034 * with VM pages for this object.
1036 caller_lookup
= FALSE
;
1038 goto phys_contig_object
;
1041 if (object
->blocked_access
) {
1043 * Access to this VM object has been blocked.
1044 * Replace our "paging_in_progress" reference with
1045 * a "activity_in_progress" reference and wait for
1046 * access to be unblocked.
1048 caller_lookup
= FALSE
; /* no longer valid after sleep */
1049 vm_object_activity_begin(object
);
1050 vm_object_paging_end(object
);
1051 while (object
->blocked_access
) {
1052 vm_object_sleep(object
,
1053 VM_OBJECT_EVENT_UNBLOCKED
,
1056 vm_fault_page_blocked_access
++;
1057 vm_object_paging_begin(object
);
1058 vm_object_activity_end(object
);
1062 * See whether the page at 'offset' is resident
1064 if (caller_lookup
== TRUE
) {
1066 * The caller has already looked up the page
1067 * and gave us the result in "result_page".
1068 * We can use this for the first lookup but
1069 * it loses its validity as soon as we unlock
1073 caller_lookup
= FALSE
; /* no longer valid after that */
1075 m
= vm_page_lookup(object
, offset
);
1078 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1080 if (m
!= VM_PAGE_NULL
) {
1083 * The page is being brought in,
1084 * wait for it and then retry.
1087 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1089 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
1091 counter(c_vm_fault_page_block_busy_kernel
++);
1093 if (wait_result
!= THREAD_AWAKENED
) {
1094 vm_fault_cleanup(object
, first_m
);
1095 thread_interrupt_level(interruptible_state
);
1097 if (wait_result
== THREAD_RESTART
) {
1098 return VM_FAULT_RETRY
;
1100 return VM_FAULT_INTERRUPTED
;
1105 if (m
->vmp_laundry
) {
1106 m
->vmp_free_when_done
= FALSE
;
1108 if (!m
->vmp_cleaning
) {
1109 vm_pageout_steal_laundry(m
, FALSE
);
1112 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
1114 * Guard page: off limits !
1116 if (fault_type
== VM_PROT_NONE
) {
1118 * The fault is not requesting any
1119 * access to the guard page, so it must
1120 * be just to wire or unwire it.
1121 * Let's pretend it succeeded...
1125 assert(first_m
== VM_PAGE_NULL
);
1126 *top_page
= first_m
;
1127 if (type_of_fault
) {
1128 *type_of_fault
= DBG_GUARD_FAULT
;
1130 thread_interrupt_level(interruptible_state
);
1131 return VM_FAULT_SUCCESS
;
1134 * The fault requests access to the
1135 * guard page: let's deny that !
1137 vm_fault_cleanup(object
, first_m
);
1138 thread_interrupt_level(interruptible_state
);
1139 return VM_FAULT_MEMORY_ERROR
;
1145 * The page is in error, give up now.
1148 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1151 *error_code
= KERN_MEMORY_ERROR
;
1155 vm_fault_cleanup(object
, first_m
);
1156 thread_interrupt_level(interruptible_state
);
1158 return VM_FAULT_MEMORY_ERROR
;
1160 if (m
->vmp_restart
) {
1162 * The pager wants us to restart
1163 * at the top of the chain,
1164 * typically because it has moved the
1165 * page to another pager, then do so.
1168 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1172 vm_fault_cleanup(object
, first_m
);
1173 thread_interrupt_level(interruptible_state
);
1175 return VM_FAULT_RETRY
;
1177 if (m
->vmp_absent
) {
1179 * The page isn't busy, but is absent,
1180 * therefore it's deemed "unavailable".
1182 * Remove the non-existent page (unless it's
1183 * in the top object) and move on down to the
1184 * next object (if there is one).
1187 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1189 next_object
= object
->shadow
;
1191 if (next_object
== VM_OBJECT_NULL
) {
1193 * Absent page at bottom of shadow
1194 * chain; zero fill the page we left
1195 * busy in the first object, and free
1198 assert(!must_be_resident
);
1201 * check for any conditions that prevent
1202 * us from creating a new zero-fill page
1203 * vm_fault_check will do all of the
1204 * fault cleanup in the case of an error condition
1205 * including resetting the thread_interrupt_level
1207 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1209 if (error
!= VM_FAULT_SUCCESS
) {
1213 if (object
!= first_object
) {
1215 * free the absent page we just found
1220 * drop reference and lock on current object
1222 vm_object_paging_end(object
);
1223 vm_object_unlock(object
);
1226 * grab the original page we
1227 * 'soldered' in place and
1228 * retake lock on 'first_object'
1231 first_m
= VM_PAGE_NULL
;
1233 object
= first_object
;
1234 offset
= first_offset
;
1236 vm_object_lock(object
);
1239 * we're going to use the absent page we just found
1240 * so convert it to a 'busy' page
1242 m
->vmp_absent
= FALSE
;
1245 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
) {
1246 m
->vmp_absent
= TRUE
;
1249 * zero-fill the page and put it on
1250 * the correct paging queue
1252 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1256 if (must_be_resident
) {
1257 vm_object_paging_end(object
);
1258 } else if (object
!= first_object
) {
1259 vm_object_paging_end(object
);
1263 m
->vmp_absent
= FALSE
;
1266 vm_page_lockspin_queues();
1267 vm_page_queues_remove(m
, FALSE
);
1268 vm_page_unlock_queues();
1271 offset
+= object
->vo_shadow_offset
;
1272 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1273 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1274 access_required
= VM_PROT_READ
;
1276 vm_object_lock(next_object
);
1277 vm_object_unlock(object
);
1278 object
= next_object
;
1279 vm_object_paging_begin(object
);
1282 * reset to default type of fault
1284 my_fault
= DBG_CACHE_HIT_FAULT
;
1289 if ((m
->vmp_cleaning
)
1290 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1291 && (fault_type
& VM_PROT_WRITE
)) {
1293 * This is a copy-on-write fault that will
1294 * cause us to revoke access to this page, but
1295 * this page is in the process of being cleaned
1296 * in a clustered pageout. We must wait until
1297 * the cleaning operation completes before
1298 * revoking access to the original page,
1299 * otherwise we might attempt to remove a
1303 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1306 * take an extra ref so that object won't die
1308 vm_object_reference_locked(object
);
1310 vm_fault_cleanup(object
, first_m
);
1312 counter(c_vm_fault_page_block_backoff_kernel
++);
1313 vm_object_lock(object
);
1314 assert(object
->ref_count
> 0);
1316 m
= vm_page_lookup(object
, offset
);
1318 if (m
!= VM_PAGE_NULL
&& m
->vmp_cleaning
) {
1319 PAGE_ASSERT_WAIT(m
, interruptible
);
1321 vm_object_unlock(object
);
1322 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1323 vm_object_deallocate(object
);
1327 vm_object_unlock(object
);
1329 vm_object_deallocate(object
);
1330 thread_interrupt_level(interruptible_state
);
1332 return VM_FAULT_RETRY
;
1335 if (type_of_fault
== NULL
&& (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) &&
1336 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1338 * If we were passed a non-NULL pointer for
1339 * "type_of_fault", than we came from
1340 * vm_fault... we'll let it deal with
1341 * this condition, since it
1342 * needs to see m->vmp_speculative to correctly
1343 * account the pageins, otherwise...
1344 * take it off the speculative queue, we'll
1345 * let the caller of vm_fault_page deal
1346 * with getting it onto the correct queue
1348 * If the caller specified in fault_info that
1349 * it wants a "stealth" fault, we also leave
1350 * the page in the speculative queue.
1352 vm_page_lockspin_queues();
1353 if (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) {
1354 vm_page_queues_remove(m
, FALSE
);
1356 vm_page_unlock_queues();
1358 assert(object
== VM_PAGE_OBJECT(m
));
1360 if (object
->code_signed
) {
1363 * We just paged in a page from a signed
1364 * memory object but we don't need to
1365 * validate it now. We'll validate it if
1366 * when it gets mapped into a user address
1367 * space for the first time or when the page
1368 * gets copied to another object as a result
1369 * of a copy-on-write.
1374 * We mark the page busy and leave it on
1375 * the pageout queues. If the pageout
1376 * deamon comes across it, then it will
1377 * remove the page from the queue, but not the object
1380 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1382 assert(!m
->vmp_busy
);
1383 assert(!m
->vmp_absent
);
1391 * we get here when there is no page present in the object at
1392 * the offset we're interested in... we'll allocate a page
1393 * at this point if the pager associated with
1394 * this object can provide the data or we're the top object...
1395 * object is locked; m == NULL
1398 if (must_be_resident
) {
1399 if (fault_type
== VM_PROT_NONE
&&
1400 object
== kernel_object
) {
1402 * We've been called from vm_fault_unwire()
1403 * while removing a map entry that was allocated
1404 * with KMA_KOBJECT and KMA_VAONLY. This page
1405 * is not present and there's nothing more to
1406 * do here (nothing to unwire).
1408 vm_fault_cleanup(object
, first_m
);
1409 thread_interrupt_level(interruptible_state
);
1411 return VM_FAULT_MEMORY_ERROR
;
1414 goto dont_look_for_page
;
1417 /* Don't expect to fault pages into the kernel object. */
1418 assert(object
!= kernel_object
);
1420 data_supply
= FALSE
;
1422 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
, external_state
) == TRUE
) && !data_supply
);
1425 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1427 if (!look_for_page
&& object
== first_object
&& !object
->phys_contiguous
) {
1429 * Allocate a new page for this object/offset pair as a placeholder
1431 m
= vm_page_grab_options(grab_options
);
1433 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1435 if (m
== VM_PAGE_NULL
) {
1436 vm_fault_cleanup(object
, first_m
);
1437 thread_interrupt_level(interruptible_state
);
1439 return VM_FAULT_MEMORY_SHORTAGE
;
1442 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1443 vm_page_insert_internal(m
, object
, offset
, VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1445 vm_page_insert(m
, object
, offset
);
1448 if (look_for_page
) {
1453 * If the memory manager is not ready, we
1454 * cannot make requests.
1456 if (!object
->pager_ready
) {
1458 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1460 if (m
!= VM_PAGE_NULL
) {
1465 * take an extra ref so object won't die
1467 vm_object_reference_locked(object
);
1468 vm_fault_cleanup(object
, first_m
);
1469 counter(c_vm_fault_page_block_backoff_kernel
++);
1471 vm_object_lock(object
);
1472 assert(object
->ref_count
> 0);
1474 if (!object
->pager_ready
) {
1475 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1477 vm_object_unlock(object
);
1478 if (wait_result
== THREAD_WAITING
) {
1479 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1481 vm_object_deallocate(object
);
1485 vm_object_unlock(object
);
1486 vm_object_deallocate(object
);
1487 thread_interrupt_level(interruptible_state
);
1489 return VM_FAULT_RETRY
;
1492 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1494 * If there are too many outstanding page
1495 * requests pending on this external object, we
1496 * wait for them to be resolved now.
1499 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1501 if (m
!= VM_PAGE_NULL
) {
1505 * take an extra ref so object won't die
1507 vm_object_reference_locked(object
);
1509 vm_fault_cleanup(object
, first_m
);
1511 counter(c_vm_fault_page_block_backoff_kernel
++);
1513 vm_object_lock(object
);
1514 assert(object
->ref_count
> 0);
1516 if (object
->paging_in_progress
>= vm_object_pagein_throttle
) {
1517 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS
, interruptible
);
1519 vm_object_unlock(object
);
1520 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1521 vm_object_deallocate(object
);
1525 vm_object_unlock(object
);
1526 vm_object_deallocate(object
);
1527 thread_interrupt_level(interruptible_state
);
1529 return VM_FAULT_RETRY
;
1532 if (object
->internal
) {
1533 int compressed_count_delta
;
1535 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
1537 if (m
== VM_PAGE_NULL
) {
1539 * Allocate a new page for this object/offset pair as a placeholder
1541 m
= vm_page_grab_options(grab_options
);
1543 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1545 if (m
== VM_PAGE_NULL
) {
1546 vm_fault_cleanup(object
, first_m
);
1547 thread_interrupt_level(interruptible_state
);
1549 return VM_FAULT_MEMORY_SHORTAGE
;
1552 m
->vmp_absent
= TRUE
;
1553 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1554 vm_page_insert_internal(m
, object
, offset
, VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1556 vm_page_insert(m
, object
, offset
);
1559 assert(m
->vmp_busy
);
1561 m
->vmp_absent
= TRUE
;
1562 pager
= object
->pager
;
1564 assert(object
->paging_in_progress
> 0);
1565 vm_object_unlock(object
);
1567 rc
= vm_compressor_pager_get(
1569 offset
+ object
->paging_offset
,
1570 VM_PAGE_GET_PHYS_PAGE(m
),
1573 &compressed_count_delta
);
1575 if (type_of_fault
== NULL
) {
1579 * we weren't called from vm_fault, so we
1580 * need to apply page creation throttling
1581 * do it before we re-acquire any locks
1583 if (my_fault_type
== DBG_COMPRESSOR_FAULT
) {
1584 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
1585 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 1, 0);
1586 delay(throttle_delay
);
1590 vm_object_lock(object
);
1591 assert(object
->paging_in_progress
> 0);
1593 vm_compressor_pager_count(
1595 compressed_count_delta
,
1596 FALSE
, /* shared_lock */
1601 m
->vmp_absent
= FALSE
;
1602 m
->vmp_dirty
= TRUE
;
1603 if ((object
->wimg_bits
&
1605 VM_WIMG_USE_DEFAULT
) {
1607 * If the page is not cacheable,
1608 * we can't let its contents
1609 * linger in the data cache
1610 * after the decompression.
1612 pmap_sync_page_attributes_phys(
1613 VM_PAGE_GET_PHYS_PAGE(m
));
1615 m
->vmp_written_by_kernel
= TRUE
;
1619 * If the object is purgeable, its
1620 * owner's purgeable ledgers have been
1621 * updated in vm_page_insert() but the
1622 * page was also accounted for in a
1623 * "compressed purgeable" ledger, so
1626 if (((object
->purgable
!=
1627 VM_PURGABLE_DENY
) ||
1628 object
->vo_ledger_tag
) &&
1629 (object
->vo_owner
!=
1632 * One less compressed
1633 * purgeable/tagged page.
1635 vm_object_owner_compressed_update(
1641 case KERN_MEMORY_FAILURE
:
1642 m
->vmp_unusual
= TRUE
;
1643 m
->vmp_error
= TRUE
;
1644 m
->vmp_absent
= FALSE
;
1646 case KERN_MEMORY_ERROR
:
1647 assert(m
->vmp_absent
);
1650 panic("vm_fault_page(): unexpected "
1652 "vm_compressor_pager_get()\n",
1655 PAGE_WAKEUP_DONE(m
);
1658 goto data_requested
;
1660 my_fault_type
= DBG_PAGEIN_FAULT
;
1662 if (m
!= VM_PAGE_NULL
) {
1668 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1672 * It's possible someone called vm_object_destroy while we weren't
1673 * holding the object lock. If that has happened, then bail out
1677 pager
= object
->pager
;
1679 if (pager
== MEMORY_OBJECT_NULL
) {
1680 vm_fault_cleanup(object
, first_m
);
1681 thread_interrupt_level(interruptible_state
);
1682 return VM_FAULT_MEMORY_ERROR
;
1686 * We have an absent page in place for the faulting offset,
1687 * so we can release the object lock.
1690 if (object
->object_is_shared_cache
) {
1691 set_thread_rwlock_boost();
1694 vm_object_unlock(object
);
1697 * If this object uses a copy_call strategy,
1698 * and we are interested in a copy of this object
1699 * (having gotten here only by following a
1700 * shadow chain), then tell the memory manager
1701 * via a flag added to the desired_access
1702 * parameter, so that it can detect a race
1703 * between our walking down the shadow chain
1704 * and its pushing pages up into a copy of
1705 * the object that it manages.
1707 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
) {
1708 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1710 wants_copy_flag
= VM_PROT_NONE
;
1713 if (object
->copy
== first_object
) {
1715 * if we issue the memory_object_data_request in
1716 * this state, we are subject to a deadlock with
1717 * the underlying filesystem if it is trying to
1718 * shrink the file resulting in a push of pages
1719 * into the copy object... that push will stall
1720 * on the placeholder page, and if the pushing thread
1721 * is holding a lock that is required on the pagein
1722 * path (such as a truncate lock), we'll deadlock...
1723 * to avoid this potential deadlock, we throw away
1724 * our placeholder page before calling memory_object_data_request
1725 * and force this thread to retry the vm_fault_page after
1726 * we have issued the I/O. the second time through this path
1727 * we will find the page already in the cache (presumably still
1728 * busy waiting for the I/O to complete) and then complete
1729 * the fault w/o having to go through memory_object_data_request again
1731 assert(first_m
!= VM_PAGE_NULL
);
1732 assert(VM_PAGE_OBJECT(first_m
) == first_object
);
1734 vm_object_lock(first_object
);
1735 VM_PAGE_FREE(first_m
);
1736 vm_object_paging_end(first_object
);
1737 vm_object_unlock(first_object
);
1739 first_m
= VM_PAGE_NULL
;
1740 force_fault_retry
= TRUE
;
1742 vm_fault_page_forced_retry
++;
1745 if (data_already_requested
== TRUE
) {
1746 orig_behavior
= fault_info
->behavior
;
1747 orig_cluster_size
= fault_info
->cluster_size
;
1749 fault_info
->behavior
= VM_BEHAVIOR_RANDOM
;
1750 fault_info
->cluster_size
= PAGE_SIZE
;
1753 * Call the memory manager to retrieve the data.
1755 rc
= memory_object_data_request(
1757 offset
+ object
->paging_offset
,
1759 access_required
| wants_copy_flag
,
1760 (memory_object_fault_info_t
)fault_info
);
1762 if (data_already_requested
== TRUE
) {
1763 fault_info
->behavior
= orig_behavior
;
1764 fault_info
->cluster_size
= orig_cluster_size
;
1766 data_already_requested
= TRUE
;
1769 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1771 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1773 vm_object_lock(object
);
1775 if (object
->object_is_shared_cache
) {
1776 clear_thread_rwlock_boost();
1780 if (rc
!= KERN_SUCCESS
) {
1781 vm_fault_cleanup(object
, first_m
);
1782 thread_interrupt_level(interruptible_state
);
1784 return (rc
== MACH_SEND_INTERRUPTED
) ?
1785 VM_FAULT_INTERRUPTED
:
1786 VM_FAULT_MEMORY_ERROR
;
1789 clock_usec_t tv_usec
;
1791 if (my_fault_type
== DBG_PAGEIN_FAULT
) {
1792 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1793 current_thread()->t_page_creation_time
= tv_sec
;
1794 current_thread()->t_page_creation_count
= 0;
1797 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_flags
& TH_SFLAG_ABORT
)) {
1798 vm_fault_cleanup(object
, first_m
);
1799 thread_interrupt_level(interruptible_state
);
1801 return VM_FAULT_INTERRUPTED
;
1803 if (force_fault_retry
== TRUE
) {
1804 vm_fault_cleanup(object
, first_m
);
1805 thread_interrupt_level(interruptible_state
);
1807 return VM_FAULT_RETRY
;
1809 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1811 * No page here means that the object we
1812 * initially looked up was "physically
1813 * contiguous" (i.e. device memory). However,
1814 * with Virtual VRAM, the object might not
1815 * be backed by that device memory anymore,
1816 * so we're done here only if the object is
1817 * still "phys_contiguous".
1818 * Otherwise, if the object is no longer
1819 * "phys_contiguous", we need to retry the
1820 * page fault against the object's new backing
1821 * store (different memory object).
1827 * potentially a pagein fault
1828 * if we make it through the state checks
1829 * above, than we'll count it as such
1831 my_fault
= my_fault_type
;
1834 * Retry with same object/offset, since new data may
1835 * be in a different page (i.e., m is meaningless at
1842 * We get here if the object has no pager, or an existence map
1843 * exists and indicates the page isn't present on the pager
1844 * or we're unwiring a page. If a pager exists, but there
1845 * is no existence map, then the m->vmp_absent case above handles
1846 * the ZF case when the pager can't provide the page
1849 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1851 if (object
== first_object
) {
1854 assert(m
== VM_PAGE_NULL
);
1857 next_object
= object
->shadow
;
1859 if (next_object
== VM_OBJECT_NULL
) {
1861 * we've hit the bottom of the shadown chain,
1862 * fill the page in the top object with zeros.
1864 assert(!must_be_resident
);
1866 if (object
!= first_object
) {
1867 vm_object_paging_end(object
);
1868 vm_object_unlock(object
);
1870 object
= first_object
;
1871 offset
= first_offset
;
1872 vm_object_lock(object
);
1875 assert(VM_PAGE_OBJECT(m
) == object
);
1876 first_m
= VM_PAGE_NULL
;
1879 * check for any conditions that prevent
1880 * us from creating a new zero-fill page
1881 * vm_fault_check will do all of the
1882 * fault cleanup in the case of an error condition
1883 * including resetting the thread_interrupt_level
1885 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1887 if (error
!= VM_FAULT_SUCCESS
) {
1891 if (m
== VM_PAGE_NULL
) {
1892 m
= vm_page_grab_options(grab_options
);
1894 if (m
== VM_PAGE_NULL
) {
1895 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1896 thread_interrupt_level(interruptible_state
);
1898 return VM_FAULT_MEMORY_SHORTAGE
;
1900 vm_page_insert(m
, object
, offset
);
1902 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
) {
1903 m
->vmp_absent
= TRUE
;
1906 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1911 * Move on to the next object. Lock the next
1912 * object before unlocking the current one.
1914 if ((object
!= first_object
) || must_be_resident
) {
1915 vm_object_paging_end(object
);
1918 offset
+= object
->vo_shadow_offset
;
1919 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1920 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1921 access_required
= VM_PROT_READ
;
1923 vm_object_lock(next_object
);
1924 vm_object_unlock(object
);
1926 object
= next_object
;
1927 vm_object_paging_begin(object
);
1932 * PAGE HAS BEEN FOUND.
1935 * busy, so that we can play with it;
1936 * not absent, so that nobody else will fill it;
1937 * possibly eligible for pageout;
1939 * The top-level page (first_m) is:
1940 * VM_PAGE_NULL if the page was found in the
1942 * busy, not absent, and ineligible for pageout.
1944 * The current object (object) is locked. A paging
1945 * reference is held for the current and top-level
1950 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1952 #if EXTRA_ASSERTIONS
1953 assert(m
->vmp_busy
&& !m
->vmp_absent
);
1954 assert((first_m
== VM_PAGE_NULL
) ||
1955 (first_m
->vmp_busy
&& !first_m
->vmp_absent
&&
1956 !first_m
->vmp_active
&& !first_m
->vmp_inactive
&& !first_m
->vmp_secluded
));
1957 #endif /* EXTRA_ASSERTIONS */
1960 * If the page is being written, but isn't
1961 * already owned by the top-level object,
1962 * we have to copy it into a new page owned
1963 * by the top-level object.
1965 if (object
!= first_object
) {
1967 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1969 if (fault_type
& VM_PROT_WRITE
) {
1973 * We only really need to copy if we
1976 assert(!must_be_resident
);
1979 * If we try to collapse first_object at this
1980 * point, we may deadlock when we try to get
1981 * the lock on an intermediate object (since we
1982 * have the bottom object locked). We can't
1983 * unlock the bottom object, because the page
1984 * we found may move (by collapse) if we do.
1986 * Instead, we first copy the page. Then, when
1987 * we have no more use for the bottom object,
1988 * we unlock it and try to collapse.
1990 * Note that we copy the page even if we didn't
1991 * need to... that's the breaks.
1995 * Allocate a page for the copy
1997 copy_m
= vm_page_grab_options(grab_options
);
1999 if (copy_m
== VM_PAGE_NULL
) {
2002 vm_fault_cleanup(object
, first_m
);
2003 thread_interrupt_level(interruptible_state
);
2005 return VM_FAULT_MEMORY_SHORTAGE
;
2008 vm_page_copy(m
, copy_m
);
2011 * If another map is truly sharing this
2012 * page with us, we have to flush all
2013 * uses of the original page, since we
2014 * can't distinguish those which want the
2015 * original from those which need the
2018 * XXXO If we know that only one map has
2019 * access to this page, then we could
2020 * avoid the pmap_disconnect() call.
2022 if (m
->vmp_pmapped
) {
2023 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2026 if (m
->vmp_clustered
) {
2027 VM_PAGE_COUNT_AS_PAGEIN(m
);
2028 VM_PAGE_CONSUME_CLUSTERED(m
);
2030 assert(!m
->vmp_cleaning
);
2033 * We no longer need the old page or object.
2038 * This check helps with marking the object as having a sequential pattern
2039 * Normally we'll miss doing this below because this fault is about COW to
2040 * the first_object i.e. bring page in from disk, push to object above but
2041 * don't update the file object's sequential pattern.
2043 if (object
->internal
== FALSE
) {
2044 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2047 vm_object_paging_end(object
);
2048 vm_object_unlock(object
);
2050 my_fault
= DBG_COW_FAULT
;
2051 VM_STAT_INCR(cow_faults
);
2052 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2053 current_task()->cow_faults
++;
2055 object
= first_object
;
2056 offset
= first_offset
;
2058 vm_object_lock(object
);
2060 * get rid of the place holder
2061 * page that we soldered in earlier
2063 VM_PAGE_FREE(first_m
);
2064 first_m
= VM_PAGE_NULL
;
2067 * and replace it with the
2068 * page we just copied into
2070 assert(copy_m
->vmp_busy
);
2071 vm_page_insert(copy_m
, object
, offset
);
2072 SET_PAGE_DIRTY(copy_m
, TRUE
);
2076 * Now that we've gotten the copy out of the
2077 * way, let's try to collapse the top object.
2078 * But we have to play ugly games with
2079 * paging_in_progress to do that...
2081 vm_object_paging_end(object
);
2082 vm_object_collapse(object
, offset
, TRUE
);
2083 vm_object_paging_begin(object
);
2085 *protection
&= (~VM_PROT_WRITE
);
2089 * Now check whether the page needs to be pushed into the
2090 * copy object. The use of asymmetric copy on write for
2091 * shared temporary objects means that we may do two copies to
2092 * satisfy the fault; one above to get the page from a
2093 * shadowed object, and one here to push it into the copy.
2095 try_failed_count
= 0;
2097 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
2098 vm_object_offset_t copy_offset
;
2102 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2105 * If the page is being written, but hasn't been
2106 * copied to the copy-object, we have to copy it there.
2108 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2109 *protection
&= ~VM_PROT_WRITE
;
2114 * If the page was guaranteed to be resident,
2115 * we must have already performed the copy.
2117 if (must_be_resident
) {
2122 * Try to get the lock on the copy_object.
2124 if (!vm_object_lock_try(copy_object
)) {
2125 vm_object_unlock(object
);
2128 mutex_pause(try_failed_count
); /* wait a bit */
2129 vm_object_lock(object
);
2133 try_failed_count
= 0;
2136 * Make another reference to the copy-object,
2137 * to keep it from disappearing during the
2140 vm_object_reference_locked(copy_object
);
2143 * Does the page exist in the copy?
2145 copy_offset
= first_offset
- copy_object
->vo_shadow_offset
;
2147 if (copy_object
->vo_size
<= copy_offset
) {
2149 * Copy object doesn't cover this page -- do nothing.
2152 } else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
2154 * Page currently exists in the copy object
2156 if (copy_m
->vmp_busy
) {
2158 * If the page is being brought
2159 * in, wait for it and then retry.
2164 * take an extra ref so object won't die
2166 vm_object_reference_locked(copy_object
);
2167 vm_object_unlock(copy_object
);
2168 vm_fault_cleanup(object
, first_m
);
2169 counter(c_vm_fault_page_block_backoff_kernel
++);
2171 vm_object_lock(copy_object
);
2172 assert(copy_object
->ref_count
> 0);
2173 VM_OBJ_RES_DECR(copy_object
);
2174 vm_object_lock_assert_exclusive(copy_object
);
2175 copy_object
->ref_count
--;
2176 assert(copy_object
->ref_count
> 0);
2177 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
2179 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->vmp_busy
) {
2180 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
2182 vm_object_unlock(copy_object
);
2183 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2184 vm_object_deallocate(copy_object
);
2188 vm_object_unlock(copy_object
);
2189 vm_object_deallocate(copy_object
);
2190 thread_interrupt_level(interruptible_state
);
2192 return VM_FAULT_RETRY
;
2195 } else if (!PAGED_OUT(copy_object
, copy_offset
)) {
2197 * If PAGED_OUT is TRUE, then the page used to exist
2198 * in the copy-object, and has already been paged out.
2199 * We don't need to repeat this. If PAGED_OUT is
2200 * FALSE, then either we don't know (!pager_created,
2201 * for example) or it hasn't been paged out.
2202 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2203 * We must copy the page to the copy object.
2205 * Allocate a page for the copy
2207 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
2209 if (copy_m
== VM_PAGE_NULL
) {
2212 VM_OBJ_RES_DECR(copy_object
);
2213 vm_object_lock_assert_exclusive(copy_object
);
2214 copy_object
->ref_count
--;
2215 assert(copy_object
->ref_count
> 0);
2217 vm_object_unlock(copy_object
);
2218 vm_fault_cleanup(object
, first_m
);
2219 thread_interrupt_level(interruptible_state
);
2221 return VM_FAULT_MEMORY_SHORTAGE
;
2224 * Must copy page into copy-object.
2226 vm_page_copy(m
, copy_m
);
2229 * If the old page was in use by any users
2230 * of the copy-object, it must be removed
2231 * from all pmaps. (We can't know which
2234 if (m
->vmp_pmapped
) {
2235 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2238 if (m
->vmp_clustered
) {
2239 VM_PAGE_COUNT_AS_PAGEIN(m
);
2240 VM_PAGE_CONSUME_CLUSTERED(m
);
2243 * If there's a pager, then immediately
2244 * page out this page, using the "initialize"
2245 * option. Else, we use the copy.
2247 if ((!copy_object
->pager_ready
)
2248 || VM_COMPRESSOR_PAGER_STATE_GET(copy_object
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2250 vm_page_lockspin_queues();
2251 assert(!m
->vmp_cleaning
);
2252 vm_page_activate(copy_m
);
2253 vm_page_unlock_queues();
2255 SET_PAGE_DIRTY(copy_m
, TRUE
);
2256 PAGE_WAKEUP_DONE(copy_m
);
2258 assert(copy_m
->vmp_busy
== TRUE
);
2259 assert(!m
->vmp_cleaning
);
2262 * dirty is protected by the object lock
2264 SET_PAGE_DIRTY(copy_m
, TRUE
);
2267 * The page is already ready for pageout:
2268 * not on pageout queues and busy.
2269 * Unlock everything except the
2270 * copy_object itself.
2272 vm_object_unlock(object
);
2275 * Write the page to the copy-object,
2276 * flushing it from the kernel.
2278 vm_pageout_initialize_page(copy_m
);
2281 * Since the pageout may have
2282 * temporarily dropped the
2283 * copy_object's lock, we
2284 * check whether we'll have
2285 * to deallocate the hard way.
2287 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2288 vm_object_unlock(copy_object
);
2289 vm_object_deallocate(copy_object
);
2290 vm_object_lock(object
);
2295 * Pick back up the old object's
2296 * lock. [It is safe to do so,
2297 * since it must be deeper in the
2300 vm_object_lock(object
);
2304 * Because we're pushing a page upward
2305 * in the object tree, we must restart
2306 * any faults that are waiting here.
2307 * [Note that this is an expansion of
2308 * PAGE_WAKEUP that uses the THREAD_RESTART
2309 * wait result]. Can't turn off the page's
2310 * busy bit because we're not done with it.
2312 if (m
->vmp_wanted
) {
2313 m
->vmp_wanted
= FALSE
;
2314 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2318 * The reference count on copy_object must be
2319 * at least 2: one for our extra reference,
2320 * and at least one from the outside world
2321 * (we checked that when we last locked
2324 vm_object_lock_assert_exclusive(copy_object
);
2325 copy_object
->ref_count
--;
2326 assert(copy_object
->ref_count
> 0);
2328 VM_OBJ_RES_DECR(copy_object
);
2329 vm_object_unlock(copy_object
);
2336 *top_page
= first_m
;
2338 if (m
!= VM_PAGE_NULL
) {
2339 assert(VM_PAGE_OBJECT(m
) == object
);
2341 retval
= VM_FAULT_SUCCESS
;
2343 if (my_fault
== DBG_PAGEIN_FAULT
) {
2344 VM_PAGE_COUNT_AS_PAGEIN(m
);
2346 if (object
->internal
) {
2347 my_fault
= DBG_PAGEIND_FAULT
;
2349 my_fault
= DBG_PAGEINV_FAULT
;
2353 * evaluate access pattern and update state
2354 * vm_fault_deactivate_behind depends on the
2355 * state being up to date
2357 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2358 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2359 } else if (type_of_fault
== NULL
&& my_fault
== DBG_CACHE_HIT_FAULT
) {
2361 * we weren't called from vm_fault, so handle the
2362 * accounting here for hits in the cache
2364 if (m
->vmp_clustered
) {
2365 VM_PAGE_COUNT_AS_PAGEIN(m
);
2366 VM_PAGE_CONSUME_CLUSTERED(m
);
2368 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2369 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2370 } else if (my_fault
== DBG_COMPRESSOR_FAULT
|| my_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
) {
2371 VM_STAT_DECOMPRESSIONS();
2373 if (type_of_fault
) {
2374 *type_of_fault
= my_fault
;
2377 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2378 assert(first_m
== VM_PAGE_NULL
);
2379 assert(object
== first_object
);
2382 thread_interrupt_level(interruptible_state
);
2385 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2390 thread_interrupt_level(interruptible_state
);
2392 if (wait_result
== THREAD_INTERRUPTED
) {
2393 return VM_FAULT_INTERRUPTED
;
2395 return VM_FAULT_RETRY
;
2404 * When soft faulting a page, we have to validate the page if:
2405 * 1. the page is being mapped in user space
2406 * 2. the page hasn't already been found to be "tainted"
2407 * 3. the page belongs to a code-signed object
2408 * 4. the page has not been validated yet or has been mapped for write.
2410 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page, page_obj) \
2411 ((pmap) != kernel_pmap /*1*/ && \
2412 !(page)->vmp_cs_tainted /*2*/ && \
2413 (page_obj)->code_signed /*3*/ && \
2414 (!(page)->vmp_cs_validated || (page)->vmp_wpmapped /*4*/ ))
2418 * page queue lock must NOT be held
2419 * m->vmp_object must be locked
2421 * NOTE: m->vmp_object could be locked "shared" only if we are called
2422 * from vm_fault() as part of a soft fault. If so, we must be
2423 * careful not to modify the VM object in any way that is not
2424 * legal under a shared lock...
2426 extern int panic_on_cs_killed
;
2427 extern int proc_selfpid(void);
2428 extern char *proc_name_address(void *p
);
2429 unsigned long cs_enter_tainted_rejected
= 0;
2430 unsigned long cs_enter_tainted_accepted
= 0;
2432 vm_fault_enter(vm_page_t m
,
2434 vm_map_offset_t vaddr
,
2436 vm_prot_t caller_prot
,
2438 boolean_t change_wiring
,
2440 vm_object_fault_info_t fault_info
,
2441 boolean_t
*need_retry
,
2444 kern_return_t kr
, pe_result
;
2445 boolean_t previously_pmapped
= m
->vmp_pmapped
;
2446 boolean_t must_disconnect
= 0;
2447 boolean_t map_is_switched
, map_is_switch_protected
;
2448 boolean_t cs_violation
;
2449 int cs_enforcement_enabled
;
2450 vm_prot_t fault_type
;
2452 boolean_t no_cache
= fault_info
->no_cache
;
2453 boolean_t cs_bypass
= fault_info
->cs_bypass
;
2454 int pmap_options
= fault_info
->pmap_options
;
2456 fault_type
= change_wiring
? VM_PROT_NONE
: caller_prot
;
2457 object
= VM_PAGE_OBJECT(m
);
2459 vm_object_lock_assert_held(object
);
2462 if (pmap
== kernel_pmap
) {
2463 kasan_notify_address(vaddr
, PAGE_SIZE
);
2467 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2469 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
2470 assert(m
->vmp_fictitious
);
2471 return KERN_SUCCESS
;
2474 if (*type_of_fault
== DBG_ZERO_FILL_FAULT
) {
2475 vm_object_lock_assert_exclusive(object
);
2476 } else if ((fault_type
& VM_PROT_WRITE
) == 0 &&
2478 #if VM_OBJECT_ACCESS_TRACKING
2479 || object
->access_tracking
2480 #endif /* VM_OBJECT_ACCESS_TRACKING */
2483 * This is not a "write" fault, so we
2484 * might not have taken the object lock
2485 * exclusively and we might not be able
2486 * to update the "wpmapped" bit in
2488 * Let's just grant read access to
2489 * the page for now and we'll
2490 * soft-fault again if we need write
2494 /* This had better not be a JIT page. */
2495 if (!pmap_has_prot_policy(prot
)) {
2496 prot
&= ~VM_PROT_WRITE
;
2501 if (m
->vmp_pmapped
== FALSE
) {
2502 if (m
->vmp_clustered
) {
2503 if (*type_of_fault
== DBG_CACHE_HIT_FAULT
) {
2505 * found it in the cache, but this
2506 * is the first fault-in of the page (m->vmp_pmapped == FALSE)
2507 * so it must have come in as part of
2508 * a cluster... account 1 pagein against it
2510 if (object
->internal
) {
2511 *type_of_fault
= DBG_PAGEIND_FAULT
;
2513 *type_of_fault
= DBG_PAGEINV_FAULT
;
2516 VM_PAGE_COUNT_AS_PAGEIN(m
);
2518 VM_PAGE_CONSUME_CLUSTERED(m
);
2522 if (*type_of_fault
!= DBG_COW_FAULT
) {
2523 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2525 if (pmap
== kernel_pmap
) {
2526 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2530 /* Validate code signature if necessary. */
2532 VM_FAULT_NEED_CS_VALIDATION(pmap
, m
, object
)) {
2533 vm_object_lock_assert_exclusive(object
);
2535 if (m
->vmp_cs_validated
) {
2536 vm_cs_revalidates
++;
2539 /* VM map is locked, so 1 ref will remain on VM object -
2540 * so no harm if vm_page_validate_cs drops the object lock */
2543 if (fault_info
->pmap_cs_associated
&&
2544 pmap_cs_enforced(pmap
) &&
2545 !m
->vmp_cs_validated
&&
2546 !m
->vmp_cs_tainted
&&
2548 (prot
& VM_PROT_EXECUTE
) &&
2549 (caller_prot
& VM_PROT_EXECUTE
)) {
2551 * With pmap_cs, the pmap layer will validate the
2552 * code signature for any executable pmap mapping.
2553 * No need for us to validate this page too:
2554 * in pmap_cs we trust...
2556 vm_cs_defer_to_pmap_cs
++;
2558 vm_cs_defer_to_pmap_cs_not
++;
2559 vm_page_validate_cs(m
);
2562 vm_page_validate_cs(m
);
2563 #endif /* PMAP_CS */
2566 #define page_immutable(m, prot) ((m)->vmp_cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/ )
2567 #define page_nx(m) ((m)->vmp_cs_nx)
2569 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2570 (pmap
== vm_map_pmap(current_thread()->map
)));
2571 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2573 /* If the map is switched, and is switch-protected, we must protect
2574 * some pages from being write-faulted: immutable pages because by
2575 * definition they may not be written, and executable pages because that
2576 * would provide a way to inject unsigned code.
2577 * If the page is immutable, we can simply return. However, we can't
2578 * immediately determine whether a page is executable anywhere. But,
2579 * we can disconnect it everywhere and remove the executable protection
2580 * from the current map. We do that below right before we do the
2583 if (pmap
== kernel_pmap
) {
2584 /* kernel fault: cs_process_enforcement() does not apply */
2585 cs_enforcement_enabled
= 0;
2587 cs_enforcement_enabled
= cs_process_enforcement(NULL
);
2590 if (cs_enforcement_enabled
&& map_is_switched
&&
2591 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2592 (prot
& VM_PROT_WRITE
)) {
2593 return KERN_CODESIGN_ERROR
;
2596 if (cs_enforcement_enabled
&& page_nx(m
) && (prot
& VM_PROT_EXECUTE
)) {
2598 printf("page marked to be NX, not letting it be mapped EXEC\n");
2600 return KERN_CODESIGN_ERROR
;
2603 /* A page could be tainted, or pose a risk of being tainted later.
2604 * Check whether the receiving process wants it, and make it feel
2605 * the consequences (that hapens in cs_invalid_page()).
2606 * For CS Enforcement, two other conditions will
2607 * cause that page to be tainted as well:
2608 * - pmapping an unsigned page executable - this means unsigned code;
2609 * - writeable mapping of a validated page - the content of that page
2610 * can be changed without the kernel noticing, therefore unsigned
2611 * code can be created
2614 /* code-signing is bypassed */
2615 cs_violation
= FALSE
;
2616 } else if (m
->vmp_cs_tainted
) {
2618 cs_violation
= TRUE
;
2619 } else if (!cs_enforcement_enabled
) {
2620 /* no further code-signing enforcement */
2621 cs_violation
= FALSE
;
2622 } else if (page_immutable(m
, prot
) &&
2623 ((prot
& VM_PROT_WRITE
) ||
2626 * The page should be immutable, but is in danger of being
2628 * This is the case where we want policy from the code
2629 * directory - is the page immutable or not? For now we have
2630 * to assume that code pages will be immutable, data pages not.
2631 * We'll assume a page is a code page if it has a code directory
2632 * and we fault for execution.
2633 * That is good enough since if we faulted the code page for
2634 * writing in another map before, it is wpmapped; if we fault
2635 * it for writing in this map later it will also be faulted for
2636 * executing at the same time; and if we fault for writing in
2637 * another map later, we will disconnect it from this pmap so
2638 * we'll notice the change.
2640 cs_violation
= TRUE
;
2641 } else if (!m
->vmp_cs_validated
&&
2642 (prot
& VM_PROT_EXECUTE
)
2645 * Executable pages will be validated by pmap_cs;
2646 * in pmap_cs we trust...
2647 * If pmap_cs is turned off, this is a code-signing
2650 && !(pmap_cs_enforced(pmap
))
2651 #endif /* PMAP_CS */
2653 cs_violation
= TRUE
;
2655 cs_violation
= FALSE
;
2659 /* We will have a tainted page. Have to handle the special case
2660 * of a switched map now. If the map is not switched, standard
2661 * procedure applies - call cs_invalid_page().
2662 * If the map is switched, the real owner is invalid already.
2663 * There is no point in invalidating the switching process since
2664 * it will not be executing from the map. So we don't call
2665 * cs_invalid_page() in that case. */
2666 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 m
->vmp_cs_validated
? "yes" : "no",
2676 m
->vmp_cs_tainted
? "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
= (char *)kalloc(__PATH_MAX
* 2);
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 kfree(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 m
->vmp_cs_validated
,
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
);
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
= (m
->vmp_cs_validated
);
2838 ceri
->ceri_page_codesig_tainted
= (m
->vmp_cs_tainted
);
2839 ceri
->ceri_page_codesig_nx
= (m
->vmp_cs_nx
);
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 m
->vmp_cs_validated
,
2894 if (file_object
!= object
) {
2895 vm_object_unlock(file_object
);
2897 if (pathname_len
!= 0) {
2898 kfree(pathname
, __PATH_MAX
* 2);
2903 /* proceed with the invalid page */
2905 if (!m
->vmp_cs_validated
&&
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 must_disconnect
= !m
->vmp_cs_tainted
;
2935 m
->vmp_cs_tainted
= TRUE
;
2937 cs_enter_tainted_accepted
++;
2939 if (kr
!= KERN_SUCCESS
) {
2941 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2942 "*** INVALID PAGE ***\n",
2946 if (cs_enforcement_panic
) {
2947 panic("CODESIGNING: panicking on invalid page\n");
2952 /* proceed with the valid page */
2956 boolean_t page_queues_locked
= FALSE
;
2957 #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \
2959 if (! page_queues_locked) { \
2960 page_queues_locked = TRUE; \
2961 vm_page_lockspin_queues(); \
2964 #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \
2966 if (page_queues_locked) { \
2967 page_queues_locked = FALSE; \
2968 vm_page_unlock_queues(); \
2973 * Hold queues lock to manipulate
2974 * the page queues. Change wiring
2977 assert((m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) || object
!= compressor_object
);
2979 #if CONFIG_BACKGROUND_QUEUE
2980 vm_page_update_background_state(m
);
2982 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
2984 * Compressor pages are neither wired
2985 * nor pageable and should never change.
2987 assert(object
== compressor_object
);
2988 } else if (change_wiring
) {
2989 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
2992 if (kr
== KERN_SUCCESS
) {
2993 vm_page_wire(m
, wire_tag
, TRUE
);
2996 vm_page_unwire(m
, TRUE
);
2998 /* we keep the page queues lock, if we need it later */
3000 if (object
->internal
== TRUE
) {
3002 * don't allow anonymous pages on
3003 * the speculative queues
3007 if (kr
!= KERN_SUCCESS
) {
3008 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3009 vm_page_deactivate(m
);
3010 /* we keep the page queues lock, if we need it later */
3011 } else if (((m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
) ||
3012 (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) ||
3013 (m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) ||
3014 ((m
->vmp_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && no_cache
)) &&
3015 !VM_PAGE_WIRED(m
)) {
3016 if (vm_page_local_q
&&
3017 (*type_of_fault
== DBG_COW_FAULT
||
3018 *type_of_fault
== DBG_ZERO_FILL_FAULT
)) {
3022 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
3024 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3025 vm_object_lock_assert_exclusive(object
);
3028 * we got a local queue to stuff this
3030 * its safe to manipulate local and
3031 * local_id at this point since we're
3032 * behind an exclusive object lock and
3033 * the page is not on any global queue.
3035 * we'll use the current cpu number to
3036 * select the queue note that we don't
3037 * need to disable preemption... we're
3038 * going to be behind the local queue's
3039 * lock to do the real work
3043 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
3045 VPL_LOCK(&lq
->vpl_lock
);
3047 vm_page_check_pageable_safe(m
);
3048 vm_page_queue_enter(&lq
->vpl_queue
, m
, vmp_pageq
);
3049 m
->vmp_q_state
= VM_PAGE_ON_ACTIVE_LOCAL_Q
;
3050 m
->vmp_local_id
= lid
;
3053 if (object
->internal
) {
3054 lq
->vpl_internal_count
++;
3056 lq
->vpl_external_count
++;
3059 VPL_UNLOCK(&lq
->vpl_lock
);
3061 if (lq
->vpl_count
> vm_page_local_q_soft_limit
) {
3063 * we're beyond the soft limit
3064 * for the local queue
3065 * vm_page_reactivate_local will
3066 * 'try' to take the global page
3067 * queue lock... if it can't
3068 * that's ok... we'll let the
3069 * queue continue to grow up
3070 * to the hard limit... at that
3071 * point we'll wait for the
3072 * lock... once we've got the
3073 * lock, we'll transfer all of
3074 * the pages from the local
3075 * queue to the global active
3078 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
3081 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3084 * test again now that we hold the
3087 if (!VM_PAGE_WIRED(m
)) {
3088 if (m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) {
3089 vm_page_queues_remove(m
, FALSE
);
3091 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated
, 1);
3092 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_fault_reactivated
, 1);
3095 if (!VM_PAGE_ACTIVE_OR_INACTIVE(m
) ||
3098 * If this is a no_cache mapping
3099 * and the page has never been
3100 * mapped before or was
3101 * previously a no_cache page,
3102 * then we want to leave pages
3103 * in the speculative state so
3104 * that they can be readily
3105 * recycled if free memory runs
3106 * low. Otherwise the page is
3107 * activated as normal.
3111 (!previously_pmapped
||
3113 m
->vmp_no_cache
= TRUE
;
3115 if (m
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
3116 vm_page_speculate(m
, FALSE
);
3118 } else if (!VM_PAGE_ACTIVE_OR_INACTIVE(m
)) {
3119 vm_page_activate(m
);
3123 /* we keep the page queues lock, if we need it later */
3127 /* we're done with the page queues lock, if we ever took it */
3128 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3131 /* If we have a KERN_SUCCESS from the previous checks, we either have
3132 * a good page, or a tainted page that has been accepted by the process.
3133 * In both cases the page will be entered into the pmap.
3134 * If the page is writeable, we need to disconnect it from other pmaps
3135 * now so those processes can take note.
3137 if (kr
== KERN_SUCCESS
) {
3139 * NOTE: we may only hold the vm_object lock SHARED
3140 * at this point, so we need the phys_page lock to
3141 * properly serialize updating the pmapped and
3144 if ((prot
& VM_PROT_EXECUTE
) && !m
->vmp_xpmapped
) {
3145 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3147 pmap_lock_phys_page(phys_page
);
3149 * go ahead and take the opportunity
3150 * to set 'pmapped' here so that we don't
3151 * need to grab this lock a 2nd time
3154 m
->vmp_pmapped
= TRUE
;
3156 if (!m
->vmp_xpmapped
) {
3157 m
->vmp_xpmapped
= TRUE
;
3159 pmap_unlock_phys_page(phys_page
);
3161 if (!object
->internal
) {
3162 OSAddAtomic(1, &vm_page_xpmapped_external_count
);
3165 #if defined(__arm__) || defined(__arm64__)
3166 pmap_sync_page_data_phys(phys_page
);
3168 if (object
->internal
&&
3169 object
->pager
!= NULL
) {
3171 * This page could have been
3172 * uncompressed by the
3173 * compressor pager and its
3174 * contents might be only in
3176 * Since it's being mapped for
3177 * "execute" for the fist time,
3178 * make sure the icache is in
3181 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3182 pmap_sync_page_data_phys(phys_page
);
3186 pmap_unlock_phys_page(phys_page
);
3189 if (m
->vmp_pmapped
== FALSE
) {
3190 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3192 pmap_lock_phys_page(phys_page
);
3193 m
->vmp_pmapped
= TRUE
;
3194 pmap_unlock_phys_page(phys_page
);
3198 if (fault_type
& VM_PROT_WRITE
) {
3199 if (m
->vmp_wpmapped
== FALSE
) {
3200 vm_object_lock_assert_exclusive(object
);
3201 if (!object
->internal
&& object
->pager
) {
3202 task_update_logical_writes(current_task(), PAGE_SIZE
, TASK_WRITE_DEFERRED
, vnode_pager_lookup_vnode(object
->pager
));
3204 m
->vmp_wpmapped
= TRUE
;
3206 if (must_disconnect
) {
3208 * We can only get here
3209 * because of the CSE logic
3211 assert(cs_enforcement_enabled
);
3212 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
3214 * If we are faulting for a write, we can clear
3215 * the execute bit - that will ensure the page is
3216 * checked again before being executable, which
3217 * protects against a map switch.
3218 * This only happens the first time the page
3219 * gets tainted, so we won't get stuck here
3220 * to make an already writeable page executable.
3223 assert(!pmap_has_prot_policy(prot
));
3224 prot
&= ~VM_PROT_EXECUTE
;
3228 assert(VM_PAGE_OBJECT(m
) == object
);
3230 #if VM_OBJECT_ACCESS_TRACKING
3231 if (object
->access_tracking
) {
3232 DTRACE_VM2(access_tracking
, vm_map_offset_t
, vaddr
, int, fault_type
);
3233 if (fault_type
& VM_PROT_WRITE
) {
3234 object
->access_tracking_writes
++;
3235 vm_object_access_tracking_writes
++;
3237 object
->access_tracking_reads
++;
3238 vm_object_access_tracking_reads
++;
3241 #endif /* VM_OBJECT_ACCESS_TRACKING */
3247 /* Prevent a deadlock by not
3248 * holding the object lock if we need to wait for a page in
3249 * pmap_enter() - <rdar://problem/7138958> */
3250 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
, 0,
3252 pmap_options
| PMAP_OPTIONS_NOWAIT
,
3256 * Retry without execute permission if we encountered a codesigning
3257 * failure on a non-execute fault. This allows applications which
3258 * don't actually need to execute code to still map it for read access.
3260 if ((pe_result
== KERN_CODESIGN_ERROR
) && pmap_cs_enforced(pmap
) &&
3261 (prot
& VM_PROT_EXECUTE
) && !(caller_prot
& VM_PROT_EXECUTE
)) {
3262 prot
&= ~VM_PROT_EXECUTE
;
3263 goto pmap_enter_retry
;
3267 if (pe_result
== KERN_INVALID_ARGUMENT
&&
3268 pmap
== PMAP_NULL
&&
3271 * Wiring a page in a pmap-less VM map:
3272 * VMware's "vmmon" kernel extension does this
3274 * Let it proceed even though the PMAP_ENTER() failed.
3276 pe_result
= KERN_SUCCESS
;
3278 #endif /* __x86_64__ */
3280 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
3283 * this will be non-null in the case where we hold the lock
3284 * on the top-object in this chain... we can't just drop
3285 * the lock on the object we're inserting the page into
3286 * and recall the PMAP_ENTER since we can still cause
3287 * a deadlock if one of the critical paths tries to
3288 * acquire the lock on the top-object and we're blocked
3289 * in PMAP_ENTER waiting for memory... our only recourse
3290 * is to deal with it at a higher level where we can
3294 vm_pmap_enter_retried
++;
3295 goto after_the_pmap_enter
;
3297 /* The nonblocking version of pmap_enter did not succeed.
3298 * and we don't need to drop other locks and retry
3299 * at the level above us, so
3300 * use the blocking version instead. Requires marking
3301 * the page busy and unlocking the object */
3302 boolean_t was_busy
= m
->vmp_busy
;
3304 vm_object_lock_assert_exclusive(object
);
3307 vm_object_unlock(object
);
3309 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
,
3311 pmap_options
, pe_result
);
3313 assert(VM_PAGE_OBJECT(m
) == object
);
3315 /* Take the object lock again. */
3316 vm_object_lock(object
);
3318 /* If the page was busy, someone else will wake it up.
3319 * Otherwise, we have to do it now. */
3320 assert(m
->vmp_busy
);
3322 PAGE_WAKEUP_DONE(m
);
3324 vm_pmap_enter_blocked
++;
3330 after_the_pmap_enter
:
3335 vm_pre_fault(vm_map_offset_t vaddr
, vm_prot_t prot
)
3337 if (pmap_find_phys(current_map()->pmap
, vaddr
) == 0) {
3338 vm_fault(current_map(), /* map */
3340 prot
, /* fault_type */
3341 FALSE
, /* change_wiring */
3342 VM_KERN_MEMORY_NONE
, /* tag - not wiring */
3343 THREAD_UNINT
, /* interruptible */
3344 NULL
, /* caller_pmap */
3345 0 /* caller_pmap_addr */);
3353 * Handle page faults, including pseudo-faults
3354 * used to change the wiring status of pages.
3356 * Explicit continuations have been removed.
3358 * vm_fault and vm_fault_page save mucho state
3359 * in the moral equivalent of a closure. The state
3360 * structure is allocated when first entering vm_fault
3361 * and deallocated when leaving vm_fault.
3364 extern int _map_enter_debug
;
3365 extern uint64_t get_current_unique_pid(void);
3367 unsigned long vm_fault_collapse_total
= 0;
3368 unsigned long vm_fault_collapse_skipped
= 0;
3374 vm_map_offset_t vaddr
,
3375 vm_prot_t fault_type
,
3376 boolean_t change_wiring
,
3379 vm_map_offset_t caller_pmap_addr
)
3381 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
, vm_tag_bt(),
3382 interruptible
, caller_pmap
, caller_pmap_addr
,
3389 vm_map_offset_t vaddr
,
3390 vm_prot_t fault_type
,
3391 boolean_t change_wiring
,
3392 vm_tag_t wire_tag
, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3395 vm_map_offset_t caller_pmap_addr
)
3397 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
, wire_tag
,
3398 interruptible
, caller_pmap
, caller_pmap_addr
,
3403 current_proc_is_privileged(void)
3405 return csproc_get_platform_binary(current_proc());
3408 uint64_t vm_copied_on_read
= 0;
3413 vm_map_offset_t vaddr
,
3414 vm_prot_t caller_prot
,
3415 boolean_t change_wiring
,
3416 vm_tag_t wire_tag
, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3419 vm_map_offset_t caller_pmap_addr
,
3420 ppnum_t
*physpage_p
)
3422 vm_map_version_t version
; /* Map version for verificiation */
3423 boolean_t wired
; /* Should mapping be wired down? */
3424 vm_object_t object
; /* Top-level object */
3425 vm_object_offset_t offset
; /* Top-level offset */
3426 vm_prot_t prot
; /* Protection for mapping */
3427 vm_object_t old_copy_object
; /* Saved copy object */
3428 vm_page_t result_page
; /* Result of vm_fault_page */
3429 vm_page_t top_page
; /* Placeholder page */
3432 vm_page_t m
; /* Fast access to result_page */
3433 kern_return_t error_code
;
3434 vm_object_t cur_object
;
3435 vm_object_t m_object
= NULL
;
3436 vm_object_offset_t cur_offset
;
3438 vm_object_t new_object
;
3441 wait_interrupt_t interruptible_state
;
3442 vm_map_t real_map
= map
;
3443 vm_map_t original_map
= map
;
3444 boolean_t object_locks_dropped
= FALSE
;
3445 vm_prot_t fault_type
;
3446 vm_prot_t original_fault_type
;
3447 struct vm_object_fault_info fault_info
= {};
3448 boolean_t need_collapse
= FALSE
;
3449 boolean_t need_retry
= FALSE
;
3450 boolean_t
*need_retry_ptr
= NULL
;
3451 int object_lock_type
= 0;
3452 int cur_object_lock_type
;
3453 vm_object_t top_object
= VM_OBJECT_NULL
;
3454 vm_object_t written_on_object
= VM_OBJECT_NULL
;
3455 memory_object_t written_on_pager
= NULL
;
3456 vm_object_offset_t written_on_offset
= 0;
3458 int compressed_count_delta
;
3460 boolean_t need_copy
;
3461 boolean_t need_copy_on_read
;
3462 vm_map_offset_t trace_vaddr
;
3463 vm_map_offset_t trace_real_vaddr
;
3464 vm_map_offset_t real_vaddr
;
3465 boolean_t resilient_media_retry
= FALSE
;
3466 vm_object_t resilient_media_object
= VM_OBJECT_NULL
;
3467 vm_object_offset_t resilient_media_offset
= (vm_object_offset_t
)-1;
3470 trace_real_vaddr
= vaddr
;
3471 vaddr
= vm_map_trunc_page(vaddr
, PAGE_MASK
);
3473 if (map
== kernel_map
) {
3474 trace_vaddr
= VM_KERNEL_ADDRHIDE(vaddr
);
3475 trace_real_vaddr
= VM_KERNEL_ADDRHIDE(trace_real_vaddr
);
3477 trace_vaddr
= vaddr
;
3480 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3481 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
3482 ((uint64_t)trace_vaddr
>> 32),
3484 (map
== kernel_map
),
3488 if (get_preemption_level() != 0) {
3489 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3490 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3491 ((uint64_t)trace_vaddr
>> 32),
3497 return KERN_FAILURE
;
3500 thread_t cthread
= current_thread();
3501 boolean_t rtfault
= (cthread
->sched_mode
== TH_MODE_REALTIME
);
3502 uint64_t fstart
= 0;
3505 fstart
= mach_continuous_time();
3508 interruptible_state
= thread_interrupt_level(interruptible
);
3510 fault_type
= (change_wiring
? VM_PROT_NONE
: caller_prot
);
3512 VM_STAT_INCR(faults
);
3513 current_task()->faults
++;
3514 original_fault_type
= fault_type
;
3517 if (fault_type
& VM_PROT_WRITE
) {
3522 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3524 object_lock_type
= OBJECT_LOCK_SHARED
;
3527 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
3529 if ((map
== kernel_map
) && (caller_prot
& VM_PROT_WRITE
)) {
3530 if (compressor_map
) {
3531 if ((vaddr
>= vm_map_min(compressor_map
)) && (vaddr
< vm_map_max(compressor_map
))) {
3532 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
));
3537 assert(written_on_object
== VM_OBJECT_NULL
);
3540 * assume we will hit a page in the cache
3541 * otherwise, explicitly override with
3542 * the real fault type once we determine it
3544 type_of_fault
= DBG_CACHE_HIT_FAULT
;
3547 * Find the backing store object and offset into
3548 * it to begin the search.
3550 fault_type
= original_fault_type
;
3552 vm_map_lock_read(map
);
3554 if (resilient_media_retry
) {
3556 * If we have to insert a fake zero-filled page to hide
3557 * a media failure to provide the real page, we need to
3558 * resolve any pending copy-on-write on this mapping.
3559 * VM_PROT_COPY tells vm_map_lookup_locked() to deal
3560 * with that even if this is not a "write" fault.
3563 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3566 kr
= vm_map_lookup_locked(&map
, vaddr
,
3567 (fault_type
| (need_copy
? VM_PROT_COPY
: 0)),
3568 object_lock_type
, &version
,
3569 &object
, &offset
, &prot
, &wired
,
3573 if (kr
!= KERN_SUCCESS
) {
3574 vm_map_unlock_read(map
);
3577 pmap
= real_map
->pmap
;
3578 fault_info
.interruptible
= interruptible
;
3579 fault_info
.stealth
= FALSE
;
3580 fault_info
.io_sync
= FALSE
;
3581 fault_info
.mark_zf_absent
= FALSE
;
3582 fault_info
.batch_pmap_op
= FALSE
;
3584 if (resilient_media_retry
) {
3586 * We're retrying this fault after having detected a media
3587 * failure from a "resilient_media" mapping.
3588 * Check that the mapping is still pointing at the object
3589 * that just failed to provide a page.
3591 assert(resilient_media_object
!= VM_OBJECT_NULL
);
3592 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
3593 if (object
!= VM_OBJECT_NULL
&&
3594 object
== resilient_media_object
&&
3595 offset
== resilient_media_offset
&&
3596 fault_info
.resilient_media
) {
3598 * This mapping still points at the same object
3599 * and is still "resilient_media": proceed in
3600 * "recovery-from-media-failure" mode, where we'll
3601 * insert a zero-filled page in the top object.
3603 // printf("RESILIENT_MEDIA %s:%d recovering for object %p offset 0x%llx\n", __FUNCTION__, __LINE__, object, offset);
3605 /* not recovering: reset state */
3606 // 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);
3607 resilient_media_retry
= FALSE
;
3608 /* release our extra reference on failed object */
3609 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
3610 vm_object_deallocate(resilient_media_object
);
3611 resilient_media_object
= VM_OBJECT_NULL
;
3612 resilient_media_offset
= (vm_object_offset_t
)-1;
3615 assert(resilient_media_object
== VM_OBJECT_NULL
);
3616 resilient_media_offset
= (vm_object_offset_t
)-1;
3620 * If the page is wired, we must fault for the current protection
3621 * value, to avoid further faults.
3624 fault_type
= prot
| VM_PROT_WRITE
;
3626 if (wired
|| need_copy
) {
3628 * since we're treating this fault as a 'write'
3629 * we must hold the top object lock exclusively
3631 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3632 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3634 if (vm_object_lock_upgrade(object
) == FALSE
) {
3636 * couldn't upgrade, so explictly
3637 * take the lock exclusively
3639 vm_object_lock(object
);
3644 #if VM_FAULT_CLASSIFY
3646 * Temporary data gathering code
3648 vm_fault_classify(object
, offset
, fault_type
);
3651 * Fast fault code. The basic idea is to do as much as
3652 * possible while holding the map lock and object locks.
3653 * Busy pages are not used until the object lock has to
3654 * be dropped to do something (copy, zero fill, pmap enter).
3655 * Similarly, paging references aren't acquired until that
3656 * point, and object references aren't used.
3658 * If we can figure out what to do
3659 * (zero fill, copy on write, pmap enter) while holding
3660 * the locks, then it gets done. Otherwise, we give up,
3661 * and use the original fault path (which doesn't hold
3662 * the map lock, and relies on busy pages).
3663 * The give up cases include:
3664 * - Have to talk to pager.
3665 * - Page is busy, absent or in error.
3666 * - Pager has locked out desired access.
3667 * - Fault needs to be restarted.
3668 * - Have to push page into copy object.
3670 * The code is an infinite loop that moves one level down
3671 * the shadow chain each time. cur_object and cur_offset
3672 * refer to the current object being examined. object and offset
3673 * are the original object from the map. The loop is at the
3674 * top level if and only if object and cur_object are the same.
3676 * Invariants: Map lock is held throughout. Lock is held on
3677 * original object and cur_object (if different) when
3678 * continuing or exiting loop.
3682 #if defined(__arm64__)
3684 * Fail if reading an execute-only page in a
3685 * pmap that enforces execute-only protection.
3687 if (fault_type
== VM_PROT_READ
&&
3688 (prot
& VM_PROT_EXECUTE
) &&
3689 !(prot
& VM_PROT_READ
) &&
3690 pmap_enforces_execute_only(pmap
)) {
3691 vm_object_unlock(object
);
3692 vm_map_unlock_read(map
);
3693 if (real_map
!= map
) {
3694 vm_map_unlock(real_map
);
3696 kr
= KERN_PROTECTION_FAILURE
;
3702 * If this page is to be inserted in a copy delay object
3703 * for writing, and if the object has a copy, then the
3704 * copy delay strategy is implemented in the slow fault page.
3706 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
3707 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
)) {
3708 goto handle_copy_delay
;
3711 cur_object
= object
;
3712 cur_offset
= offset
;
3715 #if CONFIG_SECLUDED_MEMORY
3716 if (object
->can_grab_secluded
) {
3717 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
3719 #endif /* CONFIG_SECLUDED_MEMORY */
3722 if (!cur_object
->pager_created
&&
3723 cur_object
->phys_contiguous
) { /* superpage */
3727 if (cur_object
->blocked_access
) {
3729 * Access to this VM object has been blocked.
3730 * Let the slow path handle it.
3735 m
= vm_page_lookup(cur_object
, cur_offset
);
3738 if (m
!= VM_PAGE_NULL
) {
3739 m_object
= cur_object
;
3742 wait_result_t result
;
3745 * in order to do the PAGE_ASSERT_WAIT, we must
3746 * have object that 'm' belongs to locked exclusively
3748 if (object
!= cur_object
) {
3749 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3750 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3752 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3754 * couldn't upgrade so go do a full retry
3755 * immediately since we can no longer be
3756 * certain about cur_object (since we
3757 * don't hold a reference on it)...
3758 * first drop the top object lock
3760 vm_object_unlock(object
);
3762 vm_map_unlock_read(map
);
3763 if (real_map
!= map
) {
3764 vm_map_unlock(real_map
);
3770 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3771 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3773 if (vm_object_lock_upgrade(object
) == FALSE
) {
3775 * couldn't upgrade, so explictly take the lock
3776 * exclusively and go relookup the page since we
3777 * will have dropped the object lock and
3778 * a different thread could have inserted
3779 * a page at this offset
3780 * no need for a full retry since we're
3781 * at the top level of the object chain
3783 vm_object_lock(object
);
3788 if ((m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) && m_object
->internal
) {
3790 * m->vmp_busy == TRUE and the object is locked exclusively
3791 * if m->pageout_queue == TRUE after we acquire the
3792 * queues lock, we are guaranteed that it is stable on
3793 * the pageout queue and therefore reclaimable
3795 * NOTE: this is only true for the internal pageout queue
3796 * in the compressor world
3798 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3800 vm_page_lock_queues();
3802 if (m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) {
3803 vm_pageout_throttle_up(m
);
3804 vm_page_unlock_queues();
3806 PAGE_WAKEUP_DONE(m
);
3807 goto reclaimed_from_pageout
;
3809 vm_page_unlock_queues();
3811 if (object
!= cur_object
) {
3812 vm_object_unlock(object
);
3815 vm_map_unlock_read(map
);
3816 if (real_map
!= map
) {
3817 vm_map_unlock(real_map
);
3820 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
3822 vm_object_unlock(cur_object
);
3824 if (result
== THREAD_WAITING
) {
3825 result
= thread_block(THREAD_CONTINUE_NULL
);
3827 counter(c_vm_fault_page_block_busy_kernel
++);
3829 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
) {
3836 reclaimed_from_pageout
:
3837 if (m
->vmp_laundry
) {
3838 if (object
!= cur_object
) {
3839 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3840 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3842 vm_object_unlock(object
);
3843 vm_object_unlock(cur_object
);
3845 vm_map_unlock_read(map
);
3846 if (real_map
!= map
) {
3847 vm_map_unlock(real_map
);
3852 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3853 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3855 if (vm_object_lock_upgrade(object
) == FALSE
) {
3857 * couldn't upgrade, so explictly take the lock
3858 * exclusively and go relookup the page since we
3859 * will have dropped the object lock and
3860 * a different thread could have inserted
3861 * a page at this offset
3862 * no need for a full retry since we're
3863 * at the top level of the object chain
3865 vm_object_lock(object
);
3870 vm_pageout_steal_laundry(m
, FALSE
);
3873 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
3875 * Guard page: let the slow path deal with it
3879 if (m
->vmp_unusual
&& (m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_private
|| m
->vmp_absent
)) {
3881 * Unusual case... let the slow path deal with it
3885 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object
)) {
3886 if (object
!= cur_object
) {
3887 vm_object_unlock(object
);
3889 vm_map_unlock_read(map
);
3890 if (real_map
!= map
) {
3891 vm_map_unlock(real_map
);
3893 vm_object_unlock(cur_object
);
3894 kr
= KERN_MEMORY_ERROR
;
3897 assert(m_object
== VM_PAGE_OBJECT(m
));
3899 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
, m_object
) ||
3900 (physpage_p
!= NULL
&& (prot
& VM_PROT_WRITE
))) {
3901 upgrade_lock_and_retry
:
3903 * We might need to validate this page
3904 * against its code signature, so we
3905 * want to hold the VM object exclusively.
3907 if (object
!= cur_object
) {
3908 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3909 vm_object_unlock(object
);
3910 vm_object_unlock(cur_object
);
3912 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3914 vm_map_unlock_read(map
);
3915 if (real_map
!= map
) {
3916 vm_map_unlock(real_map
);
3921 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3922 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3924 if (vm_object_lock_upgrade(object
) == FALSE
) {
3926 * couldn't upgrade, so explictly take the lock
3927 * exclusively and go relookup the page since we
3928 * will have dropped the object lock and
3929 * a different thread could have inserted
3930 * a page at this offset
3931 * no need for a full retry since we're
3932 * at the top level of the object chain
3934 vm_object_lock(object
);
3941 * Two cases of map in faults:
3942 * - At top level w/o copy object.
3943 * - Read fault anywhere.
3944 * --> must disallow write.
3947 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
3952 !fault_info
.no_copy_on_read
&&
3953 cur_object
!= object
&&
3954 !cur_object
->internal
&&
3955 !cur_object
->pager_trusted
&&
3956 vm_protect_privileged_from_untrusted
&&
3957 !((prot
& VM_PROT_EXECUTE
) &&
3958 cur_object
->code_signed
&&
3959 cs_process_enforcement(NULL
)) &&
3960 current_proc_is_privileged()) {
3962 * We're faulting on a page in "object" and
3963 * went down the shadow chain to "cur_object"
3964 * to find out that "cur_object"'s pager
3965 * is not "trusted", i.e. we can not trust it
3966 * to always return the same contents.
3967 * Since the target is a "privileged" process,
3968 * let's treat this as a copy-on-read fault, as
3969 * if it was a copy-on-write fault.
3970 * Once "object" gets a copy of this page, it
3971 * won't have to rely on "cur_object" to
3972 * provide the contents again.
3974 * This is done by setting "need_copy" and
3975 * retrying the fault from the top with the
3976 * appropriate locking.
3978 * Special case: if the mapping is executable
3979 * and the untrusted object is code-signed and
3980 * the process is "cs_enforced", we do not
3981 * copy-on-read because that would break
3982 * code-signing enforcement expectations (an
3983 * executable page must belong to a code-signed
3984 * object) and we can rely on code-signing
3985 * to re-validate the page if it gets evicted
3986 * and paged back in.
3988 // 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);
3989 vm_copied_on_read
++;
3992 vm_object_unlock(object
);
3993 vm_object_unlock(cur_object
);
3994 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3995 vm_map_unlock_read(map
);
3996 if (real_map
!= map
) {
3997 vm_map_unlock(real_map
);
4002 if (!(fault_type
& VM_PROT_WRITE
) && !need_copy
) {
4003 if (!pmap_has_prot_policy(prot
)) {
4004 prot
&= ~VM_PROT_WRITE
;
4007 * For a protection that the pmap cares
4008 * about, we must hand over the full
4009 * set of protections (so that the pmap
4010 * layer can apply any desired policy).
4011 * This means that cs_bypass must be
4012 * set, as this can force us to pass
4015 assert(fault_info
.cs_bypass
);
4018 if (object
!= cur_object
) {
4020 * We still need to hold the top object
4021 * lock here to prevent a race between
4022 * a read fault (taking only "shared"
4023 * locks) and a write fault (taking
4024 * an "exclusive" lock on the top
4026 * Otherwise, as soon as we release the
4027 * top lock, the write fault could
4028 * proceed and actually complete before
4029 * the read fault, and the copied page's
4030 * translation could then be overwritten
4031 * by the read fault's translation for
4032 * the original page.
4034 * Let's just record what the top object
4035 * is and we'll release it later.
4037 top_object
= object
;
4040 * switch to the object that has the new page
4042 object
= cur_object
;
4043 object_lock_type
= cur_object_lock_type
;
4046 assert(m_object
== VM_PAGE_OBJECT(m
));
4049 * prepare for the pmap_enter...
4050 * object and map are both locked
4051 * m contains valid data
4052 * object == m->vmp_object
4053 * cur_object == NULL or it's been unlocked
4054 * no paging references on either object or cur_object
4056 if (top_object
!= VM_OBJECT_NULL
|| object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4057 need_retry_ptr
= &need_retry
;
4059 need_retry_ptr
= NULL
;
4063 kr
= vm_fault_enter(m
,
4075 kr
= vm_fault_enter(m
,
4090 if (m_object
->internal
) {
4091 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
4092 } else if (m_object
->object_is_shared_cache
) {
4093 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
4095 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
4098 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);
4099 if (need_retry
== FALSE
) {
4100 KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_FAST
), get_current_unique_pid(), 0, 0, 0, 0);
4102 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
);
4104 if (kr
== KERN_SUCCESS
&&
4105 physpage_p
!= NULL
) {
4106 /* for vm_map_wire_and_extract() */
4107 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
4108 if (prot
& VM_PROT_WRITE
) {
4109 vm_object_lock_assert_exclusive(m_object
);
4110 m
->vmp_dirty
= TRUE
;
4114 if (top_object
!= VM_OBJECT_NULL
) {
4116 * It's safe to drop the top object
4117 * now that we've done our
4118 * vm_fault_enter(). Any other fault
4119 * in progress for that virtual
4120 * address will either find our page
4121 * and translation or put in a new page
4124 vm_object_unlock(top_object
);
4125 top_object
= VM_OBJECT_NULL
;
4128 if (need_collapse
== TRUE
) {
4129 vm_object_collapse(object
, offset
, TRUE
);
4132 if (need_retry
== FALSE
&&
4133 (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
)) {
4135 * evaluate access pattern and update state
4136 * vm_fault_deactivate_behind depends on the
4137 * state being up to date
4139 vm_fault_is_sequential(m_object
, cur_offset
, fault_info
.behavior
);
4141 vm_fault_deactivate_behind(m_object
, cur_offset
, fault_info
.behavior
);
4144 * That's it, clean up and return.
4147 PAGE_WAKEUP_DONE(m
);
4150 if (need_retry
== FALSE
&& !m_object
->internal
&& (fault_type
& VM_PROT_WRITE
)) {
4151 vm_object_paging_begin(m_object
);
4153 assert(written_on_object
== VM_OBJECT_NULL
);
4154 written_on_object
= m_object
;
4155 written_on_pager
= m_object
->pager
;
4156 written_on_offset
= m_object
->paging_offset
+ m
->vmp_offset
;
4158 vm_object_unlock(object
);
4160 vm_map_unlock_read(map
);
4161 if (real_map
!= map
) {
4162 vm_map_unlock(real_map
);
4165 if (need_retry
== TRUE
) {
4167 * vm_fault_enter couldn't complete the PMAP_ENTER...
4168 * at this point we don't hold any locks so it's safe
4169 * to ask the pmap layer to expand the page table to
4170 * accommodate this mapping... once expanded, we'll
4171 * re-drive the fault which should result in vm_fault_enter
4172 * being able to successfully enter the mapping this time around
4174 (void)pmap_enter_options(
4175 pmap
, vaddr
, 0, 0, 0, 0, 0,
4176 PMAP_OPTIONS_NOENTER
, NULL
);
4184 * COPY ON WRITE FAULT
4186 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
4189 * If objects match, then
4190 * object->copy must not be NULL (else control
4191 * would be in previous code block), and we
4192 * have a potential push into the copy object
4193 * with which we can't cope with here.
4195 if (cur_object
== object
) {
4197 * must take the slow path to
4198 * deal with the copy push
4204 * This is now a shadow based copy on write
4205 * fault -- it requires a copy up the shadow
4208 assert(m_object
== VM_PAGE_OBJECT(m
));
4210 if ((cur_object_lock_type
== OBJECT_LOCK_SHARED
) &&
4211 VM_FAULT_NEED_CS_VALIDATION(NULL
, m
, m_object
)) {
4212 goto upgrade_lock_and_retry
;
4216 * Allocate a page in the original top level
4217 * object. Give up if allocate fails. Also
4218 * need to remember current page, as it's the
4219 * source of the copy.
4221 * at this point we hold locks on both
4222 * object and cur_object... no need to take
4223 * paging refs or mark pages BUSY since
4224 * we don't drop either object lock until
4225 * the page has been copied and inserted
4228 m
= vm_page_grab_options(grab_options
);
4231 if (m
== VM_PAGE_NULL
) {
4233 * no free page currently available...
4234 * must take the slow path
4239 * Now do the copy. Mark the source page busy...
4241 * NOTE: This code holds the map lock across
4244 vm_page_copy(cur_m
, m
);
4245 vm_page_insert(m
, object
, offset
);
4247 SET_PAGE_DIRTY(m
, FALSE
);
4250 * Now cope with the source page and object
4252 if (object
->ref_count
> 1 && cur_m
->vmp_pmapped
) {
4253 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m
));
4256 if (cur_m
->vmp_clustered
) {
4257 VM_PAGE_COUNT_AS_PAGEIN(cur_m
);
4258 VM_PAGE_CONSUME_CLUSTERED(cur_m
);
4259 vm_fault_is_sequential(cur_object
, cur_offset
, fault_info
.behavior
);
4261 need_collapse
= TRUE
;
4263 if (!cur_object
->internal
&&
4264 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
4266 * The object from which we've just
4267 * copied a page is most probably backed
4268 * by a vnode. We don't want to waste too
4269 * much time trying to collapse the VM objects
4270 * and create a bottleneck when several tasks
4271 * map the same file.
4273 if (cur_object
->copy
== object
) {
4275 * Shared mapping or no COW yet.
4276 * We can never collapse a copy
4277 * object into its backing object.
4279 need_collapse
= FALSE
;
4280 } else if (cur_object
->copy
== object
->shadow
&&
4281 object
->shadow
->resident_page_count
== 0) {
4283 * Shared mapping after a COW occurred.
4285 need_collapse
= FALSE
;
4288 vm_object_unlock(cur_object
);
4290 if (need_collapse
== FALSE
) {
4291 vm_fault_collapse_skipped
++;
4293 vm_fault_collapse_total
++;
4295 type_of_fault
= DBG_COW_FAULT
;
4296 VM_STAT_INCR(cow_faults
);
4297 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
4298 current_task()->cow_faults
++;
4303 * No page at cur_object, cur_offset... m == NULL
4305 if (cur_object
->pager_created
) {
4306 int compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
4308 if (MUST_ASK_PAGER(cur_object
, cur_offset
, compressor_external_state
) == TRUE
) {
4310 int c_flags
= C_DONT_BLOCK
;
4311 boolean_t insert_cur_object
= FALSE
;
4314 * May have to talk to a pager...
4315 * if so, take the slow path by
4316 * doing a 'break' from the while (TRUE) loop
4318 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
4319 * if the compressor is active and the page exists there
4321 if (compressor_external_state
!= VM_EXTERNAL_STATE_EXISTS
) {
4325 if (map
== kernel_map
|| real_map
== kernel_map
) {
4327 * can't call into the compressor with the kernel_map
4328 * lock held, since the compressor may try to operate
4329 * on the kernel map in order to return an empty c_segment
4333 if (object
!= cur_object
) {
4334 if (fault_type
& VM_PROT_WRITE
) {
4337 insert_cur_object
= TRUE
;
4340 if (insert_cur_object
== TRUE
) {
4341 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4342 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4344 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
4346 * couldn't upgrade so go do a full retry
4347 * immediately since we can no longer be
4348 * certain about cur_object (since we
4349 * don't hold a reference on it)...
4350 * first drop the top object lock
4352 vm_object_unlock(object
);
4354 vm_map_unlock_read(map
);
4355 if (real_map
!= map
) {
4356 vm_map_unlock(real_map
);
4362 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4363 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4365 if (object
!= cur_object
) {
4367 * we can't go for the upgrade on the top
4368 * lock since the upgrade may block waiting
4369 * for readers to drain... since we hold
4370 * cur_object locked at this point, waiting
4371 * for the readers to drain would represent
4372 * a lock order inversion since the lock order
4373 * for objects is the reference order in the
4376 vm_object_unlock(object
);
4377 vm_object_unlock(cur_object
);
4379 vm_map_unlock_read(map
);
4380 if (real_map
!= map
) {
4381 vm_map_unlock(real_map
);
4386 if (vm_object_lock_upgrade(object
) == FALSE
) {
4388 * couldn't upgrade, so explictly take the lock
4389 * exclusively and go relookup the page since we
4390 * will have dropped the object lock and
4391 * a different thread could have inserted
4392 * a page at this offset
4393 * no need for a full retry since we're
4394 * at the top level of the object chain
4396 vm_object_lock(object
);
4401 m
= vm_page_grab_options(grab_options
);
4404 if (m
== VM_PAGE_NULL
) {
4406 * no free page currently available...
4407 * must take the slow path
4413 * The object is and remains locked
4414 * so no need to take a
4415 * "paging_in_progress" reference.
4417 boolean_t shared_lock
;
4418 if ((object
== cur_object
&&
4419 object_lock_type
== OBJECT_LOCK_EXCLUSIVE
) ||
4420 (object
!= cur_object
&&
4421 cur_object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)) {
4422 shared_lock
= FALSE
;
4427 kr
= vm_compressor_pager_get(
4430 cur_object
->paging_offset
),
4431 VM_PAGE_GET_PHYS_PAGE(m
),
4434 &compressed_count_delta
);
4436 vm_compressor_pager_count(
4438 compressed_count_delta
,
4442 if (kr
!= KERN_SUCCESS
) {
4443 vm_page_release(m
, FALSE
);
4447 m
->vmp_dirty
= TRUE
;
4450 * If the object is purgeable, its
4451 * owner's purgeable ledgers will be
4452 * updated in vm_page_insert() but the
4453 * page was also accounted for in a
4454 * "compressed purgeable" ledger, so
4457 if (object
!= cur_object
&&
4458 !insert_cur_object
) {
4460 * We're not going to insert
4461 * the decompressed page into
4462 * the object it came from.
4464 * We're dealing with a
4465 * copy-on-write fault on
4467 * We're going to decompress
4468 * the page directly into the
4469 * target "object" while
4470 * keepin the compressed
4471 * page for "cur_object", so
4472 * no ledger update in that
4475 } else if (((cur_object
->purgable
==
4476 VM_PURGABLE_DENY
) &&
4477 (!cur_object
->vo_ledger_tag
)) ||
4478 (cur_object
->vo_owner
==
4481 * "cur_object" is not purgeable
4482 * and is not ledger-taged, or
4483 * there's no owner for it,
4484 * so no owner's ledgers to
4489 * One less compressed
4490 * purgeable/tagged page for
4491 * cur_object's owner.
4493 vm_object_owner_compressed_update(
4498 if (insert_cur_object
) {
4499 vm_page_insert(m
, cur_object
, cur_offset
);
4500 m_object
= cur_object
;
4502 vm_page_insert(m
, object
, offset
);
4506 if ((m_object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_USE_DEFAULT
) {
4508 * If the page is not cacheable,
4509 * we can't let its contents
4510 * linger in the data cache
4511 * after the decompression.
4513 pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m
));
4516 type_of_fault
= my_fault_type
;
4518 VM_STAT_DECOMPRESSIONS();
4520 if (cur_object
!= object
) {
4521 if (insert_cur_object
) {
4522 top_object
= object
;
4524 * switch to the object that has the new page
4526 object
= cur_object
;
4527 object_lock_type
= cur_object_lock_type
;
4529 vm_object_unlock(cur_object
);
4530 cur_object
= object
;
4536 * existence map present and indicates
4537 * that the pager doesn't have this page
4540 if (cur_object
->shadow
== VM_OBJECT_NULL
||
4541 resilient_media_retry
) {
4543 * Zero fill fault. Page gets
4544 * inserted into the original object.
4546 if (cur_object
->shadow_severed
||
4547 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
) ||
4548 cur_object
== compressor_object
||
4549 cur_object
== kernel_object
||
4550 cur_object
== vm_submap_object
) {
4551 if (object
!= cur_object
) {
4552 vm_object_unlock(cur_object
);
4554 vm_object_unlock(object
);
4556 vm_map_unlock_read(map
);
4557 if (real_map
!= map
) {
4558 vm_map_unlock(real_map
);
4561 kr
= KERN_MEMORY_ERROR
;
4564 if (cur_object
!= object
) {
4565 vm_object_unlock(cur_object
);
4567 cur_object
= object
;
4569 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4570 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4572 if (vm_object_lock_upgrade(object
) == FALSE
) {
4574 * couldn't upgrade so do a full retry on the fault
4575 * since we dropped the object lock which
4576 * could allow another thread to insert
4577 * a page at this offset
4579 vm_map_unlock_read(map
);
4580 if (real_map
!= map
) {
4581 vm_map_unlock(real_map
);
4587 if (!object
->internal
) {
4588 panic("%s:%d should not zero-fill page at offset 0x%llx in external object %p", __FUNCTION__
, __LINE__
, (uint64_t)offset
, object
);
4590 m
= vm_page_alloc(object
, offset
);
4593 if (m
== VM_PAGE_NULL
) {
4595 * no free page currently available...
4596 * must take the slow path
4603 * Now zero fill page...
4604 * the page is probably going to
4605 * be written soon, so don't bother
4606 * to clear the modified bit
4608 * NOTE: This code holds the map
4609 * lock across the zero fill.
4611 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
4616 * On to the next level in the shadow chain
4618 cur_offset
+= cur_object
->vo_shadow_offset
;
4619 new_object
= cur_object
->shadow
;
4622 * take the new_object's lock with the indicated state
4624 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4625 vm_object_lock_shared(new_object
);
4627 vm_object_lock(new_object
);
4630 if (cur_object
!= object
) {
4631 vm_object_unlock(cur_object
);
4634 cur_object
= new_object
;
4640 * Cleanup from fast fault failure. Drop any object
4641 * lock other than original and drop map lock.
4643 if (object
!= cur_object
) {
4644 vm_object_unlock(cur_object
);
4648 * must own the object lock exclusively at this point
4650 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4651 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4653 if (vm_object_lock_upgrade(object
) == FALSE
) {
4655 * couldn't upgrade, so explictly
4656 * take the lock exclusively
4657 * no need to retry the fault at this
4658 * point since "vm_fault_page" will
4659 * completely re-evaluate the state
4661 vm_object_lock(object
);
4666 vm_map_unlock_read(map
);
4667 if (real_map
!= map
) {
4668 vm_map_unlock(real_map
);
4671 if (__improbable(object
== compressor_object
||
4672 object
== kernel_object
||
4673 object
== vm_submap_object
)) {
4675 * These objects are explicitly managed and populated by the
4676 * kernel. The virtual ranges backed by these objects should
4677 * either have wired pages or "holes" that are not supposed to
4678 * be accessed at all until they get explicitly populated.
4679 * We should never have to resolve a fault on a mapping backed
4680 * by one of these VM objects and providing a zero-filled page
4681 * would be wrong here, so let's fail the fault and let the
4682 * caller crash or recover.
4684 vm_object_unlock(object
);
4685 kr
= KERN_MEMORY_ERROR
;
4689 assert(object
!= compressor_object
);
4690 assert(object
!= kernel_object
);
4691 assert(object
!= vm_submap_object
);
4693 if (resilient_media_retry
) {
4695 * We could get here if we failed to get a free page
4696 * to zero-fill and had to take the slow path again.
4697 * Reset our "recovery-from-failed-media" state.
4699 assert(resilient_media_object
!= VM_OBJECT_NULL
);
4700 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
4701 /* release our extra reference on failed object */
4702 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
4703 vm_object_deallocate(resilient_media_object
);
4704 resilient_media_object
= VM_OBJECT_NULL
;
4705 resilient_media_offset
= (vm_object_offset_t
)-1;
4706 resilient_media_retry
= FALSE
;
4710 * Make a reference to this object to
4711 * prevent its disposal while we are messing with
4712 * it. Once we have the reference, the map is free
4713 * to be diddled. Since objects reference their
4714 * shadows (and copies), they will stay around as well.
4716 vm_object_reference_locked(object
);
4717 vm_object_paging_begin(object
);
4719 set_thread_pagein_error(cthread
, 0);
4722 result_page
= VM_PAGE_NULL
;
4723 kr
= vm_fault_page(object
, offset
, fault_type
,
4724 (change_wiring
&& !wired
),
4725 FALSE
, /* page not looked up */
4726 &prot
, &result_page
, &top_page
,
4728 &error_code
, map
->no_zero_fill
,
4729 FALSE
, &fault_info
);
4732 * if kr != VM_FAULT_SUCCESS, then the paging reference
4733 * has been dropped and the object unlocked... the ref_count
4736 * if kr == VM_FAULT_SUCCESS, then the paging reference
4737 * is still held along with the ref_count on the original object
4739 * the object is returned locked with a paging reference
4741 * if top_page != NULL, then it's BUSY and the
4742 * object it belongs to has a paging reference
4743 * but is returned unlocked
4745 if (kr
!= VM_FAULT_SUCCESS
&&
4746 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
4747 if (kr
== VM_FAULT_MEMORY_ERROR
&&
4748 fault_info
.resilient_media
) {
4749 assertf(object
->internal
, "object %p", object
);
4751 * This fault failed but the mapping was
4752 * "media resilient", so we'll retry the fault in
4753 * recovery mode to get a zero-filled page in the
4755 * Keep the reference on the failing object so
4756 * that we can check that the mapping is still
4757 * pointing to it when we retry the fault.
4759 // 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);
4760 assert(!resilient_media_retry
); /* no double retry */
4761 assert(resilient_media_object
== VM_OBJECT_NULL
);
4762 assert(resilient_media_offset
== (vm_object_offset_t
)-1);
4763 resilient_media_retry
= TRUE
;
4764 resilient_media_object
= object
;
4765 resilient_media_offset
= offset
;
4766 // printf("FBDP %s:%d resilient_media_object %p offset 0x%llx kept reference\n", __FUNCTION__, __LINE__, resilient_media_object, resilient_mmedia_offset);
4770 * we didn't succeed, lose the object reference
4773 vm_object_deallocate(object
);
4774 object
= VM_OBJECT_NULL
; /* no longer valid */
4778 * See why we failed, and take corrective action.
4781 case VM_FAULT_MEMORY_SHORTAGE
:
4782 if (vm_page_wait((change_wiring
) ?
4784 THREAD_ABORTSAFE
)) {
4790 case VM_FAULT_INTERRUPTED
:
4793 case VM_FAULT_RETRY
:
4795 case VM_FAULT_MEMORY_ERROR
:
4799 kr
= KERN_MEMORY_ERROR
;
4803 panic("vm_fault: unexpected error 0x%x from "
4804 "vm_fault_page()\n", kr
);
4810 if (m
!= VM_PAGE_NULL
) {
4811 m_object
= VM_PAGE_OBJECT(m
);
4812 assert((change_wiring
&& !wired
) ?
4813 (top_page
== VM_PAGE_NULL
) :
4814 ((top_page
== VM_PAGE_NULL
) == (m_object
== object
)));
4818 * What to do with the resulting page from vm_fault_page
4819 * if it doesn't get entered into the physical map:
4821 #define RELEASE_PAGE(m) \
4823 PAGE_WAKEUP_DONE(m); \
4824 if ( !VM_PAGE_PAGEABLE(m)) { \
4825 vm_page_lockspin_queues(); \
4826 if ( !VM_PAGE_PAGEABLE(m)) \
4827 vm_page_activate(m); \
4828 vm_page_unlock_queues(); \
4833 object_locks_dropped
= FALSE
;
4835 * We must verify that the maps have not changed
4836 * since our last lookup. vm_map_verify() needs the
4837 * map lock (shared) but we are holding object locks.
4838 * So we do a try_lock() first and, if that fails, we
4839 * drop the object locks and go in for the map lock again.
4841 if (!vm_map_try_lock_read(original_map
)) {
4842 if (m
!= VM_PAGE_NULL
) {
4843 old_copy_object
= m_object
->copy
;
4844 vm_object_unlock(m_object
);
4846 old_copy_object
= VM_OBJECT_NULL
;
4847 vm_object_unlock(object
);
4850 object_locks_dropped
= TRUE
;
4852 vm_map_lock_read(original_map
);
4855 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
4856 if (object_locks_dropped
== FALSE
) {
4857 if (m
!= VM_PAGE_NULL
) {
4858 old_copy_object
= m_object
->copy
;
4859 vm_object_unlock(m_object
);
4861 old_copy_object
= VM_OBJECT_NULL
;
4862 vm_object_unlock(object
);
4865 object_locks_dropped
= TRUE
;
4869 * no object locks are held at this point
4871 vm_object_t retry_object
;
4872 vm_object_offset_t retry_offset
;
4873 vm_prot_t retry_prot
;
4876 * To avoid trying to write_lock the map while another
4877 * thread has it read_locked (in vm_map_pageable), we
4878 * do not try for write permission. If the page is
4879 * still writable, we will get write permission. If it
4880 * is not, or has been marked needs_copy, we enter the
4881 * mapping without write permission, and will merely
4882 * take another fault.
4886 kr
= vm_map_lookup_locked(&map
, vaddr
,
4887 fault_type
& ~VM_PROT_WRITE
,
4888 OBJECT_LOCK_EXCLUSIVE
, &version
,
4889 &retry_object
, &retry_offset
, &retry_prot
,
4893 pmap
= real_map
->pmap
;
4895 if (kr
!= KERN_SUCCESS
) {
4896 vm_map_unlock_read(map
);
4898 if (m
!= VM_PAGE_NULL
) {
4899 assert(VM_PAGE_OBJECT(m
) == m_object
);
4902 * retake the lock so that
4903 * we can drop the paging reference
4904 * in vm_fault_cleanup and do the
4905 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4907 vm_object_lock(m_object
);
4911 vm_fault_cleanup(m_object
, top_page
);
4914 * retake the lock so that
4915 * we can drop the paging reference
4916 * in vm_fault_cleanup
4918 vm_object_lock(object
);
4920 vm_fault_cleanup(object
, top_page
);
4922 vm_object_deallocate(object
);
4926 vm_object_unlock(retry_object
);
4928 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
4929 vm_map_unlock_read(map
);
4930 if (real_map
!= map
) {
4931 vm_map_unlock(real_map
);
4934 if (m
!= VM_PAGE_NULL
) {
4935 assert(VM_PAGE_OBJECT(m
) == m_object
);
4938 * retake the lock so that
4939 * we can drop the paging reference
4940 * in vm_fault_cleanup and do the
4941 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4943 vm_object_lock(m_object
);
4947 vm_fault_cleanup(m_object
, top_page
);
4950 * retake the lock so that
4951 * we can drop the paging reference
4952 * in vm_fault_cleanup
4954 vm_object_lock(object
);
4956 vm_fault_cleanup(object
, top_page
);
4958 vm_object_deallocate(object
);
4963 * Check whether the protection has changed or the object
4964 * has been copied while we left the map unlocked.
4966 if (pmap_has_prot_policy(retry_prot
)) {
4967 /* If the pmap layer cares, pass the full set. */
4974 if (object_locks_dropped
== TRUE
) {
4975 if (m
!= VM_PAGE_NULL
) {
4976 vm_object_lock(m_object
);
4978 if (m_object
->copy
!= old_copy_object
) {
4980 * The copy object changed while the top-level object
4981 * was unlocked, so take away write permission.
4983 assert(!pmap_has_prot_policy(prot
));
4984 prot
&= ~VM_PROT_WRITE
;
4987 vm_object_lock(object
);
4990 object_locks_dropped
= FALSE
;
4994 !fault_info
.no_copy_on_read
&&
4995 m
!= VM_PAGE_NULL
&&
4996 VM_PAGE_OBJECT(m
) != object
&&
4997 !VM_PAGE_OBJECT(m
)->pager_trusted
&&
4998 vm_protect_privileged_from_untrusted
&&
4999 !((prot
& VM_PROT_EXECUTE
) &&
5000 VM_PAGE_OBJECT(m
)->code_signed
&&
5001 cs_process_enforcement(NULL
)) &&
5002 current_proc_is_privileged()) {
5004 * We found the page we want in an "untrusted" VM object
5005 * down the shadow chain. Since the target is "privileged"
5006 * we want to perform a copy-on-read of that page, so that the
5007 * mapped object gets a stable copy and does not have to
5008 * rely on the "untrusted" object to provide the same
5009 * contents if the page gets reclaimed and has to be paged
5010 * in again later on.
5012 * Special case: if the mapping is executable and the untrusted
5013 * object is code-signed and the process is "cs_enforced", we
5014 * do not copy-on-read because that would break code-signing
5015 * enforcement expectations (an executable page must belong
5016 * to a code-signed object) and we can rely on code-signing
5017 * to re-validate the page if it gets evicted and paged back in.
5019 // 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);
5020 vm_copied_on_read
++;
5021 need_copy_on_read
= TRUE
;
5024 need_copy_on_read
= FALSE
;
5028 * If we want to wire down this page, but no longer have
5029 * adequate permissions, we must start all over.
5030 * If we decided to copy-on-read, we must also start all over.
5032 if ((wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) ||
5033 need_copy_on_read
) {
5034 vm_map_unlock_read(map
);
5035 if (real_map
!= map
) {
5036 vm_map_unlock(real_map
);
5039 if (m
!= VM_PAGE_NULL
) {
5040 assert(VM_PAGE_OBJECT(m
) == m_object
);
5044 vm_fault_cleanup(m_object
, top_page
);
5046 vm_fault_cleanup(object
, top_page
);
5049 vm_object_deallocate(object
);
5053 if (m
!= VM_PAGE_NULL
) {
5055 * Put this page into the physical map.
5056 * We had to do the unlock above because pmap_enter
5057 * may cause other faults. The page may be on
5058 * the pageout queues. If the pageout daemon comes
5059 * across the page, it will remove it from the queues.
5062 kr
= vm_fault_enter(m
,
5074 kr
= vm_fault_enter(m
,
5086 assert(VM_PAGE_OBJECT(m
) == m_object
);
5091 if (m_object
->internal
) {
5092 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
5093 } else if (m_object
->object_is_shared_cache
) {
5094 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
5096 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
5099 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);
5100 KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_SLOW
), get_current_unique_pid(), 0, 0, 0, 0);
5102 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
);
5104 if (kr
!= KERN_SUCCESS
) {
5105 /* abort this page fault */
5106 vm_map_unlock_read(map
);
5107 if (real_map
!= map
) {
5108 vm_map_unlock(real_map
);
5110 PAGE_WAKEUP_DONE(m
);
5111 vm_fault_cleanup(m_object
, top_page
);
5112 vm_object_deallocate(object
);
5115 if (physpage_p
!= NULL
) {
5116 /* for vm_map_wire_and_extract() */
5117 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
5118 if (prot
& VM_PROT_WRITE
) {
5119 vm_object_lock_assert_exclusive(m_object
);
5120 m
->vmp_dirty
= TRUE
;
5124 vm_map_entry_t entry
;
5125 vm_map_offset_t laddr
;
5126 vm_map_offset_t ldelta
, hdelta
;
5129 * do a pmap block mapping from the physical address
5133 if (real_map
!= map
) {
5134 vm_map_unlock(real_map
);
5137 if (original_map
!= map
) {
5138 vm_map_unlock_read(map
);
5139 vm_map_lock_read(original_map
);
5145 hdelta
= 0xFFFFF000;
5146 ldelta
= 0xFFFFF000;
5148 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
5149 if (ldelta
> (laddr
- entry
->vme_start
)) {
5150 ldelta
= laddr
- entry
->vme_start
;
5152 if (hdelta
> (entry
->vme_end
- laddr
)) {
5153 hdelta
= entry
->vme_end
- laddr
;
5155 if (entry
->is_sub_map
) {
5156 laddr
= ((laddr
- entry
->vme_start
)
5157 + VME_OFFSET(entry
));
5158 vm_map_lock_read(VME_SUBMAP(entry
));
5160 if (map
!= real_map
) {
5161 vm_map_unlock_read(map
);
5163 if (entry
->use_pmap
) {
5164 vm_map_unlock_read(real_map
);
5165 real_map
= VME_SUBMAP(entry
);
5167 map
= VME_SUBMAP(entry
);
5173 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
5174 (VME_OBJECT(entry
) != NULL
) &&
5175 (VME_OBJECT(entry
) == object
)) {
5178 if (!object
->pager_created
&&
5179 object
->phys_contiguous
&&
5180 VME_OFFSET(entry
) == 0 &&
5181 (entry
->vme_end
- entry
->vme_start
== object
->vo_size
) &&
5182 VM_MAP_PAGE_ALIGNED(entry
->vme_start
, (object
->vo_size
- 1))) {
5183 superpage
= VM_MEM_SUPERPAGE
;
5188 if (superpage
&& physpage_p
) {
5189 /* for vm_map_wire_and_extract() */
5190 *physpage_p
= (ppnum_t
)
5191 ((((vm_map_offset_t
)
5192 object
->vo_shadow_offset
)
5194 + (laddr
- entry
->vme_start
))
5200 * Set up a block mapped area
5202 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5203 kr
= pmap_map_block(caller_pmap
,
5204 (addr64_t
)(caller_pmap_addr
- ldelta
),
5205 (ppnum_t
)((((vm_map_offset_t
) (VME_OBJECT(entry
)->vo_shadow_offset
)) +
5206 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5207 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5208 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5210 if (kr
!= KERN_SUCCESS
) {
5215 * Set up a block mapped area
5217 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5218 kr
= pmap_map_block(real_map
->pmap
,
5219 (addr64_t
)(vaddr
- ldelta
),
5220 (ppnum_t
)((((vm_map_offset_t
)(VME_OBJECT(entry
)->vo_shadow_offset
)) +
5221 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5222 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5223 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5225 if (kr
!= KERN_SUCCESS
) {
5238 * TODO: could most of the done cases just use cleanup?
5242 * Unlock everything, and return
5244 vm_map_unlock_read(map
);
5245 if (real_map
!= map
) {
5246 vm_map_unlock(real_map
);
5249 if (m
!= VM_PAGE_NULL
) {
5250 assert(VM_PAGE_OBJECT(m
) == m_object
);
5252 if (!m_object
->internal
&& (fault_type
& VM_PROT_WRITE
)) {
5253 vm_object_paging_begin(m_object
);
5255 assert(written_on_object
== VM_OBJECT_NULL
);
5256 written_on_object
= m_object
;
5257 written_on_pager
= m_object
->pager
;
5258 written_on_offset
= m_object
->paging_offset
+ m
->vmp_offset
;
5260 PAGE_WAKEUP_DONE(m
);
5262 vm_fault_cleanup(m_object
, top_page
);
5264 vm_fault_cleanup(object
, top_page
);
5267 vm_object_deallocate(object
);
5272 thread_interrupt_level(interruptible_state
);
5274 if (resilient_media_object
!= VM_OBJECT_NULL
) {
5275 assert(resilient_media_retry
);
5276 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
5277 /* release extra reference on failed object */
5278 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
5279 vm_object_deallocate(resilient_media_object
);
5280 resilient_media_object
= VM_OBJECT_NULL
;
5281 resilient_media_offset
= (vm_object_offset_t
)-1;
5282 resilient_media_retry
= FALSE
;
5284 assert(!resilient_media_retry
);
5287 * Only I/O throttle on faults which cause a pagein/swapin.
5289 if ((type_of_fault
== DBG_PAGEIND_FAULT
) || (type_of_fault
== DBG_PAGEINV_FAULT
) || (type_of_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
)) {
5290 throttle_lowpri_io(1);
5292 if (kr
== KERN_SUCCESS
&& type_of_fault
!= DBG_CACHE_HIT_FAULT
&& type_of_fault
!= DBG_GUARD_FAULT
) {
5293 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
5294 if (vm_debug_events
) {
5295 if (type_of_fault
== DBG_COMPRESSOR_FAULT
) {
5296 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5297 } else if (type_of_fault
== DBG_COW_FAULT
) {
5298 VM_DEBUG_EVENT(vmf_cowdelay
, VMF_COWDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5300 VM_DEBUG_EVENT(vmf_zfdelay
, VMF_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5303 delay(throttle_delay
);
5308 if (written_on_object
) {
5309 vnode_pager_dirtied(written_on_pager
, written_on_offset
, written_on_offset
+ PAGE_SIZE_64
);
5311 vm_object_lock(written_on_object
);
5312 vm_object_paging_end(written_on_object
);
5313 vm_object_unlock(written_on_object
);
5315 written_on_object
= VM_OBJECT_NULL
;
5319 vm_record_rtfault(cthread
, fstart
, trace_vaddr
, type_of_fault
);
5322 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5323 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
5324 ((uint64_t)trace_vaddr
>> 32),
5336 * Wire down a range of virtual addresses in a map.
5341 vm_map_entry_t entry
,
5345 vm_map_offset_t pmap_addr
,
5346 ppnum_t
*physpage_p
)
5349 vm_map_offset_t end_addr
= entry
->vme_end
;
5352 assert(entry
->in_transition
);
5354 if ((VME_OBJECT(entry
) != NULL
) &&
5355 !entry
->is_sub_map
&&
5356 VME_OBJECT(entry
)->phys_contiguous
) {
5357 return KERN_SUCCESS
;
5361 * Inform the physical mapping system that the
5362 * range of addresses may not fault, so that
5363 * page tables and such can be locked down as well.
5366 pmap_pageable(pmap
, pmap_addr
,
5367 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
5370 * We simulate a fault to get the page and enter it
5371 * in the physical map.
5374 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
5375 rc
= vm_fault_wire_fast(map
, va
, prot
, wire_tag
, entry
, pmap
,
5376 pmap_addr
+ (va
- entry
->vme_start
),
5378 if (rc
!= KERN_SUCCESS
) {
5379 rc
= vm_fault_internal(map
, va
, prot
, TRUE
, wire_tag
,
5380 ((pmap
== kernel_pmap
)
5382 : THREAD_ABORTSAFE
),
5385 (va
- entry
->vme_start
)),
5387 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
5390 if (rc
!= KERN_SUCCESS
) {
5391 struct vm_map_entry tmp_entry
= *entry
;
5393 /* unwire wired pages */
5394 tmp_entry
.vme_end
= va
;
5395 vm_fault_unwire(map
,
5396 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
5401 return KERN_SUCCESS
;
5407 * Unwire a range of virtual addresses in a map.
5412 vm_map_entry_t entry
,
5413 boolean_t deallocate
,
5415 vm_map_offset_t pmap_addr
)
5418 vm_map_offset_t end_addr
= entry
->vme_end
;
5420 struct vm_object_fault_info fault_info
= {};
5421 unsigned int unwired_pages
;
5423 object
= (entry
->is_sub_map
) ? VM_OBJECT_NULL
: VME_OBJECT(entry
);
5426 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
5427 * do anything since such memory is wired by default. So we don't have
5428 * anything to undo here.
5431 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
) {
5435 fault_info
.interruptible
= THREAD_UNINT
;
5436 fault_info
.behavior
= entry
->behavior
;
5437 fault_info
.user_tag
= VME_ALIAS(entry
);
5438 if (entry
->iokit_acct
||
5439 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
5440 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
5442 fault_info
.lo_offset
= VME_OFFSET(entry
);
5443 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + VME_OFFSET(entry
);
5444 fault_info
.no_cache
= entry
->no_cache
;
5445 fault_info
.stealth
= TRUE
;
5450 * Since the pages are wired down, we must be able to
5451 * get their mappings from the physical map system.
5454 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
5455 if (object
== VM_OBJECT_NULL
) {
5457 pmap_change_wiring(pmap
,
5458 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5460 (void) vm_fault(map
, va
, VM_PROT_NONE
,
5461 TRUE
, VM_KERN_MEMORY_NONE
, THREAD_UNINT
, pmap
, pmap_addr
);
5464 vm_page_t result_page
;
5466 vm_object_t result_object
;
5467 vm_fault_return_t result
;
5469 /* cap cluster size at maximum UPL size */
5470 upl_size_t cluster_size
;
5471 if (os_sub_overflow(end_addr
, va
, &cluster_size
)) {
5472 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
5474 fault_info
.cluster_size
= cluster_size
;
5477 prot
= VM_PROT_NONE
;
5479 vm_object_lock(object
);
5480 vm_object_paging_begin(object
);
5481 result_page
= VM_PAGE_NULL
;
5482 result
= vm_fault_page(
5484 (VME_OFFSET(entry
) +
5485 (va
- entry
->vme_start
)),
5487 FALSE
, /* page not looked up */
5488 &prot
, &result_page
, &top_page
,
5490 NULL
, map
->no_zero_fill
,
5491 FALSE
, &fault_info
);
5492 } while (result
== VM_FAULT_RETRY
);
5495 * If this was a mapping to a file on a device that has been forcibly
5496 * unmounted, then we won't get a page back from vm_fault_page(). Just
5497 * move on to the next one in case the remaining pages are mapped from
5498 * different objects. During a forced unmount, the object is terminated
5499 * so the alive flag will be false if this happens. A forced unmount will
5500 * will occur when an external disk is unplugged before the user does an
5501 * eject, so we don't want to panic in that situation.
5504 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
) {
5508 if (result
== VM_FAULT_MEMORY_ERROR
&&
5509 object
== kernel_object
) {
5511 * This must have been allocated with
5512 * KMA_KOBJECT and KMA_VAONLY and there's
5513 * no physical page at this offset.
5514 * We're done (no page to free).
5520 if (result
!= VM_FAULT_SUCCESS
) {
5521 panic("vm_fault_unwire: failure");
5524 result_object
= VM_PAGE_OBJECT(result_page
);
5527 assert(VM_PAGE_GET_PHYS_PAGE(result_page
) !=
5528 vm_page_fictitious_addr
);
5529 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page
));
5530 if (VM_PAGE_WIRED(result_page
)) {
5533 VM_PAGE_FREE(result_page
);
5535 if ((pmap
) && (VM_PAGE_GET_PHYS_PAGE(result_page
) != vm_page_guard_addr
)) {
5536 pmap_change_wiring(pmap
,
5537 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5541 if (VM_PAGE_WIRED(result_page
)) {
5542 vm_page_lockspin_queues();
5543 vm_page_unwire(result_page
, TRUE
);
5544 vm_page_unlock_queues();
5547 if (entry
->zero_wired_pages
) {
5548 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page
));
5549 entry
->zero_wired_pages
= FALSE
;
5552 PAGE_WAKEUP_DONE(result_page
);
5554 vm_fault_cleanup(result_object
, top_page
);
5559 * Inform the physical mapping system that the range
5560 * of addresses may fault, so that page tables and
5561 * such may be unwired themselves.
5564 pmap_pageable(pmap
, pmap_addr
,
5565 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
5567 if (kernel_object
== object
) {
5568 vm_tag_update_size(fault_info
.user_tag
, -ptoa_64(unwired_pages
));
5573 * vm_fault_wire_fast:
5575 * Handle common case of a wire down page fault at the given address.
5576 * If successful, the page is inserted into the associated physical map.
5577 * The map entry is passed in to avoid the overhead of a map lookup.
5579 * NOTE: the given address should be truncated to the
5580 * proper page address.
5582 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
5583 * a standard error specifying why the fault is fatal is returned.
5585 * The map in question must be referenced, and remains so.
5586 * Caller has a read lock on the map.
5588 * This is a stripped version of vm_fault() for wiring pages. Anything
5589 * other than the common case will return KERN_FAILURE, and the caller
5590 * is expected to call vm_fault().
5592 static kern_return_t
5594 __unused vm_map_t map
,
5596 __unused vm_prot_t caller_prot
,
5598 vm_map_entry_t entry
,
5600 vm_map_offset_t pmap_addr
,
5601 ppnum_t
*physpage_p
)
5604 vm_object_offset_t offset
;
5607 thread_t thread
= current_thread();
5610 struct vm_object_fault_info fault_info
= {};
5612 VM_STAT_INCR(faults
);
5614 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
) {
5615 thread
->task
->faults
++;
5623 #define RELEASE_PAGE(m) { \
5624 PAGE_WAKEUP_DONE(m); \
5625 vm_page_lockspin_queues(); \
5626 vm_page_unwire(m, TRUE); \
5627 vm_page_unlock_queues(); \
5631 #undef UNLOCK_THINGS
5632 #define UNLOCK_THINGS { \
5633 vm_object_paging_end(object); \
5634 vm_object_unlock(object); \
5637 #undef UNLOCK_AND_DEALLOCATE
5638 #define UNLOCK_AND_DEALLOCATE { \
5640 vm_object_deallocate(object); \
5643 * Give up and have caller do things the hard way.
5647 UNLOCK_AND_DEALLOCATE; \
5648 return(KERN_FAILURE); \
5653 * If this entry is not directly to a vm_object, bail out.
5655 if (entry
->is_sub_map
) {
5656 assert(physpage_p
== NULL
);
5657 return KERN_FAILURE
;
5661 * Find the backing store object and offset into it.
5664 object
= VME_OBJECT(entry
);
5665 offset
= (va
- entry
->vme_start
) + VME_OFFSET(entry
);
5666 prot
= entry
->protection
;
5669 * Make a reference to this object to prevent its
5670 * disposal while we are messing with it.
5673 vm_object_lock(object
);
5674 vm_object_reference_locked(object
);
5675 vm_object_paging_begin(object
);
5678 * INVARIANTS (through entire routine):
5680 * 1) At all times, we must either have the object
5681 * lock or a busy page in some object to prevent
5682 * some other thread from trying to bring in
5685 * 2) Once we have a busy page, we must remove it from
5686 * the pageout queues, so that the pageout daemon
5687 * will not grab it away.
5692 * Look for page in top-level object. If it's not there or
5693 * there's something going on, give up.
5695 m
= vm_page_lookup(object
, offset
);
5696 if ((m
== VM_PAGE_NULL
) || (m
->vmp_busy
) ||
5697 (m
->vmp_unusual
&& (m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_absent
))) {
5700 if (m
->vmp_fictitious
&&
5701 VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
5703 * Guard pages are fictitious pages and are never
5704 * entered into a pmap, so let's say it's been wired...
5711 * Wire the page down now. All bail outs beyond this
5712 * point must unwire the page.
5715 vm_page_lockspin_queues();
5716 vm_page_wire(m
, wire_tag
, TRUE
);
5717 vm_page_unlock_queues();
5720 * Mark page busy for other threads.
5722 assert(!m
->vmp_busy
);
5724 assert(!m
->vmp_absent
);
5727 * Give up if the page is being written and there's a copy object
5729 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
5734 fault_info
.user_tag
= VME_ALIAS(entry
);
5735 fault_info
.pmap_options
= 0;
5736 if (entry
->iokit_acct
||
5737 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
5738 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
5742 * Put this page into the physical map.
5744 type_of_fault
= DBG_CACHE_HIT_FAULT
;
5745 kr
= vm_fault_enter(m
,
5751 FALSE
, /* change_wiring */
5756 if (kr
!= KERN_SUCCESS
) {
5763 * Unlock everything, and return
5767 /* for vm_map_wire_and_extract() */
5768 if (kr
== KERN_SUCCESS
) {
5769 assert(object
== VM_PAGE_OBJECT(m
));
5770 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
5771 if (prot
& VM_PROT_WRITE
) {
5772 vm_object_lock_assert_exclusive(object
);
5773 m
->vmp_dirty
= TRUE
;
5780 PAGE_WAKEUP_DONE(m
);
5781 UNLOCK_AND_DEALLOCATE
;
5787 * Routine: vm_fault_copy_cleanup
5789 * Release a page used by vm_fault_copy.
5793 vm_fault_copy_cleanup(
5797 vm_object_t object
= VM_PAGE_OBJECT(page
);
5799 vm_object_lock(object
);
5800 PAGE_WAKEUP_DONE(page
);
5801 if (!VM_PAGE_PAGEABLE(page
)) {
5802 vm_page_lockspin_queues();
5803 if (!VM_PAGE_PAGEABLE(page
)) {
5804 vm_page_activate(page
);
5806 vm_page_unlock_queues();
5808 vm_fault_cleanup(object
, top_page
);
5812 vm_fault_copy_dst_cleanup(
5817 if (page
!= VM_PAGE_NULL
) {
5818 object
= VM_PAGE_OBJECT(page
);
5819 vm_object_lock(object
);
5820 vm_page_lockspin_queues();
5821 vm_page_unwire(page
, TRUE
);
5822 vm_page_unlock_queues();
5823 vm_object_paging_end(object
);
5824 vm_object_unlock(object
);
5829 * Routine: vm_fault_copy
5832 * Copy pages from one virtual memory object to another --
5833 * neither the source nor destination pages need be resident.
5835 * Before actually copying a page, the version associated with
5836 * the destination address map wil be verified.
5838 * In/out conditions:
5839 * The caller must hold a reference, but not a lock, to
5840 * each of the source and destination objects and to the
5844 * Returns KERN_SUCCESS if no errors were encountered in
5845 * reading or writing the data. Returns KERN_INTERRUPTED if
5846 * the operation was interrupted (only possible if the
5847 * "interruptible" argument is asserted). Other return values
5848 * indicate a permanent error in copying the data.
5850 * The actual amount of data copied will be returned in the
5851 * "copy_size" argument. In the event that the destination map
5852 * verification failed, this amount may be less than the amount
5857 vm_object_t src_object
,
5858 vm_object_offset_t src_offset
,
5859 vm_map_size_t
*copy_size
, /* INOUT */
5860 vm_object_t dst_object
,
5861 vm_object_offset_t dst_offset
,
5863 vm_map_version_t
*dst_version
,
5866 vm_page_t result_page
;
5869 vm_page_t src_top_page
;
5873 vm_page_t dst_top_page
;
5876 vm_map_size_t amount_left
;
5877 vm_object_t old_copy_object
;
5878 vm_object_t result_page_object
= NULL
;
5879 kern_return_t error
= 0;
5880 vm_fault_return_t result
;
5882 vm_map_size_t part_size
;
5883 struct vm_object_fault_info fault_info_src
= {};
5884 struct vm_object_fault_info fault_info_dst
= {};
5887 * In order not to confuse the clustered pageins, align
5888 * the different offsets on a page boundary.
5893 *copy_size -= amount_left; \
5897 amount_left
= *copy_size
;
5899 fault_info_src
.interruptible
= interruptible
;
5900 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5901 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
5902 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
5903 fault_info_src
.stealth
= TRUE
;
5905 fault_info_dst
.interruptible
= interruptible
;
5906 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5907 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
5908 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
5909 fault_info_dst
.stealth
= TRUE
;
5911 do { /* while (amount_left > 0) */
5913 * There may be a deadlock if both source and destination
5914 * pages are the same. To avoid this deadlock, the copy must
5915 * start by getting the destination page in order to apply
5916 * COW semantics if any.
5919 RetryDestinationFault
:;
5921 dst_prot
= VM_PROT_WRITE
| VM_PROT_READ
;
5923 vm_object_lock(dst_object
);
5924 vm_object_paging_begin(dst_object
);
5926 /* cap cluster size at maximum UPL size */
5927 upl_size_t cluster_size
;
5928 if (os_convert_overflow(amount_left
, &cluster_size
)) {
5929 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
5931 fault_info_dst
.cluster_size
= cluster_size
;
5933 dst_page
= VM_PAGE_NULL
;
5934 result
= vm_fault_page(dst_object
,
5935 vm_object_trunc_page(dst_offset
),
5936 VM_PROT_WRITE
| VM_PROT_READ
,
5938 FALSE
, /* page not looked up */
5939 &dst_prot
, &dst_page
, &dst_top_page
,
5942 dst_map
->no_zero_fill
,
5943 FALSE
, &fault_info_dst
);
5945 case VM_FAULT_SUCCESS
:
5947 case VM_FAULT_RETRY
:
5948 goto RetryDestinationFault
;
5949 case VM_FAULT_MEMORY_SHORTAGE
:
5950 if (vm_page_wait(interruptible
)) {
5951 goto RetryDestinationFault
;
5954 case VM_FAULT_INTERRUPTED
:
5955 RETURN(MACH_SEND_INTERRUPTED
);
5956 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5957 /* success but no VM page: fail the copy */
5958 vm_object_paging_end(dst_object
);
5959 vm_object_unlock(dst_object
);
5961 case VM_FAULT_MEMORY_ERROR
:
5965 return KERN_MEMORY_ERROR
;
5968 panic("vm_fault_copy: unexpected error 0x%x from "
5969 "vm_fault_page()\n", result
);
5971 assert((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
5973 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
5974 old_copy_object
= dst_object
->copy
;
5977 * There exists the possiblity that the source and
5978 * destination page are the same. But we can't
5979 * easily determine that now. If they are the
5980 * same, the call to vm_fault_page() for the
5981 * destination page will deadlock. To prevent this we
5982 * wire the page so we can drop busy without having
5983 * the page daemon steal the page. We clean up the
5984 * top page but keep the paging reference on the object
5985 * holding the dest page so it doesn't go away.
5988 vm_page_lockspin_queues();
5989 vm_page_wire(dst_page
, VM_KERN_MEMORY_OSFMK
, TRUE
);
5990 vm_page_unlock_queues();
5991 PAGE_WAKEUP_DONE(dst_page
);
5992 vm_object_unlock(dst_object
);
5994 if (dst_top_page
!= VM_PAGE_NULL
) {
5995 vm_object_lock(dst_object
);
5996 VM_PAGE_FREE(dst_top_page
);
5997 vm_object_paging_end(dst_object
);
5998 vm_object_unlock(dst_object
);
6003 if (src_object
== VM_OBJECT_NULL
) {
6005 * No source object. We will just
6006 * zero-fill the page in dst_object.
6008 src_page
= VM_PAGE_NULL
;
6009 result_page
= VM_PAGE_NULL
;
6011 vm_object_lock(src_object
);
6012 src_page
= vm_page_lookup(src_object
,
6013 vm_object_trunc_page(src_offset
));
6014 if (src_page
== dst_page
) {
6015 src_prot
= dst_prot
;
6016 result_page
= VM_PAGE_NULL
;
6018 src_prot
= VM_PROT_READ
;
6019 vm_object_paging_begin(src_object
);
6021 /* cap cluster size at maximum UPL size */
6022 if (os_convert_overflow(amount_left
, &cluster_size
)) {
6023 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
6025 fault_info_src
.cluster_size
= cluster_size
;
6027 result_page
= VM_PAGE_NULL
;
6028 result
= vm_fault_page(
6030 vm_object_trunc_page(src_offset
),
6031 VM_PROT_READ
, FALSE
,
6032 FALSE
, /* page not looked up */
6034 &result_page
, &src_top_page
,
6035 (int *)0, &error
, FALSE
,
6036 FALSE
, &fault_info_src
);
6039 case VM_FAULT_SUCCESS
:
6041 case VM_FAULT_RETRY
:
6042 goto RetrySourceFault
;
6043 case VM_FAULT_MEMORY_SHORTAGE
:
6044 if (vm_page_wait(interruptible
)) {
6045 goto RetrySourceFault
;
6048 case VM_FAULT_INTERRUPTED
:
6049 vm_fault_copy_dst_cleanup(dst_page
);
6050 RETURN(MACH_SEND_INTERRUPTED
);
6051 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
6052 /* success but no VM page: fail */
6053 vm_object_paging_end(src_object
);
6054 vm_object_unlock(src_object
);
6056 case VM_FAULT_MEMORY_ERROR
:
6057 vm_fault_copy_dst_cleanup(dst_page
);
6061 return KERN_MEMORY_ERROR
;
6064 panic("vm_fault_copy(2): unexpected "
6066 "vm_fault_page()\n", result
);
6069 result_page_object
= VM_PAGE_OBJECT(result_page
);
6070 assert((src_top_page
== VM_PAGE_NULL
) ==
6071 (result_page_object
== src_object
));
6073 assert((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
6074 vm_object_unlock(result_page_object
);
6077 vm_map_lock_read(dst_map
);
6079 if (!vm_map_verify(dst_map
, dst_version
)) {
6080 vm_map_unlock_read(dst_map
);
6081 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6082 vm_fault_copy_cleanup(result_page
, src_top_page
);
6084 vm_fault_copy_dst_cleanup(dst_page
);
6087 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
6089 vm_object_lock(dst_object
);
6091 if (dst_object
->copy
!= old_copy_object
) {
6092 vm_object_unlock(dst_object
);
6093 vm_map_unlock_read(dst_map
);
6094 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6095 vm_fault_copy_cleanup(result_page
, src_top_page
);
6097 vm_fault_copy_dst_cleanup(dst_page
);
6100 vm_object_unlock(dst_object
);
6103 * Copy the page, and note that it is dirty
6107 if (!page_aligned(src_offset
) ||
6108 !page_aligned(dst_offset
) ||
6109 !page_aligned(amount_left
)) {
6110 vm_object_offset_t src_po
,
6113 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
6114 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
6116 if (dst_po
> src_po
) {
6117 part_size
= PAGE_SIZE
- dst_po
;
6119 part_size
= PAGE_SIZE
- src_po
;
6121 if (part_size
> (amount_left
)) {
6122 part_size
= amount_left
;
6125 if (result_page
== VM_PAGE_NULL
) {
6126 assert((vm_offset_t
) dst_po
== dst_po
);
6127 assert((vm_size_t
) part_size
== part_size
);
6128 vm_page_part_zero_fill(dst_page
,
6129 (vm_offset_t
) dst_po
,
6130 (vm_size_t
) part_size
);
6132 assert((vm_offset_t
) src_po
== src_po
);
6133 assert((vm_offset_t
) dst_po
== dst_po
);
6134 assert((vm_size_t
) part_size
== part_size
);
6135 vm_page_part_copy(result_page
,
6136 (vm_offset_t
) src_po
,
6138 (vm_offset_t
) dst_po
,
6139 (vm_size_t
)part_size
);
6140 if (!dst_page
->vmp_dirty
) {
6141 vm_object_lock(dst_object
);
6142 SET_PAGE_DIRTY(dst_page
, TRUE
);
6143 vm_object_unlock(dst_object
);
6147 part_size
= PAGE_SIZE
;
6149 if (result_page
== VM_PAGE_NULL
) {
6150 vm_page_zero_fill(dst_page
);
6152 vm_object_lock(result_page_object
);
6153 vm_page_copy(result_page
, dst_page
);
6154 vm_object_unlock(result_page_object
);
6156 if (!dst_page
->vmp_dirty
) {
6157 vm_object_lock(dst_object
);
6158 SET_PAGE_DIRTY(dst_page
, TRUE
);
6159 vm_object_unlock(dst_object
);
6165 * Unlock everything, and return
6168 vm_map_unlock_read(dst_map
);
6170 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6171 vm_fault_copy_cleanup(result_page
, src_top_page
);
6173 vm_fault_copy_dst_cleanup(dst_page
);
6175 amount_left
-= part_size
;
6176 src_offset
+= part_size
;
6177 dst_offset
+= part_size
;
6178 } while (amount_left
> 0);
6180 RETURN(KERN_SUCCESS
);
6186 #if VM_FAULT_CLASSIFY
6188 * Temporary statistics gathering support.
6192 * Statistics arrays:
6194 #define VM_FAULT_TYPES_MAX 5
6195 #define VM_FAULT_LEVEL_MAX 8
6197 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
6199 #define VM_FAULT_TYPE_ZERO_FILL 0
6200 #define VM_FAULT_TYPE_MAP_IN 1
6201 #define VM_FAULT_TYPE_PAGER 2
6202 #define VM_FAULT_TYPE_COPY 3
6203 #define VM_FAULT_TYPE_OTHER 4
6207 vm_fault_classify(vm_object_t object
,
6208 vm_object_offset_t offset
,
6209 vm_prot_t fault_type
)
6211 int type
, level
= 0;
6215 m
= vm_page_lookup(object
, offset
);
6216 if (m
!= VM_PAGE_NULL
) {
6217 if (m
->vmp_busy
|| m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_absent
) {
6218 type
= VM_FAULT_TYPE_OTHER
;
6221 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
6222 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
6223 type
= VM_FAULT_TYPE_MAP_IN
;
6226 type
= VM_FAULT_TYPE_COPY
;
6229 if (object
->pager_created
) {
6230 type
= VM_FAULT_TYPE_PAGER
;
6233 if (object
->shadow
== VM_OBJECT_NULL
) {
6234 type
= VM_FAULT_TYPE_ZERO_FILL
;
6238 offset
+= object
->vo_shadow_offset
;
6239 object
= object
->shadow
;
6245 if (level
> VM_FAULT_LEVEL_MAX
) {
6246 level
= VM_FAULT_LEVEL_MAX
;
6249 vm_fault_stats
[type
][level
] += 1;
6254 /* cleanup routine to call from debugger */
6257 vm_fault_classify_init(void)
6261 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
6262 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
6263 vm_fault_stats
[type
][level
] = 0;
6269 #endif /* VM_FAULT_CLASSIFY */
6272 kdp_lightweight_fault(vm_map_t map
, vm_offset_t cur_target_addr
)
6274 vm_map_entry_t entry
;
6276 vm_offset_t object_offset
;
6278 int compressor_external_state
, compressed_count_delta
;
6279 int compressor_flags
= (C_DONT_BLOCK
| C_KEEP
| C_KDP
);
6280 int my_fault_type
= VM_PROT_READ
;
6284 panic("kdp_lightweight_fault called from outside of debugger context");
6287 assert(map
!= VM_MAP_NULL
);
6289 assert((cur_target_addr
& PAGE_MASK
) == 0);
6290 if ((cur_target_addr
& PAGE_MASK
) != 0) {
6294 if (kdp_lck_rw_lock_is_acquired_exclusive(&map
->lock
)) {
6298 if (!vm_map_lookup_entry(map
, cur_target_addr
, &entry
)) {
6302 if (entry
->is_sub_map
) {
6306 object
= VME_OBJECT(entry
);
6307 if (object
== VM_OBJECT_NULL
) {
6311 object_offset
= cur_target_addr
- entry
->vme_start
+ VME_OFFSET(entry
);
6314 if (kdp_lck_rw_lock_is_acquired_exclusive(&object
->Lock
)) {
6318 if (object
->pager_created
&& (object
->paging_in_progress
||
6319 object
->activity_in_progress
)) {
6323 m
= kdp_vm_page_lookup(object
, object_offset
);
6325 if (m
!= VM_PAGE_NULL
) {
6326 if ((object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_DEFAULT
) {
6330 if (m
->vmp_laundry
|| m
->vmp_busy
|| m
->vmp_free_when_done
|| m
->vmp_absent
|| m
->vmp_error
|| m
->vmp_cleaning
||
6331 m
->vmp_overwriting
|| m
->vmp_restart
|| m
->vmp_unusual
) {
6335 assert(!m
->vmp_private
);
6336 if (m
->vmp_private
) {
6340 assert(!m
->vmp_fictitious
);
6341 if (m
->vmp_fictitious
) {
6345 assert(m
->vmp_q_state
!= VM_PAGE_USED_BY_COMPRESSOR
);
6346 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
6350 return ptoa(VM_PAGE_GET_PHYS_PAGE(m
));
6353 compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
6355 if (object
->pager_created
&& MUST_ASK_PAGER(object
, object_offset
, compressor_external_state
)) {
6356 if (compressor_external_state
== VM_EXTERNAL_STATE_EXISTS
) {
6357 kr
= vm_compressor_pager_get(object
->pager
, (object_offset
+ object
->paging_offset
),
6358 kdp_compressor_decompressed_page_ppnum
, &my_fault_type
,
6359 compressor_flags
, &compressed_count_delta
);
6360 if (kr
== KERN_SUCCESS
) {
6361 return kdp_compressor_decompressed_page_paddr
;
6368 if (object
->shadow
== VM_OBJECT_NULL
) {
6372 object_offset
+= object
->vo_shadow_offset
;
6373 object
= object
->shadow
;
6378 * vm_page_validate_cs_fast():
6379 * Performs a few quick checks to determine if the page's code signature
6380 * really needs to be fully validated. It could:
6381 * 1. have been modified (i.e. automatically tainted),
6382 * 2. have already been validated,
6383 * 3. have already been found to be tainted,
6384 * 4. no longer have a backing store.
6385 * Returns FALSE if the page needs to be fully validated.
6388 vm_page_validate_cs_fast(
6393 object
= VM_PAGE_OBJECT(page
);
6394 vm_object_lock_assert_held(object
);
6396 if (page
->vmp_wpmapped
&& !page
->vmp_cs_tainted
) {
6398 * This page was mapped for "write" access sometime in the
6399 * past and could still be modifiable in the future.
6400 * Consider it tainted.
6401 * [ If the page was already found to be "tainted", no
6402 * need to re-validate. ]
6404 vm_object_lock_assert_exclusive(object
);
6405 page
->vmp_cs_validated
= TRUE
;
6406 page
->vmp_cs_tainted
= TRUE
;
6408 printf("CODESIGNING: %s: "
6409 "page %p obj %p off 0x%llx "
6412 page
, object
, page
->vmp_offset
);
6414 vm_cs_validated_dirtied
++;
6417 if (page
->vmp_cs_validated
|| page
->vmp_cs_tainted
) {
6420 vm_object_lock_assert_exclusive(object
);
6422 #if CHECK_CS_VALIDATION_BITMAP
6425 kr
= vnode_pager_cs_check_validation_bitmap(
6427 page
->vmp_offset
+ object
->paging_offset
,
6429 if (kr
== KERN_SUCCESS
) {
6430 page
->vmp_cs_validated
= TRUE
;
6431 page
->vmp_cs_tainted
= FALSE
;
6432 vm_cs_bitmap_validated
++;
6435 #endif /* CHECK_CS_VALIDATION_BITMAP */
6437 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
6439 * The object is terminating and we don't have its pager
6440 * so we can't validate the data...
6445 /* we need to really validate this page */
6446 vm_object_lock_assert_exclusive(object
);
6451 vm_page_validate_cs_mapped_slow(
6456 memory_object_offset_t mo_offset
;
6457 memory_object_t pager
;
6458 struct vnode
*vnode
;
6459 boolean_t validated
;
6462 assert(page
->vmp_busy
);
6463 object
= VM_PAGE_OBJECT(page
);
6464 vm_object_lock_assert_exclusive(object
);
6469 * Since we get here to validate a page that was brought in by
6470 * the pager, we know that this pager is all setup and ready
6473 assert(object
->code_signed
);
6474 assert(!object
->internal
);
6475 assert(object
->pager
!= NULL
);
6476 assert(object
->pager_ready
);
6478 pager
= object
->pager
;
6479 assert(object
->paging_in_progress
);
6480 vnode
= vnode_pager_lookup_vnode(pager
);
6481 mo_offset
= page
->vmp_offset
+ object
->paging_offset
;
6483 /* verify the SHA1 hash for this page */
6485 validated
= cs_validate_range(vnode
,
6488 (const void *)((const char *)kaddr
),
6492 if (tainted
& CS_VALIDATE_TAINTED
) {
6493 page
->vmp_cs_tainted
= TRUE
;
6495 if (tainted
& CS_VALIDATE_NX
) {
6496 page
->vmp_cs_nx
= TRUE
;
6499 page
->vmp_cs_validated
= TRUE
;
6502 #if CHECK_CS_VALIDATION_BITMAP
6503 if (page
->vmp_cs_validated
&& !page
->vmp_cs_tainted
) {
6504 vnode_pager_cs_check_validation_bitmap(object
->pager
,
6508 #endif /* CHECK_CS_VALIDATION_BITMAP */
6512 vm_page_validate_cs_mapped(
6516 if (!vm_page_validate_cs_fast(page
)) {
6517 vm_page_validate_cs_mapped_slow(page
, kaddr
);
6522 vm_page_validate_cs(
6526 vm_object_offset_t offset
;
6527 vm_map_offset_t koffset
;
6528 vm_map_size_t ksize
;
6531 boolean_t busy_page
;
6532 boolean_t need_unmap
;
6534 object
= VM_PAGE_OBJECT(page
);
6535 vm_object_lock_assert_held(object
);
6537 if (vm_page_validate_cs_fast(page
)) {
6540 vm_object_lock_assert_exclusive(object
);
6542 assert(object
->code_signed
);
6543 offset
= page
->vmp_offset
;
6545 busy_page
= page
->vmp_busy
;
6547 /* keep page busy while we map (and unlock) the VM object */
6548 page
->vmp_busy
= TRUE
;
6552 * Take a paging reference on the VM object
6553 * to protect it from collapse or bypass,
6554 * and keep it from disappearing too.
6556 vm_object_paging_begin(object
);
6558 /* map the page in the kernel address space */
6559 ksize
= PAGE_SIZE_64
;
6562 kr
= vm_paging_map_object(page
,
6566 FALSE
, /* can't unlock object ! */
6570 if (kr
!= KERN_SUCCESS
) {
6571 panic("%s: could not map page: 0x%x\n", __FUNCTION__
, kr
);
6573 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
6575 /* validate the mapped page */
6576 vm_page_validate_cs_mapped_slow(page
, (const void *) kaddr
);
6578 assert(page
->vmp_busy
);
6579 assert(object
== VM_PAGE_OBJECT(page
));
6580 vm_object_lock_assert_exclusive(object
);
6583 PAGE_WAKEUP_DONE(page
);
6586 /* unmap the map from the kernel address space */
6587 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
6592 vm_object_paging_end(object
);
6596 vm_page_validate_cs_mapped_chunk(
6599 vm_offset_t chunk_offset
,
6600 vm_size_t chunk_size
,
6601 boolean_t
*validated_p
,
6602 unsigned *tainted_p
)
6605 vm_object_offset_t offset
, offset_in_page
;
6606 memory_object_t pager
;
6607 struct vnode
*vnode
;
6608 boolean_t validated
;
6611 *validated_p
= FALSE
;
6614 assert(page
->vmp_busy
);
6615 object
= VM_PAGE_OBJECT(page
);
6616 vm_object_lock_assert_exclusive(object
);
6618 assert(object
->code_signed
);
6619 offset
= page
->vmp_offset
;
6621 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
6623 * The object is terminating and we don't have its pager
6624 * so we can't validate the data...
6629 * Since we get here to validate a page that was brought in by
6630 * the pager, we know that this pager is all setup and ready
6633 assert(!object
->internal
);
6634 assert(object
->pager
!= NULL
);
6635 assert(object
->pager_ready
);
6637 pager
= object
->pager
;
6638 assert(object
->paging_in_progress
);
6639 vnode
= vnode_pager_lookup_vnode(pager
);
6641 /* verify the signature for this chunk */
6642 offset_in_page
= chunk_offset
;
6643 assert(offset_in_page
< PAGE_SIZE
);
6646 validated
= cs_validate_range(vnode
,
6648 (object
->paging_offset
+
6651 (const void *)((const char *)kaddr
6656 *validated_p
= TRUE
;
6659 *tainted_p
= tainted
;
6664 vm_rtfrecord_lock(void)
6666 lck_spin_lock(&vm_rtfr_slock
);
6670 vm_rtfrecord_unlock(void)
6672 lck_spin_unlock(&vm_rtfr_slock
);
6676 vmrtfaultinfo_bufsz(void)
6678 return vmrtf_num_records
* sizeof(vm_rtfault_record_t
);
6681 #include <kern/backtrace.h>
6684 vm_record_rtfault(thread_t cthread
, uint64_t fstart
, vm_map_offset_t fault_vaddr
, int type_of_fault
)
6686 uint64_t fend
= mach_continuous_time();
6689 uint64_t ctid
= cthread
->thread_id
;
6690 uint64_t cupid
= get_current_unique_pid();
6696 /* Capture a single-frame backtrace; this extracts just the program
6697 * counter at the point of the fault into "bpc", and should perform no
6698 * further user stack traversals, thus avoiding copyin()s and further
6701 unsigned int bfrs
= backtrace_thread_user(cthread
, &bpc
, 1U, &btr
, &u64
, NULL
);
6703 if ((btr
== 0) && (bfrs
> 0)) {
6707 assert((fstart
!= 0) && fend
>= fstart
);
6708 vm_rtfrecord_lock();
6709 assert(vmrtfrs
.vmrtfr_curi
<= vmrtfrs
.vmrtfr_maxi
);
6711 vmrtfrs
.vmrtf_total
++;
6712 vm_rtfault_record_t
*cvmr
= &vmrtfrs
.vm_rtf_records
[vmrtfrs
.vmrtfr_curi
++];
6714 cvmr
->rtfabstime
= fstart
;
6715 cvmr
->rtfduration
= fend
- fstart
;
6716 cvmr
->rtfaddr
= fault_vaddr
;
6718 cvmr
->rtftype
= type_of_fault
;
6719 cvmr
->rtfupid
= cupid
;
6720 cvmr
->rtftid
= ctid
;
6722 if (vmrtfrs
.vmrtfr_curi
> vmrtfrs
.vmrtfr_maxi
) {
6723 vmrtfrs
.vmrtfr_curi
= 0;
6726 vm_rtfrecord_unlock();
6730 vmrtf_extract(uint64_t cupid
, __unused boolean_t isroot
, int vrecordsz
, void *vrecords
, int *vmrtfrv
)
6732 vm_rtfault_record_t
*cvmrd
= vrecords
;
6733 size_t residue
= vrecordsz
;
6734 int numextracted
= 0;
6735 boolean_t early_exit
= FALSE
;
6737 vm_rtfrecord_lock();
6739 for (int vmfi
= 0; vmfi
<= vmrtfrs
.vmrtfr_maxi
; vmfi
++) {
6740 if (residue
< sizeof(vm_rtfault_record_t
)) {
6745 if (vmrtfrs
.vm_rtf_records
[vmfi
].rtfupid
!= cupid
) {
6746 #if DEVELOPMENT || DEBUG
6747 if (isroot
== FALSE
) {
6752 #endif /* DEVDEBUG */
6755 *cvmrd
= vmrtfrs
.vm_rtf_records
[vmfi
];
6757 residue
-= sizeof(vm_rtfault_record_t
);
6761 vm_rtfrecord_unlock();
6763 *vmrtfrv
= numextracted
;