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
;
142 #define NEED_TO_HARD_THROTTLE_THIS_TASK() (vm_wants_task_throttled(current_task()) || \
143 ((vm_page_free_count < vm_page_throttle_limit || \
144 HARD_THROTTLE_LIMIT_REACHED()) && \
145 proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO) >= THROTTLE_LEVEL_THROTTLED))
148 #define HARD_THROTTLE_DELAY 10000 /* 10000 us == 10 ms */
149 #define SOFT_THROTTLE_DELAY 200 /* 200 us == .2 ms */
151 #define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS 6
152 #define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC 20000
155 #define VM_STAT_DECOMPRESSIONS() \
157 VM_STAT_INCR(decompressions); \
158 current_thread()->decompressions++; \
161 boolean_t
current_thread_aborted(void);
163 /* Forward declarations of internal routines. */
164 static kern_return_t
vm_fault_wire_fast(
169 vm_map_entry_t entry
,
171 vm_map_offset_t pmap_addr
,
172 ppnum_t
*physpage_p
);
174 static kern_return_t
vm_fault_internal(
176 vm_map_offset_t vaddr
,
177 vm_prot_t caller_prot
,
178 boolean_t change_wiring
,
182 vm_map_offset_t pmap_addr
,
183 ppnum_t
*physpage_p
);
185 static void vm_fault_copy_cleanup(
189 static void vm_fault_copy_dst_cleanup(
192 #if VM_FAULT_CLASSIFY
193 extern void vm_fault_classify(vm_object_t object
,
194 vm_object_offset_t offset
,
195 vm_prot_t fault_type
);
197 extern void vm_fault_classify_init(void);
200 unsigned long vm_pmap_enter_blocked
= 0;
201 unsigned long vm_pmap_enter_retried
= 0;
203 unsigned long vm_cs_validates
= 0;
204 unsigned long vm_cs_revalidates
= 0;
205 unsigned long vm_cs_query_modified
= 0;
206 unsigned long vm_cs_validated_dirtied
= 0;
207 unsigned long vm_cs_bitmap_validated
= 0;
209 uint64_t vm_cs_defer_to_pmap_cs
= 0;
210 uint64_t vm_cs_defer_to_pmap_cs_not
= 0;
213 void vm_pre_fault(vm_map_offset_t
, vm_prot_t
);
215 extern char *kdp_compressor_decompressed_page
;
216 extern addr64_t kdp_compressor_decompressed_page_paddr
;
217 extern ppnum_t kdp_compressor_decompressed_page_ppnum
;
223 vm_rtfault_record_t
*vm_rtf_records
;
225 #define VMRTF_DEFAULT_BUFSIZE (4096)
226 #define VMRTF_NUM_RECORDS_DEFAULT (VMRTF_DEFAULT_BUFSIZE / sizeof(vm_rtfault_record_t))
227 int vmrtf_num_records
= VMRTF_NUM_RECORDS_DEFAULT
;
229 static void vm_rtfrecord_lock(void);
230 static void vm_rtfrecord_unlock(void);
231 static void vm_record_rtfault(thread_t
, uint64_t, vm_map_offset_t
, int);
233 lck_spin_t vm_rtfr_slock
;
234 extern lck_grp_t vm_page_lck_grp_bucket
;
235 extern lck_attr_t vm_page_lck_attr
;
238 * Routine: vm_fault_init
240 * Initialize our private data structures.
245 int i
, vm_compressor_temp
;
246 boolean_t need_default_val
= TRUE
;
248 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
249 * computed as a percentage of available memory, and the percentage used is scaled inversely with
250 * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems
251 * and reduce the value down to 10% for very large memory configurations. This helps give us a
252 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
253 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
256 vm_hard_throttle_threshold
= sane_size
* (35 - MIN((int)(sane_size
/ (1024 * 1024 * 1024)), 25)) / 100;
259 * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry.
262 if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp
, sizeof(vm_compressor_temp
))) {
263 for (i
= 0; i
< VM_PAGER_MAX_MODES
; i
++) {
264 if (vm_compressor_temp
> 0 &&
265 ((vm_compressor_temp
& (1 << i
)) == vm_compressor_temp
)) {
266 need_default_val
= FALSE
;
267 vm_compressor_mode
= vm_compressor_temp
;
271 if (need_default_val
) {
272 printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp
);
275 if (need_default_val
) {
276 /* If no boot arg or incorrect boot arg, try device tree. */
277 PE_get_default("kern.vm_compressor", &vm_compressor_mode
, sizeof(vm_compressor_mode
));
279 printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode
);
281 PE_parse_boot_argn("vm_protect_privileged_from_untrusted", &vm_protect_privileged_from_untrusted
, sizeof(vm_protect_privileged_from_untrusted
));
285 vm_rtfault_record_init(void)
287 PE_parse_boot_argn("vm_rtfault_records", &vmrtf_num_records
, sizeof(vmrtf_num_records
));
289 assert(vmrtf_num_records
>= 1);
290 vmrtf_num_records
= MAX(vmrtf_num_records
, 1);
291 size_t kallocsz
= vmrtf_num_records
* sizeof(vm_rtfault_record_t
);
292 vmrtfrs
.vm_rtf_records
= kalloc(kallocsz
);
293 bzero(vmrtfrs
.vm_rtf_records
, kallocsz
);
294 vmrtfrs
.vmrtfr_maxi
= vmrtf_num_records
- 1;
295 lck_spin_init(&vm_rtfr_slock
, &vm_page_lck_grp_bucket
, &vm_page_lck_attr
);
298 * Routine: vm_fault_cleanup
300 * Clean up the result of vm_fault_page.
302 * The paging reference for "object" is released.
303 * "object" is unlocked.
304 * If "top_page" is not null, "top_page" is
305 * freed and the paging reference for the object
306 * containing it is released.
309 * "object" must be locked.
316 vm_object_paging_end(object
);
317 vm_object_unlock(object
);
319 if (top_page
!= VM_PAGE_NULL
) {
320 object
= VM_PAGE_OBJECT(top_page
);
322 vm_object_lock(object
);
323 VM_PAGE_FREE(top_page
);
324 vm_object_paging_end(object
);
325 vm_object_unlock(object
);
329 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
332 boolean_t vm_page_deactivate_behind
= TRUE
;
334 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
336 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
337 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
338 /* we use it to size an array on the stack */
340 int vm_default_behind
= VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW
;
342 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
345 * vm_page_is_sequential
347 * Determine if sequential access is in progress
348 * in accordance with the behavior specified.
349 * Update state to indicate current access pattern.
351 * object must have at least the shared lock held
355 vm_fault_is_sequential(
357 vm_object_offset_t offset
,
358 vm_behavior_t behavior
)
360 vm_object_offset_t last_alloc
;
364 last_alloc
= object
->last_alloc
;
365 sequential
= object
->sequential
;
366 orig_sequential
= sequential
;
369 case VM_BEHAVIOR_RANDOM
:
371 * reset indicator of sequential behavior
376 case VM_BEHAVIOR_SEQUENTIAL
:
377 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
379 * advance indicator of sequential behavior
381 if (sequential
< MAX_SEQUENTIAL_RUN
) {
382 sequential
+= PAGE_SIZE
;
386 * reset indicator of sequential behavior
392 case VM_BEHAVIOR_RSEQNTL
:
393 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
395 * advance indicator of sequential behavior
397 if (sequential
> -MAX_SEQUENTIAL_RUN
) {
398 sequential
-= PAGE_SIZE
;
402 * reset indicator of sequential behavior
408 case VM_BEHAVIOR_DEFAULT
:
410 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
412 * advance indicator of sequential behavior
414 if (sequential
< 0) {
417 if (sequential
< MAX_SEQUENTIAL_RUN
) {
418 sequential
+= PAGE_SIZE
;
420 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
422 * advance indicator of sequential behavior
424 if (sequential
> 0) {
427 if (sequential
> -MAX_SEQUENTIAL_RUN
) {
428 sequential
-= PAGE_SIZE
;
432 * reset indicator of sequential behavior
438 if (sequential
!= orig_sequential
) {
439 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
441 * if someone else has already updated object->sequential
442 * don't bother trying to update it or object->last_alloc
448 * I'd like to do this with a OSCompareAndSwap64, but that
449 * doesn't exist for PPC... however, it shouldn't matter
450 * that much... last_alloc is maintained so that we can determine
451 * if a sequential access pattern is taking place... if only
452 * one thread is banging on this object, no problem with the unprotected
453 * update... if 2 or more threads are banging away, we run the risk of
454 * someone seeing a mangled update... however, in the face of multiple
455 * accesses, no sequential access pattern can develop anyway, so we
456 * haven't lost any real info.
458 object
->last_alloc
= offset
;
462 int vm_page_deactivate_behind_count
= 0;
465 * vm_page_deactivate_behind
467 * Determine if sequential access is in progress
468 * in accordance with the behavior specified. If
469 * so, compute a potential page to deactivate and
472 * object must be locked.
474 * return TRUE if we actually deactivate a page
478 vm_fault_deactivate_behind(
480 vm_object_offset_t offset
,
481 vm_behavior_t behavior
)
484 int pages_in_run
= 0;
485 int max_pages_in_run
= 0;
487 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
488 vm_object_offset_t run_offset
= 0;
489 vm_object_offset_t pg_offset
= 0;
491 vm_page_t page_run
[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
];
495 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
498 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
500 * Do not deactivate pages from the kernel object: they
501 * are not intended to become pageable.
502 * or we've disabled the deactivate behind mechanism
506 if ((sequential_run
= object
->sequential
)) {
507 if (sequential_run
< 0) {
508 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
509 sequential_run
= 0 - sequential_run
;
511 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
515 case VM_BEHAVIOR_RANDOM
:
517 case VM_BEHAVIOR_SEQUENTIAL
:
518 if (sequential_run
>= (int)PAGE_SIZE
) {
519 run_offset
= 0 - PAGE_SIZE_64
;
520 max_pages_in_run
= 1;
523 case VM_BEHAVIOR_RSEQNTL
:
524 if (sequential_run
>= (int)PAGE_SIZE
) {
525 run_offset
= PAGE_SIZE_64
;
526 max_pages_in_run
= 1;
529 case VM_BEHAVIOR_DEFAULT
:
531 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
534 * determine if the run of sequential accesss has been
535 * long enough on an object with default access behavior
536 * to consider it for deactivation
538 if ((uint64_t)sequential_run
>= behind
&& (sequential_run
% (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
* PAGE_SIZE
)) == 0) {
540 * the comparisons between offset and behind are done
541 * in this kind of odd fashion in order to prevent wrap around
544 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
545 if (offset
>= behind
) {
546 run_offset
= 0 - behind
;
547 pg_offset
= PAGE_SIZE_64
;
548 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
551 if (offset
< -behind
) {
553 pg_offset
= 0 - PAGE_SIZE_64
;
554 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
560 for (n
= 0; n
< max_pages_in_run
; n
++) {
561 m
= vm_page_lookup(object
, offset
+ run_offset
+ (n
* pg_offset
));
563 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
) {
564 page_run
[pages_in_run
++] = m
;
567 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
569 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
570 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
571 * new reference happens. If no futher references happen on the page after that remote TLB flushes
572 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
573 * by pageout_scan, which is just fine since the last reference would have happened quite far
574 * in the past (TLB caches don't hang around for very long), and of course could just as easily
575 * have happened before we did the deactivate_behind.
577 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m
), VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
581 vm_page_lockspin_queues();
583 for (n
= 0; n
< pages_in_run
; n
++) {
586 vm_page_deactivate_internal(m
, FALSE
);
588 vm_page_deactivate_behind_count
++;
590 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
593 vm_page_unlock_queues();
601 #if (DEVELOPMENT || DEBUG)
602 uint32_t vm_page_creation_throttled_hard
= 0;
603 uint32_t vm_page_creation_throttled_soft
= 0;
604 uint64_t vm_page_creation_throttle_avoided
= 0;
605 #endif /* DEVELOPMENT || DEBUG */
608 vm_page_throttled(boolean_t page_kept
)
610 clock_sec_t elapsed_sec
;
612 clock_usec_t tv_usec
;
614 thread_t thread
= current_thread();
616 if (thread
->options
& TH_OPT_VMPRIV
) {
620 if (thread
->t_page_creation_throttled
) {
621 thread
->t_page_creation_throttled
= 0;
623 if (page_kept
== FALSE
) {
627 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
628 #if (DEVELOPMENT || DEBUG)
629 thread
->t_page_creation_throttled_hard
++;
630 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
631 #endif /* DEVELOPMENT || DEBUG */
632 return HARD_THROTTLE_DELAY
;
635 if ((vm_page_free_count
< vm_page_throttle_limit
|| (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& SWAPPER_NEEDS_TO_UNTHROTTLE())) &&
636 thread
->t_page_creation_count
> (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
* VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
)) {
637 if (vm_page_free_wanted
== 0 && vm_page_free_wanted_privileged
== 0) {
638 #if (DEVELOPMENT || DEBUG)
639 OSAddAtomic64(1, &vm_page_creation_throttle_avoided
);
643 clock_get_system_microtime(&tv_sec
, &tv_usec
);
645 elapsed_sec
= tv_sec
- thread
->t_page_creation_time
;
647 if (elapsed_sec
<= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
||
648 (thread
->t_page_creation_count
/ elapsed_sec
) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
) {
649 if (elapsed_sec
>= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
)) {
651 * we'll reset our stats to give a well behaved app
652 * that was unlucky enough to accumulate a bunch of pages
653 * over a long period of time a chance to get out of
654 * the throttled state... we reset the counter and timestamp
655 * so that if it stays under the rate limit for the next second
656 * it will be back in our good graces... if it exceeds it, it
657 * will remain in the throttled state
659 thread
->t_page_creation_time
= tv_sec
;
660 thread
->t_page_creation_count
= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
* (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
- 1);
662 VM_PAGEOUT_DEBUG(vm_page_throttle_count
, 1);
664 thread
->t_page_creation_throttled
= 1;
666 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& HARD_THROTTLE_LIMIT_REACHED()) {
667 #if (DEVELOPMENT || DEBUG)
668 thread
->t_page_creation_throttled_hard
++;
669 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
670 #endif /* DEVELOPMENT || DEBUG */
671 return HARD_THROTTLE_DELAY
;
673 #if (DEVELOPMENT || DEBUG)
674 thread
->t_page_creation_throttled_soft
++;
675 OSAddAtomic(1, &vm_page_creation_throttled_soft
);
676 #endif /* DEVELOPMENT || DEBUG */
677 return SOFT_THROTTLE_DELAY
;
680 thread
->t_page_creation_time
= tv_sec
;
681 thread
->t_page_creation_count
= 0;
684 thread
->t_page_creation_count
++;
691 * check for various conditions that would
692 * prevent us from creating a ZF page...
693 * cleanup is based on being called from vm_fault_page
695 * object must be locked
696 * object == m->vmp_object
698 static vm_fault_return_t
699 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
)
703 if (object
->shadow_severed
||
704 VM_OBJECT_PURGEABLE_FAULT_ERROR(object
)) {
707 * 1. the shadow chain was severed,
708 * 2. the purgeable object is volatile or empty and is marked
709 * to fault on access while volatile.
710 * Just have to return an error at this point
712 if (m
!= VM_PAGE_NULL
) {
715 vm_fault_cleanup(object
, first_m
);
717 thread_interrupt_level(interruptible_state
);
719 return VM_FAULT_MEMORY_ERROR
;
721 if (page_throttle
== TRUE
) {
722 if ((throttle_delay
= vm_page_throttled(FALSE
))) {
724 * we're throttling zero-fills...
725 * treat this as if we couldn't grab a page
727 if (m
!= VM_PAGE_NULL
) {
730 vm_fault_cleanup(object
, first_m
);
732 VM_DEBUG_EVENT(vmf_check_zfdelay
, VMF_CHECK_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
734 delay(throttle_delay
);
736 if (current_thread_aborted()) {
737 thread_interrupt_level(interruptible_state
);
738 return VM_FAULT_INTERRUPTED
;
740 thread_interrupt_level(interruptible_state
);
742 return VM_FAULT_MEMORY_SHORTAGE
;
745 return VM_FAULT_SUCCESS
;
750 * do the work to zero fill a page and
751 * inject it into the correct paging queue
753 * m->vmp_object must be locked
754 * page queue lock must NOT be held
757 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
759 int my_fault
= DBG_ZERO_FILL_FAULT
;
762 object
= VM_PAGE_OBJECT(m
);
765 * This is is a zero-fill page fault...
767 * Checking the page lock is a waste of
768 * time; this page was absent, so
769 * it can't be page locked by a pager.
771 * we also consider it undefined
772 * with respect to instruction
773 * execution. i.e. it is the responsibility
774 * of higher layers to call for an instruction
775 * sync after changing the contents and before
776 * sending a program into this area. We
777 * choose this approach for performance
779 m
->vmp_pmapped
= TRUE
;
781 m
->vmp_cs_validated
= FALSE
;
782 m
->vmp_cs_tainted
= FALSE
;
783 m
->vmp_cs_nx
= FALSE
;
785 if (no_zero_fill
== TRUE
) {
786 my_fault
= DBG_NZF_PAGE_FAULT
;
788 if (m
->vmp_absent
&& m
->vmp_busy
) {
792 vm_page_zero_fill(m
);
794 VM_STAT_INCR(zero_fill_count
);
795 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
797 assert(!m
->vmp_laundry
);
798 assert(object
!= kernel_object
);
799 //assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0);
801 if (!VM_DYNAMIC_PAGING_ENABLED() &&
802 (object
->purgable
== VM_PURGABLE_DENY
||
803 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
804 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
805 vm_page_lockspin_queues();
807 if (!VM_DYNAMIC_PAGING_ENABLED()) {
808 assert(!VM_PAGE_WIRED(m
));
811 * can't be on the pageout queue since we don't
812 * have a pager to try and clean to
814 vm_page_queues_remove(m
, TRUE
);
815 vm_page_check_pageable_safe(m
);
816 vm_page_queue_enter(&vm_page_queue_throttled
, m
, vmp_pageq
);
817 m
->vmp_q_state
= VM_PAGE_ON_THROTTLED_Q
;
818 vm_page_throttled_count
++;
820 vm_page_unlock_queues();
827 * Routine: vm_fault_page
829 * Find the resident page for the virtual memory
830 * specified by the given virtual memory object
832 * Additional arguments:
833 * The required permissions for the page is given
834 * in "fault_type". Desired permissions are included
836 * fault_info is passed along to determine pagein cluster
837 * limits... it contains the expected reference pattern,
838 * cluster size if available, etc...
840 * If the desired page is known to be resident (for
841 * example, because it was previously wired down), asserting
842 * the "unwiring" parameter will speed the search.
844 * If the operation can be interrupted (by thread_abort
845 * or thread_terminate), then the "interruptible"
846 * parameter should be asserted.
849 * The page containing the proper data is returned
853 * The source object must be locked and referenced,
854 * and must donate one paging reference. The reference
855 * is not affected. The paging reference and lock are
858 * If the call succeeds, the object in which "result_page"
859 * resides is left locked and holding a paging reference.
860 * If this is not the original object, a busy page in the
861 * original object is returned in "top_page", to prevent other
862 * callers from pursuing this same data, along with a paging
863 * reference for the original object. The "top_page" should
864 * be destroyed when this guarantee is no longer required.
865 * The "result_page" is also left busy. It is not removed
866 * from the pageout queues.
868 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
869 * fault succeeded but there's no VM page (i.e. the VM object
870 * does not actually hold VM pages, but device memory or
871 * large pages). The object is still locked and we still hold a
872 * paging_in_progress reference.
874 unsigned int vm_fault_page_blocked_access
= 0;
875 unsigned int vm_fault_page_forced_retry
= 0;
880 vm_object_t first_object
, /* Object to begin search */
881 vm_object_offset_t first_offset
, /* Offset into object */
882 vm_prot_t fault_type
, /* What access is requested */
883 boolean_t must_be_resident
,/* Must page be resident? */
884 boolean_t caller_lookup
, /* caller looked up page */
885 /* Modifies in place: */
886 vm_prot_t
*protection
, /* Protection for mapping */
887 vm_page_t
*result_page
, /* Page found, if successful */
889 vm_page_t
*top_page
, /* Page in top object, if
890 * not result_page. */
891 int *type_of_fault
, /* if non-null, fill in with type of fault
892 * COW, zero-fill, etc... returned in trace point */
893 /* More arguments: */
894 kern_return_t
*error_code
, /* code if page is in error */
895 boolean_t no_zero_fill
, /* don't zero fill absent pages */
896 boolean_t data_supply
, /* treat as data_supply if
897 * it is a write fault and a full
898 * page is provided */
899 vm_object_fault_info_t fault_info
)
903 vm_object_offset_t offset
;
905 vm_object_t next_object
;
906 vm_object_t copy_object
;
907 boolean_t look_for_page
;
908 boolean_t force_fault_retry
= FALSE
;
909 vm_prot_t access_required
= fault_type
;
910 vm_prot_t wants_copy_flag
;
911 kern_return_t wait_result
;
912 wait_interrupt_t interruptible_state
;
913 boolean_t data_already_requested
= FALSE
;
914 vm_behavior_t orig_behavior
;
915 vm_size_t orig_cluster_size
;
916 vm_fault_return_t error
;
918 uint32_t try_failed_count
;
919 int interruptible
; /* how may fault be interrupted? */
920 int external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
921 memory_object_t pager
;
922 vm_fault_return_t retval
;
926 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
927 * marked as paged out in the compressor pager or the pager doesn't exist.
928 * Note also that if the pager for an internal object
929 * has not been created, the pager is not invoked regardless of the value
930 * of MUST_ASK_PAGER().
932 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
933 * is marked as paged out in the compressor pager.
934 * PAGED_OUT() is used to determine if a page has already been pushed
935 * into a copy object in order to avoid a redundant page out operation.
937 #define MUST_ASK_PAGER(o, f, s) \
938 ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
940 #define PAGED_OUT(o, f) \
941 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
946 #define RELEASE_PAGE(m) \
948 PAGE_WAKEUP_DONE(m); \
949 if ( !VM_PAGE_PAGEABLE(m)) { \
950 vm_page_lockspin_queues(); \
951 if ( !VM_PAGE_PAGEABLE(m)) { \
952 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE) \
953 vm_page_deactivate(m); \
955 vm_page_activate(m); \
957 vm_page_unlock_queues(); \
962 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
965 interruptible
= fault_info
->interruptible
;
966 interruptible_state
= thread_interrupt_level(interruptible
);
969 * INVARIANTS (through entire routine):
971 * 1) At all times, we must either have the object
972 * lock or a busy page in some object to prevent
973 * some other thread from trying to bring in
976 * Note that we cannot hold any locks during the
977 * pager access or when waiting for memory, so
978 * we use a busy page then.
980 * 2) To prevent another thread from racing us down the
981 * shadow chain and entering a new page in the top
982 * object before we do, we must keep a busy page in
983 * the top object while following the shadow chain.
985 * 3) We must increment paging_in_progress on any object
986 * for which we have a busy page before dropping
989 * 4) We leave busy pages on the pageout queues.
990 * If the pageout daemon comes across a busy page,
991 * it will remove the page from the pageout queues.
994 object
= first_object
;
995 offset
= first_offset
;
996 first_m
= VM_PAGE_NULL
;
997 access_required
= fault_type
;
1000 * default type of fault
1002 my_fault
= DBG_CACHE_HIT_FAULT
;
1006 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1010 #if CONFIG_SECLUDED_MEMORY
1011 if (object
->can_grab_secluded
) {
1012 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
1014 #endif /* CONFIG_SECLUDED_MEMORY */
1016 if (!object
->alive
) {
1018 * object is no longer valid
1019 * clean up and return error
1021 vm_fault_cleanup(object
, first_m
);
1022 thread_interrupt_level(interruptible_state
);
1024 return VM_FAULT_MEMORY_ERROR
;
1027 if (!object
->pager_created
&& object
->phys_contiguous
) {
1029 * A physically-contiguous object without a pager:
1030 * must be a "large page" object. We do not deal
1031 * with VM pages for this object.
1033 caller_lookup
= FALSE
;
1035 goto phys_contig_object
;
1038 if (object
->blocked_access
) {
1040 * Access to this VM object has been blocked.
1041 * Replace our "paging_in_progress" reference with
1042 * a "activity_in_progress" reference and wait for
1043 * access to be unblocked.
1045 caller_lookup
= FALSE
; /* no longer valid after sleep */
1046 vm_object_activity_begin(object
);
1047 vm_object_paging_end(object
);
1048 while (object
->blocked_access
) {
1049 vm_object_sleep(object
,
1050 VM_OBJECT_EVENT_UNBLOCKED
,
1053 vm_fault_page_blocked_access
++;
1054 vm_object_paging_begin(object
);
1055 vm_object_activity_end(object
);
1059 * See whether the page at 'offset' is resident
1061 if (caller_lookup
== TRUE
) {
1063 * The caller has already looked up the page
1064 * and gave us the result in "result_page".
1065 * We can use this for the first lookup but
1066 * it loses its validity as soon as we unlock
1070 caller_lookup
= FALSE
; /* no longer valid after that */
1072 m
= vm_page_lookup(object
, offset
);
1075 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1077 if (m
!= VM_PAGE_NULL
) {
1080 * The page is being brought in,
1081 * wait for it and then retry.
1084 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1086 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
1088 counter(c_vm_fault_page_block_busy_kernel
++);
1090 if (wait_result
!= THREAD_AWAKENED
) {
1091 vm_fault_cleanup(object
, first_m
);
1092 thread_interrupt_level(interruptible_state
);
1094 if (wait_result
== THREAD_RESTART
) {
1095 return VM_FAULT_RETRY
;
1097 return VM_FAULT_INTERRUPTED
;
1102 if (m
->vmp_laundry
) {
1103 m
->vmp_free_when_done
= FALSE
;
1105 if (!m
->vmp_cleaning
) {
1106 vm_pageout_steal_laundry(m
, FALSE
);
1109 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
1111 * Guard page: off limits !
1113 if (fault_type
== VM_PROT_NONE
) {
1115 * The fault is not requesting any
1116 * access to the guard page, so it must
1117 * be just to wire or unwire it.
1118 * Let's pretend it succeeded...
1122 assert(first_m
== VM_PAGE_NULL
);
1123 *top_page
= first_m
;
1124 if (type_of_fault
) {
1125 *type_of_fault
= DBG_GUARD_FAULT
;
1127 thread_interrupt_level(interruptible_state
);
1128 return VM_FAULT_SUCCESS
;
1131 * The fault requests access to the
1132 * guard page: let's deny that !
1134 vm_fault_cleanup(object
, first_m
);
1135 thread_interrupt_level(interruptible_state
);
1136 return VM_FAULT_MEMORY_ERROR
;
1142 * The page is in error, give up now.
1145 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1148 *error_code
= KERN_MEMORY_ERROR
;
1152 vm_fault_cleanup(object
, first_m
);
1153 thread_interrupt_level(interruptible_state
);
1155 return VM_FAULT_MEMORY_ERROR
;
1157 if (m
->vmp_restart
) {
1159 * The pager wants us to restart
1160 * at the top of the chain,
1161 * typically because it has moved the
1162 * page to another pager, then do so.
1165 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1169 vm_fault_cleanup(object
, first_m
);
1170 thread_interrupt_level(interruptible_state
);
1172 return VM_FAULT_RETRY
;
1174 if (m
->vmp_absent
) {
1176 * The page isn't busy, but is absent,
1177 * therefore it's deemed "unavailable".
1179 * Remove the non-existent page (unless it's
1180 * in the top object) and move on down to the
1181 * next object (if there is one).
1184 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1186 next_object
= object
->shadow
;
1188 if (next_object
== VM_OBJECT_NULL
) {
1190 * Absent page at bottom of shadow
1191 * chain; zero fill the page we left
1192 * busy in the first object, and free
1195 assert(!must_be_resident
);
1198 * check for any conditions that prevent
1199 * us from creating a new zero-fill page
1200 * vm_fault_check will do all of the
1201 * fault cleanup in the case of an error condition
1202 * including resetting the thread_interrupt_level
1204 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1206 if (error
!= VM_FAULT_SUCCESS
) {
1210 if (object
!= first_object
) {
1212 * free the absent page we just found
1217 * drop reference and lock on current object
1219 vm_object_paging_end(object
);
1220 vm_object_unlock(object
);
1223 * grab the original page we
1224 * 'soldered' in place and
1225 * retake lock on 'first_object'
1228 first_m
= VM_PAGE_NULL
;
1230 object
= first_object
;
1231 offset
= first_offset
;
1233 vm_object_lock(object
);
1236 * we're going to use the absent page we just found
1237 * so convert it to a 'busy' page
1239 m
->vmp_absent
= FALSE
;
1242 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
) {
1243 m
->vmp_absent
= TRUE
;
1246 * zero-fill the page and put it on
1247 * the correct paging queue
1249 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1253 if (must_be_resident
) {
1254 vm_object_paging_end(object
);
1255 } else if (object
!= first_object
) {
1256 vm_object_paging_end(object
);
1260 m
->vmp_absent
= FALSE
;
1263 vm_page_lockspin_queues();
1264 vm_page_queues_remove(m
, FALSE
);
1265 vm_page_unlock_queues();
1268 offset
+= object
->vo_shadow_offset
;
1269 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1270 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1271 access_required
= VM_PROT_READ
;
1273 vm_object_lock(next_object
);
1274 vm_object_unlock(object
);
1275 object
= next_object
;
1276 vm_object_paging_begin(object
);
1279 * reset to default type of fault
1281 my_fault
= DBG_CACHE_HIT_FAULT
;
1286 if ((m
->vmp_cleaning
)
1287 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1288 && (fault_type
& VM_PROT_WRITE
)) {
1290 * This is a copy-on-write fault that will
1291 * cause us to revoke access to this page, but
1292 * this page is in the process of being cleaned
1293 * in a clustered pageout. We must wait until
1294 * the cleaning operation completes before
1295 * revoking access to the original page,
1296 * otherwise we might attempt to remove a
1300 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1303 * take an extra ref so that object won't die
1305 vm_object_reference_locked(object
);
1307 vm_fault_cleanup(object
, first_m
);
1309 counter(c_vm_fault_page_block_backoff_kernel
++);
1310 vm_object_lock(object
);
1311 assert(object
->ref_count
> 0);
1313 m
= vm_page_lookup(object
, offset
);
1315 if (m
!= VM_PAGE_NULL
&& m
->vmp_cleaning
) {
1316 PAGE_ASSERT_WAIT(m
, interruptible
);
1318 vm_object_unlock(object
);
1319 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1320 vm_object_deallocate(object
);
1324 vm_object_unlock(object
);
1326 vm_object_deallocate(object
);
1327 thread_interrupt_level(interruptible_state
);
1329 return VM_FAULT_RETRY
;
1332 if (type_of_fault
== NULL
&& (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) &&
1333 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1335 * If we were passed a non-NULL pointer for
1336 * "type_of_fault", than we came from
1337 * vm_fault... we'll let it deal with
1338 * this condition, since it
1339 * needs to see m->vmp_speculative to correctly
1340 * account the pageins, otherwise...
1341 * take it off the speculative queue, we'll
1342 * let the caller of vm_fault_page deal
1343 * with getting it onto the correct queue
1345 * If the caller specified in fault_info that
1346 * it wants a "stealth" fault, we also leave
1347 * the page in the speculative queue.
1349 vm_page_lockspin_queues();
1350 if (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) {
1351 vm_page_queues_remove(m
, FALSE
);
1353 vm_page_unlock_queues();
1355 assert(object
== VM_PAGE_OBJECT(m
));
1357 if (object
->code_signed
) {
1360 * We just paged in a page from a signed
1361 * memory object but we don't need to
1362 * validate it now. We'll validate it if
1363 * when it gets mapped into a user address
1364 * space for the first time or when the page
1365 * gets copied to another object as a result
1366 * of a copy-on-write.
1371 * We mark the page busy and leave it on
1372 * the pageout queues. If the pageout
1373 * deamon comes across it, then it will
1374 * remove the page from the queue, but not the object
1377 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1379 assert(!m
->vmp_busy
);
1380 assert(!m
->vmp_absent
);
1388 * we get here when there is no page present in the object at
1389 * the offset we're interested in... we'll allocate a page
1390 * at this point if the pager associated with
1391 * this object can provide the data or we're the top object...
1392 * object is locked; m == NULL
1395 if (must_be_resident
) {
1396 if (fault_type
== VM_PROT_NONE
&&
1397 object
== kernel_object
) {
1399 * We've been called from vm_fault_unwire()
1400 * while removing a map entry that was allocated
1401 * with KMA_KOBJECT and KMA_VAONLY. This page
1402 * is not present and there's nothing more to
1403 * do here (nothing to unwire).
1405 vm_fault_cleanup(object
, first_m
);
1406 thread_interrupt_level(interruptible_state
);
1408 return VM_FAULT_MEMORY_ERROR
;
1411 goto dont_look_for_page
;
1414 /* Don't expect to fault pages into the kernel object. */
1415 assert(object
!= kernel_object
);
1417 data_supply
= FALSE
;
1419 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
, external_state
) == TRUE
) && !data_supply
);
1422 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1424 if (!look_for_page
&& object
== first_object
&& !object
->phys_contiguous
) {
1426 * Allocate a new page for this object/offset pair as a placeholder
1428 m
= vm_page_grab_options(grab_options
);
1430 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1432 if (m
== VM_PAGE_NULL
) {
1433 vm_fault_cleanup(object
, first_m
);
1434 thread_interrupt_level(interruptible_state
);
1436 return VM_FAULT_MEMORY_SHORTAGE
;
1439 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1440 vm_page_insert_internal(m
, object
, offset
, VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1442 vm_page_insert(m
, object
, offset
);
1445 if (look_for_page
) {
1450 * If the memory manager is not ready, we
1451 * cannot make requests.
1453 if (!object
->pager_ready
) {
1455 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1457 if (m
!= VM_PAGE_NULL
) {
1462 * take an extra ref so object won't die
1464 vm_object_reference_locked(object
);
1465 vm_fault_cleanup(object
, first_m
);
1466 counter(c_vm_fault_page_block_backoff_kernel
++);
1468 vm_object_lock(object
);
1469 assert(object
->ref_count
> 0);
1471 if (!object
->pager_ready
) {
1472 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1474 vm_object_unlock(object
);
1475 if (wait_result
== THREAD_WAITING
) {
1476 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1478 vm_object_deallocate(object
);
1482 vm_object_unlock(object
);
1483 vm_object_deallocate(object
);
1484 thread_interrupt_level(interruptible_state
);
1486 return VM_FAULT_RETRY
;
1489 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1491 * If there are too many outstanding page
1492 * requests pending on this external object, we
1493 * wait for them to be resolved now.
1496 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1498 if (m
!= VM_PAGE_NULL
) {
1502 * take an extra ref so object won't die
1504 vm_object_reference_locked(object
);
1506 vm_fault_cleanup(object
, first_m
);
1508 counter(c_vm_fault_page_block_backoff_kernel
++);
1510 vm_object_lock(object
);
1511 assert(object
->ref_count
> 0);
1513 if (object
->paging_in_progress
>= vm_object_pagein_throttle
) {
1514 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS
, interruptible
);
1516 vm_object_unlock(object
);
1517 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1518 vm_object_deallocate(object
);
1522 vm_object_unlock(object
);
1523 vm_object_deallocate(object
);
1524 thread_interrupt_level(interruptible_state
);
1526 return VM_FAULT_RETRY
;
1529 if (object
->internal
) {
1530 int compressed_count_delta
;
1532 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
1534 if (m
== VM_PAGE_NULL
) {
1536 * Allocate a new page for this object/offset pair as a placeholder
1538 m
= vm_page_grab_options(grab_options
);
1540 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1542 if (m
== VM_PAGE_NULL
) {
1543 vm_fault_cleanup(object
, first_m
);
1544 thread_interrupt_level(interruptible_state
);
1546 return VM_FAULT_MEMORY_SHORTAGE
;
1549 m
->vmp_absent
= TRUE
;
1550 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1551 vm_page_insert_internal(m
, object
, offset
, VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1553 vm_page_insert(m
, object
, offset
);
1556 assert(m
->vmp_busy
);
1558 m
->vmp_absent
= TRUE
;
1559 pager
= object
->pager
;
1561 assert(object
->paging_in_progress
> 0);
1562 vm_object_unlock(object
);
1564 rc
= vm_compressor_pager_get(
1566 offset
+ object
->paging_offset
,
1567 VM_PAGE_GET_PHYS_PAGE(m
),
1570 &compressed_count_delta
);
1572 if (type_of_fault
== NULL
) {
1576 * we weren't called from vm_fault, so we
1577 * need to apply page creation throttling
1578 * do it before we re-acquire any locks
1580 if (my_fault_type
== DBG_COMPRESSOR_FAULT
) {
1581 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
1582 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 1, 0);
1583 delay(throttle_delay
);
1587 vm_object_lock(object
);
1588 assert(object
->paging_in_progress
> 0);
1590 vm_compressor_pager_count(
1592 compressed_count_delta
,
1593 FALSE
, /* shared_lock */
1598 m
->vmp_absent
= FALSE
;
1599 m
->vmp_dirty
= TRUE
;
1600 if ((object
->wimg_bits
&
1602 VM_WIMG_USE_DEFAULT
) {
1604 * If the page is not cacheable,
1605 * we can't let its contents
1606 * linger in the data cache
1607 * after the decompression.
1609 pmap_sync_page_attributes_phys(
1610 VM_PAGE_GET_PHYS_PAGE(m
));
1612 m
->vmp_written_by_kernel
= TRUE
;
1616 * If the object is purgeable, its
1617 * owner's purgeable ledgers have been
1618 * updated in vm_page_insert() but the
1619 * page was also accounted for in a
1620 * "compressed purgeable" ledger, so
1623 if (((object
->purgable
!=
1624 VM_PURGABLE_DENY
) ||
1625 object
->vo_ledger_tag
) &&
1626 (object
->vo_owner
!=
1629 * One less compressed
1630 * purgeable/tagged page.
1632 vm_object_owner_compressed_update(
1638 case KERN_MEMORY_FAILURE
:
1639 m
->vmp_unusual
= TRUE
;
1640 m
->vmp_error
= TRUE
;
1641 m
->vmp_absent
= FALSE
;
1643 case KERN_MEMORY_ERROR
:
1644 assert(m
->vmp_absent
);
1647 panic("vm_fault_page(): unexpected "
1649 "vm_compressor_pager_get()\n",
1652 PAGE_WAKEUP_DONE(m
);
1655 goto data_requested
;
1657 my_fault_type
= DBG_PAGEIN_FAULT
;
1659 if (m
!= VM_PAGE_NULL
) {
1665 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1669 * It's possible someone called vm_object_destroy while we weren't
1670 * holding the object lock. If that has happened, then bail out
1674 pager
= object
->pager
;
1676 if (pager
== MEMORY_OBJECT_NULL
) {
1677 vm_fault_cleanup(object
, first_m
);
1678 thread_interrupt_level(interruptible_state
);
1679 return VM_FAULT_MEMORY_ERROR
;
1683 * We have an absent page in place for the faulting offset,
1684 * so we can release the object lock.
1687 if (object
->object_is_shared_cache
) {
1688 set_thread_rwlock_boost();
1691 vm_object_unlock(object
);
1694 * If this object uses a copy_call strategy,
1695 * and we are interested in a copy of this object
1696 * (having gotten here only by following a
1697 * shadow chain), then tell the memory manager
1698 * via a flag added to the desired_access
1699 * parameter, so that it can detect a race
1700 * between our walking down the shadow chain
1701 * and its pushing pages up into a copy of
1702 * the object that it manages.
1704 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
) {
1705 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1707 wants_copy_flag
= VM_PROT_NONE
;
1710 if (object
->copy
== first_object
) {
1712 * if we issue the memory_object_data_request in
1713 * this state, we are subject to a deadlock with
1714 * the underlying filesystem if it is trying to
1715 * shrink the file resulting in a push of pages
1716 * into the copy object... that push will stall
1717 * on the placeholder page, and if the pushing thread
1718 * is holding a lock that is required on the pagein
1719 * path (such as a truncate lock), we'll deadlock...
1720 * to avoid this potential deadlock, we throw away
1721 * our placeholder page before calling memory_object_data_request
1722 * and force this thread to retry the vm_fault_page after
1723 * we have issued the I/O. the second time through this path
1724 * we will find the page already in the cache (presumably still
1725 * busy waiting for the I/O to complete) and then complete
1726 * the fault w/o having to go through memory_object_data_request again
1728 assert(first_m
!= VM_PAGE_NULL
);
1729 assert(VM_PAGE_OBJECT(first_m
) == first_object
);
1731 vm_object_lock(first_object
);
1732 VM_PAGE_FREE(first_m
);
1733 vm_object_paging_end(first_object
);
1734 vm_object_unlock(first_object
);
1736 first_m
= VM_PAGE_NULL
;
1737 force_fault_retry
= TRUE
;
1739 vm_fault_page_forced_retry
++;
1742 if (data_already_requested
== TRUE
) {
1743 orig_behavior
= fault_info
->behavior
;
1744 orig_cluster_size
= fault_info
->cluster_size
;
1746 fault_info
->behavior
= VM_BEHAVIOR_RANDOM
;
1747 fault_info
->cluster_size
= PAGE_SIZE
;
1750 * Call the memory manager to retrieve the data.
1752 rc
= memory_object_data_request(
1754 offset
+ object
->paging_offset
,
1756 access_required
| wants_copy_flag
,
1757 (memory_object_fault_info_t
)fault_info
);
1759 if (data_already_requested
== TRUE
) {
1760 fault_info
->behavior
= orig_behavior
;
1761 fault_info
->cluster_size
= orig_cluster_size
;
1763 data_already_requested
= TRUE
;
1766 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1768 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1770 vm_object_lock(object
);
1772 if (object
->object_is_shared_cache
) {
1773 clear_thread_rwlock_boost();
1777 if (rc
!= KERN_SUCCESS
) {
1778 vm_fault_cleanup(object
, first_m
);
1779 thread_interrupt_level(interruptible_state
);
1781 return (rc
== MACH_SEND_INTERRUPTED
) ?
1782 VM_FAULT_INTERRUPTED
:
1783 VM_FAULT_MEMORY_ERROR
;
1786 clock_usec_t tv_usec
;
1788 if (my_fault_type
== DBG_PAGEIN_FAULT
) {
1789 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1790 current_thread()->t_page_creation_time
= tv_sec
;
1791 current_thread()->t_page_creation_count
= 0;
1794 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_flags
& TH_SFLAG_ABORT
)) {
1795 vm_fault_cleanup(object
, first_m
);
1796 thread_interrupt_level(interruptible_state
);
1798 return VM_FAULT_INTERRUPTED
;
1800 if (force_fault_retry
== TRUE
) {
1801 vm_fault_cleanup(object
, first_m
);
1802 thread_interrupt_level(interruptible_state
);
1804 return VM_FAULT_RETRY
;
1806 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1808 * No page here means that the object we
1809 * initially looked up was "physically
1810 * contiguous" (i.e. device memory). However,
1811 * with Virtual VRAM, the object might not
1812 * be backed by that device memory anymore,
1813 * so we're done here only if the object is
1814 * still "phys_contiguous".
1815 * Otherwise, if the object is no longer
1816 * "phys_contiguous", we need to retry the
1817 * page fault against the object's new backing
1818 * store (different memory object).
1824 * potentially a pagein fault
1825 * if we make it through the state checks
1826 * above, than we'll count it as such
1828 my_fault
= my_fault_type
;
1831 * Retry with same object/offset, since new data may
1832 * be in a different page (i.e., m is meaningless at
1839 * We get here if the object has no pager, or an existence map
1840 * exists and indicates the page isn't present on the pager
1841 * or we're unwiring a page. If a pager exists, but there
1842 * is no existence map, then the m->vmp_absent case above handles
1843 * the ZF case when the pager can't provide the page
1846 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1848 if (object
== first_object
) {
1851 assert(m
== VM_PAGE_NULL
);
1854 next_object
= object
->shadow
;
1856 if (next_object
== VM_OBJECT_NULL
) {
1858 * we've hit the bottom of the shadown chain,
1859 * fill the page in the top object with zeros.
1861 assert(!must_be_resident
);
1863 if (object
!= first_object
) {
1864 vm_object_paging_end(object
);
1865 vm_object_unlock(object
);
1867 object
= first_object
;
1868 offset
= first_offset
;
1869 vm_object_lock(object
);
1872 assert(VM_PAGE_OBJECT(m
) == object
);
1873 first_m
= VM_PAGE_NULL
;
1876 * check for any conditions that prevent
1877 * us from creating a new zero-fill page
1878 * vm_fault_check will do all of the
1879 * fault cleanup in the case of an error condition
1880 * including resetting the thread_interrupt_level
1882 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1884 if (error
!= VM_FAULT_SUCCESS
) {
1888 if (m
== VM_PAGE_NULL
) {
1889 m
= vm_page_grab_options(grab_options
);
1891 if (m
== VM_PAGE_NULL
) {
1892 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1893 thread_interrupt_level(interruptible_state
);
1895 return VM_FAULT_MEMORY_SHORTAGE
;
1897 vm_page_insert(m
, object
, offset
);
1899 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
) {
1900 m
->vmp_absent
= TRUE
;
1903 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1908 * Move on to the next object. Lock the next
1909 * object before unlocking the current one.
1911 if ((object
!= first_object
) || must_be_resident
) {
1912 vm_object_paging_end(object
);
1915 offset
+= object
->vo_shadow_offset
;
1916 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1917 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1918 access_required
= VM_PROT_READ
;
1920 vm_object_lock(next_object
);
1921 vm_object_unlock(object
);
1923 object
= next_object
;
1924 vm_object_paging_begin(object
);
1929 * PAGE HAS BEEN FOUND.
1932 * busy, so that we can play with it;
1933 * not absent, so that nobody else will fill it;
1934 * possibly eligible for pageout;
1936 * The top-level page (first_m) is:
1937 * VM_PAGE_NULL if the page was found in the
1939 * busy, not absent, and ineligible for pageout.
1941 * The current object (object) is locked. A paging
1942 * reference is held for the current and top-level
1947 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1949 #if EXTRA_ASSERTIONS
1950 assert(m
->vmp_busy
&& !m
->vmp_absent
);
1951 assert((first_m
== VM_PAGE_NULL
) ||
1952 (first_m
->vmp_busy
&& !first_m
->vmp_absent
&&
1953 !first_m
->vmp_active
&& !first_m
->vmp_inactive
&& !first_m
->vmp_secluded
));
1954 #endif /* EXTRA_ASSERTIONS */
1957 * If the page is being written, but isn't
1958 * already owned by the top-level object,
1959 * we have to copy it into a new page owned
1960 * by the top-level object.
1962 if (object
!= first_object
) {
1964 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1966 if (fault_type
& VM_PROT_WRITE
) {
1970 * We only really need to copy if we
1973 assert(!must_be_resident
);
1976 * If we try to collapse first_object at this
1977 * point, we may deadlock when we try to get
1978 * the lock on an intermediate object (since we
1979 * have the bottom object locked). We can't
1980 * unlock the bottom object, because the page
1981 * we found may move (by collapse) if we do.
1983 * Instead, we first copy the page. Then, when
1984 * we have no more use for the bottom object,
1985 * we unlock it and try to collapse.
1987 * Note that we copy the page even if we didn't
1988 * need to... that's the breaks.
1992 * Allocate a page for the copy
1994 copy_m
= vm_page_grab_options(grab_options
);
1996 if (copy_m
== VM_PAGE_NULL
) {
1999 vm_fault_cleanup(object
, first_m
);
2000 thread_interrupt_level(interruptible_state
);
2002 return VM_FAULT_MEMORY_SHORTAGE
;
2005 vm_page_copy(m
, copy_m
);
2008 * If another map is truly sharing this
2009 * page with us, we have to flush all
2010 * uses of the original page, since we
2011 * can't distinguish those which want the
2012 * original from those which need the
2015 * XXXO If we know that only one map has
2016 * access to this page, then we could
2017 * avoid the pmap_disconnect() call.
2019 if (m
->vmp_pmapped
) {
2020 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2023 if (m
->vmp_clustered
) {
2024 VM_PAGE_COUNT_AS_PAGEIN(m
);
2025 VM_PAGE_CONSUME_CLUSTERED(m
);
2027 assert(!m
->vmp_cleaning
);
2030 * We no longer need the old page or object.
2035 * This check helps with marking the object as having a sequential pattern
2036 * Normally we'll miss doing this below because this fault is about COW to
2037 * the first_object i.e. bring page in from disk, push to object above but
2038 * don't update the file object's sequential pattern.
2040 if (object
->internal
== FALSE
) {
2041 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2044 vm_object_paging_end(object
);
2045 vm_object_unlock(object
);
2047 my_fault
= DBG_COW_FAULT
;
2048 VM_STAT_INCR(cow_faults
);
2049 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2050 current_task()->cow_faults
++;
2052 object
= first_object
;
2053 offset
= first_offset
;
2055 vm_object_lock(object
);
2057 * get rid of the place holder
2058 * page that we soldered in earlier
2060 VM_PAGE_FREE(first_m
);
2061 first_m
= VM_PAGE_NULL
;
2064 * and replace it with the
2065 * page we just copied into
2067 assert(copy_m
->vmp_busy
);
2068 vm_page_insert(copy_m
, object
, offset
);
2069 SET_PAGE_DIRTY(copy_m
, TRUE
);
2073 * Now that we've gotten the copy out of the
2074 * way, let's try to collapse the top object.
2075 * But we have to play ugly games with
2076 * paging_in_progress to do that...
2078 vm_object_paging_end(object
);
2079 vm_object_collapse(object
, offset
, TRUE
);
2080 vm_object_paging_begin(object
);
2082 *protection
&= (~VM_PROT_WRITE
);
2086 * Now check whether the page needs to be pushed into the
2087 * copy object. The use of asymmetric copy on write for
2088 * shared temporary objects means that we may do two copies to
2089 * satisfy the fault; one above to get the page from a
2090 * shadowed object, and one here to push it into the copy.
2092 try_failed_count
= 0;
2094 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
2095 vm_object_offset_t copy_offset
;
2099 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2102 * If the page is being written, but hasn't been
2103 * copied to the copy-object, we have to copy it there.
2105 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2106 *protection
&= ~VM_PROT_WRITE
;
2111 * If the page was guaranteed to be resident,
2112 * we must have already performed the copy.
2114 if (must_be_resident
) {
2119 * Try to get the lock on the copy_object.
2121 if (!vm_object_lock_try(copy_object
)) {
2122 vm_object_unlock(object
);
2125 mutex_pause(try_failed_count
); /* wait a bit */
2126 vm_object_lock(object
);
2130 try_failed_count
= 0;
2133 * Make another reference to the copy-object,
2134 * to keep it from disappearing during the
2137 vm_object_reference_locked(copy_object
);
2140 * Does the page exist in the copy?
2142 copy_offset
= first_offset
- copy_object
->vo_shadow_offset
;
2144 if (copy_object
->vo_size
<= copy_offset
) {
2146 * Copy object doesn't cover this page -- do nothing.
2149 } else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
2151 * Page currently exists in the copy object
2153 if (copy_m
->vmp_busy
) {
2155 * If the page is being brought
2156 * in, wait for it and then retry.
2161 * take an extra ref so object won't die
2163 vm_object_reference_locked(copy_object
);
2164 vm_object_unlock(copy_object
);
2165 vm_fault_cleanup(object
, first_m
);
2166 counter(c_vm_fault_page_block_backoff_kernel
++);
2168 vm_object_lock(copy_object
);
2169 assert(copy_object
->ref_count
> 0);
2170 VM_OBJ_RES_DECR(copy_object
);
2171 vm_object_lock_assert_exclusive(copy_object
);
2172 copy_object
->ref_count
--;
2173 assert(copy_object
->ref_count
> 0);
2174 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
2176 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->vmp_busy
) {
2177 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
2179 vm_object_unlock(copy_object
);
2180 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2181 vm_object_deallocate(copy_object
);
2185 vm_object_unlock(copy_object
);
2186 vm_object_deallocate(copy_object
);
2187 thread_interrupt_level(interruptible_state
);
2189 return VM_FAULT_RETRY
;
2192 } else if (!PAGED_OUT(copy_object
, copy_offset
)) {
2194 * If PAGED_OUT is TRUE, then the page used to exist
2195 * in the copy-object, and has already been paged out.
2196 * We don't need to repeat this. If PAGED_OUT is
2197 * FALSE, then either we don't know (!pager_created,
2198 * for example) or it hasn't been paged out.
2199 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2200 * We must copy the page to the copy object.
2202 * Allocate a page for the copy
2204 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
2206 if (copy_m
== VM_PAGE_NULL
) {
2209 VM_OBJ_RES_DECR(copy_object
);
2210 vm_object_lock_assert_exclusive(copy_object
);
2211 copy_object
->ref_count
--;
2212 assert(copy_object
->ref_count
> 0);
2214 vm_object_unlock(copy_object
);
2215 vm_fault_cleanup(object
, first_m
);
2216 thread_interrupt_level(interruptible_state
);
2218 return VM_FAULT_MEMORY_SHORTAGE
;
2221 * Must copy page into copy-object.
2223 vm_page_copy(m
, copy_m
);
2226 * If the old page was in use by any users
2227 * of the copy-object, it must be removed
2228 * from all pmaps. (We can't know which
2231 if (m
->vmp_pmapped
) {
2232 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2235 if (m
->vmp_clustered
) {
2236 VM_PAGE_COUNT_AS_PAGEIN(m
);
2237 VM_PAGE_CONSUME_CLUSTERED(m
);
2240 * If there's a pager, then immediately
2241 * page out this page, using the "initialize"
2242 * option. Else, we use the copy.
2244 if ((!copy_object
->pager_ready
)
2245 || VM_COMPRESSOR_PAGER_STATE_GET(copy_object
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2247 vm_page_lockspin_queues();
2248 assert(!m
->vmp_cleaning
);
2249 vm_page_activate(copy_m
);
2250 vm_page_unlock_queues();
2252 SET_PAGE_DIRTY(copy_m
, TRUE
);
2253 PAGE_WAKEUP_DONE(copy_m
);
2255 assert(copy_m
->vmp_busy
== TRUE
);
2256 assert(!m
->vmp_cleaning
);
2259 * dirty is protected by the object lock
2261 SET_PAGE_DIRTY(copy_m
, TRUE
);
2264 * The page is already ready for pageout:
2265 * not on pageout queues and busy.
2266 * Unlock everything except the
2267 * copy_object itself.
2269 vm_object_unlock(object
);
2272 * Write the page to the copy-object,
2273 * flushing it from the kernel.
2275 vm_pageout_initialize_page(copy_m
);
2278 * Since the pageout may have
2279 * temporarily dropped the
2280 * copy_object's lock, we
2281 * check whether we'll have
2282 * to deallocate the hard way.
2284 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2285 vm_object_unlock(copy_object
);
2286 vm_object_deallocate(copy_object
);
2287 vm_object_lock(object
);
2292 * Pick back up the old object's
2293 * lock. [It is safe to do so,
2294 * since it must be deeper in the
2297 vm_object_lock(object
);
2301 * Because we're pushing a page upward
2302 * in the object tree, we must restart
2303 * any faults that are waiting here.
2304 * [Note that this is an expansion of
2305 * PAGE_WAKEUP that uses the THREAD_RESTART
2306 * wait result]. Can't turn off the page's
2307 * busy bit because we're not done with it.
2309 if (m
->vmp_wanted
) {
2310 m
->vmp_wanted
= FALSE
;
2311 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2315 * The reference count on copy_object must be
2316 * at least 2: one for our extra reference,
2317 * and at least one from the outside world
2318 * (we checked that when we last locked
2321 vm_object_lock_assert_exclusive(copy_object
);
2322 copy_object
->ref_count
--;
2323 assert(copy_object
->ref_count
> 0);
2325 VM_OBJ_RES_DECR(copy_object
);
2326 vm_object_unlock(copy_object
);
2333 *top_page
= first_m
;
2335 if (m
!= VM_PAGE_NULL
) {
2336 assert(VM_PAGE_OBJECT(m
) == object
);
2338 retval
= VM_FAULT_SUCCESS
;
2340 if (my_fault
== DBG_PAGEIN_FAULT
) {
2341 VM_PAGE_COUNT_AS_PAGEIN(m
);
2343 if (object
->internal
) {
2344 my_fault
= DBG_PAGEIND_FAULT
;
2346 my_fault
= DBG_PAGEINV_FAULT
;
2350 * evaluate access pattern and update state
2351 * vm_fault_deactivate_behind depends on the
2352 * state being up to date
2354 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2355 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2356 } else if (type_of_fault
== NULL
&& my_fault
== DBG_CACHE_HIT_FAULT
) {
2358 * we weren't called from vm_fault, so handle the
2359 * accounting here for hits in the cache
2361 if (m
->vmp_clustered
) {
2362 VM_PAGE_COUNT_AS_PAGEIN(m
);
2363 VM_PAGE_CONSUME_CLUSTERED(m
);
2365 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2366 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2367 } else if (my_fault
== DBG_COMPRESSOR_FAULT
|| my_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
) {
2368 VM_STAT_DECOMPRESSIONS();
2370 if (type_of_fault
) {
2371 *type_of_fault
= my_fault
;
2374 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2375 assert(first_m
== VM_PAGE_NULL
);
2376 assert(object
== first_object
);
2379 thread_interrupt_level(interruptible_state
);
2382 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2387 thread_interrupt_level(interruptible_state
);
2389 if (wait_result
== THREAD_INTERRUPTED
) {
2390 return VM_FAULT_INTERRUPTED
;
2392 return VM_FAULT_RETRY
;
2401 * When soft faulting a page, we have to validate the page if:
2402 * 1. the page is being mapped in user space
2403 * 2. the page hasn't already been found to be "tainted"
2404 * 3. the page belongs to a code-signed object
2405 * 4. the page has not been validated yet or has been mapped for write.
2407 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page, page_obj) \
2408 ((pmap) != kernel_pmap /*1*/ && \
2409 !(page)->vmp_cs_tainted /*2*/ && \
2410 (page_obj)->code_signed /*3*/ && \
2411 (!(page)->vmp_cs_validated || (page)->vmp_wpmapped /*4*/ ))
2415 * page queue lock must NOT be held
2416 * m->vmp_object must be locked
2418 * NOTE: m->vmp_object could be locked "shared" only if we are called
2419 * from vm_fault() as part of a soft fault. If so, we must be
2420 * careful not to modify the VM object in any way that is not
2421 * legal under a shared lock...
2423 extern int panic_on_cs_killed
;
2424 extern int proc_selfpid(void);
2425 extern char *proc_name_address(void *p
);
2426 unsigned long cs_enter_tainted_rejected
= 0;
2427 unsigned long cs_enter_tainted_accepted
= 0;
2429 vm_fault_enter(vm_page_t m
,
2431 vm_map_offset_t vaddr
,
2433 vm_prot_t caller_prot
,
2435 boolean_t change_wiring
,
2437 vm_object_fault_info_t fault_info
,
2438 boolean_t
*need_retry
,
2441 kern_return_t kr
, pe_result
;
2442 boolean_t previously_pmapped
= m
->vmp_pmapped
;
2443 boolean_t must_disconnect
= 0;
2444 boolean_t map_is_switched
, map_is_switch_protected
;
2445 boolean_t cs_violation
;
2446 int cs_enforcement_enabled
;
2447 vm_prot_t fault_type
;
2449 boolean_t no_cache
= fault_info
->no_cache
;
2450 boolean_t cs_bypass
= fault_info
->cs_bypass
;
2451 int pmap_options
= fault_info
->pmap_options
;
2453 fault_type
= change_wiring
? VM_PROT_NONE
: caller_prot
;
2454 object
= VM_PAGE_OBJECT(m
);
2456 vm_object_lock_assert_held(object
);
2459 if (pmap
== kernel_pmap
) {
2460 kasan_notify_address(vaddr
, PAGE_SIZE
);
2464 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2466 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
2467 assert(m
->vmp_fictitious
);
2468 return KERN_SUCCESS
;
2471 if (*type_of_fault
== DBG_ZERO_FILL_FAULT
) {
2472 vm_object_lock_assert_exclusive(object
);
2473 } else if ((fault_type
& VM_PROT_WRITE
) == 0 &&
2475 #if VM_OBJECT_ACCESS_TRACKING
2476 || object
->access_tracking
2477 #endif /* VM_OBJECT_ACCESS_TRACKING */
2480 * This is not a "write" fault, so we
2481 * might not have taken the object lock
2482 * exclusively and we might not be able
2483 * to update the "wpmapped" bit in
2485 * Let's just grant read access to
2486 * the page for now and we'll
2487 * soft-fault again if we need write
2491 /* This had better not be a JIT page. */
2492 if (!pmap_has_prot_policy(prot
)) {
2493 prot
&= ~VM_PROT_WRITE
;
2498 if (m
->vmp_pmapped
== FALSE
) {
2499 if (m
->vmp_clustered
) {
2500 if (*type_of_fault
== DBG_CACHE_HIT_FAULT
) {
2502 * found it in the cache, but this
2503 * is the first fault-in of the page (m->vmp_pmapped == FALSE)
2504 * so it must have come in as part of
2505 * a cluster... account 1 pagein against it
2507 if (object
->internal
) {
2508 *type_of_fault
= DBG_PAGEIND_FAULT
;
2510 *type_of_fault
= DBG_PAGEINV_FAULT
;
2513 VM_PAGE_COUNT_AS_PAGEIN(m
);
2515 VM_PAGE_CONSUME_CLUSTERED(m
);
2519 if (*type_of_fault
!= DBG_COW_FAULT
) {
2520 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2522 if (pmap
== kernel_pmap
) {
2523 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2527 /* Validate code signature if necessary. */
2529 VM_FAULT_NEED_CS_VALIDATION(pmap
, m
, object
)) {
2530 vm_object_lock_assert_exclusive(object
);
2532 if (m
->vmp_cs_validated
) {
2533 vm_cs_revalidates
++;
2536 /* VM map is locked, so 1 ref will remain on VM object -
2537 * so no harm if vm_page_validate_cs drops the object lock */
2540 if (fault_info
->pmap_cs_associated
&&
2541 pmap_cs_enforced(pmap
) &&
2542 !m
->vmp_cs_validated
&&
2543 !m
->vmp_cs_tainted
&&
2545 (prot
& VM_PROT_EXECUTE
) &&
2546 (caller_prot
& VM_PROT_EXECUTE
)) {
2548 * With pmap_cs, the pmap layer will validate the
2549 * code signature for any executable pmap mapping.
2550 * No need for us to validate this page too:
2551 * in pmap_cs we trust...
2553 vm_cs_defer_to_pmap_cs
++;
2555 vm_cs_defer_to_pmap_cs_not
++;
2556 vm_page_validate_cs(m
);
2559 vm_page_validate_cs(m
);
2560 #endif /* PMAP_CS */
2563 #define page_immutable(m, prot) ((m)->vmp_cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/ )
2564 #define page_nx(m) ((m)->vmp_cs_nx)
2566 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2567 (pmap
== vm_map_pmap(current_thread()->map
)));
2568 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2570 /* If the map is switched, and is switch-protected, we must protect
2571 * some pages from being write-faulted: immutable pages because by
2572 * definition they may not be written, and executable pages because that
2573 * would provide a way to inject unsigned code.
2574 * If the page is immutable, we can simply return. However, we can't
2575 * immediately determine whether a page is executable anywhere. But,
2576 * we can disconnect it everywhere and remove the executable protection
2577 * from the current map. We do that below right before we do the
2580 cs_enforcement_enabled
= cs_process_enforcement(NULL
);
2582 if (cs_enforcement_enabled
&& map_is_switched
&&
2583 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2584 (prot
& VM_PROT_WRITE
)) {
2585 return KERN_CODESIGN_ERROR
;
2588 if (cs_enforcement_enabled
&& page_nx(m
) && (prot
& VM_PROT_EXECUTE
)) {
2590 printf("page marked to be NX, not letting it be mapped EXEC\n");
2592 return KERN_CODESIGN_ERROR
;
2595 /* A page could be tainted, or pose a risk of being tainted later.
2596 * Check whether the receiving process wants it, and make it feel
2597 * the consequences (that hapens in cs_invalid_page()).
2598 * For CS Enforcement, two other conditions will
2599 * cause that page to be tainted as well:
2600 * - pmapping an unsigned page executable - this means unsigned code;
2601 * - writeable mapping of a validated page - the content of that page
2602 * can be changed without the kernel noticing, therefore unsigned
2603 * code can be created
2606 /* code-signing is bypassed */
2607 cs_violation
= FALSE
;
2608 } else if (m
->vmp_cs_tainted
) {
2610 cs_violation
= TRUE
;
2611 } else if (!cs_enforcement_enabled
) {
2612 /* no further code-signing enforcement */
2613 cs_violation
= FALSE
;
2614 } else if (page_immutable(m
, prot
) &&
2615 ((prot
& VM_PROT_WRITE
) ||
2618 * The page should be immutable, but is in danger of being
2620 * This is the case where we want policy from the code
2621 * directory - is the page immutable or not? For now we have
2622 * to assume that code pages will be immutable, data pages not.
2623 * We'll assume a page is a code page if it has a code directory
2624 * and we fault for execution.
2625 * That is good enough since if we faulted the code page for
2626 * writing in another map before, it is wpmapped; if we fault
2627 * it for writing in this map later it will also be faulted for
2628 * executing at the same time; and if we fault for writing in
2629 * another map later, we will disconnect it from this pmap so
2630 * we'll notice the change.
2632 cs_violation
= TRUE
;
2633 } else if (!m
->vmp_cs_validated
&&
2634 (prot
& VM_PROT_EXECUTE
)
2637 * Executable pages will be validated by pmap_cs;
2638 * in pmap_cs we trust...
2639 * If pmap_cs is turned off, this is a code-signing
2642 && !(pmap_cs_enforced(pmap
))
2643 #endif /* PMAP_CS */
2645 cs_violation
= TRUE
;
2647 cs_violation
= FALSE
;
2651 /* We will have a tainted page. Have to handle the special case
2652 * of a switched map now. If the map is not switched, standard
2653 * procedure applies - call cs_invalid_page().
2654 * If the map is switched, the real owner is invalid already.
2655 * There is no point in invalidating the switching process since
2656 * it will not be executing from the map. So we don't call
2657 * cs_invalid_page() in that case. */
2658 boolean_t reject_page
, cs_killed
;
2659 if (map_is_switched
) {
2660 assert(pmap
== vm_map_pmap(current_thread()->map
));
2661 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2662 reject_page
= FALSE
;
2665 printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s prot: 0x%x\n",
2666 object
->code_signed
? "yes" : "no",
2667 m
->vmp_cs_validated
? "yes" : "no",
2668 m
->vmp_cs_tainted
? "yes" : "no",
2669 m
->vmp_wpmapped
? "yes" : "no",
2672 reject_page
= cs_invalid_page((addr64_t
) vaddr
, &cs_killed
);
2676 /* reject the invalid page: abort the page fault */
2678 const char *procname
;
2680 vm_object_t file_object
, shadow
;
2681 vm_object_offset_t file_offset
;
2682 char *pathname
, *filename
;
2683 vm_size_t pathname_len
, filename_len
;
2684 boolean_t truncated_path
;
2685 #define __PATH_MAX 1024
2686 struct timespec mtime
, cs_mtime
;
2688 os_reason_t codesigning_exit_reason
= OS_REASON_NULL
;
2690 kr
= KERN_CODESIGN_ERROR
;
2691 cs_enter_tainted_rejected
++;
2693 /* get process name and pid */
2695 task
= current_task();
2696 pid
= proc_selfpid();
2697 if (task
->bsd_info
!= NULL
) {
2698 procname
= proc_name_address(task
->bsd_info
);
2701 /* get file's VM object */
2702 file_object
= object
;
2703 file_offset
= m
->vmp_offset
;
2704 for (shadow
= file_object
->shadow
,
2706 shadow
!= VM_OBJECT_NULL
;
2707 shadow
= file_object
->shadow
,
2709 vm_object_lock_shared(shadow
);
2710 if (file_object
!= object
) {
2711 vm_object_unlock(file_object
);
2713 file_offset
+= file_object
->vo_shadow_offset
;
2714 file_object
= shadow
;
2719 cs_mtime
.tv_sec
= 0;
2720 cs_mtime
.tv_nsec
= 0;
2722 /* get file's pathname and/or filename */
2727 truncated_path
= FALSE
;
2728 /* no pager -> no file -> no pathname, use "<nil>" in that case */
2729 if (file_object
->pager
!= NULL
) {
2730 pathname
= (char *)kalloc(__PATH_MAX
* 2);
2733 pathname_len
= __PATH_MAX
;
2734 filename
= pathname
+ pathname_len
;
2735 filename_len
= __PATH_MAX
;
2737 if (vnode_pager_get_object_name(file_object
->pager
,
2742 &truncated_path
) == KERN_SUCCESS
) {
2743 /* safety first... */
2744 pathname
[__PATH_MAX
- 1] = '\0';
2745 filename
[__PATH_MAX
- 1] = '\0';
2747 vnode_pager_get_object_mtime(file_object
->pager
,
2751 kfree(pathname
, __PATH_MAX
* 2);
2756 truncated_path
= FALSE
;
2760 printf("CODE SIGNING: process %d[%s]: "
2761 "rejecting invalid page at address 0x%llx "
2762 "from offset 0x%llx in file \"%s%s%s\" "
2763 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2764 "(signed:%d validated:%d tainted:%d nx:%d "
2765 "wpmapped:%d dirty:%d depth:%d)\n",
2766 pid
, procname
, (addr64_t
) vaddr
,
2768 (pathname
? pathname
: "<nil>"),
2769 (truncated_path
? "/.../" : ""),
2770 (truncated_path
? filename
: ""),
2771 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2772 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2773 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2776 mtime
.tv_sec
, mtime
.tv_nsec
,
2777 object
->code_signed
,
2778 m
->vmp_cs_validated
,
2786 * We currently only generate an exit reason if cs_invalid_page directly killed a process. If cs_invalid_page
2787 * did not kill the process (more the case on desktop), vm_fault_enter will not satisfy the fault and whether the
2788 * process dies is dependent on whether there is a signal handler registered for SIGSEGV and how that handler
2789 * will deal with the segmentation fault.
2792 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
2793 pid
, OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_INVALID_PAGE
, 0, 0);
2795 codesigning_exit_reason
= os_reason_create(OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_INVALID_PAGE
);
2796 if (codesigning_exit_reason
== NULL
) {
2797 printf("vm_fault_enter: failed to allocate codesigning exit reason\n");
2799 mach_vm_address_t data_addr
= 0;
2800 struct codesigning_exit_reason_info
*ceri
= NULL
;
2801 uint32_t reason_buffer_size_estimate
= kcdata_estimate_required_buffer_size(1, sizeof(*ceri
));
2803 if (os_reason_alloc_buffer_noblock(codesigning_exit_reason
, reason_buffer_size_estimate
)) {
2804 printf("vm_fault_enter: failed to allocate buffer for codesigning exit reason\n");
2806 if (KERN_SUCCESS
== kcdata_get_memory_addr(&codesigning_exit_reason
->osr_kcd_descriptor
,
2807 EXIT_REASON_CODESIGNING_INFO
, sizeof(*ceri
), &data_addr
)) {
2808 ceri
= (struct codesigning_exit_reason_info
*)data_addr
;
2809 static_assert(__PATH_MAX
== sizeof(ceri
->ceri_pathname
));
2811 ceri
->ceri_virt_addr
= vaddr
;
2812 ceri
->ceri_file_offset
= file_offset
;
2814 strncpy((char *)&ceri
->ceri_pathname
, pathname
, sizeof(ceri
->ceri_pathname
));
2816 ceri
->ceri_pathname
[0] = '\0';
2819 strncpy((char *)&ceri
->ceri_filename
, filename
, sizeof(ceri
->ceri_filename
));
2821 ceri
->ceri_filename
[0] = '\0';
2823 ceri
->ceri_path_truncated
= (truncated_path
);
2824 ceri
->ceri_codesig_modtime_secs
= cs_mtime
.tv_sec
;
2825 ceri
->ceri_codesig_modtime_nsecs
= cs_mtime
.tv_nsec
;
2826 ceri
->ceri_page_modtime_secs
= mtime
.tv_sec
;
2827 ceri
->ceri_page_modtime_nsecs
= mtime
.tv_nsec
;
2828 ceri
->ceri_object_codesigned
= (object
->code_signed
);
2829 ceri
->ceri_page_codesig_validated
= (m
->vmp_cs_validated
);
2830 ceri
->ceri_page_codesig_tainted
= (m
->vmp_cs_tainted
);
2831 ceri
->ceri_page_codesig_nx
= (m
->vmp_cs_nx
);
2832 ceri
->ceri_page_wpmapped
= (m
->vmp_wpmapped
);
2833 ceri
->ceri_page_slid
= 0;
2834 ceri
->ceri_page_dirty
= (m
->vmp_dirty
);
2835 ceri
->ceri_page_shadow_depth
= shadow_depth
;
2837 #if DEBUG || DEVELOPMENT
2838 panic("vm_fault_enter: failed to allocate kcdata for codesigning exit reason");
2840 printf("vm_fault_enter: failed to allocate kcdata for codesigning exit reason\n");
2841 #endif /* DEBUG || DEVELOPMENT */
2842 /* Free the buffer */
2843 os_reason_alloc_buffer_noblock(codesigning_exit_reason
, 0);
2848 set_thread_exit_reason(current_thread(), codesigning_exit_reason
, FALSE
);
2850 if (panic_on_cs_killed
&&
2851 object
->object_is_shared_cache
) {
2852 char *tainted_contents
;
2853 vm_map_offset_t src_vaddr
;
2854 src_vaddr
= (vm_map_offset_t
) phystokv((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(m
) << PAGE_SHIFT
);
2855 tainted_contents
= kalloc(PAGE_SIZE
);
2856 bcopy((const char *)src_vaddr
, tainted_contents
, PAGE_SIZE
);
2857 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
);
2858 panic("CODE SIGNING: process %d[%s]: "
2859 "rejecting invalid page (phys#0x%x) at address 0x%llx "
2860 "from offset 0x%llx in file \"%s%s%s\" "
2861 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2862 "(signed:%d validated:%d tainted:%d nx:%d"
2863 "wpmapped:%d dirty:%d depth:%d)\n",
2865 VM_PAGE_GET_PHYS_PAGE(m
),
2868 (pathname
? pathname
: "<nil>"),
2869 (truncated_path
? "/.../" : ""),
2870 (truncated_path
? filename
: ""),
2871 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2872 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2873 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2876 mtime
.tv_sec
, mtime
.tv_nsec
,
2877 object
->code_signed
,
2878 m
->vmp_cs_validated
,
2886 if (file_object
!= object
) {
2887 vm_object_unlock(file_object
);
2889 if (pathname_len
!= 0) {
2890 kfree(pathname
, __PATH_MAX
* 2);
2895 /* proceed with the invalid page */
2897 if (!m
->vmp_cs_validated
&&
2898 !object
->code_signed
) {
2900 * This page has not been (fully) validated but
2901 * does not belong to a code-signed object
2902 * so it should not be forcefully considered
2904 * We're just concerned about it here because
2905 * we've been asked to "execute" it but that
2906 * does not mean that it should cause other
2908 * This happens when a debugger sets a
2909 * breakpoint and we then execute code in
2910 * that page. Marking the page as "tainted"
2911 * would cause any inspection tool ("leaks",
2912 * "vmmap", "CrashReporter", ...) to get killed
2913 * due to code-signing violation on that page,
2914 * even though they're just reading it and not
2915 * executing from it.
2919 * Page might have been tainted before or not;
2920 * now it definitively is. If the page wasn't
2921 * tainted, we must disconnect it from all
2922 * pmaps later, to force existing mappings
2923 * through that code path for re-consideration
2924 * of the validity of that page.
2926 must_disconnect
= !m
->vmp_cs_tainted
;
2927 m
->vmp_cs_tainted
= TRUE
;
2929 cs_enter_tainted_accepted
++;
2931 if (kr
!= KERN_SUCCESS
) {
2933 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2934 "*** INVALID PAGE ***\n",
2938 if (cs_enforcement_panic
) {
2939 panic("CODESIGNING: panicking on invalid page\n");
2944 /* proceed with the valid page */
2948 boolean_t page_queues_locked
= FALSE
;
2949 #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \
2951 if (! page_queues_locked) { \
2952 page_queues_locked = TRUE; \
2953 vm_page_lockspin_queues(); \
2956 #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \
2958 if (page_queues_locked) { \
2959 page_queues_locked = FALSE; \
2960 vm_page_unlock_queues(); \
2965 * Hold queues lock to manipulate
2966 * the page queues. Change wiring
2969 assert((m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) || object
!= compressor_object
);
2971 #if CONFIG_BACKGROUND_QUEUE
2972 vm_page_update_background_state(m
);
2974 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
2976 * Compressor pages are neither wired
2977 * nor pageable and should never change.
2979 assert(object
== compressor_object
);
2980 } else if (change_wiring
) {
2981 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
2984 if (kr
== KERN_SUCCESS
) {
2985 vm_page_wire(m
, wire_tag
, TRUE
);
2988 vm_page_unwire(m
, TRUE
);
2990 /* we keep the page queues lock, if we need it later */
2992 if (object
->internal
== TRUE
) {
2994 * don't allow anonymous pages on
2995 * the speculative queues
2999 if (kr
!= KERN_SUCCESS
) {
3000 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3001 vm_page_deactivate(m
);
3002 /* we keep the page queues lock, if we need it later */
3003 } else if (((m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
) ||
3004 (m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) ||
3005 (m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) ||
3006 ((m
->vmp_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && no_cache
)) &&
3007 !VM_PAGE_WIRED(m
)) {
3008 if (vm_page_local_q
&&
3009 (*type_of_fault
== DBG_COW_FAULT
||
3010 *type_of_fault
== DBG_ZERO_FILL_FAULT
)) {
3014 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
3016 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3017 vm_object_lock_assert_exclusive(object
);
3020 * we got a local queue to stuff this
3022 * its safe to manipulate local and
3023 * local_id at this point since we're
3024 * behind an exclusive object lock and
3025 * the page is not on any global queue.
3027 * we'll use the current cpu number to
3028 * select the queue note that we don't
3029 * need to disable preemption... we're
3030 * going to be behind the local queue's
3031 * lock to do the real work
3035 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
3037 VPL_LOCK(&lq
->vpl_lock
);
3039 vm_page_check_pageable_safe(m
);
3040 vm_page_queue_enter(&lq
->vpl_queue
, m
, vmp_pageq
);
3041 m
->vmp_q_state
= VM_PAGE_ON_ACTIVE_LOCAL_Q
;
3042 m
->vmp_local_id
= lid
;
3045 if (object
->internal
) {
3046 lq
->vpl_internal_count
++;
3048 lq
->vpl_external_count
++;
3051 VPL_UNLOCK(&lq
->vpl_lock
);
3053 if (lq
->vpl_count
> vm_page_local_q_soft_limit
) {
3055 * we're beyond the soft limit
3056 * for the local queue
3057 * vm_page_reactivate_local will
3058 * 'try' to take the global page
3059 * queue lock... if it can't
3060 * that's ok... we'll let the
3061 * queue continue to grow up
3062 * to the hard limit... at that
3063 * point we'll wait for the
3064 * lock... once we've got the
3065 * lock, we'll transfer all of
3066 * the pages from the local
3067 * queue to the global active
3070 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
3073 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3076 * test again now that we hold the
3079 if (!VM_PAGE_WIRED(m
)) {
3080 if (m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) {
3081 vm_page_queues_remove(m
, FALSE
);
3083 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated
, 1);
3084 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_fault_reactivated
, 1);
3087 if (!VM_PAGE_ACTIVE_OR_INACTIVE(m
) ||
3090 * If this is a no_cache mapping
3091 * and the page has never been
3092 * mapped before or was
3093 * previously a no_cache page,
3094 * then we want to leave pages
3095 * in the speculative state so
3096 * that they can be readily
3097 * recycled if free memory runs
3098 * low. Otherwise the page is
3099 * activated as normal.
3103 (!previously_pmapped
||
3105 m
->vmp_no_cache
= TRUE
;
3107 if (m
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
3108 vm_page_speculate(m
, FALSE
);
3110 } else if (!VM_PAGE_ACTIVE_OR_INACTIVE(m
)) {
3111 vm_page_activate(m
);
3115 /* we keep the page queues lock, if we need it later */
3119 /* we're done with the page queues lock, if we ever took it */
3120 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3123 /* If we have a KERN_SUCCESS from the previous checks, we either have
3124 * a good page, or a tainted page that has been accepted by the process.
3125 * In both cases the page will be entered into the pmap.
3126 * If the page is writeable, we need to disconnect it from other pmaps
3127 * now so those processes can take note.
3129 if (kr
== KERN_SUCCESS
) {
3131 * NOTE: we may only hold the vm_object lock SHARED
3132 * at this point, so we need the phys_page lock to
3133 * properly serialize updating the pmapped and
3136 if ((prot
& VM_PROT_EXECUTE
) && !m
->vmp_xpmapped
) {
3137 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3139 pmap_lock_phys_page(phys_page
);
3141 * go ahead and take the opportunity
3142 * to set 'pmapped' here so that we don't
3143 * need to grab this lock a 2nd time
3146 m
->vmp_pmapped
= TRUE
;
3148 if (!m
->vmp_xpmapped
) {
3149 m
->vmp_xpmapped
= TRUE
;
3151 pmap_unlock_phys_page(phys_page
);
3153 if (!object
->internal
) {
3154 OSAddAtomic(1, &vm_page_xpmapped_external_count
);
3157 #if defined(__arm__) || defined(__arm64__)
3158 pmap_sync_page_data_phys(phys_page
);
3160 if (object
->internal
&&
3161 object
->pager
!= NULL
) {
3163 * This page could have been
3164 * uncompressed by the
3165 * compressor pager and its
3166 * contents might be only in
3168 * Since it's being mapped for
3169 * "execute" for the fist time,
3170 * make sure the icache is in
3173 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3174 pmap_sync_page_data_phys(phys_page
);
3178 pmap_unlock_phys_page(phys_page
);
3181 if (m
->vmp_pmapped
== FALSE
) {
3182 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3184 pmap_lock_phys_page(phys_page
);
3185 m
->vmp_pmapped
= TRUE
;
3186 pmap_unlock_phys_page(phys_page
);
3190 if (fault_type
& VM_PROT_WRITE
) {
3191 if (m
->vmp_wpmapped
== FALSE
) {
3192 vm_object_lock_assert_exclusive(object
);
3193 if (!object
->internal
&& object
->pager
) {
3194 task_update_logical_writes(current_task(), PAGE_SIZE
, TASK_WRITE_DEFERRED
, vnode_pager_lookup_vnode(object
->pager
));
3196 m
->vmp_wpmapped
= TRUE
;
3198 if (must_disconnect
) {
3200 * We can only get here
3201 * because of the CSE logic
3203 assert(cs_enforcement_enabled
);
3204 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
3206 * If we are faulting for a write, we can clear
3207 * the execute bit - that will ensure the page is
3208 * checked again before being executable, which
3209 * protects against a map switch.
3210 * This only happens the first time the page
3211 * gets tainted, so we won't get stuck here
3212 * to make an already writeable page executable.
3215 assert(!pmap_has_prot_policy(prot
));
3216 prot
&= ~VM_PROT_EXECUTE
;
3220 assert(VM_PAGE_OBJECT(m
) == object
);
3222 #if VM_OBJECT_ACCESS_TRACKING
3223 if (object
->access_tracking
) {
3224 DTRACE_VM2(access_tracking
, vm_map_offset_t
, vaddr
, int, fault_type
);
3225 if (fault_type
& VM_PROT_WRITE
) {
3226 object
->access_tracking_writes
++;
3227 vm_object_access_tracking_writes
++;
3229 object
->access_tracking_reads
++;
3230 vm_object_access_tracking_reads
++;
3233 #endif /* VM_OBJECT_ACCESS_TRACKING */
3239 /* Prevent a deadlock by not
3240 * holding the object lock if we need to wait for a page in
3241 * pmap_enter() - <rdar://problem/7138958> */
3242 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
, 0,
3244 pmap_options
| PMAP_OPTIONS_NOWAIT
,
3248 * Retry without execute permission if we encountered a codesigning
3249 * failure on a non-execute fault. This allows applications which
3250 * don't actually need to execute code to still map it for read access.
3252 if ((pe_result
== KERN_CODESIGN_ERROR
) && pmap_cs_enforced(pmap
) &&
3253 (prot
& VM_PROT_EXECUTE
) && !(caller_prot
& VM_PROT_EXECUTE
)) {
3254 prot
&= ~VM_PROT_EXECUTE
;
3255 goto pmap_enter_retry
;
3259 if (pe_result
== KERN_INVALID_ARGUMENT
&&
3260 pmap
== PMAP_NULL
&&
3263 * Wiring a page in a pmap-less VM map:
3264 * VMware's "vmmon" kernel extension does this
3266 * Let it proceed even though the PMAP_ENTER() failed.
3268 pe_result
= KERN_SUCCESS
;
3270 #endif /* __x86_64__ */
3272 if (pe_result
== KERN_RESOURCE_SHORTAGE
) {
3275 * this will be non-null in the case where we hold the lock
3276 * on the top-object in this chain... we can't just drop
3277 * the lock on the object we're inserting the page into
3278 * and recall the PMAP_ENTER since we can still cause
3279 * a deadlock if one of the critical paths tries to
3280 * acquire the lock on the top-object and we're blocked
3281 * in PMAP_ENTER waiting for memory... our only recourse
3282 * is to deal with it at a higher level where we can
3286 vm_pmap_enter_retried
++;
3287 goto after_the_pmap_enter
;
3289 /* The nonblocking version of pmap_enter did not succeed.
3290 * and we don't need to drop other locks and retry
3291 * at the level above us, so
3292 * use the blocking version instead. Requires marking
3293 * the page busy and unlocking the object */
3294 boolean_t was_busy
= m
->vmp_busy
;
3296 vm_object_lock_assert_exclusive(object
);
3299 vm_object_unlock(object
);
3301 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
,
3303 pmap_options
, pe_result
);
3305 assert(VM_PAGE_OBJECT(m
) == object
);
3307 /* Take the object lock again. */
3308 vm_object_lock(object
);
3310 /* If the page was busy, someone else will wake it up.
3311 * Otherwise, we have to do it now. */
3312 assert(m
->vmp_busy
);
3314 PAGE_WAKEUP_DONE(m
);
3316 vm_pmap_enter_blocked
++;
3322 after_the_pmap_enter
:
3327 vm_pre_fault(vm_map_offset_t vaddr
, vm_prot_t prot
)
3329 if (pmap_find_phys(current_map()->pmap
, vaddr
) == 0) {
3330 vm_fault(current_map(), /* map */
3332 prot
, /* fault_type */
3333 FALSE
, /* change_wiring */
3334 VM_KERN_MEMORY_NONE
, /* tag - not wiring */
3335 THREAD_UNINT
, /* interruptible */
3336 NULL
, /* caller_pmap */
3337 0 /* caller_pmap_addr */);
3345 * Handle page faults, including pseudo-faults
3346 * used to change the wiring status of pages.
3348 * Explicit continuations have been removed.
3350 * vm_fault and vm_fault_page save mucho state
3351 * in the moral equivalent of a closure. The state
3352 * structure is allocated when first entering vm_fault
3353 * and deallocated when leaving vm_fault.
3356 extern int _map_enter_debug
;
3357 extern uint64_t get_current_unique_pid(void);
3359 unsigned long vm_fault_collapse_total
= 0;
3360 unsigned long vm_fault_collapse_skipped
= 0;
3366 vm_map_offset_t vaddr
,
3367 vm_prot_t fault_type
,
3368 boolean_t change_wiring
,
3371 vm_map_offset_t caller_pmap_addr
)
3373 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
, vm_tag_bt(),
3374 interruptible
, caller_pmap
, caller_pmap_addr
,
3381 vm_map_offset_t vaddr
,
3382 vm_prot_t fault_type
,
3383 boolean_t change_wiring
,
3384 vm_tag_t wire_tag
, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3387 vm_map_offset_t caller_pmap_addr
)
3389 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
, wire_tag
,
3390 interruptible
, caller_pmap
, caller_pmap_addr
,
3395 current_proc_is_privileged(void)
3397 return csproc_get_platform_binary(current_proc());
3400 uint64_t vm_copied_on_read
= 0;
3405 vm_map_offset_t vaddr
,
3406 vm_prot_t caller_prot
,
3407 boolean_t change_wiring
,
3408 vm_tag_t wire_tag
, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3411 vm_map_offset_t caller_pmap_addr
,
3412 ppnum_t
*physpage_p
)
3414 vm_map_version_t version
; /* Map version for verificiation */
3415 boolean_t wired
; /* Should mapping be wired down? */
3416 vm_object_t object
; /* Top-level object */
3417 vm_object_offset_t offset
; /* Top-level offset */
3418 vm_prot_t prot
; /* Protection for mapping */
3419 vm_object_t old_copy_object
; /* Saved copy object */
3420 vm_page_t result_page
; /* Result of vm_fault_page */
3421 vm_page_t top_page
; /* Placeholder page */
3424 vm_page_t m
; /* Fast access to result_page */
3425 kern_return_t error_code
;
3426 vm_object_t cur_object
;
3427 vm_object_t m_object
= NULL
;
3428 vm_object_offset_t cur_offset
;
3430 vm_object_t new_object
;
3433 wait_interrupt_t interruptible_state
;
3434 vm_map_t real_map
= map
;
3435 vm_map_t original_map
= map
;
3436 boolean_t object_locks_dropped
= FALSE
;
3437 vm_prot_t fault_type
;
3438 vm_prot_t original_fault_type
;
3439 struct vm_object_fault_info fault_info
= {};
3440 boolean_t need_collapse
= FALSE
;
3441 boolean_t need_retry
= FALSE
;
3442 boolean_t
*need_retry_ptr
= NULL
;
3443 int object_lock_type
= 0;
3444 int cur_object_lock_type
;
3445 vm_object_t top_object
= VM_OBJECT_NULL
;
3446 vm_object_t written_on_object
= VM_OBJECT_NULL
;
3447 memory_object_t written_on_pager
= NULL
;
3448 vm_object_offset_t written_on_offset
= 0;
3450 int compressed_count_delta
;
3452 boolean_t need_copy
;
3453 boolean_t need_copy_on_read
;
3454 vm_map_offset_t trace_vaddr
;
3455 vm_map_offset_t trace_real_vaddr
;
3456 vm_map_offset_t real_vaddr
;
3457 boolean_t resilient_media_retry
= FALSE
;
3458 vm_object_t resilient_media_object
= VM_OBJECT_NULL
;
3459 vm_object_offset_t resilient_media_offset
= (vm_object_offset_t
)-1;
3462 trace_real_vaddr
= vaddr
;
3463 vaddr
= vm_map_trunc_page(vaddr
, PAGE_MASK
);
3465 if (map
== kernel_map
) {
3466 trace_vaddr
= VM_KERNEL_ADDRHIDE(vaddr
);
3467 trace_real_vaddr
= VM_KERNEL_ADDRHIDE(trace_real_vaddr
);
3469 trace_vaddr
= vaddr
;
3472 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3473 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
3474 ((uint64_t)trace_vaddr
>> 32),
3476 (map
== kernel_map
),
3480 if (get_preemption_level() != 0) {
3481 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3482 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3483 ((uint64_t)trace_vaddr
>> 32),
3489 return KERN_FAILURE
;
3492 thread_t cthread
= current_thread();
3493 boolean_t rtfault
= (cthread
->sched_mode
== TH_MODE_REALTIME
);
3494 uint64_t fstart
= 0;
3497 fstart
= mach_continuous_time();
3500 interruptible_state
= thread_interrupt_level(interruptible
);
3502 fault_type
= (change_wiring
? VM_PROT_NONE
: caller_prot
);
3504 VM_STAT_INCR(faults
);
3505 current_task()->faults
++;
3506 original_fault_type
= fault_type
;
3509 if (fault_type
& VM_PROT_WRITE
) {
3514 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3516 object_lock_type
= OBJECT_LOCK_SHARED
;
3519 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
3521 if ((map
== kernel_map
) && (caller_prot
& VM_PROT_WRITE
)) {
3522 if (compressor_map
) {
3523 if ((vaddr
>= vm_map_min(compressor_map
)) && (vaddr
< vm_map_max(compressor_map
))) {
3524 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
));
3529 assert(written_on_object
== VM_OBJECT_NULL
);
3532 * assume we will hit a page in the cache
3533 * otherwise, explicitly override with
3534 * the real fault type once we determine it
3536 type_of_fault
= DBG_CACHE_HIT_FAULT
;
3539 * Find the backing store object and offset into
3540 * it to begin the search.
3542 fault_type
= original_fault_type
;
3544 vm_map_lock_read(map
);
3546 if (resilient_media_retry
) {
3548 * If we have to insert a fake zero-filled page to hide
3549 * a media failure to provide the real page, we need to
3550 * resolve any pending copy-on-write on this mapping.
3551 * VM_PROT_COPY tells vm_map_lookup_locked() to deal
3552 * with that even if this is not a "write" fault.
3555 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3558 kr
= vm_map_lookup_locked(&map
, vaddr
,
3559 (fault_type
| (need_copy
? VM_PROT_COPY
: 0)),
3560 object_lock_type
, &version
,
3561 &object
, &offset
, &prot
, &wired
,
3565 if (kr
!= KERN_SUCCESS
) {
3566 vm_map_unlock_read(map
);
3569 pmap
= real_map
->pmap
;
3570 fault_info
.interruptible
= interruptible
;
3571 fault_info
.stealth
= FALSE
;
3572 fault_info
.io_sync
= FALSE
;
3573 fault_info
.mark_zf_absent
= FALSE
;
3574 fault_info
.batch_pmap_op
= FALSE
;
3576 if (resilient_media_retry
) {
3578 * We're retrying this fault after having detected a media
3579 * failure from a "resilient_media" mapping.
3580 * Check that the mapping is still pointing at the object
3581 * that just failed to provide a page.
3583 assert(resilient_media_object
!= VM_OBJECT_NULL
);
3584 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
3585 if (object
!= VM_OBJECT_NULL
&&
3586 object
== resilient_media_object
&&
3587 offset
== resilient_media_offset
&&
3588 fault_info
.resilient_media
) {
3590 * This mapping still points at the same object
3591 * and is still "resilient_media": proceed in
3592 * "recovery-from-media-failure" mode, where we'll
3593 * insert a zero-filled page in the top object.
3595 // printf("RESILIENT_MEDIA %s:%d recovering for object %p offset 0x%llx\n", __FUNCTION__, __LINE__, object, offset);
3597 /* not recovering: reset state */
3598 // 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);
3599 resilient_media_retry
= FALSE
;
3600 /* release our extra reference on failed object */
3601 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
3602 vm_object_deallocate(resilient_media_object
);
3603 resilient_media_object
= VM_OBJECT_NULL
;
3604 resilient_media_offset
= (vm_object_offset_t
)-1;
3607 assert(resilient_media_object
== VM_OBJECT_NULL
);
3608 resilient_media_offset
= (vm_object_offset_t
)-1;
3612 * If the page is wired, we must fault for the current protection
3613 * value, to avoid further faults.
3616 fault_type
= prot
| VM_PROT_WRITE
;
3618 if (wired
|| need_copy
) {
3620 * since we're treating this fault as a 'write'
3621 * we must hold the top object lock exclusively
3623 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3624 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3626 if (vm_object_lock_upgrade(object
) == FALSE
) {
3628 * couldn't upgrade, so explictly
3629 * take the lock exclusively
3631 vm_object_lock(object
);
3636 #if VM_FAULT_CLASSIFY
3638 * Temporary data gathering code
3640 vm_fault_classify(object
, offset
, fault_type
);
3643 * Fast fault code. The basic idea is to do as much as
3644 * possible while holding the map lock and object locks.
3645 * Busy pages are not used until the object lock has to
3646 * be dropped to do something (copy, zero fill, pmap enter).
3647 * Similarly, paging references aren't acquired until that
3648 * point, and object references aren't used.
3650 * If we can figure out what to do
3651 * (zero fill, copy on write, pmap enter) while holding
3652 * the locks, then it gets done. Otherwise, we give up,
3653 * and use the original fault path (which doesn't hold
3654 * the map lock, and relies on busy pages).
3655 * The give up cases include:
3656 * - Have to talk to pager.
3657 * - Page is busy, absent or in error.
3658 * - Pager has locked out desired access.
3659 * - Fault needs to be restarted.
3660 * - Have to push page into copy object.
3662 * The code is an infinite loop that moves one level down
3663 * the shadow chain each time. cur_object and cur_offset
3664 * refer to the current object being examined. object and offset
3665 * are the original object from the map. The loop is at the
3666 * top level if and only if object and cur_object are the same.
3668 * Invariants: Map lock is held throughout. Lock is held on
3669 * original object and cur_object (if different) when
3670 * continuing or exiting loop.
3674 #if defined(__arm64__)
3676 * Fail if reading an execute-only page in a
3677 * pmap that enforces execute-only protection.
3679 if (fault_type
== VM_PROT_READ
&&
3680 (prot
& VM_PROT_EXECUTE
) &&
3681 !(prot
& VM_PROT_READ
) &&
3682 pmap_enforces_execute_only(pmap
)) {
3683 vm_object_unlock(object
);
3684 vm_map_unlock_read(map
);
3685 if (real_map
!= map
) {
3686 vm_map_unlock(real_map
);
3688 kr
= KERN_PROTECTION_FAILURE
;
3694 * If this page is to be inserted in a copy delay object
3695 * for writing, and if the object has a copy, then the
3696 * copy delay strategy is implemented in the slow fault page.
3698 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
3699 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
)) {
3700 goto handle_copy_delay
;
3703 cur_object
= object
;
3704 cur_offset
= offset
;
3707 #if CONFIG_SECLUDED_MEMORY
3708 if (object
->can_grab_secluded
) {
3709 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
3711 #endif /* CONFIG_SECLUDED_MEMORY */
3714 if (!cur_object
->pager_created
&&
3715 cur_object
->phys_contiguous
) { /* superpage */
3719 if (cur_object
->blocked_access
) {
3721 * Access to this VM object has been blocked.
3722 * Let the slow path handle it.
3727 m
= vm_page_lookup(cur_object
, cur_offset
);
3730 if (m
!= VM_PAGE_NULL
) {
3731 m_object
= cur_object
;
3734 wait_result_t result
;
3737 * in order to do the PAGE_ASSERT_WAIT, we must
3738 * have object that 'm' belongs to locked exclusively
3740 if (object
!= cur_object
) {
3741 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3742 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3744 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3746 * couldn't upgrade so go do a full retry
3747 * immediately since we can no longer be
3748 * certain about cur_object (since we
3749 * don't hold a reference on it)...
3750 * first drop the top object lock
3752 vm_object_unlock(object
);
3754 vm_map_unlock_read(map
);
3755 if (real_map
!= map
) {
3756 vm_map_unlock(real_map
);
3762 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3763 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3765 if (vm_object_lock_upgrade(object
) == FALSE
) {
3767 * couldn't upgrade, so explictly take the lock
3768 * exclusively and go relookup the page since we
3769 * will have dropped the object lock and
3770 * a different thread could have inserted
3771 * a page at this offset
3772 * no need for a full retry since we're
3773 * at the top level of the object chain
3775 vm_object_lock(object
);
3780 if ((m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) && m_object
->internal
) {
3782 * m->vmp_busy == TRUE and the object is locked exclusively
3783 * if m->pageout_queue == TRUE after we acquire the
3784 * queues lock, we are guaranteed that it is stable on
3785 * the pageout queue and therefore reclaimable
3787 * NOTE: this is only true for the internal pageout queue
3788 * in the compressor world
3790 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3792 vm_page_lock_queues();
3794 if (m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) {
3795 vm_pageout_throttle_up(m
);
3796 vm_page_unlock_queues();
3798 PAGE_WAKEUP_DONE(m
);
3799 goto reclaimed_from_pageout
;
3801 vm_page_unlock_queues();
3803 if (object
!= cur_object
) {
3804 vm_object_unlock(object
);
3807 vm_map_unlock_read(map
);
3808 if (real_map
!= map
) {
3809 vm_map_unlock(real_map
);
3812 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
3814 vm_object_unlock(cur_object
);
3816 if (result
== THREAD_WAITING
) {
3817 result
= thread_block(THREAD_CONTINUE_NULL
);
3819 counter(c_vm_fault_page_block_busy_kernel
++);
3821 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
) {
3828 reclaimed_from_pageout
:
3829 if (m
->vmp_laundry
) {
3830 if (object
!= cur_object
) {
3831 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3832 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3834 vm_object_unlock(object
);
3835 vm_object_unlock(cur_object
);
3837 vm_map_unlock_read(map
);
3838 if (real_map
!= map
) {
3839 vm_map_unlock(real_map
);
3844 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3845 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3847 if (vm_object_lock_upgrade(object
) == FALSE
) {
3849 * couldn't upgrade, so explictly take the lock
3850 * exclusively and go relookup the page since we
3851 * will have dropped the object lock and
3852 * a different thread could have inserted
3853 * a page at this offset
3854 * no need for a full retry since we're
3855 * at the top level of the object chain
3857 vm_object_lock(object
);
3862 vm_pageout_steal_laundry(m
, FALSE
);
3865 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
3867 * Guard page: let the slow path deal with it
3871 if (m
->vmp_unusual
&& (m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_private
|| m
->vmp_absent
)) {
3873 * Unusual case... let the slow path deal with it
3877 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object
)) {
3878 if (object
!= cur_object
) {
3879 vm_object_unlock(object
);
3881 vm_map_unlock_read(map
);
3882 if (real_map
!= map
) {
3883 vm_map_unlock(real_map
);
3885 vm_object_unlock(cur_object
);
3886 kr
= KERN_MEMORY_ERROR
;
3889 assert(m_object
== VM_PAGE_OBJECT(m
));
3891 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
, m_object
) ||
3892 (physpage_p
!= NULL
&& (prot
& VM_PROT_WRITE
))) {
3893 upgrade_lock_and_retry
:
3895 * We might need to validate this page
3896 * against its code signature, so we
3897 * want to hold the VM object exclusively.
3899 if (object
!= cur_object
) {
3900 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3901 vm_object_unlock(object
);
3902 vm_object_unlock(cur_object
);
3904 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3906 vm_map_unlock_read(map
);
3907 if (real_map
!= map
) {
3908 vm_map_unlock(real_map
);
3913 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3914 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3916 if (vm_object_lock_upgrade(object
) == FALSE
) {
3918 * couldn't upgrade, so explictly take the lock
3919 * exclusively and go relookup the page since we
3920 * will have dropped the object lock and
3921 * a different thread could have inserted
3922 * a page at this offset
3923 * no need for a full retry since we're
3924 * at the top level of the object chain
3926 vm_object_lock(object
);
3933 * Two cases of map in faults:
3934 * - At top level w/o copy object.
3935 * - Read fault anywhere.
3936 * --> must disallow write.
3939 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
3944 !fault_info
.no_copy_on_read
&&
3945 cur_object
!= object
&&
3946 !cur_object
->internal
&&
3947 !cur_object
->pager_trusted
&&
3948 vm_protect_privileged_from_untrusted
&&
3949 !((prot
& VM_PROT_EXECUTE
) &&
3950 cur_object
->code_signed
&&
3951 cs_process_enforcement(NULL
)) &&
3952 current_proc_is_privileged()) {
3954 * We're faulting on a page in "object" and
3955 * went down the shadow chain to "cur_object"
3956 * to find out that "cur_object"'s pager
3957 * is not "trusted", i.e. we can not trust it
3958 * to always return the same contents.
3959 * Since the target is a "privileged" process,
3960 * let's treat this as a copy-on-read fault, as
3961 * if it was a copy-on-write fault.
3962 * Once "object" gets a copy of this page, it
3963 * won't have to rely on "cur_object" to
3964 * provide the contents again.
3966 * This is done by setting "need_copy" and
3967 * retrying the fault from the top with the
3968 * appropriate locking.
3970 * Special case: if the mapping is executable
3971 * and the untrusted object is code-signed and
3972 * the process is "cs_enforced", we do not
3973 * copy-on-read because that would break
3974 * code-signing enforcement expectations (an
3975 * executable page must belong to a code-signed
3976 * object) and we can rely on code-signing
3977 * to re-validate the page if it gets evicted
3978 * and paged back in.
3980 // 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);
3981 vm_copied_on_read
++;
3984 vm_object_unlock(object
);
3985 vm_object_unlock(cur_object
);
3986 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3987 vm_map_unlock_read(map
);
3988 if (real_map
!= map
) {
3989 vm_map_unlock(real_map
);
3994 if (!(fault_type
& VM_PROT_WRITE
) && !need_copy
) {
3995 if (!pmap_has_prot_policy(prot
)) {
3996 prot
&= ~VM_PROT_WRITE
;
3999 * For a protection that the pmap cares
4000 * about, we must hand over the full
4001 * set of protections (so that the pmap
4002 * layer can apply any desired policy).
4003 * This means that cs_bypass must be
4004 * set, as this can force us to pass
4007 assert(fault_info
.cs_bypass
);
4010 if (object
!= cur_object
) {
4012 * We still need to hold the top object
4013 * lock here to prevent a race between
4014 * a read fault (taking only "shared"
4015 * locks) and a write fault (taking
4016 * an "exclusive" lock on the top
4018 * Otherwise, as soon as we release the
4019 * top lock, the write fault could
4020 * proceed and actually complete before
4021 * the read fault, and the copied page's
4022 * translation could then be overwritten
4023 * by the read fault's translation for
4024 * the original page.
4026 * Let's just record what the top object
4027 * is and we'll release it later.
4029 top_object
= object
;
4032 * switch to the object that has the new page
4034 object
= cur_object
;
4035 object_lock_type
= cur_object_lock_type
;
4038 assert(m_object
== VM_PAGE_OBJECT(m
));
4041 * prepare for the pmap_enter...
4042 * object and map are both locked
4043 * m contains valid data
4044 * object == m->vmp_object
4045 * cur_object == NULL or it's been unlocked
4046 * no paging references on either object or cur_object
4048 if (top_object
!= VM_OBJECT_NULL
|| object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4049 need_retry_ptr
= &need_retry
;
4051 need_retry_ptr
= NULL
;
4055 kr
= vm_fault_enter(m
,
4067 kr
= vm_fault_enter(m
,
4082 if (m_object
->internal
) {
4083 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
4084 } else if (m_object
->object_is_shared_cache
) {
4085 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
4087 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
4090 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);
4091 if (need_retry
== FALSE
) {
4092 KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_FAST
), get_current_unique_pid(), 0, 0, 0, 0);
4094 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
);
4096 if (kr
== KERN_SUCCESS
&&
4097 physpage_p
!= NULL
) {
4098 /* for vm_map_wire_and_extract() */
4099 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
4100 if (prot
& VM_PROT_WRITE
) {
4101 vm_object_lock_assert_exclusive(m_object
);
4102 m
->vmp_dirty
= TRUE
;
4106 if (top_object
!= VM_OBJECT_NULL
) {
4108 * It's safe to drop the top object
4109 * now that we've done our
4110 * vm_fault_enter(). Any other fault
4111 * in progress for that virtual
4112 * address will either find our page
4113 * and translation or put in a new page
4116 vm_object_unlock(top_object
);
4117 top_object
= VM_OBJECT_NULL
;
4120 if (need_collapse
== TRUE
) {
4121 vm_object_collapse(object
, offset
, TRUE
);
4124 if (need_retry
== FALSE
&&
4125 (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
)) {
4127 * evaluate access pattern and update state
4128 * vm_fault_deactivate_behind depends on the
4129 * state being up to date
4131 vm_fault_is_sequential(m_object
, cur_offset
, fault_info
.behavior
);
4133 vm_fault_deactivate_behind(m_object
, cur_offset
, fault_info
.behavior
);
4136 * That's it, clean up and return.
4139 PAGE_WAKEUP_DONE(m
);
4142 if (need_retry
== FALSE
&& !m_object
->internal
&& (fault_type
& VM_PROT_WRITE
)) {
4143 vm_object_paging_begin(m_object
);
4145 assert(written_on_object
== VM_OBJECT_NULL
);
4146 written_on_object
= m_object
;
4147 written_on_pager
= m_object
->pager
;
4148 written_on_offset
= m_object
->paging_offset
+ m
->vmp_offset
;
4150 vm_object_unlock(object
);
4152 vm_map_unlock_read(map
);
4153 if (real_map
!= map
) {
4154 vm_map_unlock(real_map
);
4157 if (need_retry
== TRUE
) {
4159 * vm_fault_enter couldn't complete the PMAP_ENTER...
4160 * at this point we don't hold any locks so it's safe
4161 * to ask the pmap layer to expand the page table to
4162 * accommodate this mapping... once expanded, we'll
4163 * re-drive the fault which should result in vm_fault_enter
4164 * being able to successfully enter the mapping this time around
4166 (void)pmap_enter_options(
4167 pmap
, vaddr
, 0, 0, 0, 0, 0,
4168 PMAP_OPTIONS_NOENTER
, NULL
);
4176 * COPY ON WRITE FAULT
4178 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
4181 * If objects match, then
4182 * object->copy must not be NULL (else control
4183 * would be in previous code block), and we
4184 * have a potential push into the copy object
4185 * with which we can't cope with here.
4187 if (cur_object
== object
) {
4189 * must take the slow path to
4190 * deal with the copy push
4196 * This is now a shadow based copy on write
4197 * fault -- it requires a copy up the shadow
4200 assert(m_object
== VM_PAGE_OBJECT(m
));
4202 if ((cur_object_lock_type
== OBJECT_LOCK_SHARED
) &&
4203 VM_FAULT_NEED_CS_VALIDATION(NULL
, m
, m_object
)) {
4204 goto upgrade_lock_and_retry
;
4208 * Allocate a page in the original top level
4209 * object. Give up if allocate fails. Also
4210 * need to remember current page, as it's the
4211 * source of the copy.
4213 * at this point we hold locks on both
4214 * object and cur_object... no need to take
4215 * paging refs or mark pages BUSY since
4216 * we don't drop either object lock until
4217 * the page has been copied and inserted
4220 m
= vm_page_grab_options(grab_options
);
4223 if (m
== VM_PAGE_NULL
) {
4225 * no free page currently available...
4226 * must take the slow path
4231 * Now do the copy. Mark the source page busy...
4233 * NOTE: This code holds the map lock across
4236 vm_page_copy(cur_m
, m
);
4237 vm_page_insert(m
, object
, offset
);
4239 SET_PAGE_DIRTY(m
, FALSE
);
4242 * Now cope with the source page and object
4244 if (object
->ref_count
> 1 && cur_m
->vmp_pmapped
) {
4245 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m
));
4248 if (cur_m
->vmp_clustered
) {
4249 VM_PAGE_COUNT_AS_PAGEIN(cur_m
);
4250 VM_PAGE_CONSUME_CLUSTERED(cur_m
);
4251 vm_fault_is_sequential(cur_object
, cur_offset
, fault_info
.behavior
);
4253 need_collapse
= TRUE
;
4255 if (!cur_object
->internal
&&
4256 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
4258 * The object from which we've just
4259 * copied a page is most probably backed
4260 * by a vnode. We don't want to waste too
4261 * much time trying to collapse the VM objects
4262 * and create a bottleneck when several tasks
4263 * map the same file.
4265 if (cur_object
->copy
== object
) {
4267 * Shared mapping or no COW yet.
4268 * We can never collapse a copy
4269 * object into its backing object.
4271 need_collapse
= FALSE
;
4272 } else if (cur_object
->copy
== object
->shadow
&&
4273 object
->shadow
->resident_page_count
== 0) {
4275 * Shared mapping after a COW occurred.
4277 need_collapse
= FALSE
;
4280 vm_object_unlock(cur_object
);
4282 if (need_collapse
== FALSE
) {
4283 vm_fault_collapse_skipped
++;
4285 vm_fault_collapse_total
++;
4287 type_of_fault
= DBG_COW_FAULT
;
4288 VM_STAT_INCR(cow_faults
);
4289 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
4290 current_task()->cow_faults
++;
4295 * No page at cur_object, cur_offset... m == NULL
4297 if (cur_object
->pager_created
) {
4298 int compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
4300 if (MUST_ASK_PAGER(cur_object
, cur_offset
, compressor_external_state
) == TRUE
) {
4302 int c_flags
= C_DONT_BLOCK
;
4303 boolean_t insert_cur_object
= FALSE
;
4306 * May have to talk to a pager...
4307 * if so, take the slow path by
4308 * doing a 'break' from the while (TRUE) loop
4310 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
4311 * if the compressor is active and the page exists there
4313 if (compressor_external_state
!= VM_EXTERNAL_STATE_EXISTS
) {
4317 if (map
== kernel_map
|| real_map
== kernel_map
) {
4319 * can't call into the compressor with the kernel_map
4320 * lock held, since the compressor may try to operate
4321 * on the kernel map in order to return an empty c_segment
4325 if (object
!= cur_object
) {
4326 if (fault_type
& VM_PROT_WRITE
) {
4329 insert_cur_object
= TRUE
;
4332 if (insert_cur_object
== TRUE
) {
4333 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4334 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4336 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
4338 * couldn't upgrade so go do a full retry
4339 * immediately since we can no longer be
4340 * certain about cur_object (since we
4341 * don't hold a reference on it)...
4342 * first drop the top object lock
4344 vm_object_unlock(object
);
4346 vm_map_unlock_read(map
);
4347 if (real_map
!= map
) {
4348 vm_map_unlock(real_map
);
4354 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4355 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4357 if (object
!= cur_object
) {
4359 * we can't go for the upgrade on the top
4360 * lock since the upgrade may block waiting
4361 * for readers to drain... since we hold
4362 * cur_object locked at this point, waiting
4363 * for the readers to drain would represent
4364 * a lock order inversion since the lock order
4365 * for objects is the reference order in the
4368 vm_object_unlock(object
);
4369 vm_object_unlock(cur_object
);
4371 vm_map_unlock_read(map
);
4372 if (real_map
!= map
) {
4373 vm_map_unlock(real_map
);
4378 if (vm_object_lock_upgrade(object
) == FALSE
) {
4380 * couldn't upgrade, so explictly take the lock
4381 * exclusively and go relookup the page since we
4382 * will have dropped the object lock and
4383 * a different thread could have inserted
4384 * a page at this offset
4385 * no need for a full retry since we're
4386 * at the top level of the object chain
4388 vm_object_lock(object
);
4393 m
= vm_page_grab_options(grab_options
);
4396 if (m
== VM_PAGE_NULL
) {
4398 * no free page currently available...
4399 * must take the slow path
4405 * The object is and remains locked
4406 * so no need to take a
4407 * "paging_in_progress" reference.
4409 boolean_t shared_lock
;
4410 if ((object
== cur_object
&&
4411 object_lock_type
== OBJECT_LOCK_EXCLUSIVE
) ||
4412 (object
!= cur_object
&&
4413 cur_object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)) {
4414 shared_lock
= FALSE
;
4419 kr
= vm_compressor_pager_get(
4422 cur_object
->paging_offset
),
4423 VM_PAGE_GET_PHYS_PAGE(m
),
4426 &compressed_count_delta
);
4428 vm_compressor_pager_count(
4430 compressed_count_delta
,
4434 if (kr
!= KERN_SUCCESS
) {
4435 vm_page_release(m
, FALSE
);
4439 m
->vmp_dirty
= TRUE
;
4442 * If the object is purgeable, its
4443 * owner's purgeable ledgers will be
4444 * updated in vm_page_insert() but the
4445 * page was also accounted for in a
4446 * "compressed purgeable" ledger, so
4449 if (object
!= cur_object
&&
4450 !insert_cur_object
) {
4452 * We're not going to insert
4453 * the decompressed page into
4454 * the object it came from.
4456 * We're dealing with a
4457 * copy-on-write fault on
4459 * We're going to decompress
4460 * the page directly into the
4461 * target "object" while
4462 * keepin the compressed
4463 * page for "cur_object", so
4464 * no ledger update in that
4467 } else if (((cur_object
->purgable
==
4468 VM_PURGABLE_DENY
) &&
4469 (!cur_object
->vo_ledger_tag
)) ||
4470 (cur_object
->vo_owner
==
4473 * "cur_object" is not purgeable
4474 * and is not ledger-taged, or
4475 * there's no owner for it,
4476 * so no owner's ledgers to
4481 * One less compressed
4482 * purgeable/tagged page for
4483 * cur_object's owner.
4485 vm_object_owner_compressed_update(
4490 if (insert_cur_object
) {
4491 vm_page_insert(m
, cur_object
, cur_offset
);
4492 m_object
= cur_object
;
4494 vm_page_insert(m
, object
, offset
);
4498 if ((m_object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_USE_DEFAULT
) {
4500 * If the page is not cacheable,
4501 * we can't let its contents
4502 * linger in the data cache
4503 * after the decompression.
4505 pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m
));
4508 type_of_fault
= my_fault_type
;
4510 VM_STAT_DECOMPRESSIONS();
4512 if (cur_object
!= object
) {
4513 if (insert_cur_object
) {
4514 top_object
= object
;
4516 * switch to the object that has the new page
4518 object
= cur_object
;
4519 object_lock_type
= cur_object_lock_type
;
4521 vm_object_unlock(cur_object
);
4522 cur_object
= object
;
4528 * existence map present and indicates
4529 * that the pager doesn't have this page
4532 if (cur_object
->shadow
== VM_OBJECT_NULL
||
4533 resilient_media_retry
) {
4535 * Zero fill fault. Page gets
4536 * inserted into the original object.
4538 if (cur_object
->shadow_severed
||
4539 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
) ||
4540 cur_object
== compressor_object
||
4541 cur_object
== kernel_object
||
4542 cur_object
== vm_submap_object
) {
4543 if (object
!= cur_object
) {
4544 vm_object_unlock(cur_object
);
4546 vm_object_unlock(object
);
4548 vm_map_unlock_read(map
);
4549 if (real_map
!= map
) {
4550 vm_map_unlock(real_map
);
4553 kr
= KERN_MEMORY_ERROR
;
4556 if (cur_object
!= object
) {
4557 vm_object_unlock(cur_object
);
4559 cur_object
= object
;
4561 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4562 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4564 if (vm_object_lock_upgrade(object
) == FALSE
) {
4566 * couldn't upgrade so do a full retry on the fault
4567 * since we dropped the object lock which
4568 * could allow another thread to insert
4569 * a page at this offset
4571 vm_map_unlock_read(map
);
4572 if (real_map
!= map
) {
4573 vm_map_unlock(real_map
);
4579 if (!object
->internal
) {
4580 panic("%s:%d should not zero-fill page at offset 0x%llx in external object %p", __FUNCTION__
, __LINE__
, (uint64_t)offset
, object
);
4582 m
= vm_page_alloc(object
, offset
);
4585 if (m
== VM_PAGE_NULL
) {
4587 * no free page currently available...
4588 * must take the slow path
4595 * Now zero fill page...
4596 * the page is probably going to
4597 * be written soon, so don't bother
4598 * to clear the modified bit
4600 * NOTE: This code holds the map
4601 * lock across the zero fill.
4603 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
4608 * On to the next level in the shadow chain
4610 cur_offset
+= cur_object
->vo_shadow_offset
;
4611 new_object
= cur_object
->shadow
;
4614 * take the new_object's lock with the indicated state
4616 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4617 vm_object_lock_shared(new_object
);
4619 vm_object_lock(new_object
);
4622 if (cur_object
!= object
) {
4623 vm_object_unlock(cur_object
);
4626 cur_object
= new_object
;
4632 * Cleanup from fast fault failure. Drop any object
4633 * lock other than original and drop map lock.
4635 if (object
!= cur_object
) {
4636 vm_object_unlock(cur_object
);
4640 * must own the object lock exclusively at this point
4642 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4643 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4645 if (vm_object_lock_upgrade(object
) == FALSE
) {
4647 * couldn't upgrade, so explictly
4648 * take the lock exclusively
4649 * no need to retry the fault at this
4650 * point since "vm_fault_page" will
4651 * completely re-evaluate the state
4653 vm_object_lock(object
);
4658 vm_map_unlock_read(map
);
4659 if (real_map
!= map
) {
4660 vm_map_unlock(real_map
);
4663 if (__improbable(object
== compressor_object
||
4664 object
== kernel_object
||
4665 object
== vm_submap_object
)) {
4667 * These objects are explicitly managed and populated by the
4668 * kernel. The virtual ranges backed by these objects should
4669 * either have wired pages or "holes" that are not supposed to
4670 * be accessed at all until they get explicitly populated.
4671 * We should never have to resolve a fault on a mapping backed
4672 * by one of these VM objects and providing a zero-filled page
4673 * would be wrong here, so let's fail the fault and let the
4674 * caller crash or recover.
4676 vm_object_unlock(object
);
4677 kr
= KERN_MEMORY_ERROR
;
4681 assert(object
!= compressor_object
);
4682 assert(object
!= kernel_object
);
4683 assert(object
!= vm_submap_object
);
4685 if (resilient_media_retry
) {
4687 * We could get here if we failed to get a free page
4688 * to zero-fill and had to take the slow path again.
4689 * Reset our "recovery-from-failed-media" state.
4691 assert(resilient_media_object
!= VM_OBJECT_NULL
);
4692 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
4693 /* release our extra reference on failed object */
4694 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
4695 vm_object_deallocate(resilient_media_object
);
4696 resilient_media_object
= VM_OBJECT_NULL
;
4697 resilient_media_offset
= (vm_object_offset_t
)-1;
4698 resilient_media_retry
= FALSE
;
4702 * Make a reference to this object to
4703 * prevent its disposal while we are messing with
4704 * it. Once we have the reference, the map is free
4705 * to be diddled. Since objects reference their
4706 * shadows (and copies), they will stay around as well.
4708 vm_object_reference_locked(object
);
4709 vm_object_paging_begin(object
);
4711 set_thread_pagein_error(cthread
, 0);
4714 result_page
= VM_PAGE_NULL
;
4715 kr
= vm_fault_page(object
, offset
, fault_type
,
4716 (change_wiring
&& !wired
),
4717 FALSE
, /* page not looked up */
4718 &prot
, &result_page
, &top_page
,
4720 &error_code
, map
->no_zero_fill
,
4721 FALSE
, &fault_info
);
4724 * if kr != VM_FAULT_SUCCESS, then the paging reference
4725 * has been dropped and the object unlocked... the ref_count
4728 * if kr == VM_FAULT_SUCCESS, then the paging reference
4729 * is still held along with the ref_count on the original object
4731 * the object is returned locked with a paging reference
4733 * if top_page != NULL, then it's BUSY and the
4734 * object it belongs to has a paging reference
4735 * but is returned unlocked
4737 if (kr
!= VM_FAULT_SUCCESS
&&
4738 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
4739 if (kr
== VM_FAULT_MEMORY_ERROR
&&
4740 fault_info
.resilient_media
) {
4741 assertf(object
->internal
, "object %p", object
);
4743 * This fault failed but the mapping was
4744 * "media resilient", so we'll retry the fault in
4745 * recovery mode to get a zero-filled page in the
4747 * Keep the reference on the failing object so
4748 * that we can check that the mapping is still
4749 * pointing to it when we retry the fault.
4751 // 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);
4752 assert(!resilient_media_retry
); /* no double retry */
4753 assert(resilient_media_object
== VM_OBJECT_NULL
);
4754 assert(resilient_media_offset
== (vm_object_offset_t
)-1);
4755 resilient_media_retry
= TRUE
;
4756 resilient_media_object
= object
;
4757 resilient_media_offset
= offset
;
4758 // printf("FBDP %s:%d resilient_media_object %p offset 0x%llx kept reference\n", __FUNCTION__, __LINE__, resilient_media_object, resilient_mmedia_offset);
4762 * we didn't succeed, lose the object reference
4765 vm_object_deallocate(object
);
4766 object
= VM_OBJECT_NULL
; /* no longer valid */
4770 * See why we failed, and take corrective action.
4773 case VM_FAULT_MEMORY_SHORTAGE
:
4774 if (vm_page_wait((change_wiring
) ?
4776 THREAD_ABORTSAFE
)) {
4782 case VM_FAULT_INTERRUPTED
:
4785 case VM_FAULT_RETRY
:
4787 case VM_FAULT_MEMORY_ERROR
:
4791 kr
= KERN_MEMORY_ERROR
;
4795 panic("vm_fault: unexpected error 0x%x from "
4796 "vm_fault_page()\n", kr
);
4802 if (m
!= VM_PAGE_NULL
) {
4803 m_object
= VM_PAGE_OBJECT(m
);
4804 assert((change_wiring
&& !wired
) ?
4805 (top_page
== VM_PAGE_NULL
) :
4806 ((top_page
== VM_PAGE_NULL
) == (m_object
== object
)));
4810 * What to do with the resulting page from vm_fault_page
4811 * if it doesn't get entered into the physical map:
4813 #define RELEASE_PAGE(m) \
4815 PAGE_WAKEUP_DONE(m); \
4816 if ( !VM_PAGE_PAGEABLE(m)) { \
4817 vm_page_lockspin_queues(); \
4818 if ( !VM_PAGE_PAGEABLE(m)) \
4819 vm_page_activate(m); \
4820 vm_page_unlock_queues(); \
4825 object_locks_dropped
= FALSE
;
4827 * We must verify that the maps have not changed
4828 * since our last lookup. vm_map_verify() needs the
4829 * map lock (shared) but we are holding object locks.
4830 * So we do a try_lock() first and, if that fails, we
4831 * drop the object locks and go in for the map lock again.
4833 if (!vm_map_try_lock_read(original_map
)) {
4834 if (m
!= VM_PAGE_NULL
) {
4835 old_copy_object
= m_object
->copy
;
4836 vm_object_unlock(m_object
);
4838 old_copy_object
= VM_OBJECT_NULL
;
4839 vm_object_unlock(object
);
4842 object_locks_dropped
= TRUE
;
4844 vm_map_lock_read(original_map
);
4847 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
4848 if (object_locks_dropped
== FALSE
) {
4849 if (m
!= VM_PAGE_NULL
) {
4850 old_copy_object
= m_object
->copy
;
4851 vm_object_unlock(m_object
);
4853 old_copy_object
= VM_OBJECT_NULL
;
4854 vm_object_unlock(object
);
4857 object_locks_dropped
= TRUE
;
4861 * no object locks are held at this point
4863 vm_object_t retry_object
;
4864 vm_object_offset_t retry_offset
;
4865 vm_prot_t retry_prot
;
4868 * To avoid trying to write_lock the map while another
4869 * thread has it read_locked (in vm_map_pageable), we
4870 * do not try for write permission. If the page is
4871 * still writable, we will get write permission. If it
4872 * is not, or has been marked needs_copy, we enter the
4873 * mapping without write permission, and will merely
4874 * take another fault.
4878 kr
= vm_map_lookup_locked(&map
, vaddr
,
4879 fault_type
& ~VM_PROT_WRITE
,
4880 OBJECT_LOCK_EXCLUSIVE
, &version
,
4881 &retry_object
, &retry_offset
, &retry_prot
,
4885 pmap
= real_map
->pmap
;
4887 if (kr
!= KERN_SUCCESS
) {
4888 vm_map_unlock_read(map
);
4890 if (m
!= VM_PAGE_NULL
) {
4891 assert(VM_PAGE_OBJECT(m
) == m_object
);
4894 * retake the lock so that
4895 * we can drop the paging reference
4896 * in vm_fault_cleanup and do the
4897 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4899 vm_object_lock(m_object
);
4903 vm_fault_cleanup(m_object
, top_page
);
4906 * retake the lock so that
4907 * we can drop the paging reference
4908 * in vm_fault_cleanup
4910 vm_object_lock(object
);
4912 vm_fault_cleanup(object
, top_page
);
4914 vm_object_deallocate(object
);
4918 vm_object_unlock(retry_object
);
4920 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
4921 vm_map_unlock_read(map
);
4922 if (real_map
!= map
) {
4923 vm_map_unlock(real_map
);
4926 if (m
!= VM_PAGE_NULL
) {
4927 assert(VM_PAGE_OBJECT(m
) == m_object
);
4930 * retake the lock so that
4931 * we can drop the paging reference
4932 * in vm_fault_cleanup and do the
4933 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4935 vm_object_lock(m_object
);
4939 vm_fault_cleanup(m_object
, top_page
);
4942 * retake the lock so that
4943 * we can drop the paging reference
4944 * in vm_fault_cleanup
4946 vm_object_lock(object
);
4948 vm_fault_cleanup(object
, top_page
);
4950 vm_object_deallocate(object
);
4955 * Check whether the protection has changed or the object
4956 * has been copied while we left the map unlocked.
4958 if (pmap_has_prot_policy(retry_prot
)) {
4959 /* If the pmap layer cares, pass the full set. */
4966 if (object_locks_dropped
== TRUE
) {
4967 if (m
!= VM_PAGE_NULL
) {
4968 vm_object_lock(m_object
);
4970 if (m_object
->copy
!= old_copy_object
) {
4972 * The copy object changed while the top-level object
4973 * was unlocked, so take away write permission.
4975 assert(!pmap_has_prot_policy(prot
));
4976 prot
&= ~VM_PROT_WRITE
;
4979 vm_object_lock(object
);
4982 object_locks_dropped
= FALSE
;
4986 !fault_info
.no_copy_on_read
&&
4987 m
!= VM_PAGE_NULL
&&
4988 VM_PAGE_OBJECT(m
) != object
&&
4989 !VM_PAGE_OBJECT(m
)->pager_trusted
&&
4990 vm_protect_privileged_from_untrusted
&&
4991 !((prot
& VM_PROT_EXECUTE
) &&
4992 VM_PAGE_OBJECT(m
)->code_signed
&&
4993 cs_process_enforcement(NULL
)) &&
4994 current_proc_is_privileged()) {
4996 * We found the page we want in an "untrusted" VM object
4997 * down the shadow chain. Since the target is "privileged"
4998 * we want to perform a copy-on-read of that page, so that the
4999 * mapped object gets a stable copy and does not have to
5000 * rely on the "untrusted" object to provide the same
5001 * contents if the page gets reclaimed and has to be paged
5002 * in again later on.
5004 * Special case: if the mapping is executable and the untrusted
5005 * object is code-signed and the process is "cs_enforced", we
5006 * do not copy-on-read because that would break code-signing
5007 * enforcement expectations (an executable page must belong
5008 * to a code-signed object) and we can rely on code-signing
5009 * to re-validate the page if it gets evicted and paged back in.
5011 // 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);
5012 vm_copied_on_read
++;
5013 need_copy_on_read
= TRUE
;
5016 need_copy_on_read
= FALSE
;
5020 * If we want to wire down this page, but no longer have
5021 * adequate permissions, we must start all over.
5022 * If we decided to copy-on-read, we must also start all over.
5024 if ((wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) ||
5025 need_copy_on_read
) {
5026 vm_map_unlock_read(map
);
5027 if (real_map
!= map
) {
5028 vm_map_unlock(real_map
);
5031 if (m
!= VM_PAGE_NULL
) {
5032 assert(VM_PAGE_OBJECT(m
) == m_object
);
5036 vm_fault_cleanup(m_object
, top_page
);
5038 vm_fault_cleanup(object
, top_page
);
5041 vm_object_deallocate(object
);
5045 if (m
!= VM_PAGE_NULL
) {
5047 * Put this page into the physical map.
5048 * We had to do the unlock above because pmap_enter
5049 * may cause other faults. The page may be on
5050 * the pageout queues. If the pageout daemon comes
5051 * across the page, it will remove it from the queues.
5054 kr
= vm_fault_enter(m
,
5066 kr
= vm_fault_enter(m
,
5078 assert(VM_PAGE_OBJECT(m
) == m_object
);
5083 if (m_object
->internal
) {
5084 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
5085 } else if (m_object
->object_is_shared_cache
) {
5086 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
5088 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
5091 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);
5092 KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_SLOW
), get_current_unique_pid(), 0, 0, 0, 0);
5094 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
);
5096 if (kr
!= KERN_SUCCESS
) {
5097 /* abort this page fault */
5098 vm_map_unlock_read(map
);
5099 if (real_map
!= map
) {
5100 vm_map_unlock(real_map
);
5102 PAGE_WAKEUP_DONE(m
);
5103 vm_fault_cleanup(m_object
, top_page
);
5104 vm_object_deallocate(object
);
5107 if (physpage_p
!= NULL
) {
5108 /* for vm_map_wire_and_extract() */
5109 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
5110 if (prot
& VM_PROT_WRITE
) {
5111 vm_object_lock_assert_exclusive(m_object
);
5112 m
->vmp_dirty
= TRUE
;
5116 vm_map_entry_t entry
;
5117 vm_map_offset_t laddr
;
5118 vm_map_offset_t ldelta
, hdelta
;
5121 * do a pmap block mapping from the physical address
5125 if (real_map
!= map
) {
5126 vm_map_unlock(real_map
);
5129 if (original_map
!= map
) {
5130 vm_map_unlock_read(map
);
5131 vm_map_lock_read(original_map
);
5137 hdelta
= 0xFFFFF000;
5138 ldelta
= 0xFFFFF000;
5140 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
5141 if (ldelta
> (laddr
- entry
->vme_start
)) {
5142 ldelta
= laddr
- entry
->vme_start
;
5144 if (hdelta
> (entry
->vme_end
- laddr
)) {
5145 hdelta
= entry
->vme_end
- laddr
;
5147 if (entry
->is_sub_map
) {
5148 laddr
= ((laddr
- entry
->vme_start
)
5149 + VME_OFFSET(entry
));
5150 vm_map_lock_read(VME_SUBMAP(entry
));
5152 if (map
!= real_map
) {
5153 vm_map_unlock_read(map
);
5155 if (entry
->use_pmap
) {
5156 vm_map_unlock_read(real_map
);
5157 real_map
= VME_SUBMAP(entry
);
5159 map
= VME_SUBMAP(entry
);
5165 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
5166 (VME_OBJECT(entry
) != NULL
) &&
5167 (VME_OBJECT(entry
) == object
)) {
5170 if (!object
->pager_created
&&
5171 object
->phys_contiguous
&&
5172 VME_OFFSET(entry
) == 0 &&
5173 (entry
->vme_end
- entry
->vme_start
== object
->vo_size
) &&
5174 VM_MAP_PAGE_ALIGNED(entry
->vme_start
, (object
->vo_size
- 1))) {
5175 superpage
= VM_MEM_SUPERPAGE
;
5180 if (superpage
&& physpage_p
) {
5181 /* for vm_map_wire_and_extract() */
5182 *physpage_p
= (ppnum_t
)
5183 ((((vm_map_offset_t
)
5184 object
->vo_shadow_offset
)
5186 + (laddr
- entry
->vme_start
))
5192 * Set up a block mapped area
5194 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5195 kr
= pmap_map_block(caller_pmap
,
5196 (addr64_t
)(caller_pmap_addr
- ldelta
),
5197 (ppnum_t
)((((vm_map_offset_t
) (VME_OBJECT(entry
)->vo_shadow_offset
)) +
5198 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5199 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5200 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5202 if (kr
!= KERN_SUCCESS
) {
5207 * Set up a block mapped area
5209 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5210 kr
= pmap_map_block(real_map
->pmap
,
5211 (addr64_t
)(vaddr
- ldelta
),
5212 (ppnum_t
)((((vm_map_offset_t
)(VME_OBJECT(entry
)->vo_shadow_offset
)) +
5213 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5214 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5215 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5217 if (kr
!= KERN_SUCCESS
) {
5230 * TODO: could most of the done cases just use cleanup?
5234 * Unlock everything, and return
5236 vm_map_unlock_read(map
);
5237 if (real_map
!= map
) {
5238 vm_map_unlock(real_map
);
5241 if (m
!= VM_PAGE_NULL
) {
5242 assert(VM_PAGE_OBJECT(m
) == m_object
);
5244 if (!m_object
->internal
&& (fault_type
& VM_PROT_WRITE
)) {
5245 vm_object_paging_begin(m_object
);
5247 assert(written_on_object
== VM_OBJECT_NULL
);
5248 written_on_object
= m_object
;
5249 written_on_pager
= m_object
->pager
;
5250 written_on_offset
= m_object
->paging_offset
+ m
->vmp_offset
;
5252 PAGE_WAKEUP_DONE(m
);
5254 vm_fault_cleanup(m_object
, top_page
);
5256 vm_fault_cleanup(object
, top_page
);
5259 vm_object_deallocate(object
);
5264 thread_interrupt_level(interruptible_state
);
5266 if (resilient_media_object
!= VM_OBJECT_NULL
) {
5267 assert(resilient_media_retry
);
5268 assert(resilient_media_offset
!= (vm_object_offset_t
)-1);
5269 /* release extra reference on failed object */
5270 // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
5271 vm_object_deallocate(resilient_media_object
);
5272 resilient_media_object
= VM_OBJECT_NULL
;
5273 resilient_media_offset
= (vm_object_offset_t
)-1;
5274 resilient_media_retry
= FALSE
;
5276 assert(!resilient_media_retry
);
5279 * Only I/O throttle on faults which cause a pagein/swapin.
5281 if ((type_of_fault
== DBG_PAGEIND_FAULT
) || (type_of_fault
== DBG_PAGEINV_FAULT
) || (type_of_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
)) {
5282 throttle_lowpri_io(1);
5284 if (kr
== KERN_SUCCESS
&& type_of_fault
!= DBG_CACHE_HIT_FAULT
&& type_of_fault
!= DBG_GUARD_FAULT
) {
5285 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
5286 if (vm_debug_events
) {
5287 if (type_of_fault
== DBG_COMPRESSOR_FAULT
) {
5288 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5289 } else if (type_of_fault
== DBG_COW_FAULT
) {
5290 VM_DEBUG_EVENT(vmf_cowdelay
, VMF_COWDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5292 VM_DEBUG_EVENT(vmf_zfdelay
, VMF_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5295 delay(throttle_delay
);
5300 if (written_on_object
) {
5301 vnode_pager_dirtied(written_on_pager
, written_on_offset
, written_on_offset
+ PAGE_SIZE_64
);
5303 vm_object_lock(written_on_object
);
5304 vm_object_paging_end(written_on_object
);
5305 vm_object_unlock(written_on_object
);
5307 written_on_object
= VM_OBJECT_NULL
;
5311 vm_record_rtfault(cthread
, fstart
, trace_vaddr
, type_of_fault
);
5314 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5315 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
5316 ((uint64_t)trace_vaddr
>> 32),
5328 * Wire down a range of virtual addresses in a map.
5333 vm_map_entry_t entry
,
5337 vm_map_offset_t pmap_addr
,
5338 ppnum_t
*physpage_p
)
5341 vm_map_offset_t end_addr
= entry
->vme_end
;
5344 assert(entry
->in_transition
);
5346 if ((VME_OBJECT(entry
) != NULL
) &&
5347 !entry
->is_sub_map
&&
5348 VME_OBJECT(entry
)->phys_contiguous
) {
5349 return KERN_SUCCESS
;
5353 * Inform the physical mapping system that the
5354 * range of addresses may not fault, so that
5355 * page tables and such can be locked down as well.
5358 pmap_pageable(pmap
, pmap_addr
,
5359 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
5362 * We simulate a fault to get the page and enter it
5363 * in the physical map.
5366 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
5367 rc
= vm_fault_wire_fast(map
, va
, prot
, wire_tag
, entry
, pmap
,
5368 pmap_addr
+ (va
- entry
->vme_start
),
5370 if (rc
!= KERN_SUCCESS
) {
5371 rc
= vm_fault_internal(map
, va
, prot
, TRUE
, wire_tag
,
5372 ((pmap
== kernel_pmap
)
5374 : THREAD_ABORTSAFE
),
5377 (va
- entry
->vme_start
)),
5379 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
5382 if (rc
!= KERN_SUCCESS
) {
5383 struct vm_map_entry tmp_entry
= *entry
;
5385 /* unwire wired pages */
5386 tmp_entry
.vme_end
= va
;
5387 vm_fault_unwire(map
,
5388 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
5393 return KERN_SUCCESS
;
5399 * Unwire a range of virtual addresses in a map.
5404 vm_map_entry_t entry
,
5405 boolean_t deallocate
,
5407 vm_map_offset_t pmap_addr
)
5410 vm_map_offset_t end_addr
= entry
->vme_end
;
5412 struct vm_object_fault_info fault_info
= {};
5413 unsigned int unwired_pages
;
5415 object
= (entry
->is_sub_map
) ? VM_OBJECT_NULL
: VME_OBJECT(entry
);
5418 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
5419 * do anything since such memory is wired by default. So we don't have
5420 * anything to undo here.
5423 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
) {
5427 fault_info
.interruptible
= THREAD_UNINT
;
5428 fault_info
.behavior
= entry
->behavior
;
5429 fault_info
.user_tag
= VME_ALIAS(entry
);
5430 if (entry
->iokit_acct
||
5431 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
5432 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
5434 fault_info
.lo_offset
= VME_OFFSET(entry
);
5435 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + VME_OFFSET(entry
);
5436 fault_info
.no_cache
= entry
->no_cache
;
5437 fault_info
.stealth
= TRUE
;
5442 * Since the pages are wired down, we must be able to
5443 * get their mappings from the physical map system.
5446 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
5447 if (object
== VM_OBJECT_NULL
) {
5449 pmap_change_wiring(pmap
,
5450 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5452 (void) vm_fault(map
, va
, VM_PROT_NONE
,
5453 TRUE
, VM_KERN_MEMORY_NONE
, THREAD_UNINT
, pmap
, pmap_addr
);
5456 vm_page_t result_page
;
5458 vm_object_t result_object
;
5459 vm_fault_return_t result
;
5461 /* cap cluster size at maximum UPL size */
5462 upl_size_t cluster_size
;
5463 if (os_sub_overflow(end_addr
, va
, &cluster_size
)) {
5464 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
5466 fault_info
.cluster_size
= cluster_size
;
5469 prot
= VM_PROT_NONE
;
5471 vm_object_lock(object
);
5472 vm_object_paging_begin(object
);
5473 result_page
= VM_PAGE_NULL
;
5474 result
= vm_fault_page(
5476 (VME_OFFSET(entry
) +
5477 (va
- entry
->vme_start
)),
5479 FALSE
, /* page not looked up */
5480 &prot
, &result_page
, &top_page
,
5482 NULL
, map
->no_zero_fill
,
5483 FALSE
, &fault_info
);
5484 } while (result
== VM_FAULT_RETRY
);
5487 * If this was a mapping to a file on a device that has been forcibly
5488 * unmounted, then we won't get a page back from vm_fault_page(). Just
5489 * move on to the next one in case the remaining pages are mapped from
5490 * different objects. During a forced unmount, the object is terminated
5491 * so the alive flag will be false if this happens. A forced unmount will
5492 * will occur when an external disk is unplugged before the user does an
5493 * eject, so we don't want to panic in that situation.
5496 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
) {
5500 if (result
== VM_FAULT_MEMORY_ERROR
&&
5501 object
== kernel_object
) {
5503 * This must have been allocated with
5504 * KMA_KOBJECT and KMA_VAONLY and there's
5505 * no physical page at this offset.
5506 * We're done (no page to free).
5512 if (result
!= VM_FAULT_SUCCESS
) {
5513 panic("vm_fault_unwire: failure");
5516 result_object
= VM_PAGE_OBJECT(result_page
);
5519 assert(VM_PAGE_GET_PHYS_PAGE(result_page
) !=
5520 vm_page_fictitious_addr
);
5521 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page
));
5522 if (VM_PAGE_WIRED(result_page
)) {
5525 VM_PAGE_FREE(result_page
);
5527 if ((pmap
) && (VM_PAGE_GET_PHYS_PAGE(result_page
) != vm_page_guard_addr
)) {
5528 pmap_change_wiring(pmap
,
5529 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5533 if (VM_PAGE_WIRED(result_page
)) {
5534 vm_page_lockspin_queues();
5535 vm_page_unwire(result_page
, TRUE
);
5536 vm_page_unlock_queues();
5539 if (entry
->zero_wired_pages
) {
5540 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page
));
5541 entry
->zero_wired_pages
= FALSE
;
5544 PAGE_WAKEUP_DONE(result_page
);
5546 vm_fault_cleanup(result_object
, top_page
);
5551 * Inform the physical mapping system that the range
5552 * of addresses may fault, so that page tables and
5553 * such may be unwired themselves.
5556 pmap_pageable(pmap
, pmap_addr
,
5557 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
5559 if (kernel_object
== object
) {
5560 vm_tag_update_size(fault_info
.user_tag
, -ptoa_64(unwired_pages
));
5565 * vm_fault_wire_fast:
5567 * Handle common case of a wire down page fault at the given address.
5568 * If successful, the page is inserted into the associated physical map.
5569 * The map entry is passed in to avoid the overhead of a map lookup.
5571 * NOTE: the given address should be truncated to the
5572 * proper page address.
5574 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
5575 * a standard error specifying why the fault is fatal is returned.
5577 * The map in question must be referenced, and remains so.
5578 * Caller has a read lock on the map.
5580 * This is a stripped version of vm_fault() for wiring pages. Anything
5581 * other than the common case will return KERN_FAILURE, and the caller
5582 * is expected to call vm_fault().
5584 static kern_return_t
5586 __unused vm_map_t map
,
5588 __unused vm_prot_t caller_prot
,
5590 vm_map_entry_t entry
,
5592 vm_map_offset_t pmap_addr
,
5593 ppnum_t
*physpage_p
)
5596 vm_object_offset_t offset
;
5599 thread_t thread
= current_thread();
5602 struct vm_object_fault_info fault_info
= {};
5604 VM_STAT_INCR(faults
);
5606 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
) {
5607 thread
->task
->faults
++;
5615 #define RELEASE_PAGE(m) { \
5616 PAGE_WAKEUP_DONE(m); \
5617 vm_page_lockspin_queues(); \
5618 vm_page_unwire(m, TRUE); \
5619 vm_page_unlock_queues(); \
5623 #undef UNLOCK_THINGS
5624 #define UNLOCK_THINGS { \
5625 vm_object_paging_end(object); \
5626 vm_object_unlock(object); \
5629 #undef UNLOCK_AND_DEALLOCATE
5630 #define UNLOCK_AND_DEALLOCATE { \
5632 vm_object_deallocate(object); \
5635 * Give up and have caller do things the hard way.
5639 UNLOCK_AND_DEALLOCATE; \
5640 return(KERN_FAILURE); \
5645 * If this entry is not directly to a vm_object, bail out.
5647 if (entry
->is_sub_map
) {
5648 assert(physpage_p
== NULL
);
5649 return KERN_FAILURE
;
5653 * Find the backing store object and offset into it.
5656 object
= VME_OBJECT(entry
);
5657 offset
= (va
- entry
->vme_start
) + VME_OFFSET(entry
);
5658 prot
= entry
->protection
;
5661 * Make a reference to this object to prevent its
5662 * disposal while we are messing with it.
5665 vm_object_lock(object
);
5666 vm_object_reference_locked(object
);
5667 vm_object_paging_begin(object
);
5670 * INVARIANTS (through entire routine):
5672 * 1) At all times, we must either have the object
5673 * lock or a busy page in some object to prevent
5674 * some other thread from trying to bring in
5677 * 2) Once we have a busy page, we must remove it from
5678 * the pageout queues, so that the pageout daemon
5679 * will not grab it away.
5684 * Look for page in top-level object. If it's not there or
5685 * there's something going on, give up.
5687 m
= vm_page_lookup(object
, offset
);
5688 if ((m
== VM_PAGE_NULL
) || (m
->vmp_busy
) ||
5689 (m
->vmp_unusual
&& (m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_absent
))) {
5692 if (m
->vmp_fictitious
&&
5693 VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
5695 * Guard pages are fictitious pages and are never
5696 * entered into a pmap, so let's say it's been wired...
5703 * Wire the page down now. All bail outs beyond this
5704 * point must unwire the page.
5707 vm_page_lockspin_queues();
5708 vm_page_wire(m
, wire_tag
, TRUE
);
5709 vm_page_unlock_queues();
5712 * Mark page busy for other threads.
5714 assert(!m
->vmp_busy
);
5716 assert(!m
->vmp_absent
);
5719 * Give up if the page is being written and there's a copy object
5721 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
5726 fault_info
.user_tag
= VME_ALIAS(entry
);
5727 fault_info
.pmap_options
= 0;
5728 if (entry
->iokit_acct
||
5729 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
5730 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
5734 * Put this page into the physical map.
5736 type_of_fault
= DBG_CACHE_HIT_FAULT
;
5737 kr
= vm_fault_enter(m
,
5743 FALSE
, /* change_wiring */
5748 if (kr
!= KERN_SUCCESS
) {
5755 * Unlock everything, and return
5759 /* for vm_map_wire_and_extract() */
5760 if (kr
== KERN_SUCCESS
) {
5761 assert(object
== VM_PAGE_OBJECT(m
));
5762 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
5763 if (prot
& VM_PROT_WRITE
) {
5764 vm_object_lock_assert_exclusive(object
);
5765 m
->vmp_dirty
= TRUE
;
5772 PAGE_WAKEUP_DONE(m
);
5773 UNLOCK_AND_DEALLOCATE
;
5779 * Routine: vm_fault_copy_cleanup
5781 * Release a page used by vm_fault_copy.
5785 vm_fault_copy_cleanup(
5789 vm_object_t object
= VM_PAGE_OBJECT(page
);
5791 vm_object_lock(object
);
5792 PAGE_WAKEUP_DONE(page
);
5793 if (!VM_PAGE_PAGEABLE(page
)) {
5794 vm_page_lockspin_queues();
5795 if (!VM_PAGE_PAGEABLE(page
)) {
5796 vm_page_activate(page
);
5798 vm_page_unlock_queues();
5800 vm_fault_cleanup(object
, top_page
);
5804 vm_fault_copy_dst_cleanup(
5809 if (page
!= VM_PAGE_NULL
) {
5810 object
= VM_PAGE_OBJECT(page
);
5811 vm_object_lock(object
);
5812 vm_page_lockspin_queues();
5813 vm_page_unwire(page
, TRUE
);
5814 vm_page_unlock_queues();
5815 vm_object_paging_end(object
);
5816 vm_object_unlock(object
);
5821 * Routine: vm_fault_copy
5824 * Copy pages from one virtual memory object to another --
5825 * neither the source nor destination pages need be resident.
5827 * Before actually copying a page, the version associated with
5828 * the destination address map wil be verified.
5830 * In/out conditions:
5831 * The caller must hold a reference, but not a lock, to
5832 * each of the source and destination objects and to the
5836 * Returns KERN_SUCCESS if no errors were encountered in
5837 * reading or writing the data. Returns KERN_INTERRUPTED if
5838 * the operation was interrupted (only possible if the
5839 * "interruptible" argument is asserted). Other return values
5840 * indicate a permanent error in copying the data.
5842 * The actual amount of data copied will be returned in the
5843 * "copy_size" argument. In the event that the destination map
5844 * verification failed, this amount may be less than the amount
5849 vm_object_t src_object
,
5850 vm_object_offset_t src_offset
,
5851 vm_map_size_t
*copy_size
, /* INOUT */
5852 vm_object_t dst_object
,
5853 vm_object_offset_t dst_offset
,
5855 vm_map_version_t
*dst_version
,
5858 vm_page_t result_page
;
5861 vm_page_t src_top_page
;
5865 vm_page_t dst_top_page
;
5868 vm_map_size_t amount_left
;
5869 vm_object_t old_copy_object
;
5870 vm_object_t result_page_object
= NULL
;
5871 kern_return_t error
= 0;
5872 vm_fault_return_t result
;
5874 vm_map_size_t part_size
;
5875 struct vm_object_fault_info fault_info_src
= {};
5876 struct vm_object_fault_info fault_info_dst
= {};
5879 * In order not to confuse the clustered pageins, align
5880 * the different offsets on a page boundary.
5885 *copy_size -= amount_left; \
5889 amount_left
= *copy_size
;
5891 fault_info_src
.interruptible
= interruptible
;
5892 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5893 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
5894 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
5895 fault_info_src
.stealth
= TRUE
;
5897 fault_info_dst
.interruptible
= interruptible
;
5898 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5899 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
5900 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
5901 fault_info_dst
.stealth
= TRUE
;
5903 do { /* while (amount_left > 0) */
5905 * There may be a deadlock if both source and destination
5906 * pages are the same. To avoid this deadlock, the copy must
5907 * start by getting the destination page in order to apply
5908 * COW semantics if any.
5911 RetryDestinationFault
:;
5913 dst_prot
= VM_PROT_WRITE
| VM_PROT_READ
;
5915 vm_object_lock(dst_object
);
5916 vm_object_paging_begin(dst_object
);
5918 /* cap cluster size at maximum UPL size */
5919 upl_size_t cluster_size
;
5920 if (os_convert_overflow(amount_left
, &cluster_size
)) {
5921 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
5923 fault_info_dst
.cluster_size
= cluster_size
;
5925 dst_page
= VM_PAGE_NULL
;
5926 result
= vm_fault_page(dst_object
,
5927 vm_object_trunc_page(dst_offset
),
5928 VM_PROT_WRITE
| VM_PROT_READ
,
5930 FALSE
, /* page not looked up */
5931 &dst_prot
, &dst_page
, &dst_top_page
,
5934 dst_map
->no_zero_fill
,
5935 FALSE
, &fault_info_dst
);
5937 case VM_FAULT_SUCCESS
:
5939 case VM_FAULT_RETRY
:
5940 goto RetryDestinationFault
;
5941 case VM_FAULT_MEMORY_SHORTAGE
:
5942 if (vm_page_wait(interruptible
)) {
5943 goto RetryDestinationFault
;
5946 case VM_FAULT_INTERRUPTED
:
5947 RETURN(MACH_SEND_INTERRUPTED
);
5948 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5949 /* success but no VM page: fail the copy */
5950 vm_object_paging_end(dst_object
);
5951 vm_object_unlock(dst_object
);
5953 case VM_FAULT_MEMORY_ERROR
:
5957 return KERN_MEMORY_ERROR
;
5960 panic("vm_fault_copy: unexpected error 0x%x from "
5961 "vm_fault_page()\n", result
);
5963 assert((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
5965 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
5966 old_copy_object
= dst_object
->copy
;
5969 * There exists the possiblity that the source and
5970 * destination page are the same. But we can't
5971 * easily determine that now. If they are the
5972 * same, the call to vm_fault_page() for the
5973 * destination page will deadlock. To prevent this we
5974 * wire the page so we can drop busy without having
5975 * the page daemon steal the page. We clean up the
5976 * top page but keep the paging reference on the object
5977 * holding the dest page so it doesn't go away.
5980 vm_page_lockspin_queues();
5981 vm_page_wire(dst_page
, VM_KERN_MEMORY_OSFMK
, TRUE
);
5982 vm_page_unlock_queues();
5983 PAGE_WAKEUP_DONE(dst_page
);
5984 vm_object_unlock(dst_object
);
5986 if (dst_top_page
!= VM_PAGE_NULL
) {
5987 vm_object_lock(dst_object
);
5988 VM_PAGE_FREE(dst_top_page
);
5989 vm_object_paging_end(dst_object
);
5990 vm_object_unlock(dst_object
);
5995 if (src_object
== VM_OBJECT_NULL
) {
5997 * No source object. We will just
5998 * zero-fill the page in dst_object.
6000 src_page
= VM_PAGE_NULL
;
6001 result_page
= VM_PAGE_NULL
;
6003 vm_object_lock(src_object
);
6004 src_page
= vm_page_lookup(src_object
,
6005 vm_object_trunc_page(src_offset
));
6006 if (src_page
== dst_page
) {
6007 src_prot
= dst_prot
;
6008 result_page
= VM_PAGE_NULL
;
6010 src_prot
= VM_PROT_READ
;
6011 vm_object_paging_begin(src_object
);
6013 /* cap cluster size at maximum UPL size */
6014 if (os_convert_overflow(amount_left
, &cluster_size
)) {
6015 cluster_size
= 0 - (upl_size_t
)PAGE_SIZE
;
6017 fault_info_src
.cluster_size
= cluster_size
;
6019 result_page
= VM_PAGE_NULL
;
6020 result
= vm_fault_page(
6022 vm_object_trunc_page(src_offset
),
6023 VM_PROT_READ
, FALSE
,
6024 FALSE
, /* page not looked up */
6026 &result_page
, &src_top_page
,
6027 (int *)0, &error
, FALSE
,
6028 FALSE
, &fault_info_src
);
6031 case VM_FAULT_SUCCESS
:
6033 case VM_FAULT_RETRY
:
6034 goto RetrySourceFault
;
6035 case VM_FAULT_MEMORY_SHORTAGE
:
6036 if (vm_page_wait(interruptible
)) {
6037 goto RetrySourceFault
;
6040 case VM_FAULT_INTERRUPTED
:
6041 vm_fault_copy_dst_cleanup(dst_page
);
6042 RETURN(MACH_SEND_INTERRUPTED
);
6043 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
6044 /* success but no VM page: fail */
6045 vm_object_paging_end(src_object
);
6046 vm_object_unlock(src_object
);
6048 case VM_FAULT_MEMORY_ERROR
:
6049 vm_fault_copy_dst_cleanup(dst_page
);
6053 return KERN_MEMORY_ERROR
;
6056 panic("vm_fault_copy(2): unexpected "
6058 "vm_fault_page()\n", result
);
6061 result_page_object
= VM_PAGE_OBJECT(result_page
);
6062 assert((src_top_page
== VM_PAGE_NULL
) ==
6063 (result_page_object
== src_object
));
6065 assert((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
6066 vm_object_unlock(result_page_object
);
6069 vm_map_lock_read(dst_map
);
6071 if (!vm_map_verify(dst_map
, dst_version
)) {
6072 vm_map_unlock_read(dst_map
);
6073 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6074 vm_fault_copy_cleanup(result_page
, src_top_page
);
6076 vm_fault_copy_dst_cleanup(dst_page
);
6079 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
6081 vm_object_lock(dst_object
);
6083 if (dst_object
->copy
!= old_copy_object
) {
6084 vm_object_unlock(dst_object
);
6085 vm_map_unlock_read(dst_map
);
6086 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6087 vm_fault_copy_cleanup(result_page
, src_top_page
);
6089 vm_fault_copy_dst_cleanup(dst_page
);
6092 vm_object_unlock(dst_object
);
6095 * Copy the page, and note that it is dirty
6099 if (!page_aligned(src_offset
) ||
6100 !page_aligned(dst_offset
) ||
6101 !page_aligned(amount_left
)) {
6102 vm_object_offset_t src_po
,
6105 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
6106 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
6108 if (dst_po
> src_po
) {
6109 part_size
= PAGE_SIZE
- dst_po
;
6111 part_size
= PAGE_SIZE
- src_po
;
6113 if (part_size
> (amount_left
)) {
6114 part_size
= amount_left
;
6117 if (result_page
== VM_PAGE_NULL
) {
6118 assert((vm_offset_t
) dst_po
== dst_po
);
6119 assert((vm_size_t
) part_size
== part_size
);
6120 vm_page_part_zero_fill(dst_page
,
6121 (vm_offset_t
) dst_po
,
6122 (vm_size_t
) part_size
);
6124 assert((vm_offset_t
) src_po
== src_po
);
6125 assert((vm_offset_t
) dst_po
== dst_po
);
6126 assert((vm_size_t
) part_size
== part_size
);
6127 vm_page_part_copy(result_page
,
6128 (vm_offset_t
) src_po
,
6130 (vm_offset_t
) dst_po
,
6131 (vm_size_t
)part_size
);
6132 if (!dst_page
->vmp_dirty
) {
6133 vm_object_lock(dst_object
);
6134 SET_PAGE_DIRTY(dst_page
, TRUE
);
6135 vm_object_unlock(dst_object
);
6139 part_size
= PAGE_SIZE
;
6141 if (result_page
== VM_PAGE_NULL
) {
6142 vm_page_zero_fill(dst_page
);
6144 vm_object_lock(result_page_object
);
6145 vm_page_copy(result_page
, dst_page
);
6146 vm_object_unlock(result_page_object
);
6148 if (!dst_page
->vmp_dirty
) {
6149 vm_object_lock(dst_object
);
6150 SET_PAGE_DIRTY(dst_page
, TRUE
);
6151 vm_object_unlock(dst_object
);
6157 * Unlock everything, and return
6160 vm_map_unlock_read(dst_map
);
6162 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
) {
6163 vm_fault_copy_cleanup(result_page
, src_top_page
);
6165 vm_fault_copy_dst_cleanup(dst_page
);
6167 amount_left
-= part_size
;
6168 src_offset
+= part_size
;
6169 dst_offset
+= part_size
;
6170 } while (amount_left
> 0);
6172 RETURN(KERN_SUCCESS
);
6178 #if VM_FAULT_CLASSIFY
6180 * Temporary statistics gathering support.
6184 * Statistics arrays:
6186 #define VM_FAULT_TYPES_MAX 5
6187 #define VM_FAULT_LEVEL_MAX 8
6189 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
6191 #define VM_FAULT_TYPE_ZERO_FILL 0
6192 #define VM_FAULT_TYPE_MAP_IN 1
6193 #define VM_FAULT_TYPE_PAGER 2
6194 #define VM_FAULT_TYPE_COPY 3
6195 #define VM_FAULT_TYPE_OTHER 4
6199 vm_fault_classify(vm_object_t object
,
6200 vm_object_offset_t offset
,
6201 vm_prot_t fault_type
)
6203 int type
, level
= 0;
6207 m
= vm_page_lookup(object
, offset
);
6208 if (m
!= VM_PAGE_NULL
) {
6209 if (m
->vmp_busy
|| m
->vmp_error
|| m
->vmp_restart
|| m
->vmp_absent
) {
6210 type
= VM_FAULT_TYPE_OTHER
;
6213 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
6214 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
6215 type
= VM_FAULT_TYPE_MAP_IN
;
6218 type
= VM_FAULT_TYPE_COPY
;
6221 if (object
->pager_created
) {
6222 type
= VM_FAULT_TYPE_PAGER
;
6225 if (object
->shadow
== VM_OBJECT_NULL
) {
6226 type
= VM_FAULT_TYPE_ZERO_FILL
;
6230 offset
+= object
->vo_shadow_offset
;
6231 object
= object
->shadow
;
6237 if (level
> VM_FAULT_LEVEL_MAX
) {
6238 level
= VM_FAULT_LEVEL_MAX
;
6241 vm_fault_stats
[type
][level
] += 1;
6246 /* cleanup routine to call from debugger */
6249 vm_fault_classify_init(void)
6253 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
6254 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
6255 vm_fault_stats
[type
][level
] = 0;
6261 #endif /* VM_FAULT_CLASSIFY */
6264 kdp_lightweight_fault(vm_map_t map
, vm_offset_t cur_target_addr
)
6266 vm_map_entry_t entry
;
6268 vm_offset_t object_offset
;
6270 int compressor_external_state
, compressed_count_delta
;
6271 int compressor_flags
= (C_DONT_BLOCK
| C_KEEP
| C_KDP
);
6272 int my_fault_type
= VM_PROT_READ
;
6276 panic("kdp_lightweight_fault called from outside of debugger context");
6279 assert(map
!= VM_MAP_NULL
);
6281 assert((cur_target_addr
& PAGE_MASK
) == 0);
6282 if ((cur_target_addr
& PAGE_MASK
) != 0) {
6286 if (kdp_lck_rw_lock_is_acquired_exclusive(&map
->lock
)) {
6290 if (!vm_map_lookup_entry(map
, cur_target_addr
, &entry
)) {
6294 if (entry
->is_sub_map
) {
6298 object
= VME_OBJECT(entry
);
6299 if (object
== VM_OBJECT_NULL
) {
6303 object_offset
= cur_target_addr
- entry
->vme_start
+ VME_OFFSET(entry
);
6306 if (kdp_lck_rw_lock_is_acquired_exclusive(&object
->Lock
)) {
6310 if (object
->pager_created
&& (object
->paging_in_progress
||
6311 object
->activity_in_progress
)) {
6315 m
= kdp_vm_page_lookup(object
, object_offset
);
6317 if (m
!= VM_PAGE_NULL
) {
6318 if ((object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_DEFAULT
) {
6322 if (m
->vmp_laundry
|| m
->vmp_busy
|| m
->vmp_free_when_done
|| m
->vmp_absent
|| m
->vmp_error
|| m
->vmp_cleaning
||
6323 m
->vmp_overwriting
|| m
->vmp_restart
|| m
->vmp_unusual
) {
6327 assert(!m
->vmp_private
);
6328 if (m
->vmp_private
) {
6332 assert(!m
->vmp_fictitious
);
6333 if (m
->vmp_fictitious
) {
6337 assert(m
->vmp_q_state
!= VM_PAGE_USED_BY_COMPRESSOR
);
6338 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
6342 return ptoa(VM_PAGE_GET_PHYS_PAGE(m
));
6345 compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
6347 if (object
->pager_created
&& MUST_ASK_PAGER(object
, object_offset
, compressor_external_state
)) {
6348 if (compressor_external_state
== VM_EXTERNAL_STATE_EXISTS
) {
6349 kr
= vm_compressor_pager_get(object
->pager
, (object_offset
+ object
->paging_offset
),
6350 kdp_compressor_decompressed_page_ppnum
, &my_fault_type
,
6351 compressor_flags
, &compressed_count_delta
);
6352 if (kr
== KERN_SUCCESS
) {
6353 return kdp_compressor_decompressed_page_paddr
;
6360 if (object
->shadow
== VM_OBJECT_NULL
) {
6364 object_offset
+= object
->vo_shadow_offset
;
6365 object
= object
->shadow
;
6370 * vm_page_validate_cs_fast():
6371 * Performs a few quick checks to determine if the page's code signature
6372 * really needs to be fully validated. It could:
6373 * 1. have been modified (i.e. automatically tainted),
6374 * 2. have already been validated,
6375 * 3. have already been found to be tainted,
6376 * 4. no longer have a backing store.
6377 * Returns FALSE if the page needs to be fully validated.
6380 vm_page_validate_cs_fast(
6385 object
= VM_PAGE_OBJECT(page
);
6386 vm_object_lock_assert_held(object
);
6388 if (page
->vmp_wpmapped
&& !page
->vmp_cs_tainted
) {
6390 * This page was mapped for "write" access sometime in the
6391 * past and could still be modifiable in the future.
6392 * Consider it tainted.
6393 * [ If the page was already found to be "tainted", no
6394 * need to re-validate. ]
6396 vm_object_lock_assert_exclusive(object
);
6397 page
->vmp_cs_validated
= TRUE
;
6398 page
->vmp_cs_tainted
= TRUE
;
6400 printf("CODESIGNING: %s: "
6401 "page %p obj %p off 0x%llx "
6404 page
, object
, page
->vmp_offset
);
6406 vm_cs_validated_dirtied
++;
6409 if (page
->vmp_cs_validated
|| page
->vmp_cs_tainted
) {
6412 vm_object_lock_assert_exclusive(object
);
6414 #if CHECK_CS_VALIDATION_BITMAP
6417 kr
= vnode_pager_cs_check_validation_bitmap(
6419 page
->vmp_offset
+ object
->paging_offset
,
6421 if (kr
== KERN_SUCCESS
) {
6422 page
->vmp_cs_validated
= TRUE
;
6423 page
->vmp_cs_tainted
= FALSE
;
6424 vm_cs_bitmap_validated
++;
6427 #endif /* CHECK_CS_VALIDATION_BITMAP */
6429 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
6431 * The object is terminating and we don't have its pager
6432 * so we can't validate the data...
6437 /* we need to really validate this page */
6438 vm_object_lock_assert_exclusive(object
);
6443 vm_page_validate_cs_mapped_slow(
6448 memory_object_offset_t mo_offset
;
6449 memory_object_t pager
;
6450 struct vnode
*vnode
;
6451 boolean_t validated
;
6454 assert(page
->vmp_busy
);
6455 object
= VM_PAGE_OBJECT(page
);
6456 vm_object_lock_assert_exclusive(object
);
6461 * Since we get here to validate a page that was brought in by
6462 * the pager, we know that this pager is all setup and ready
6465 assert(object
->code_signed
);
6466 assert(!object
->internal
);
6467 assert(object
->pager
!= NULL
);
6468 assert(object
->pager_ready
);
6470 pager
= object
->pager
;
6471 assert(object
->paging_in_progress
);
6472 vnode
= vnode_pager_lookup_vnode(pager
);
6473 mo_offset
= page
->vmp_offset
+ object
->paging_offset
;
6475 /* verify the SHA1 hash for this page */
6477 validated
= cs_validate_range(vnode
,
6480 (const void *)((const char *)kaddr
),
6484 if (tainted
& CS_VALIDATE_TAINTED
) {
6485 page
->vmp_cs_tainted
= TRUE
;
6487 if (tainted
& CS_VALIDATE_NX
) {
6488 page
->vmp_cs_nx
= TRUE
;
6491 page
->vmp_cs_validated
= TRUE
;
6494 #if CHECK_CS_VALIDATION_BITMAP
6495 if (page
->vmp_cs_validated
&& !page
->vmp_cs_tainted
) {
6496 vnode_pager_cs_check_validation_bitmap(object
->pager
,
6500 #endif /* CHECK_CS_VALIDATION_BITMAP */
6504 vm_page_validate_cs_mapped(
6508 if (!vm_page_validate_cs_fast(page
)) {
6509 vm_page_validate_cs_mapped_slow(page
, kaddr
);
6514 vm_page_validate_cs(
6518 vm_object_offset_t offset
;
6519 vm_map_offset_t koffset
;
6520 vm_map_size_t ksize
;
6523 boolean_t busy_page
;
6524 boolean_t need_unmap
;
6526 object
= VM_PAGE_OBJECT(page
);
6527 vm_object_lock_assert_held(object
);
6529 if (vm_page_validate_cs_fast(page
)) {
6532 vm_object_lock_assert_exclusive(object
);
6534 assert(object
->code_signed
);
6535 offset
= page
->vmp_offset
;
6537 busy_page
= page
->vmp_busy
;
6539 /* keep page busy while we map (and unlock) the VM object */
6540 page
->vmp_busy
= TRUE
;
6544 * Take a paging reference on the VM object
6545 * to protect it from collapse or bypass,
6546 * and keep it from disappearing too.
6548 vm_object_paging_begin(object
);
6550 /* map the page in the kernel address space */
6551 ksize
= PAGE_SIZE_64
;
6554 kr
= vm_paging_map_object(page
,
6558 FALSE
, /* can't unlock object ! */
6562 if (kr
!= KERN_SUCCESS
) {
6563 panic("%s: could not map page: 0x%x\n", __FUNCTION__
, kr
);
6565 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
6567 /* validate the mapped page */
6568 vm_page_validate_cs_mapped_slow(page
, (const void *) kaddr
);
6570 assert(page
->vmp_busy
);
6571 assert(object
== VM_PAGE_OBJECT(page
));
6572 vm_object_lock_assert_exclusive(object
);
6575 PAGE_WAKEUP_DONE(page
);
6578 /* unmap the map from the kernel address space */
6579 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
6584 vm_object_paging_end(object
);
6588 vm_page_validate_cs_mapped_chunk(
6591 vm_offset_t chunk_offset
,
6592 vm_size_t chunk_size
,
6593 boolean_t
*validated_p
,
6594 unsigned *tainted_p
)
6597 vm_object_offset_t offset
, offset_in_page
;
6598 memory_object_t pager
;
6599 struct vnode
*vnode
;
6600 boolean_t validated
;
6603 *validated_p
= FALSE
;
6606 assert(page
->vmp_busy
);
6607 object
= VM_PAGE_OBJECT(page
);
6608 vm_object_lock_assert_exclusive(object
);
6610 assert(object
->code_signed
);
6611 offset
= page
->vmp_offset
;
6613 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
6615 * The object is terminating and we don't have its pager
6616 * so we can't validate the data...
6621 * Since we get here to validate a page that was brought in by
6622 * the pager, we know that this pager is all setup and ready
6625 assert(!object
->internal
);
6626 assert(object
->pager
!= NULL
);
6627 assert(object
->pager_ready
);
6629 pager
= object
->pager
;
6630 assert(object
->paging_in_progress
);
6631 vnode
= vnode_pager_lookup_vnode(pager
);
6633 /* verify the signature for this chunk */
6634 offset_in_page
= chunk_offset
;
6635 assert(offset_in_page
< PAGE_SIZE
);
6638 validated
= cs_validate_range(vnode
,
6640 (object
->paging_offset
+
6643 (const void *)((const char *)kaddr
6648 *validated_p
= TRUE
;
6651 *tainted_p
= tainted
;
6656 vm_rtfrecord_lock(void)
6658 lck_spin_lock(&vm_rtfr_slock
);
6662 vm_rtfrecord_unlock(void)
6664 lck_spin_unlock(&vm_rtfr_slock
);
6668 vmrtfaultinfo_bufsz(void)
6670 return vmrtf_num_records
* sizeof(vm_rtfault_record_t
);
6673 #include <kern/backtrace.h>
6676 vm_record_rtfault(thread_t cthread
, uint64_t fstart
, vm_map_offset_t fault_vaddr
, int type_of_fault
)
6678 uint64_t fend
= mach_continuous_time();
6681 uint64_t ctid
= cthread
->thread_id
;
6682 uint64_t cupid
= get_current_unique_pid();
6688 /* Capture a single-frame backtrace; this extracts just the program
6689 * counter at the point of the fault into "bpc", and should perform no
6690 * further user stack traversals, thus avoiding copyin()s and further
6693 unsigned int bfrs
= backtrace_thread_user(cthread
, &bpc
, 1U, &btr
, &u64
, NULL
);
6695 if ((btr
== 0) && (bfrs
> 0)) {
6699 assert((fstart
!= 0) && fend
>= fstart
);
6700 vm_rtfrecord_lock();
6701 assert(vmrtfrs
.vmrtfr_curi
<= vmrtfrs
.vmrtfr_maxi
);
6703 vmrtfrs
.vmrtf_total
++;
6704 vm_rtfault_record_t
*cvmr
= &vmrtfrs
.vm_rtf_records
[vmrtfrs
.vmrtfr_curi
++];
6706 cvmr
->rtfabstime
= fstart
;
6707 cvmr
->rtfduration
= fend
- fstart
;
6708 cvmr
->rtfaddr
= fault_vaddr
;
6710 cvmr
->rtftype
= type_of_fault
;
6711 cvmr
->rtfupid
= cupid
;
6712 cvmr
->rtftid
= ctid
;
6714 if (vmrtfrs
.vmrtfr_curi
> vmrtfrs
.vmrtfr_maxi
) {
6715 vmrtfrs
.vmrtfr_curi
= 0;
6718 vm_rtfrecord_unlock();
6722 vmrtf_extract(uint64_t cupid
, __unused boolean_t isroot
, int vrecordsz
, void *vrecords
, int *vmrtfrv
)
6724 vm_rtfault_record_t
*cvmrd
= vrecords
;
6725 size_t residue
= vrecordsz
;
6726 int numextracted
= 0;
6727 boolean_t early_exit
= FALSE
;
6729 vm_rtfrecord_lock();
6731 for (int vmfi
= 0; vmfi
<= vmrtfrs
.vmrtfr_maxi
; vmfi
++) {
6732 if (residue
< sizeof(vm_rtfault_record_t
)) {
6737 if (vmrtfrs
.vm_rtf_records
[vmfi
].rtfupid
!= cupid
) {
6738 #if DEVELOPMENT || DEBUG
6739 if (isroot
== FALSE
) {
6744 #endif /* DEVDEBUG */
6747 *cvmrd
= vmrtfrs
.vm_rtf_records
[vmfi
];
6749 residue
-= sizeof(vm_rtfault_record_t
);
6753 vm_rtfrecord_unlock();
6755 *vmrtfrv
= numextracted
;