xref: /xnu-10002.41.9/osfmk/vm/vm_fault.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1 /*
2  * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34  * All Rights Reserved.
35  *
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.
41  *
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.
45  *
46  * Carnegie Mellon requests users of this software to return to
47  *
48  *  Software Distribution Coordinator  or  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 /*
57  */
58 /*
59  *	File:	vm_fault.c
60  *	Author:	Avadis Tevanian, Jr., Michael Wayne Young
61  *
62  *	Page fault handling module.
63  */
64 
65 #include <libkern/OSAtomic.h>
66 
67 #include <mach/mach_types.h>
68 #include <mach/kern_return.h>
69 #include <mach/message.h>       /* for error codes */
70 #include <mach/vm_param.h>
71 #include <mach/vm_behavior.h>
72 #include <mach/memory_object.h>
73 /* For memory_object_data_{request,unlock} */
74 #include <mach/sdt.h>
75 
76 #include <kern/kern_types.h>
77 #include <kern/host_statistics.h>
78 #include <kern/counter.h>
79 #include <kern/task.h>
80 #include <kern/thread.h>
81 #include <kern/sched_prim.h>
82 #include <kern/host.h>
83 #include <kern/mach_param.h>
84 #include <kern/macro_help.h>
85 #include <kern/zalloc_internal.h>
86 #include <kern/misc_protos.h>
87 #include <kern/policy_internal.h>
88 
89 #include <vm/vm_compressor.h>
90 #include <vm/vm_compressor_pager.h>
91 #include <vm/vm_fault.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_kern.h>
96 #include <vm/pmap.h>
97 #include <vm/vm_pageout.h>
98 #include <vm/vm_protos.h>
99 #include <vm/vm_external.h>
100 #include <vm/memory_object.h>
101 #include <vm/vm_purgeable_internal.h>   /* Needed by some vm_page.h macros */
102 #include <vm/vm_shared_region.h>
103 
104 #include <sys/codesign.h>
105 #include <sys/code_signing.h>
106 #include <sys/reason.h>
107 #include <sys/signalvar.h>
108 
109 #include <sys/kdebug_triage.h>
110 
111 #include <san/kasan.h>
112 #include <libkern/coreanalytics/coreanalytics.h>
113 
114 #define VM_FAULT_CLASSIFY       0
115 
116 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
117 
118 int vm_protect_privileged_from_untrusted = 1;
119 
120 unsigned int    vm_object_pagein_throttle = 16;
121 
122 /*
123  * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
124  * kicks in when swap space runs out.  64-bit programs have massive address spaces and can leak enormous amounts
125  * of memory if they're buggy and can run the system completely out of swap space.  If this happens, we
126  * impose a hard throttle on them to prevent them from taking the last bit of memory left.  This helps
127  * keep the UI active so that the user has a chance to kill the offending task before the system
128  * completely hangs.
129  *
130  * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
131  * to tasks that appear to be bloated.  When swap runs out, any task using more than vm_hard_throttle_threshold
132  * will be throttled.  The throttling is done by giving the thread that's trying to demand zero a page a
133  * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
134  */
135 
136 extern void throttle_lowpri_io(int);
137 
138 extern struct vnode *vnode_pager_lookup_vnode(memory_object_t);
139 
140 uint64_t vm_hard_throttle_threshold;
141 
142 #if DEBUG || DEVELOPMENT
143 static bool vmtc_panic_instead = false;
144 int panic_object_not_alive = 1;
145 #endif /* DEBUG || DEVELOPMENT */
146 
147 OS_ALWAYS_INLINE
148 boolean_t
NEED_TO_HARD_THROTTLE_THIS_TASK(void)149 NEED_TO_HARD_THROTTLE_THIS_TASK(void)
150 {
151 	return vm_wants_task_throttled(current_task()) ||
152 	       ((vm_page_free_count < vm_page_throttle_limit ||
153 	       HARD_THROTTLE_LIMIT_REACHED()) &&
154 	       proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO) >= THROTTLE_LEVEL_THROTTLED);
155 }
156 
157 #define HARD_THROTTLE_DELAY     10000   /* 10000 us == 10 ms */
158 #define SOFT_THROTTLE_DELAY     200     /* 200 us == .2 ms */
159 
160 #define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS   6
161 #define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC  20000
162 
163 
164 #define VM_STAT_DECOMPRESSIONS()        \
165 MACRO_BEGIN                             \
166 	counter_inc(&vm_statistics_decompressions); \
167 	current_thread()->decompressions++; \
168 MACRO_END
169 
170 boolean_t current_thread_aborted(void);
171 
172 /* Forward declarations of internal routines. */
173 static kern_return_t vm_fault_wire_fast(
174 	vm_map_t        map,
175 	vm_map_offset_t va,
176 	vm_prot_t       prot,
177 	vm_tag_t        wire_tag,
178 	vm_map_entry_t  entry,
179 	pmap_t          pmap,
180 	vm_map_offset_t pmap_addr,
181 	ppnum_t         *physpage_p);
182 
183 static kern_return_t vm_fault_internal(
184 	vm_map_t        map,
185 	vm_map_offset_t vaddr,
186 	vm_prot_t       caller_prot,
187 	boolean_t       change_wiring,
188 	vm_tag_t        wire_tag,
189 	int             interruptible,
190 	pmap_t          pmap,
191 	vm_map_offset_t pmap_addr,
192 	ppnum_t         *physpage_p);
193 
194 static void vm_fault_copy_cleanup(
195 	vm_page_t       page,
196 	vm_page_t       top_page);
197 
198 static void vm_fault_copy_dst_cleanup(
199 	vm_page_t       page);
200 
201 #if     VM_FAULT_CLASSIFY
202 extern void vm_fault_classify(vm_object_t       object,
203     vm_object_offset_t    offset,
204     vm_prot_t             fault_type);
205 
206 extern void vm_fault_classify_init(void);
207 #endif
208 
209 unsigned long vm_pmap_enter_blocked = 0;
210 unsigned long vm_pmap_enter_retried = 0;
211 
212 unsigned long vm_cs_validates = 0;
213 unsigned long vm_cs_revalidates = 0;
214 unsigned long vm_cs_query_modified = 0;
215 unsigned long vm_cs_validated_dirtied = 0;
216 unsigned long vm_cs_bitmap_validated = 0;
217 
218 #if CODE_SIGNING_MONITOR
219 uint64_t vm_cs_defer_to_csm = 0;
220 uint64_t vm_cs_defer_to_csm_not = 0;
221 #endif /* CODE_SIGNING_MONITOR */
222 
223 void vm_pre_fault(vm_map_offset_t, vm_prot_t);
224 
225 extern char *kdp_compressor_decompressed_page;
226 extern addr64_t kdp_compressor_decompressed_page_paddr;
227 extern ppnum_t  kdp_compressor_decompressed_page_ppnum;
228 
229 struct vmrtfr {
230 	int vmrtfr_maxi;
231 	int vmrtfr_curi;
232 	int64_t vmrtf_total;
233 	vm_rtfault_record_t *vm_rtf_records;
234 } vmrtfrs;
235 #define VMRTF_DEFAULT_BUFSIZE (4096)
236 #define VMRTF_NUM_RECORDS_DEFAULT (VMRTF_DEFAULT_BUFSIZE / sizeof(vm_rtfault_record_t))
237 TUNABLE(int, vmrtf_num_records, "vm_rtfault_records", VMRTF_NUM_RECORDS_DEFAULT);
238 
239 static void vm_rtfrecord_lock(void);
240 static void vm_rtfrecord_unlock(void);
241 static void vm_record_rtfault(thread_t, uint64_t, vm_map_offset_t, int);
242 
243 extern lck_grp_t vm_page_lck_grp_bucket;
244 extern lck_attr_t vm_page_lck_attr;
245 LCK_SPIN_DECLARE_ATTR(vm_rtfr_slock, &vm_page_lck_grp_bucket, &vm_page_lck_attr);
246 
247 #if DEVELOPMENT || DEBUG
248 extern int madvise_free_debug;
249 extern int madvise_free_debug_sometimes;
250 #endif /* DEVELOPMENT || DEBUG */
251 
252 extern int vm_pageout_protect_realtime;
253 
254 #if CONFIG_FREEZE
255 #endif /* CONFIG_FREEZE */
256 
257 /*
258  *	Routine:	vm_fault_init
259  *	Purpose:
260  *		Initialize our private data structures.
261  */
262 __startup_func
263 void
vm_fault_init(void)264 vm_fault_init(void)
265 {
266 	int i, vm_compressor_temp;
267 	boolean_t need_default_val = TRUE;
268 	/*
269 	 * Choose a value for the hard throttle threshold based on the amount of ram.  The threshold is
270 	 * computed as a percentage of available memory, and the percentage used is scaled inversely with
271 	 * the amount of memory.  The percentage runs between 10% and 35%.  We use 35% for small memory systems
272 	 * and reduce the value down to 10% for very large memory configurations.  This helps give us a
273 	 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
274 	 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
275 	 */
276 
277 	vm_hard_throttle_threshold = sane_size * (35 - MIN((int)(sane_size / (1024 * 1024 * 1024)), 25)) / 100;
278 
279 	/*
280 	 * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry.
281 	 */
282 
283 	if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp, sizeof(vm_compressor_temp))) {
284 		for (i = 0; i < VM_PAGER_MAX_MODES; i++) {
285 			if (((vm_compressor_temp & (1 << i)) == vm_compressor_temp)) {
286 				need_default_val = FALSE;
287 				vm_compressor_mode = vm_compressor_temp;
288 				break;
289 			}
290 		}
291 		if (need_default_val) {
292 			printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp);
293 		}
294 	}
295 #if CONFIG_FREEZE
296 	if (need_default_val) {
297 		if (osenvironment_is_diagnostics()) {
298 			printf("osenvironment == \"diagnostics\". Setting \"vm_compressor_mode\" to in-core compressor only\n");
299 			vm_compressor_mode = VM_PAGER_COMPRESSOR_NO_SWAP;
300 			need_default_val = false;
301 		}
302 	}
303 #endif /* CONFIG_FREEZE */
304 	if (need_default_val) {
305 		/* If no boot arg or incorrect boot arg, try device tree. */
306 		PE_get_default("kern.vm_compressor", &vm_compressor_mode, sizeof(vm_compressor_mode));
307 	}
308 	printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode);
309 	vm_config_init();
310 
311 	PE_parse_boot_argn("vm_protect_privileged_from_untrusted",
312 	    &vm_protect_privileged_from_untrusted,
313 	    sizeof(vm_protect_privileged_from_untrusted));
314 
315 #if DEBUG || DEVELOPMENT
316 	(void)PE_parse_boot_argn("text_corruption_panic", &vmtc_panic_instead, sizeof(vmtc_panic_instead));
317 
318 	if (kern_feature_override(KF_MADVISE_FREE_DEBUG_OVRD)) {
319 		madvise_free_debug = 0;
320 		madvise_free_debug_sometimes = 0;
321 	}
322 
323 	PE_parse_boot_argn("panic_object_not_alive", &panic_object_not_alive, sizeof(panic_object_not_alive));
324 #endif /* DEBUG || DEVELOPMENT */
325 }
326 
327 __startup_func
328 static void
vm_rtfault_record_init(void)329 vm_rtfault_record_init(void)
330 {
331 	size_t size;
332 
333 	vmrtf_num_records = MAX(vmrtf_num_records, 1);
334 	size = vmrtf_num_records * sizeof(vm_rtfault_record_t);
335 	vmrtfrs.vm_rtf_records = zalloc_permanent_tag(size,
336 	    ZALIGN(vm_rtfault_record_t), VM_KERN_MEMORY_DIAG);
337 	vmrtfrs.vmrtfr_maxi = vmrtf_num_records - 1;
338 }
339 STARTUP(ZALLOC, STARTUP_RANK_MIDDLE, vm_rtfault_record_init);
340 
341 /*
342  *	Routine:	vm_fault_cleanup
343  *	Purpose:
344  *		Clean up the result of vm_fault_page.
345  *	Results:
346  *		The paging reference for "object" is released.
347  *		"object" is unlocked.
348  *		If "top_page" is not null,  "top_page" is
349  *		freed and the paging reference for the object
350  *		containing it is released.
351  *
352  *	In/out conditions:
353  *		"object" must be locked.
354  */
355 void
vm_fault_cleanup(vm_object_t object,vm_page_t top_page)356 vm_fault_cleanup(
357 	vm_object_t     object,
358 	vm_page_t       top_page)
359 {
360 	vm_object_paging_end(object);
361 	vm_object_unlock(object);
362 
363 	if (top_page != VM_PAGE_NULL) {
364 		object = VM_PAGE_OBJECT(top_page);
365 
366 		vm_object_lock(object);
367 		VM_PAGE_FREE(top_page);
368 		vm_object_paging_end(object);
369 		vm_object_unlock(object);
370 	}
371 }
372 
373 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
374 
375 
376 boolean_t       vm_page_deactivate_behind = TRUE;
377 /*
378  * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
379  */
380 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW     128
381 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER    16              /* don't make this too big... */
382                                                                 /* we use it to size an array on the stack */
383 
384 int vm_default_behind = VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW;
385 
386 #define MAX_SEQUENTIAL_RUN      (1024 * 1024 * 1024)
387 
388 /*
389  * vm_page_is_sequential
390  *
391  * Determine if sequential access is in progress
392  * in accordance with the behavior specified.
393  * Update state to indicate current access pattern.
394  *
395  * object must have at least the shared lock held
396  */
397 static
398 void
vm_fault_is_sequential(vm_object_t object,vm_object_offset_t offset,vm_behavior_t behavior)399 vm_fault_is_sequential(
400 	vm_object_t             object,
401 	vm_object_offset_t      offset,
402 	vm_behavior_t           behavior)
403 {
404 	vm_object_offset_t      last_alloc;
405 	int                     sequential;
406 	int                     orig_sequential;
407 
408 	last_alloc = object->last_alloc;
409 	sequential = object->sequential;
410 	orig_sequential = sequential;
411 
412 	offset = vm_object_trunc_page(offset);
413 	if (offset == last_alloc && behavior != VM_BEHAVIOR_RANDOM) {
414 		/* re-faulting in the same page: no change in behavior */
415 		return;
416 	}
417 
418 	switch (behavior) {
419 	case VM_BEHAVIOR_RANDOM:
420 		/*
421 		 * reset indicator of sequential behavior
422 		 */
423 		sequential = 0;
424 		break;
425 
426 	case VM_BEHAVIOR_SEQUENTIAL:
427 		if (offset && last_alloc == offset - PAGE_SIZE_64) {
428 			/*
429 			 * advance indicator of sequential behavior
430 			 */
431 			if (sequential < MAX_SEQUENTIAL_RUN) {
432 				sequential += PAGE_SIZE;
433 			}
434 		} else {
435 			/*
436 			 * reset indicator of sequential behavior
437 			 */
438 			sequential = 0;
439 		}
440 		break;
441 
442 	case VM_BEHAVIOR_RSEQNTL:
443 		if (last_alloc && last_alloc == offset + PAGE_SIZE_64) {
444 			/*
445 			 * advance indicator of sequential behavior
446 			 */
447 			if (sequential > -MAX_SEQUENTIAL_RUN) {
448 				sequential -= PAGE_SIZE;
449 			}
450 		} else {
451 			/*
452 			 * reset indicator of sequential behavior
453 			 */
454 			sequential = 0;
455 		}
456 		break;
457 
458 	case VM_BEHAVIOR_DEFAULT:
459 	default:
460 		if (offset && last_alloc == (offset - PAGE_SIZE_64)) {
461 			/*
462 			 * advance indicator of sequential behavior
463 			 */
464 			if (sequential < 0) {
465 				sequential = 0;
466 			}
467 			if (sequential < MAX_SEQUENTIAL_RUN) {
468 				sequential += PAGE_SIZE;
469 			}
470 		} else if (last_alloc && last_alloc == (offset + PAGE_SIZE_64)) {
471 			/*
472 			 * advance indicator of sequential behavior
473 			 */
474 			if (sequential > 0) {
475 				sequential = 0;
476 			}
477 			if (sequential > -MAX_SEQUENTIAL_RUN) {
478 				sequential -= PAGE_SIZE;
479 			}
480 		} else {
481 			/*
482 			 * reset indicator of sequential behavior
483 			 */
484 			sequential = 0;
485 		}
486 		break;
487 	}
488 	if (sequential != orig_sequential) {
489 		if (!OSCompareAndSwap(orig_sequential, sequential, (UInt32 *)&object->sequential)) {
490 			/*
491 			 * if someone else has already updated object->sequential
492 			 * don't bother trying to update it or object->last_alloc
493 			 */
494 			return;
495 		}
496 	}
497 	/*
498 	 * I'd like to do this with a OSCompareAndSwap64, but that
499 	 * doesn't exist for PPC...  however, it shouldn't matter
500 	 * that much... last_alloc is maintained so that we can determine
501 	 * if a sequential access pattern is taking place... if only
502 	 * one thread is banging on this object, no problem with the unprotected
503 	 * update... if 2 or more threads are banging away, we run the risk of
504 	 * someone seeing a mangled update... however, in the face of multiple
505 	 * accesses, no sequential access pattern can develop anyway, so we
506 	 * haven't lost any real info.
507 	 */
508 	object->last_alloc = offset;
509 }
510 
511 #if DEVELOPMENT || DEBUG
512 uint64_t vm_page_deactivate_behind_count = 0;
513 #endif /* DEVELOPMENT || DEBUG */
514 
515 /*
516  * vm_page_deactivate_behind
517  *
518  * Determine if sequential access is in progress
519  * in accordance with the behavior specified.  If
520  * so, compute a potential page to deactivate and
521  * deactivate it.
522  *
523  * object must be locked.
524  *
525  * return TRUE if we actually deactivate a page
526  */
527 static
528 boolean_t
vm_fault_deactivate_behind(vm_object_t object,vm_object_offset_t offset,vm_behavior_t behavior)529 vm_fault_deactivate_behind(
530 	vm_object_t             object,
531 	vm_object_offset_t      offset,
532 	vm_behavior_t           behavior)
533 {
534 	int             n;
535 	int             pages_in_run = 0;
536 	int             max_pages_in_run = 0;
537 	int             sequential_run;
538 	int             sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
539 	vm_object_offset_t      run_offset = 0;
540 	vm_object_offset_t      pg_offset = 0;
541 	vm_page_t       m;
542 	vm_page_t       page_run[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER];
543 
544 	pages_in_run = 0;
545 #if TRACEFAULTPAGE
546 	dbgTrace(0xBEEF0018, (unsigned int) object, (unsigned int) vm_fault_deactivate_behind); /* (TEST/DEBUG) */
547 #endif
548 	if (is_kernel_object(object) || vm_page_deactivate_behind == FALSE || (vm_object_trunc_page(offset) != offset)) {
549 		/*
550 		 * Do not deactivate pages from the kernel object: they
551 		 * are not intended to become pageable.
552 		 * or we've disabled the deactivate behind mechanism
553 		 * or we are dealing with an offset that is not aligned to
554 		 * the system's PAGE_SIZE because in that case we will
555 		 * handle the deactivation on the aligned offset and, thus,
556 		 * the full PAGE_SIZE page once. This helps us avoid the redundant
557 		 * deactivates and the extra faults.
558 		 */
559 		return FALSE;
560 	}
561 	if ((sequential_run = object->sequential)) {
562 		if (sequential_run < 0) {
563 			sequential_behavior = VM_BEHAVIOR_RSEQNTL;
564 			sequential_run = 0 - sequential_run;
565 		} else {
566 			sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
567 		}
568 	}
569 	switch (behavior) {
570 	case VM_BEHAVIOR_RANDOM:
571 		break;
572 	case VM_BEHAVIOR_SEQUENTIAL:
573 		if (sequential_run >= (int)PAGE_SIZE) {
574 			run_offset = 0 - PAGE_SIZE_64;
575 			max_pages_in_run = 1;
576 		}
577 		break;
578 	case VM_BEHAVIOR_RSEQNTL:
579 		if (sequential_run >= (int)PAGE_SIZE) {
580 			run_offset = PAGE_SIZE_64;
581 			max_pages_in_run = 1;
582 		}
583 		break;
584 	case VM_BEHAVIOR_DEFAULT:
585 	default:
586 	{       vm_object_offset_t behind = vm_default_behind * PAGE_SIZE_64;
587 
588 		/*
589 		 * determine if the run of sequential accesss has been
590 		 * long enough on an object with default access behavior
591 		 * to consider it for deactivation
592 		 */
593 		if ((uint64_t)sequential_run >= behind && (sequential_run % (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER * PAGE_SIZE)) == 0) {
594 			/*
595 			 * the comparisons between offset and behind are done
596 			 * in this kind of odd fashion in order to prevent wrap around
597 			 * at the end points
598 			 */
599 			if (sequential_behavior == VM_BEHAVIOR_SEQUENTIAL) {
600 				if (offset >= behind) {
601 					run_offset = 0 - behind;
602 					pg_offset = PAGE_SIZE_64;
603 					max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER;
604 				}
605 			} else {
606 				if (offset < -behind) {
607 					run_offset = behind;
608 					pg_offset = 0 - PAGE_SIZE_64;
609 					max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER;
610 				}
611 			}
612 		}
613 		break;}
614 	}
615 	for (n = 0; n < max_pages_in_run; n++) {
616 		m = vm_page_lookup(object, offset + run_offset + (n * pg_offset));
617 
618 		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) {
619 			page_run[pages_in_run++] = m;
620 
621 			/*
622 			 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
623 			 *
624 			 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
625 			 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
626 			 * new reference happens. If no futher references happen on the page after that remote TLB flushes
627 			 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
628 			 * by pageout_scan, which is just fine since the last reference would have happened quite far
629 			 * in the past (TLB caches don't hang around for very long), and of course could just as easily
630 			 * have happened before we did the deactivate_behind.
631 			 */
632 			pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m), VM_MEM_REFERENCED, PMAP_OPTIONS_NOFLUSH, (void *)NULL);
633 		}
634 	}
635 	if (pages_in_run) {
636 		vm_page_lockspin_queues();
637 
638 		for (n = 0; n < pages_in_run; n++) {
639 			m = page_run[n];
640 
641 			vm_page_deactivate_internal(m, FALSE);
642 
643 #if DEVELOPMENT || DEBUG
644 			vm_page_deactivate_behind_count++;
645 #endif /* DEVELOPMENT || DEBUG */
646 
647 #if TRACEFAULTPAGE
648 			dbgTrace(0xBEEF0019, (unsigned int) object, (unsigned int) m);  /* (TEST/DEBUG) */
649 #endif
650 		}
651 		vm_page_unlock_queues();
652 
653 		return TRUE;
654 	}
655 	return FALSE;
656 }
657 
658 
659 #if (DEVELOPMENT || DEBUG)
660 uint32_t        vm_page_creation_throttled_hard = 0;
661 uint32_t        vm_page_creation_throttled_soft = 0;
662 uint64_t        vm_page_creation_throttle_avoided = 0;
663 #endif /* DEVELOPMENT || DEBUG */
664 
665 static int
vm_page_throttled(boolean_t page_kept)666 vm_page_throttled(boolean_t page_kept)
667 {
668 	clock_sec_t     elapsed_sec;
669 	clock_sec_t     tv_sec;
670 	clock_usec_t    tv_usec;
671 	task_t          curtask = current_task_early();
672 
673 	thread_t thread = current_thread();
674 
675 	if (thread->options & TH_OPT_VMPRIV) {
676 		return 0;
677 	}
678 
679 	if (curtask && !curtask->active) {
680 		return 0;
681 	}
682 
683 	if (thread->t_page_creation_throttled) {
684 		thread->t_page_creation_throttled = 0;
685 
686 		if (page_kept == FALSE) {
687 			goto no_throttle;
688 		}
689 	}
690 	if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
691 #if (DEVELOPMENT || DEBUG)
692 		thread->t_page_creation_throttled_hard++;
693 		OSAddAtomic(1, &vm_page_creation_throttled_hard);
694 #endif /* DEVELOPMENT || DEBUG */
695 		return HARD_THROTTLE_DELAY;
696 	}
697 
698 	if ((vm_page_free_count < vm_page_throttle_limit || (VM_CONFIG_COMPRESSOR_IS_PRESENT && SWAPPER_NEEDS_TO_UNTHROTTLE())) &&
699 	    thread->t_page_creation_count > (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS * VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC)) {
700 		if (vm_page_free_wanted == 0 && vm_page_free_wanted_privileged == 0) {
701 #if (DEVELOPMENT || DEBUG)
702 			OSAddAtomic64(1, &vm_page_creation_throttle_avoided);
703 #endif
704 			goto no_throttle;
705 		}
706 		clock_get_system_microtime(&tv_sec, &tv_usec);
707 
708 		elapsed_sec = tv_sec - thread->t_page_creation_time;
709 
710 		if (elapsed_sec <= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS ||
711 		    (thread->t_page_creation_count / elapsed_sec) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC) {
712 			if (elapsed_sec >= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS)) {
713 				/*
714 				 * we'll reset our stats to give a well behaved app
715 				 * that was unlucky enough to accumulate a bunch of pages
716 				 * over a long period of time a chance to get out of
717 				 * the throttled state... we reset the counter and timestamp
718 				 * so that if it stays under the rate limit for the next second
719 				 * it will be back in our good graces... if it exceeds it, it
720 				 * will remain in the throttled state
721 				 */
722 				thread->t_page_creation_time = tv_sec;
723 				thread->t_page_creation_count = VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC * (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS - 1);
724 			}
725 			VM_PAGEOUT_DEBUG(vm_page_throttle_count, 1);
726 
727 			thread->t_page_creation_throttled = 1;
728 
729 			if (VM_CONFIG_COMPRESSOR_IS_PRESENT && HARD_THROTTLE_LIMIT_REACHED()) {
730 #if (DEVELOPMENT || DEBUG)
731 				thread->t_page_creation_throttled_hard++;
732 				OSAddAtomic(1, &vm_page_creation_throttled_hard);
733 #endif /* DEVELOPMENT || DEBUG */
734 				return HARD_THROTTLE_DELAY;
735 			} else {
736 #if (DEVELOPMENT || DEBUG)
737 				thread->t_page_creation_throttled_soft++;
738 				OSAddAtomic(1, &vm_page_creation_throttled_soft);
739 #endif /* DEVELOPMENT || DEBUG */
740 				return SOFT_THROTTLE_DELAY;
741 			}
742 		}
743 		thread->t_page_creation_time = tv_sec;
744 		thread->t_page_creation_count = 0;
745 	}
746 no_throttle:
747 	thread->t_page_creation_count++;
748 
749 	return 0;
750 }
751 
752 extern boolean_t vm_pageout_running;
753 static __attribute__((noinline, not_tail_called)) void
__VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__(int throttle_delay)754 __VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__(
755 	int throttle_delay)
756 {
757 	/* make sure vm_pageout_scan() gets to work while we're throttled */
758 	if (!vm_pageout_running) {
759 		thread_wakeup((event_t)&vm_page_free_wanted);
760 	}
761 	delay(throttle_delay);
762 }
763 
764 
765 /*
766  * check for various conditions that would
767  * prevent us from creating a ZF page...
768  * cleanup is based on being called from vm_fault_page
769  *
770  * object must be locked
771  * object == m->vmp_object
772  */
773 static vm_fault_return_t
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)774 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)
775 {
776 	int throttle_delay;
777 
778 	if (object->shadow_severed ||
779 	    VM_OBJECT_PURGEABLE_FAULT_ERROR(object)) {
780 		/*
781 		 * Either:
782 		 * 1. the shadow chain was severed,
783 		 * 2. the purgeable object is volatile or empty and is marked
784 		 *    to fault on access while volatile.
785 		 * Just have to return an error at this point
786 		 */
787 		if (m != VM_PAGE_NULL) {
788 			VM_PAGE_FREE(m);
789 		}
790 		vm_fault_cleanup(object, first_m);
791 
792 		thread_interrupt_level(interruptible_state);
793 
794 		if (VM_OBJECT_PURGEABLE_FAULT_ERROR(object)) {
795 			ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PURGEABLE_FAULT_ERROR), 0 /* arg */);
796 		}
797 
798 		if (object->shadow_severed) {
799 			ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_OBJECT_SHADOW_SEVERED), 0 /* arg */);
800 		}
801 		return VM_FAULT_MEMORY_ERROR;
802 	}
803 	if (page_throttle == TRUE) {
804 		if ((throttle_delay = vm_page_throttled(FALSE))) {
805 			/*
806 			 * we're throttling zero-fills...
807 			 * treat this as if we couldn't grab a page
808 			 */
809 			if (m != VM_PAGE_NULL) {
810 				VM_PAGE_FREE(m);
811 			}
812 			vm_fault_cleanup(object, first_m);
813 
814 			VM_DEBUG_EVENT(vmf_check_zfdelay, VMF_CHECK_ZFDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0);
815 
816 			__VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__(throttle_delay);
817 
818 			if (current_thread_aborted()) {
819 				thread_interrupt_level(interruptible_state);
820 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_INTERRUPTED), 0 /* arg */);
821 				return VM_FAULT_INTERRUPTED;
822 			}
823 			thread_interrupt_level(interruptible_state);
824 
825 			return VM_FAULT_MEMORY_SHORTAGE;
826 		}
827 	}
828 	return VM_FAULT_SUCCESS;
829 }
830 
831 /*
832  * Clear the code signing bits on the given page_t
833  */
834 static void
vm_fault_cs_clear(vm_page_t m)835 vm_fault_cs_clear(vm_page_t m)
836 {
837 	m->vmp_cs_validated = VMP_CS_ALL_FALSE;
838 	m->vmp_cs_tainted = VMP_CS_ALL_FALSE;
839 	m->vmp_cs_nx = VMP_CS_ALL_FALSE;
840 }
841 
842 /*
843  * Enqueues the given page on the throttled queue.
844  * The caller must hold the vm_page_queue_lock and it will be held on return.
845  */
846 static void
vm_fault_enqueue_throttled_locked(vm_page_t m)847 vm_fault_enqueue_throttled_locked(vm_page_t m)
848 {
849 	LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED);
850 	assert(!VM_PAGE_WIRED(m));
851 
852 	/*
853 	 * can't be on the pageout queue since we don't
854 	 * have a pager to try and clean to
855 	 */
856 	vm_page_queues_remove(m, TRUE);
857 	vm_page_check_pageable_safe(m);
858 	vm_page_queue_enter(&vm_page_queue_throttled, m, vmp_pageq);
859 	m->vmp_q_state = VM_PAGE_ON_THROTTLED_Q;
860 	vm_page_throttled_count++;
861 }
862 
863 /*
864  * do the work to zero fill a page and
865  * inject it into the correct paging queue
866  *
867  * m->vmp_object must be locked
868  * page queue lock must NOT be held
869  */
870 static int
vm_fault_zero_page(vm_page_t m,boolean_t no_zero_fill)871 vm_fault_zero_page(vm_page_t m, boolean_t no_zero_fill)
872 {
873 	int my_fault = DBG_ZERO_FILL_FAULT;
874 	vm_object_t     object;
875 
876 	object = VM_PAGE_OBJECT(m);
877 
878 	/*
879 	 * This is is a zero-fill page fault...
880 	 *
881 	 * Checking the page lock is a waste of
882 	 * time;  this page was absent, so
883 	 * it can't be page locked by a pager.
884 	 *
885 	 * we also consider it undefined
886 	 * with respect to instruction
887 	 * execution.  i.e. it is the responsibility
888 	 * of higher layers to call for an instruction
889 	 * sync after changing the contents and before
890 	 * sending a program into this area.  We
891 	 * choose this approach for performance
892 	 */
893 	vm_fault_cs_clear(m);
894 	m->vmp_pmapped = TRUE;
895 
896 	if (no_zero_fill == TRUE) {
897 		my_fault = DBG_NZF_PAGE_FAULT;
898 
899 		if (m->vmp_absent && m->vmp_busy) {
900 			return my_fault;
901 		}
902 	} else {
903 		vm_page_zero_fill(m);
904 
905 		counter_inc(&vm_statistics_zero_fill_count);
906 		DTRACE_VM2(zfod, int, 1, (uint64_t *), NULL);
907 	}
908 	assert(!m->vmp_laundry);
909 	assert(!is_kernel_object(object));
910 	//assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0);
911 	if (!VM_DYNAMIC_PAGING_ENABLED() &&
912 	    (object->purgable == VM_PURGABLE_DENY ||
913 	    object->purgable == VM_PURGABLE_NONVOLATILE ||
914 	    object->purgable == VM_PURGABLE_VOLATILE)) {
915 		vm_page_lockspin_queues();
916 		if (!VM_DYNAMIC_PAGING_ENABLED()) {
917 			vm_fault_enqueue_throttled_locked(m);
918 		}
919 		vm_page_unlock_queues();
920 	}
921 	return my_fault;
922 }
923 
924 
925 /*
926  *	Routine:	vm_fault_page
927  *	Purpose:
928  *		Find the resident page for the virtual memory
929  *		specified by the given virtual memory object
930  *		and offset.
931  *	Additional arguments:
932  *		The required permissions for the page is given
933  *		in "fault_type".  Desired permissions are included
934  *		in "protection".
935  *		fault_info is passed along to determine pagein cluster
936  *		limits... it contains the expected reference pattern,
937  *		cluster size if available, etc...
938  *
939  *		If the desired page is known to be resident (for
940  *		example, because it was previously wired down), asserting
941  *		the "unwiring" parameter will speed the search.
942  *
943  *		If the operation can be interrupted (by thread_abort
944  *		or thread_terminate), then the "interruptible"
945  *		parameter should be asserted.
946  *
947  *	Results:
948  *		The page containing the proper data is returned
949  *		in "result_page".
950  *
951  *	In/out conditions:
952  *		The source object must be locked and referenced,
953  *		and must donate one paging reference.  The reference
954  *		is not affected.  The paging reference and lock are
955  *		consumed.
956  *
957  *		If the call succeeds, the object in which "result_page"
958  *		resides is left locked and holding a paging reference.
959  *		If this is not the original object, a busy page in the
960  *		original object is returned in "top_page", to prevent other
961  *		callers from pursuing this same data, along with a paging
962  *		reference for the original object.  The "top_page" should
963  *		be destroyed when this guarantee is no longer required.
964  *		The "result_page" is also left busy.  It is not removed
965  *		from the pageout queues.
966  *	Special Case:
967  *		A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
968  *		fault succeeded but there's no VM page (i.e. the VM object
969  *              does not actually hold VM pages, but device memory or
970  *		large pages).  The object is still locked and we still hold a
971  *		paging_in_progress reference.
972  */
973 unsigned int vm_fault_page_blocked_access = 0;
974 unsigned int vm_fault_page_forced_retry = 0;
975 
976 vm_fault_return_t
vm_fault_page(vm_object_t first_object,vm_object_offset_t first_offset,vm_prot_t fault_type,boolean_t must_be_resident,boolean_t caller_lookup,vm_prot_t * protection,vm_page_t * result_page,vm_page_t * top_page,int * type_of_fault,kern_return_t * error_code,boolean_t no_zero_fill,vm_object_fault_info_t fault_info)977 vm_fault_page(
978 	/* Arguments: */
979 	vm_object_t     first_object,   /* Object to begin search */
980 	vm_object_offset_t first_offset,        /* Offset into object */
981 	vm_prot_t       fault_type,     /* What access is requested */
982 	boolean_t       must_be_resident,/* Must page be resident? */
983 	boolean_t       caller_lookup,  /* caller looked up page */
984 	/* Modifies in place: */
985 	vm_prot_t       *protection,    /* Protection for mapping */
986 	vm_page_t       *result_page,   /* Page found, if successful */
987 	/* Returns: */
988 	vm_page_t       *top_page,      /* Page in top object, if
989                                          * not result_page.  */
990 	int             *type_of_fault, /* if non-null, fill in with type of fault
991                                          * COW, zero-fill, etc... returned in trace point */
992 	/* More arguments: */
993 	kern_return_t   *error_code,    /* code if page is in error */
994 	boolean_t       no_zero_fill,   /* don't zero fill absent pages */
995 	vm_object_fault_info_t fault_info)
996 {
997 	vm_page_t               m;
998 	vm_object_t             object;
999 	vm_object_offset_t      offset;
1000 	vm_page_t               first_m;
1001 	vm_object_t             next_object;
1002 	vm_object_t             copy_object;
1003 	boolean_t               look_for_page;
1004 	boolean_t               force_fault_retry = FALSE;
1005 	vm_prot_t               access_required = fault_type;
1006 	vm_prot_t               wants_copy_flag;
1007 	kern_return_t           wait_result;
1008 	wait_interrupt_t        interruptible_state;
1009 	boolean_t               data_already_requested = FALSE;
1010 	vm_behavior_t           orig_behavior;
1011 	vm_size_t               orig_cluster_size;
1012 	vm_fault_return_t       error;
1013 	int                     my_fault;
1014 	uint32_t                try_failed_count;
1015 	int                     interruptible; /* how may fault be interrupted? */
1016 	int                     external_state = VM_EXTERNAL_STATE_UNKNOWN;
1017 	memory_object_t         pager;
1018 	vm_fault_return_t       retval;
1019 	int                     grab_options;
1020 	bool                    clear_absent_on_error = false;
1021 
1022 /*
1023  * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
1024  * marked as paged out in the compressor pager or the pager doesn't exist.
1025  * Note also that if the pager for an internal object
1026  * has not been created, the pager is not invoked regardless of the value
1027  * of MUST_ASK_PAGER().
1028  *
1029  * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
1030  * is marked as paged out in the compressor pager.
1031  * PAGED_OUT() is used to determine if a page has already been pushed
1032  * into a copy object in order to avoid a redundant page out operation.
1033  */
1034 #define MUST_ASK_PAGER(o, f, s)                                 \
1035 	((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
1036 
1037 #define PAGED_OUT(o, f) \
1038 	(VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
1039 
1040 /*
1041  *	Recovery actions
1042  */
1043 #define RELEASE_PAGE(m)                                 \
1044 	MACRO_BEGIN                                     \
1045 	PAGE_WAKEUP_DONE(m);                            \
1046 	if ( !VM_PAGE_PAGEABLE(m)) {                    \
1047 	        vm_page_lockspin_queues();              \
1048 	        if (clear_absent_on_error && m->vmp_absent) {\
1049 	                vm_page_zero_fill(m);           \
1050 	                counter_inc(&vm_statistics_zero_fill_count);\
1051 	                DTRACE_VM2(zfod, int, 1, (uint64_t *), NULL);\
1052 	                m->vmp_absent = false;          \
1053 	        }                                       \
1054 	        if ( !VM_PAGE_PAGEABLE(m)) {            \
1055 	                if (VM_CONFIG_COMPRESSOR_IS_ACTIVE)     \
1056 	                        vm_page_deactivate(m);          \
1057 	                else                                    \
1058 	                        vm_page_activate(m);            \
1059 	        }                                               \
1060 	        vm_page_unlock_queues();                        \
1061 	}                                                       \
1062 	clear_absent_on_error = false;                  \
1063 	MACRO_END
1064 
1065 #if TRACEFAULTPAGE
1066 	dbgTrace(0xBEEF0002, (unsigned int) first_object, (unsigned int) first_offset); /* (TEST/DEBUG) */
1067 #endif
1068 
1069 	interruptible = fault_info->interruptible;
1070 	interruptible_state = thread_interrupt_level(interruptible);
1071 
1072 	/*
1073 	 *	INVARIANTS (through entire routine):
1074 	 *
1075 	 *	1)	At all times, we must either have the object
1076 	 *		lock or a busy page in some object to prevent
1077 	 *		some other thread from trying to bring in
1078 	 *		the same page.
1079 	 *
1080 	 *		Note that we cannot hold any locks during the
1081 	 *		pager access or when waiting for memory, so
1082 	 *		we use a busy page then.
1083 	 *
1084 	 *	2)	To prevent another thread from racing us down the
1085 	 *		shadow chain and entering a new page in the top
1086 	 *		object before we do, we must keep a busy page in
1087 	 *		the top object while following the shadow chain.
1088 	 *
1089 	 *	3)	We must increment paging_in_progress on any object
1090 	 *		for which we have a busy page before dropping
1091 	 *		the object lock
1092 	 *
1093 	 *	4)	We leave busy pages on the pageout queues.
1094 	 *		If the pageout daemon comes across a busy page,
1095 	 *		it will remove the page from the pageout queues.
1096 	 */
1097 
1098 	object = first_object;
1099 	offset = first_offset;
1100 	first_m = VM_PAGE_NULL;
1101 	access_required = fault_type;
1102 
1103 	/*
1104 	 * default type of fault
1105 	 */
1106 	my_fault = DBG_CACHE_HIT_FAULT;
1107 	thread_pri_floor_t token;
1108 	bool    drop_floor = false;
1109 
1110 	while (TRUE) {
1111 #if TRACEFAULTPAGE
1112 		dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0);       /* (TEST/DEBUG) */
1113 #endif
1114 
1115 		grab_options = 0;
1116 #if CONFIG_SECLUDED_MEMORY
1117 		if (object->can_grab_secluded) {
1118 			grab_options |= VM_PAGE_GRAB_SECLUDED;
1119 		}
1120 #endif /* CONFIG_SECLUDED_MEMORY */
1121 
1122 		if (!object->alive) {
1123 			/*
1124 			 * object is no longer valid
1125 			 * clean up and return error
1126 			 */
1127 #if DEVELOPMENT || DEBUG
1128 			printf("FBDP rdar://93769854 %s:%d object %p internal %d pager %p (%s) copy %p shadow %p alive %d terminating %d named %d ref %d shadow_severed %d\n", __FUNCTION__, __LINE__, object, object->internal, object->pager, object->pager ? object->pager->mo_pager_ops->memory_object_pager_name : "?", object->vo_copy, object->shadow, object->alive, object->terminating, object->named, object->ref_count, object->shadow_severed);
1129 			if (panic_object_not_alive) {
1130 				panic("FBDP rdar://93769854 %s:%d object %p internal %d pager %p (%s) copy %p shadow %p alive %d terminating %d named %d ref %d shadow_severed %d\n", __FUNCTION__, __LINE__, object, object->internal, object->pager, object->pager ? object->pager->mo_pager_ops->memory_object_pager_name : "?", object->vo_copy, object->shadow, object->alive, object->terminating, object->named, object->ref_count, object->shadow_severed);
1131 			}
1132 #endif /* DEVELOPMENT || DEBUG */
1133 			vm_fault_cleanup(object, first_m);
1134 			thread_interrupt_level(interruptible_state);
1135 
1136 			ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_OBJECT_NOT_ALIVE), 0 /* arg */);
1137 			return VM_FAULT_MEMORY_ERROR;
1138 		}
1139 
1140 		if (!object->pager_created && object->phys_contiguous) {
1141 			/*
1142 			 * A physically-contiguous object without a pager:
1143 			 * must be a "large page" object.  We do not deal
1144 			 * with VM pages for this object.
1145 			 */
1146 			caller_lookup = FALSE;
1147 			m = VM_PAGE_NULL;
1148 			goto phys_contig_object;
1149 		}
1150 
1151 		if (object->blocked_access) {
1152 			/*
1153 			 * Access to this VM object has been blocked.
1154 			 * Replace our "paging_in_progress" reference with
1155 			 * a "activity_in_progress" reference and wait for
1156 			 * access to be unblocked.
1157 			 */
1158 			caller_lookup = FALSE; /* no longer valid after sleep */
1159 			vm_object_activity_begin(object);
1160 			vm_object_paging_end(object);
1161 			while (object->blocked_access) {
1162 				vm_object_sleep(object,
1163 				    VM_OBJECT_EVENT_UNBLOCKED,
1164 				    THREAD_UNINT);
1165 			}
1166 			vm_fault_page_blocked_access++;
1167 			vm_object_paging_begin(object);
1168 			vm_object_activity_end(object);
1169 		}
1170 
1171 		/*
1172 		 * See whether the page at 'offset' is resident
1173 		 */
1174 		if (caller_lookup == TRUE) {
1175 			/*
1176 			 * The caller has already looked up the page
1177 			 * and gave us the result in "result_page".
1178 			 * We can use this for the first lookup but
1179 			 * it loses its validity as soon as we unlock
1180 			 * the object.
1181 			 */
1182 			m = *result_page;
1183 			caller_lookup = FALSE; /* no longer valid after that */
1184 		} else {
1185 			m = vm_page_lookup(object, vm_object_trunc_page(offset));
1186 		}
1187 #if TRACEFAULTPAGE
1188 		dbgTrace(0xBEEF0004, (unsigned int) m, (unsigned int) object);  /* (TEST/DEBUG) */
1189 #endif
1190 		if (m != VM_PAGE_NULL) {
1191 			if (m->vmp_busy) {
1192 				/*
1193 				 * The page is being brought in,
1194 				 * wait for it and then retry.
1195 				 */
1196 #if TRACEFAULTPAGE
1197 				dbgTrace(0xBEEF0005, (unsigned int) m, (unsigned int) 0);       /* (TEST/DEBUG) */
1198 #endif
1199 				wait_result = PAGE_SLEEP(object, m, interruptible);
1200 
1201 				if (wait_result != THREAD_AWAKENED) {
1202 					vm_fault_cleanup(object, first_m);
1203 					thread_interrupt_level(interruptible_state);
1204 
1205 					if (wait_result == THREAD_RESTART) {
1206 						return VM_FAULT_RETRY;
1207 					} else {
1208 						ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_BUSYPAGE_WAIT_INTERRUPTED), 0 /* arg */);
1209 						return VM_FAULT_INTERRUPTED;
1210 					}
1211 				}
1212 				continue;
1213 			}
1214 			if (m->vmp_laundry) {
1215 				m->vmp_free_when_done = FALSE;
1216 
1217 				if (!m->vmp_cleaning) {
1218 					vm_pageout_steal_laundry(m, FALSE);
1219 				}
1220 			}
1221 			vm_object_lock_assert_exclusive(VM_PAGE_OBJECT(m));
1222 			if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) {
1223 				/*
1224 				 * Guard page: off limits !
1225 				 */
1226 				if (fault_type == VM_PROT_NONE) {
1227 					/*
1228 					 * The fault is not requesting any
1229 					 * access to the guard page, so it must
1230 					 * be just to wire or unwire it.
1231 					 * Let's pretend it succeeded...
1232 					 */
1233 					m->vmp_busy = TRUE;
1234 					*result_page = m;
1235 					assert(first_m == VM_PAGE_NULL);
1236 					*top_page = first_m;
1237 					if (type_of_fault) {
1238 						*type_of_fault = DBG_GUARD_FAULT;
1239 					}
1240 					thread_interrupt_level(interruptible_state);
1241 					return VM_FAULT_SUCCESS;
1242 				} else {
1243 					/*
1244 					 * The fault requests access to the
1245 					 * guard page: let's deny that !
1246 					 */
1247 					vm_fault_cleanup(object, first_m);
1248 					thread_interrupt_level(interruptible_state);
1249 					ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_GUARDPAGE_FAULT), 0 /* arg */);
1250 					return VM_FAULT_MEMORY_ERROR;
1251 				}
1252 			}
1253 
1254 
1255 			if (m->vmp_error) {
1256 				/*
1257 				 * The page is in error, give up now.
1258 				 */
1259 #if TRACEFAULTPAGE
1260 				dbgTrace(0xBEEF0006, (unsigned int) m, (unsigned int) error_code);      /* (TEST/DEBUG) */
1261 #endif
1262 				if (error_code) {
1263 					*error_code = KERN_MEMORY_ERROR;
1264 				}
1265 				VM_PAGE_FREE(m);
1266 
1267 				vm_fault_cleanup(object, first_m);
1268 				thread_interrupt_level(interruptible_state);
1269 
1270 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PAGE_HAS_ERROR), 0 /* arg */);
1271 				return VM_FAULT_MEMORY_ERROR;
1272 			}
1273 			if (m->vmp_restart) {
1274 				/*
1275 				 * The pager wants us to restart
1276 				 * at the top of the chain,
1277 				 * typically because it has moved the
1278 				 * page to another pager, then do so.
1279 				 */
1280 #if TRACEFAULTPAGE
1281 				dbgTrace(0xBEEF0007, (unsigned int) m, (unsigned int) 0);       /* (TEST/DEBUG) */
1282 #endif
1283 				VM_PAGE_FREE(m);
1284 
1285 				vm_fault_cleanup(object, first_m);
1286 				thread_interrupt_level(interruptible_state);
1287 
1288 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PAGE_HAS_RESTART), 0 /* arg */);
1289 				return VM_FAULT_RETRY;
1290 			}
1291 			if (m->vmp_absent) {
1292 				/*
1293 				 * The page isn't busy, but is absent,
1294 				 * therefore it's deemed "unavailable".
1295 				 *
1296 				 * Remove the non-existent page (unless it's
1297 				 * in the top object) and move on down to the
1298 				 * next object (if there is one).
1299 				 */
1300 #if TRACEFAULTPAGE
1301 				dbgTrace(0xBEEF0008, (unsigned int) m, (unsigned int) object->shadow);  /* (TEST/DEBUG) */
1302 #endif
1303 				next_object = object->shadow;
1304 
1305 				if (next_object == VM_OBJECT_NULL) {
1306 					/*
1307 					 * Absent page at bottom of shadow
1308 					 * chain; zero fill the page we left
1309 					 * busy in the first object, and free
1310 					 * the absent page.
1311 					 */
1312 					assert(!must_be_resident);
1313 
1314 					/*
1315 					 * check for any conditions that prevent
1316 					 * us from creating a new zero-fill page
1317 					 * vm_fault_check will do all of the
1318 					 * fault cleanup in the case of an error condition
1319 					 * including resetting the thread_interrupt_level
1320 					 */
1321 					error = vm_fault_check(object, m, first_m, interruptible_state, (type_of_fault == NULL) ? TRUE : FALSE);
1322 
1323 					if (error != VM_FAULT_SUCCESS) {
1324 						return error;
1325 					}
1326 
1327 					if (object != first_object) {
1328 						/*
1329 						 * free the absent page we just found
1330 						 */
1331 						VM_PAGE_FREE(m);
1332 
1333 						/*
1334 						 * drop reference and lock on current object
1335 						 */
1336 						vm_object_paging_end(object);
1337 						vm_object_unlock(object);
1338 
1339 						/*
1340 						 * grab the original page we
1341 						 * 'soldered' in place and
1342 						 * retake lock on 'first_object'
1343 						 */
1344 						m = first_m;
1345 						first_m = VM_PAGE_NULL;
1346 
1347 						object = first_object;
1348 						offset = first_offset;
1349 
1350 						vm_object_lock(object);
1351 					} else {
1352 						/*
1353 						 * we're going to use the absent page we just found
1354 						 * so convert it to a 'busy' page
1355 						 */
1356 						m->vmp_absent = FALSE;
1357 						m->vmp_busy = TRUE;
1358 					}
1359 					if (fault_info->mark_zf_absent && no_zero_fill == TRUE) {
1360 						m->vmp_absent = TRUE;
1361 						clear_absent_on_error = true;
1362 					}
1363 					/*
1364 					 * zero-fill the page and put it on
1365 					 * the correct paging queue
1366 					 */
1367 					my_fault = vm_fault_zero_page(m, no_zero_fill);
1368 
1369 					break;
1370 				} else {
1371 					if (must_be_resident) {
1372 						vm_object_paging_end(object);
1373 					} else if (object != first_object) {
1374 						vm_object_paging_end(object);
1375 						VM_PAGE_FREE(m);
1376 					} else {
1377 						first_m = m;
1378 						m->vmp_absent = FALSE;
1379 						m->vmp_busy = TRUE;
1380 
1381 						vm_page_lockspin_queues();
1382 						vm_page_queues_remove(m, FALSE);
1383 						vm_page_unlock_queues();
1384 					}
1385 
1386 					offset += object->vo_shadow_offset;
1387 					fault_info->lo_offset += object->vo_shadow_offset;
1388 					fault_info->hi_offset += object->vo_shadow_offset;
1389 					access_required = VM_PROT_READ;
1390 
1391 					vm_object_lock(next_object);
1392 					vm_object_unlock(object);
1393 					object = next_object;
1394 					vm_object_paging_begin(object);
1395 
1396 					/*
1397 					 * reset to default type of fault
1398 					 */
1399 					my_fault = DBG_CACHE_HIT_FAULT;
1400 
1401 					continue;
1402 				}
1403 			}
1404 			if ((m->vmp_cleaning)
1405 			    && ((object != first_object) || (object->vo_copy != VM_OBJECT_NULL))
1406 			    && (fault_type & VM_PROT_WRITE)) {
1407 				/*
1408 				 * This is a copy-on-write fault that will
1409 				 * cause us to revoke access to this page, but
1410 				 * this page is in the process of being cleaned
1411 				 * in a clustered pageout. We must wait until
1412 				 * the cleaning operation completes before
1413 				 * revoking access to the original page,
1414 				 * otherwise we might attempt to remove a
1415 				 * wired mapping.
1416 				 */
1417 #if TRACEFAULTPAGE
1418 				dbgTrace(0xBEEF0009, (unsigned int) m, (unsigned int) offset);  /* (TEST/DEBUG) */
1419 #endif
1420 				/*
1421 				 * take an extra ref so that object won't die
1422 				 */
1423 				vm_object_reference_locked(object);
1424 
1425 				vm_fault_cleanup(object, first_m);
1426 
1427 				vm_object_lock(object);
1428 				assert(object->ref_count > 0);
1429 
1430 				m = vm_page_lookup(object, vm_object_trunc_page(offset));
1431 
1432 				if (m != VM_PAGE_NULL && m->vmp_cleaning) {
1433 					PAGE_ASSERT_WAIT(m, interruptible);
1434 
1435 					vm_object_unlock(object);
1436 					wait_result = thread_block(THREAD_CONTINUE_NULL);
1437 					vm_object_deallocate(object);
1438 
1439 					goto backoff;
1440 				} else {
1441 					vm_object_unlock(object);
1442 
1443 					vm_object_deallocate(object);
1444 					thread_interrupt_level(interruptible_state);
1445 
1446 					return VM_FAULT_RETRY;
1447 				}
1448 			}
1449 			if (type_of_fault == NULL && (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) &&
1450 			    !(fault_info != NULL && fault_info->stealth)) {
1451 				/*
1452 				 * If we were passed a non-NULL pointer for
1453 				 * "type_of_fault", than we came from
1454 				 * vm_fault... we'll let it deal with
1455 				 * this condition, since it
1456 				 * needs to see m->vmp_speculative to correctly
1457 				 * account the pageins, otherwise...
1458 				 * take it off the speculative queue, we'll
1459 				 * let the caller of vm_fault_page deal
1460 				 * with getting it onto the correct queue
1461 				 *
1462 				 * If the caller specified in fault_info that
1463 				 * it wants a "stealth" fault, we also leave
1464 				 * the page in the speculative queue.
1465 				 */
1466 				vm_page_lockspin_queues();
1467 				if (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) {
1468 					vm_page_queues_remove(m, FALSE);
1469 				}
1470 				vm_page_unlock_queues();
1471 			}
1472 			assert(object == VM_PAGE_OBJECT(m));
1473 
1474 			if (object->code_signed) {
1475 				/*
1476 				 * CODE SIGNING:
1477 				 * We just paged in a page from a signed
1478 				 * memory object but we don't need to
1479 				 * validate it now.  We'll validate it if
1480 				 * when it gets mapped into a user address
1481 				 * space for the first time or when the page
1482 				 * gets copied to another object as a result
1483 				 * of a copy-on-write.
1484 				 */
1485 			}
1486 
1487 			/*
1488 			 * We mark the page busy and leave it on
1489 			 * the pageout queues.  If the pageout
1490 			 * deamon comes across it, then it will
1491 			 * remove the page from the queue, but not the object
1492 			 */
1493 #if TRACEFAULTPAGE
1494 			dbgTrace(0xBEEF000B, (unsigned int) m, (unsigned int) 0);       /* (TEST/DEBUG) */
1495 #endif
1496 			assert(!m->vmp_busy);
1497 			assert(!m->vmp_absent);
1498 
1499 			m->vmp_busy = TRUE;
1500 			break;
1501 		}
1502 
1503 		/*
1504 		 * we get here when there is no page present in the object at
1505 		 * the offset we're interested in... we'll allocate a page
1506 		 * at this point if the pager associated with
1507 		 * this object can provide the data or we're the top object...
1508 		 * object is locked;  m == NULL
1509 		 */
1510 
1511 		if (must_be_resident) {
1512 			if (fault_type == VM_PROT_NONE &&
1513 			    is_kernel_object(object)) {
1514 				/*
1515 				 * We've been called from vm_fault_unwire()
1516 				 * while removing a map entry that was allocated
1517 				 * with KMA_KOBJECT and KMA_VAONLY.  This page
1518 				 * is not present and there's nothing more to
1519 				 * do here (nothing to unwire).
1520 				 */
1521 				vm_fault_cleanup(object, first_m);
1522 				thread_interrupt_level(interruptible_state);
1523 
1524 				return VM_FAULT_MEMORY_ERROR;
1525 			}
1526 
1527 			goto dont_look_for_page;
1528 		}
1529 
1530 		/* Don't expect to fault pages into the kernel object. */
1531 		assert(!is_kernel_object(object));
1532 
1533 		look_for_page = (object->pager_created && (MUST_ASK_PAGER(object, offset, external_state) == TRUE));
1534 
1535 #if TRACEFAULTPAGE
1536 		dbgTrace(0xBEEF000C, (unsigned int) look_for_page, (unsigned int) object);      /* (TEST/DEBUG) */
1537 #endif
1538 		if (!look_for_page && object == first_object && !object->phys_contiguous) {
1539 			/*
1540 			 * Allocate a new page for this object/offset pair as a placeholder
1541 			 */
1542 			m = vm_page_grab_options(grab_options);
1543 #if TRACEFAULTPAGE
1544 			dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object);  /* (TEST/DEBUG) */
1545 #endif
1546 			if (m == VM_PAGE_NULL) {
1547 				vm_fault_cleanup(object, first_m);
1548 				thread_interrupt_level(interruptible_state);
1549 
1550 				return VM_FAULT_MEMORY_SHORTAGE;
1551 			}
1552 
1553 			if (fault_info && fault_info->batch_pmap_op == TRUE) {
1554 				vm_page_insert_internal(m, object,
1555 				    vm_object_trunc_page(offset),
1556 				    VM_KERN_MEMORY_NONE, FALSE, TRUE, TRUE, FALSE, NULL);
1557 			} else {
1558 				vm_page_insert(m, object, vm_object_trunc_page(offset));
1559 			}
1560 		}
1561 		if (look_for_page) {
1562 			kern_return_t   rc;
1563 			int             my_fault_type;
1564 
1565 			/*
1566 			 *	If the memory manager is not ready, we
1567 			 *	cannot make requests.
1568 			 */
1569 			if (!object->pager_ready) {
1570 #if TRACEFAULTPAGE
1571 				dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0);       /* (TEST/DEBUG) */
1572 #endif
1573 				if (m != VM_PAGE_NULL) {
1574 					VM_PAGE_FREE(m);
1575 				}
1576 
1577 				/*
1578 				 * take an extra ref so object won't die
1579 				 */
1580 				vm_object_reference_locked(object);
1581 				vm_fault_cleanup(object, first_m);
1582 
1583 				vm_object_lock(object);
1584 				assert(object->ref_count > 0);
1585 
1586 				if (!object->pager_ready) {
1587 					wait_result = vm_object_assert_wait(object, VM_OBJECT_EVENT_PAGER_READY, interruptible);
1588 
1589 					vm_object_unlock(object);
1590 					if (wait_result == THREAD_WAITING) {
1591 						wait_result = thread_block(THREAD_CONTINUE_NULL);
1592 					}
1593 					vm_object_deallocate(object);
1594 
1595 					goto backoff;
1596 				} else {
1597 					vm_object_unlock(object);
1598 					vm_object_deallocate(object);
1599 					thread_interrupt_level(interruptible_state);
1600 
1601 					return VM_FAULT_RETRY;
1602 				}
1603 			}
1604 			if (!object->internal && !object->phys_contiguous && object->paging_in_progress > vm_object_pagein_throttle) {
1605 				/*
1606 				 * If there are too many outstanding page
1607 				 * requests pending on this external object, we
1608 				 * wait for them to be resolved now.
1609 				 */
1610 #if TRACEFAULTPAGE
1611 				dbgTrace(0xBEEF0010, (unsigned int) m, (unsigned int) 0);       /* (TEST/DEBUG) */
1612 #endif
1613 				if (m != VM_PAGE_NULL) {
1614 					VM_PAGE_FREE(m);
1615 				}
1616 				/*
1617 				 * take an extra ref so object won't die
1618 				 */
1619 				vm_object_reference_locked(object);
1620 
1621 				vm_fault_cleanup(object, first_m);
1622 
1623 				vm_object_lock(object);
1624 				assert(object->ref_count > 0);
1625 
1626 				if (object->paging_in_progress >= vm_object_pagein_throttle) {
1627 					vm_object_assert_wait(object, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS, interruptible);
1628 
1629 					vm_object_unlock(object);
1630 					wait_result = thread_block(THREAD_CONTINUE_NULL);
1631 					vm_object_deallocate(object);
1632 
1633 					goto backoff;
1634 				} else {
1635 					vm_object_unlock(object);
1636 					vm_object_deallocate(object);
1637 					thread_interrupt_level(interruptible_state);
1638 
1639 					return VM_FAULT_RETRY;
1640 				}
1641 			}
1642 			if (object->internal) {
1643 				int compressed_count_delta;
1644 
1645 				assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);
1646 
1647 				if (m == VM_PAGE_NULL) {
1648 					/*
1649 					 * Allocate a new page for this object/offset pair as a placeholder
1650 					 */
1651 					m = vm_page_grab_options(grab_options);
1652 #if TRACEFAULTPAGE
1653 					dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object);  /* (TEST/DEBUG) */
1654 #endif
1655 					if (m == VM_PAGE_NULL) {
1656 						vm_fault_cleanup(object, first_m);
1657 						thread_interrupt_level(interruptible_state);
1658 
1659 						return VM_FAULT_MEMORY_SHORTAGE;
1660 					}
1661 
1662 					m->vmp_absent = TRUE;
1663 					if (fault_info && fault_info->batch_pmap_op == TRUE) {
1664 						vm_page_insert_internal(m, object, vm_object_trunc_page(offset), VM_KERN_MEMORY_NONE, FALSE, TRUE, TRUE, FALSE, NULL);
1665 					} else {
1666 						vm_page_insert(m, object, vm_object_trunc_page(offset));
1667 					}
1668 				}
1669 				assert(m->vmp_busy);
1670 
1671 				m->vmp_absent = TRUE;
1672 				pager = object->pager;
1673 
1674 				assert(object->paging_in_progress > 0);
1675 				vm_object_unlock(object);
1676 
1677 				rc = vm_compressor_pager_get(
1678 					pager,
1679 					offset + object->paging_offset,
1680 					VM_PAGE_GET_PHYS_PAGE(m),
1681 					&my_fault_type,
1682 					0,
1683 					&compressed_count_delta);
1684 
1685 				if (type_of_fault == NULL) {
1686 					int     throttle_delay;
1687 
1688 					/*
1689 					 * we weren't called from vm_fault, so we
1690 					 * need to apply page creation throttling
1691 					 * do it before we re-acquire any locks
1692 					 */
1693 					if (my_fault_type == DBG_COMPRESSOR_FAULT) {
1694 						if ((throttle_delay = vm_page_throttled(TRUE))) {
1695 							VM_DEBUG_EVENT(vmf_compressordelay, VMF_COMPRESSORDELAY, DBG_FUNC_NONE, throttle_delay, 0, 1, 0);
1696 							__VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__(throttle_delay);
1697 						}
1698 					}
1699 				}
1700 				vm_object_lock(object);
1701 				assert(object->paging_in_progress > 0);
1702 
1703 				vm_compressor_pager_count(
1704 					pager,
1705 					compressed_count_delta,
1706 					FALSE, /* shared_lock */
1707 					object);
1708 
1709 				switch (rc) {
1710 				case KERN_SUCCESS:
1711 					m->vmp_absent = FALSE;
1712 					m->vmp_dirty = TRUE;
1713 					if ((object->wimg_bits &
1714 					    VM_WIMG_MASK) !=
1715 					    VM_WIMG_USE_DEFAULT) {
1716 						/*
1717 						 * If the page is not cacheable,
1718 						 * we can't let its contents
1719 						 * linger in the data cache
1720 						 * after the decompression.
1721 						 */
1722 						pmap_sync_page_attributes_phys(
1723 							VM_PAGE_GET_PHYS_PAGE(m));
1724 					} else {
1725 						m->vmp_written_by_kernel = TRUE;
1726 					}
1727 #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES
1728 					if ((fault_type & VM_PROT_WRITE) == 0) {
1729 						vm_object_lock_assert_exclusive(object);
1730 						vm_page_lockspin_queues();
1731 						m->vmp_unmodified_ro = true;
1732 						vm_page_unlock_queues();
1733 						os_atomic_inc(&compressor_ro_uncompressed, relaxed);
1734 						*protection &= ~VM_PROT_WRITE;
1735 					}
1736 #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */
1737 
1738 					/*
1739 					 * If the object is purgeable, its
1740 					 * owner's purgeable ledgers have been
1741 					 * updated in vm_page_insert() but the
1742 					 * page was also accounted for in a
1743 					 * "compressed purgeable" ledger, so
1744 					 * update that now.
1745 					 */
1746 					if (((object->purgable !=
1747 					    VM_PURGABLE_DENY) ||
1748 					    object->vo_ledger_tag) &&
1749 					    (object->vo_owner !=
1750 					    NULL)) {
1751 						/*
1752 						 * One less compressed
1753 						 * purgeable/tagged page.
1754 						 */
1755 						if (compressed_count_delta) {
1756 							vm_object_owner_compressed_update(
1757 								object,
1758 								-1);
1759 						}
1760 					}
1761 
1762 					break;
1763 				case KERN_MEMORY_FAILURE:
1764 					m->vmp_unusual = TRUE;
1765 					m->vmp_error = TRUE;
1766 					m->vmp_absent = FALSE;
1767 					break;
1768 				case KERN_MEMORY_ERROR:
1769 					assert(m->vmp_absent);
1770 					break;
1771 				default:
1772 					panic("vm_fault_page(): unexpected "
1773 					    "error %d from "
1774 					    "vm_compressor_pager_get()\n",
1775 					    rc);
1776 				}
1777 				PAGE_WAKEUP_DONE(m);
1778 
1779 				rc = KERN_SUCCESS;
1780 				goto data_requested;
1781 			}
1782 			my_fault_type = DBG_PAGEIN_FAULT;
1783 
1784 			if (m != VM_PAGE_NULL) {
1785 				VM_PAGE_FREE(m);
1786 				m = VM_PAGE_NULL;
1787 			}
1788 
1789 #if TRACEFAULTPAGE
1790 			dbgTrace(0xBEEF0012, (unsigned int) object, (unsigned int) 0);  /* (TEST/DEBUG) */
1791 #endif
1792 
1793 			/*
1794 			 * It's possible someone called vm_object_destroy while we weren't
1795 			 * holding the object lock.  If that has happened, then bail out
1796 			 * here.
1797 			 */
1798 
1799 			pager = object->pager;
1800 
1801 			if (pager == MEMORY_OBJECT_NULL) {
1802 				vm_fault_cleanup(object, first_m);
1803 				thread_interrupt_level(interruptible_state);
1804 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_OBJECT_NO_PAGER), 0 /* arg */);
1805 				return VM_FAULT_MEMORY_ERROR;
1806 			}
1807 
1808 			/*
1809 			 * We have an absent page in place for the faulting offset,
1810 			 * so we can release the object lock.
1811 			 */
1812 
1813 			if (object->object_is_shared_cache) {
1814 				token = thread_priority_floor_start();
1815 				/*
1816 				 * A non-native shared cache object might
1817 				 * be getting set up in parallel with this
1818 				 * fault and so we can't assume that this
1819 				 * check will be valid after we drop the
1820 				 * object lock below.
1821 				 */
1822 				drop_floor = true;
1823 			}
1824 
1825 			vm_object_unlock(object);
1826 
1827 			/*
1828 			 * If this object uses a copy_call strategy,
1829 			 * and we are interested in a copy of this object
1830 			 * (having gotten here only by following a
1831 			 * shadow chain), then tell the memory manager
1832 			 * via a flag added to the desired_access
1833 			 * parameter, so that it can detect a race
1834 			 * between our walking down the shadow chain
1835 			 * and its pushing pages up into a copy of
1836 			 * the object that it manages.
1837 			 */
1838 			if (object->copy_strategy == MEMORY_OBJECT_COPY_CALL && object != first_object) {
1839 				wants_copy_flag = VM_PROT_WANTS_COPY;
1840 			} else {
1841 				wants_copy_flag = VM_PROT_NONE;
1842 			}
1843 
1844 			if (object->vo_copy == first_object) {
1845 				/*
1846 				 * if we issue the memory_object_data_request in
1847 				 * this state, we are subject to a deadlock with
1848 				 * the underlying filesystem if it is trying to
1849 				 * shrink the file resulting in a push of pages
1850 				 * into the copy object...  that push will stall
1851 				 * on the placeholder page, and if the pushing thread
1852 				 * is holding a lock that is required on the pagein
1853 				 * path (such as a truncate lock), we'll deadlock...
1854 				 * to avoid this potential deadlock, we throw away
1855 				 * our placeholder page before calling memory_object_data_request
1856 				 * and force this thread to retry the vm_fault_page after
1857 				 * we have issued the I/O.  the second time through this path
1858 				 * we will find the page already in the cache (presumably still
1859 				 * busy waiting for the I/O to complete) and then complete
1860 				 * the fault w/o having to go through memory_object_data_request again
1861 				 */
1862 				assert(first_m != VM_PAGE_NULL);
1863 				assert(VM_PAGE_OBJECT(first_m) == first_object);
1864 
1865 				vm_object_lock(first_object);
1866 				VM_PAGE_FREE(first_m);
1867 				vm_object_paging_end(first_object);
1868 				vm_object_unlock(first_object);
1869 
1870 				first_m = VM_PAGE_NULL;
1871 				force_fault_retry = TRUE;
1872 
1873 				vm_fault_page_forced_retry++;
1874 			}
1875 
1876 			if (data_already_requested == TRUE) {
1877 				orig_behavior = fault_info->behavior;
1878 				orig_cluster_size = fault_info->cluster_size;
1879 
1880 				fault_info->behavior = VM_BEHAVIOR_RANDOM;
1881 				fault_info->cluster_size = PAGE_SIZE;
1882 			}
1883 			/*
1884 			 * Call the memory manager to retrieve the data.
1885 			 */
1886 			rc = memory_object_data_request(
1887 				pager,
1888 				vm_object_trunc_page(offset) + object->paging_offset,
1889 				PAGE_SIZE,
1890 				access_required | wants_copy_flag,
1891 				(memory_object_fault_info_t)fault_info);
1892 
1893 			if (data_already_requested == TRUE) {
1894 				fault_info->behavior = orig_behavior;
1895 				fault_info->cluster_size = orig_cluster_size;
1896 			} else {
1897 				data_already_requested = TRUE;
1898 			}
1899 
1900 			DTRACE_VM2(maj_fault, int, 1, (uint64_t *), NULL);
1901 #if TRACEFAULTPAGE
1902 			dbgTrace(0xBEEF0013, (unsigned int) object, (unsigned int) rc); /* (TEST/DEBUG) */
1903 #endif
1904 			vm_object_lock(object);
1905 
1906 			if (drop_floor && object->object_is_shared_cache) {
1907 				thread_priority_floor_end(&token);
1908 				drop_floor = false;
1909 			}
1910 
1911 data_requested:
1912 			if (rc != KERN_SUCCESS) {
1913 				vm_fault_cleanup(object, first_m);
1914 				thread_interrupt_level(interruptible_state);
1915 
1916 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_NO_DATA), 0 /* arg */);
1917 
1918 				return (rc == MACH_SEND_INTERRUPTED) ?
1919 				       VM_FAULT_INTERRUPTED :
1920 				       VM_FAULT_MEMORY_ERROR;
1921 			} else {
1922 				clock_sec_t     tv_sec;
1923 				clock_usec_t    tv_usec;
1924 
1925 				if (my_fault_type == DBG_PAGEIN_FAULT) {
1926 					clock_get_system_microtime(&tv_sec, &tv_usec);
1927 					current_thread()->t_page_creation_time = tv_sec;
1928 					current_thread()->t_page_creation_count = 0;
1929 				}
1930 			}
1931 			if ((interruptible != THREAD_UNINT) && (current_thread()->sched_flags & TH_SFLAG_ABORT)) {
1932 				vm_fault_cleanup(object, first_m);
1933 				thread_interrupt_level(interruptible_state);
1934 
1935 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_INTERRUPTED), 0 /* arg */);
1936 				return VM_FAULT_INTERRUPTED;
1937 			}
1938 			if (force_fault_retry == TRUE) {
1939 				vm_fault_cleanup(object, first_m);
1940 				thread_interrupt_level(interruptible_state);
1941 
1942 				return VM_FAULT_RETRY;
1943 			}
1944 			if (m == VM_PAGE_NULL && object->phys_contiguous) {
1945 				/*
1946 				 * No page here means that the object we
1947 				 * initially looked up was "physically
1948 				 * contiguous" (i.e. device memory).  However,
1949 				 * with Virtual VRAM, the object might not
1950 				 * be backed by that device memory anymore,
1951 				 * so we're done here only if the object is
1952 				 * still "phys_contiguous".
1953 				 * Otherwise, if the object is no longer
1954 				 * "phys_contiguous", we need to retry the
1955 				 * page fault against the object's new backing
1956 				 * store (different memory object).
1957 				 */
1958 phys_contig_object:
1959 				goto done;
1960 			}
1961 			/*
1962 			 * potentially a pagein fault
1963 			 * if we make it through the state checks
1964 			 * above, than we'll count it as such
1965 			 */
1966 			my_fault = my_fault_type;
1967 
1968 			/*
1969 			 * Retry with same object/offset, since new data may
1970 			 * be in a different page (i.e., m is meaningless at
1971 			 * this point).
1972 			 */
1973 			continue;
1974 		}
1975 dont_look_for_page:
1976 		/*
1977 		 * We get here if the object has no pager, or an existence map
1978 		 * exists and indicates the page isn't present on the pager
1979 		 * or we're unwiring a page.  If a pager exists, but there
1980 		 * is no existence map, then the m->vmp_absent case above handles
1981 		 * the ZF case when the pager can't provide the page
1982 		 */
1983 #if TRACEFAULTPAGE
1984 		dbgTrace(0xBEEF0014, (unsigned int) object, (unsigned int) m);  /* (TEST/DEBUG) */
1985 #endif
1986 		if (object == first_object) {
1987 			first_m = m;
1988 		} else {
1989 			assert(m == VM_PAGE_NULL);
1990 		}
1991 
1992 		next_object = object->shadow;
1993 
1994 		if (next_object == VM_OBJECT_NULL) {
1995 			/*
1996 			 * we've hit the bottom of the shadown chain,
1997 			 * fill the page in the top object with zeros.
1998 			 */
1999 			assert(!must_be_resident);
2000 
2001 			if (object != first_object) {
2002 				vm_object_paging_end(object);
2003 				vm_object_unlock(object);
2004 
2005 				object = first_object;
2006 				offset = first_offset;
2007 				vm_object_lock(object);
2008 			}
2009 			m = first_m;
2010 			assert(VM_PAGE_OBJECT(m) == object);
2011 			first_m = VM_PAGE_NULL;
2012 
2013 			/*
2014 			 * check for any conditions that prevent
2015 			 * us from creating a new zero-fill page
2016 			 * vm_fault_check will do all of the
2017 			 * fault cleanup in the case of an error condition
2018 			 * including resetting the thread_interrupt_level
2019 			 */
2020 			error = vm_fault_check(object, m, first_m, interruptible_state, (type_of_fault == NULL) ? TRUE : FALSE);
2021 
2022 			if (error != VM_FAULT_SUCCESS) {
2023 				return error;
2024 			}
2025 
2026 			if (m == VM_PAGE_NULL) {
2027 				m = vm_page_grab_options(grab_options);
2028 
2029 				if (m == VM_PAGE_NULL) {
2030 					vm_fault_cleanup(object, VM_PAGE_NULL);
2031 					thread_interrupt_level(interruptible_state);
2032 
2033 					return VM_FAULT_MEMORY_SHORTAGE;
2034 				}
2035 				vm_page_insert(m, object, vm_object_trunc_page(offset));
2036 			}
2037 			if (fault_info->mark_zf_absent && no_zero_fill == TRUE) {
2038 				m->vmp_absent = TRUE;
2039 				clear_absent_on_error = true;
2040 			}
2041 
2042 			my_fault = vm_fault_zero_page(m, no_zero_fill);
2043 
2044 			break;
2045 		} else {
2046 			/*
2047 			 * Move on to the next object.  Lock the next
2048 			 * object before unlocking the current one.
2049 			 */
2050 			if ((object != first_object) || must_be_resident) {
2051 				vm_object_paging_end(object);
2052 			}
2053 
2054 			offset += object->vo_shadow_offset;
2055 			fault_info->lo_offset += object->vo_shadow_offset;
2056 			fault_info->hi_offset += object->vo_shadow_offset;
2057 			access_required = VM_PROT_READ;
2058 
2059 			vm_object_lock(next_object);
2060 			vm_object_unlock(object);
2061 
2062 			object = next_object;
2063 			vm_object_paging_begin(object);
2064 		}
2065 	}
2066 
2067 	/*
2068 	 *	PAGE HAS BEEN FOUND.
2069 	 *
2070 	 *	This page (m) is:
2071 	 *		busy, so that we can play with it;
2072 	 *		not absent, so that nobody else will fill it;
2073 	 *		possibly eligible for pageout;
2074 	 *
2075 	 *	The top-level page (first_m) is:
2076 	 *		VM_PAGE_NULL if the page was found in the
2077 	 *		 top-level object;
2078 	 *		busy, not absent, and ineligible for pageout.
2079 	 *
2080 	 *	The current object (object) is locked.  A paging
2081 	 *	reference is held for the current and top-level
2082 	 *	objects.
2083 	 */
2084 
2085 #if TRACEFAULTPAGE
2086 	dbgTrace(0xBEEF0015, (unsigned int) object, (unsigned int) m);  /* (TEST/DEBUG) */
2087 #endif
2088 #if     EXTRA_ASSERTIONS
2089 	assert(m->vmp_busy && !m->vmp_absent);
2090 	assert((first_m == VM_PAGE_NULL) ||
2091 	    (first_m->vmp_busy && !first_m->vmp_absent &&
2092 	    !first_m->vmp_active && !first_m->vmp_inactive && !first_m->vmp_secluded));
2093 #endif  /* EXTRA_ASSERTIONS */
2094 
2095 	/*
2096 	 * If the page is being written, but isn't
2097 	 * already owned by the top-level object,
2098 	 * we have to copy it into a new page owned
2099 	 * by the top-level object.
2100 	 */
2101 	if (object != first_object) {
2102 #if TRACEFAULTPAGE
2103 		dbgTrace(0xBEEF0016, (unsigned int) object, (unsigned int) fault_type); /* (TEST/DEBUG) */
2104 #endif
2105 		if (fault_type & VM_PROT_WRITE) {
2106 			vm_page_t copy_m;
2107 
2108 			/*
2109 			 * We only really need to copy if we
2110 			 * want to write it.
2111 			 */
2112 			assert(!must_be_resident);
2113 
2114 			/*
2115 			 * If we try to collapse first_object at this
2116 			 * point, we may deadlock when we try to get
2117 			 * the lock on an intermediate object (since we
2118 			 * have the bottom object locked).  We can't
2119 			 * unlock the bottom object, because the page
2120 			 * we found may move (by collapse) if we do.
2121 			 *
2122 			 * Instead, we first copy the page.  Then, when
2123 			 * we have no more use for the bottom object,
2124 			 * we unlock it and try to collapse.
2125 			 *
2126 			 * Note that we copy the page even if we didn't
2127 			 * need to... that's the breaks.
2128 			 */
2129 
2130 			/*
2131 			 * Allocate a page for the copy
2132 			 */
2133 			copy_m = vm_page_grab_options(grab_options);
2134 
2135 			if (copy_m == VM_PAGE_NULL) {
2136 				RELEASE_PAGE(m);
2137 
2138 				vm_fault_cleanup(object, first_m);
2139 				thread_interrupt_level(interruptible_state);
2140 
2141 				return VM_FAULT_MEMORY_SHORTAGE;
2142 			}
2143 
2144 			vm_page_copy(m, copy_m);
2145 
2146 			/*
2147 			 * If another map is truly sharing this
2148 			 * page with us, we have to flush all
2149 			 * uses of the original page, since we
2150 			 * can't distinguish those which want the
2151 			 * original from those which need the
2152 			 * new copy.
2153 			 *
2154 			 * XXXO If we know that only one map has
2155 			 * access to this page, then we could
2156 			 * avoid the pmap_disconnect() call.
2157 			 */
2158 			if (m->vmp_pmapped) {
2159 				pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
2160 			}
2161 
2162 			if (m->vmp_clustered) {
2163 				VM_PAGE_COUNT_AS_PAGEIN(m);
2164 				VM_PAGE_CONSUME_CLUSTERED(m);
2165 			}
2166 			assert(!m->vmp_cleaning);
2167 
2168 			/*
2169 			 * We no longer need the old page or object.
2170 			 */
2171 			RELEASE_PAGE(m);
2172 
2173 			/*
2174 			 * This check helps with marking the object as having a sequential pattern
2175 			 * Normally we'll miss doing this below because this fault is about COW to
2176 			 * the first_object i.e. bring page in from disk, push to object above but
2177 			 * don't update the file object's sequential pattern.
2178 			 */
2179 			if (object->internal == FALSE) {
2180 				vm_fault_is_sequential(object, offset, fault_info->behavior);
2181 			}
2182 
2183 			vm_object_paging_end(object);
2184 			vm_object_unlock(object);
2185 
2186 			my_fault = DBG_COW_FAULT;
2187 			counter_inc(&vm_statistics_cow_faults);
2188 			DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL);
2189 			counter_inc(&current_task()->cow_faults);
2190 
2191 			object = first_object;
2192 			offset = first_offset;
2193 
2194 			vm_object_lock(object);
2195 			/*
2196 			 * get rid of the place holder
2197 			 * page that we soldered in earlier
2198 			 */
2199 			VM_PAGE_FREE(first_m);
2200 			first_m = VM_PAGE_NULL;
2201 
2202 			/*
2203 			 * and replace it with the
2204 			 * page we just copied into
2205 			 */
2206 			assert(copy_m->vmp_busy);
2207 			vm_page_insert(copy_m, object, vm_object_trunc_page(offset));
2208 			SET_PAGE_DIRTY(copy_m, TRUE);
2209 
2210 			m = copy_m;
2211 			/*
2212 			 * Now that we've gotten the copy out of the
2213 			 * way, let's try to collapse the top object.
2214 			 * But we have to play ugly games with
2215 			 * paging_in_progress to do that...
2216 			 */
2217 			vm_object_paging_end(object);
2218 			vm_object_collapse(object, vm_object_trunc_page(offset), TRUE);
2219 			vm_object_paging_begin(object);
2220 		} else {
2221 			*protection &= (~VM_PROT_WRITE);
2222 		}
2223 	}
2224 	/*
2225 	 * Now check whether the page needs to be pushed into the
2226 	 * copy object.  The use of asymmetric copy on write for
2227 	 * shared temporary objects means that we may do two copies to
2228 	 * satisfy the fault; one above to get the page from a
2229 	 * shadowed object, and one here to push it into the copy.
2230 	 */
2231 	try_failed_count = 0;
2232 
2233 	while ((copy_object = first_object->vo_copy) != VM_OBJECT_NULL) {
2234 		vm_object_offset_t      copy_offset;
2235 		vm_page_t               copy_m;
2236 
2237 #if TRACEFAULTPAGE
2238 		dbgTrace(0xBEEF0017, (unsigned int) copy_object, (unsigned int) fault_type);    /* (TEST/DEBUG) */
2239 #endif
2240 		/*
2241 		 * If the page is being written, but hasn't been
2242 		 * copied to the copy-object, we have to copy it there.
2243 		 */
2244 		if ((fault_type & VM_PROT_WRITE) == 0) {
2245 			*protection &= ~VM_PROT_WRITE;
2246 			break;
2247 		}
2248 
2249 		/*
2250 		 * If the page was guaranteed to be resident,
2251 		 * we must have already performed the copy.
2252 		 */
2253 		if (must_be_resident) {
2254 			break;
2255 		}
2256 
2257 		/*
2258 		 * Try to get the lock on the copy_object.
2259 		 */
2260 		if (!vm_object_lock_try(copy_object)) {
2261 			vm_object_unlock(object);
2262 			try_failed_count++;
2263 
2264 			mutex_pause(try_failed_count);  /* wait a bit */
2265 			vm_object_lock(object);
2266 
2267 			continue;
2268 		}
2269 		try_failed_count = 0;
2270 
2271 		/*
2272 		 * Make another reference to the copy-object,
2273 		 * to keep it from disappearing during the
2274 		 * copy.
2275 		 */
2276 		vm_object_reference_locked(copy_object);
2277 
2278 		/*
2279 		 * Does the page exist in the copy?
2280 		 */
2281 		copy_offset = first_offset - copy_object->vo_shadow_offset;
2282 		copy_offset = vm_object_trunc_page(copy_offset);
2283 
2284 		if (copy_object->vo_size <= copy_offset) {
2285 			/*
2286 			 * Copy object doesn't cover this page -- do nothing.
2287 			 */
2288 			;
2289 		} else if ((copy_m = vm_page_lookup(copy_object, copy_offset)) != VM_PAGE_NULL) {
2290 			/*
2291 			 * Page currently exists in the copy object
2292 			 */
2293 			if (copy_m->vmp_busy) {
2294 				/*
2295 				 * If the page is being brought
2296 				 * in, wait for it and then retry.
2297 				 */
2298 				RELEASE_PAGE(m);
2299 
2300 				/*
2301 				 * take an extra ref so object won't die
2302 				 */
2303 				vm_object_reference_locked(copy_object);
2304 				vm_object_unlock(copy_object);
2305 				vm_fault_cleanup(object, first_m);
2306 
2307 				vm_object_lock(copy_object);
2308 				assert(copy_object->ref_count > 0);
2309 				vm_object_lock_assert_exclusive(copy_object);
2310 				copy_object->ref_count--;
2311 				assert(copy_object->ref_count > 0);
2312 				copy_m = vm_page_lookup(copy_object, copy_offset);
2313 
2314 				if (copy_m != VM_PAGE_NULL && copy_m->vmp_busy) {
2315 					PAGE_ASSERT_WAIT(copy_m, interruptible);
2316 
2317 					vm_object_unlock(copy_object);
2318 					wait_result = thread_block(THREAD_CONTINUE_NULL);
2319 					vm_object_deallocate(copy_object);
2320 
2321 					goto backoff;
2322 				} else {
2323 					vm_object_unlock(copy_object);
2324 					vm_object_deallocate(copy_object);
2325 					thread_interrupt_level(interruptible_state);
2326 
2327 					return VM_FAULT_RETRY;
2328 				}
2329 			}
2330 		} else if (!PAGED_OUT(copy_object, copy_offset)) {
2331 			/*
2332 			 * If PAGED_OUT is TRUE, then the page used to exist
2333 			 * in the copy-object, and has already been paged out.
2334 			 * We don't need to repeat this. If PAGED_OUT is
2335 			 * FALSE, then either we don't know (!pager_created,
2336 			 * for example) or it hasn't been paged out.
2337 			 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2338 			 * We must copy the page to the copy object.
2339 			 *
2340 			 * Allocate a page for the copy
2341 			 */
2342 			copy_m = vm_page_alloc(copy_object, copy_offset);
2343 
2344 			if (copy_m == VM_PAGE_NULL) {
2345 				RELEASE_PAGE(m);
2346 
2347 				vm_object_lock_assert_exclusive(copy_object);
2348 				copy_object->ref_count--;
2349 				assert(copy_object->ref_count > 0);
2350 
2351 				vm_object_unlock(copy_object);
2352 				vm_fault_cleanup(object, first_m);
2353 				thread_interrupt_level(interruptible_state);
2354 
2355 				return VM_FAULT_MEMORY_SHORTAGE;
2356 			}
2357 			/*
2358 			 * Must copy page into copy-object.
2359 			 */
2360 			vm_page_copy(m, copy_m);
2361 
2362 			/*
2363 			 * If the old page was in use by any users
2364 			 * of the copy-object, it must be removed
2365 			 * from all pmaps.  (We can't know which
2366 			 * pmaps use it.)
2367 			 */
2368 			if (m->vmp_pmapped) {
2369 				pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
2370 			}
2371 
2372 			if (m->vmp_clustered) {
2373 				VM_PAGE_COUNT_AS_PAGEIN(m);
2374 				VM_PAGE_CONSUME_CLUSTERED(m);
2375 			}
2376 			/*
2377 			 * If there's a pager, then immediately
2378 			 * page out this page, using the "initialize"
2379 			 * option.  Else, we use the copy.
2380 			 */
2381 			if ((!copy_object->pager_ready)
2382 			    || VM_COMPRESSOR_PAGER_STATE_GET(copy_object, copy_offset) == VM_EXTERNAL_STATE_ABSENT
2383 			    ) {
2384 				vm_page_lockspin_queues();
2385 				assert(!m->vmp_cleaning);
2386 				vm_page_activate(copy_m);
2387 				vm_page_unlock_queues();
2388 
2389 				SET_PAGE_DIRTY(copy_m, TRUE);
2390 				PAGE_WAKEUP_DONE(copy_m);
2391 			} else {
2392 				assert(copy_m->vmp_busy == TRUE);
2393 				assert(!m->vmp_cleaning);
2394 
2395 				/*
2396 				 * dirty is protected by the object lock
2397 				 */
2398 				SET_PAGE_DIRTY(copy_m, TRUE);
2399 
2400 				/*
2401 				 * The page is already ready for pageout:
2402 				 * not on pageout queues and busy.
2403 				 * Unlock everything except the
2404 				 * copy_object itself.
2405 				 */
2406 				vm_object_unlock(object);
2407 
2408 				/*
2409 				 * Write the page to the copy-object,
2410 				 * flushing it from the kernel.
2411 				 */
2412 				vm_pageout_initialize_page(copy_m);
2413 
2414 				/*
2415 				 * Since the pageout may have
2416 				 * temporarily dropped the
2417 				 * copy_object's lock, we
2418 				 * check whether we'll have
2419 				 * to deallocate the hard way.
2420 				 */
2421 				if ((copy_object->shadow != object) || (copy_object->ref_count == 1)) {
2422 					vm_object_unlock(copy_object);
2423 					vm_object_deallocate(copy_object);
2424 					vm_object_lock(object);
2425 
2426 					continue;
2427 				}
2428 				/*
2429 				 * Pick back up the old object's
2430 				 * lock.  [It is safe to do so,
2431 				 * since it must be deeper in the
2432 				 * object tree.]
2433 				 */
2434 				vm_object_lock(object);
2435 			}
2436 
2437 			/*
2438 			 * Because we're pushing a page upward
2439 			 * in the object tree, we must restart
2440 			 * any faults that are waiting here.
2441 			 * [Note that this is an expansion of
2442 			 * PAGE_WAKEUP that uses the THREAD_RESTART
2443 			 * wait result].  Can't turn off the page's
2444 			 * busy bit because we're not done with it.
2445 			 */
2446 			if (m->vmp_wanted) {
2447 				m->vmp_wanted = FALSE;
2448 				thread_wakeup_with_result((event_t) m, THREAD_RESTART);
2449 			}
2450 		}
2451 		/*
2452 		 * The reference count on copy_object must be
2453 		 * at least 2: one for our extra reference,
2454 		 * and at least one from the outside world
2455 		 * (we checked that when we last locked
2456 		 * copy_object).
2457 		 */
2458 		vm_object_lock_assert_exclusive(copy_object);
2459 		copy_object->ref_count--;
2460 		assert(copy_object->ref_count > 0);
2461 
2462 		vm_object_unlock(copy_object);
2463 
2464 		break;
2465 	}
2466 
2467 done:
2468 	*result_page = m;
2469 	*top_page = first_m;
2470 
2471 	if (m != VM_PAGE_NULL) {
2472 		assert(VM_PAGE_OBJECT(m) == object);
2473 
2474 		retval = VM_FAULT_SUCCESS;
2475 
2476 		if (my_fault == DBG_PAGEIN_FAULT) {
2477 			VM_PAGE_COUNT_AS_PAGEIN(m);
2478 
2479 			if (object->internal) {
2480 				my_fault = DBG_PAGEIND_FAULT;
2481 			} else {
2482 				my_fault = DBG_PAGEINV_FAULT;
2483 			}
2484 
2485 			/*
2486 			 * evaluate access pattern and update state
2487 			 * vm_fault_deactivate_behind depends on the
2488 			 * state being up to date
2489 			 */
2490 			vm_fault_is_sequential(object, offset, fault_info->behavior);
2491 			vm_fault_deactivate_behind(object, offset, fault_info->behavior);
2492 		} else if (type_of_fault == NULL && my_fault == DBG_CACHE_HIT_FAULT) {
2493 			/*
2494 			 * we weren't called from vm_fault, so handle the
2495 			 * accounting here for hits in the cache
2496 			 */
2497 			if (m->vmp_clustered) {
2498 				VM_PAGE_COUNT_AS_PAGEIN(m);
2499 				VM_PAGE_CONSUME_CLUSTERED(m);
2500 			}
2501 			vm_fault_is_sequential(object, offset, fault_info->behavior);
2502 			vm_fault_deactivate_behind(object, offset, fault_info->behavior);
2503 		} else if (my_fault == DBG_COMPRESSOR_FAULT || my_fault == DBG_COMPRESSOR_SWAPIN_FAULT) {
2504 			VM_STAT_DECOMPRESSIONS();
2505 		}
2506 		if (type_of_fault) {
2507 			*type_of_fault = my_fault;
2508 		}
2509 	} else {
2510 		ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_SUCCESS_NO_PAGE), 0 /* arg */);
2511 		retval = VM_FAULT_SUCCESS_NO_VM_PAGE;
2512 		assert(first_m == VM_PAGE_NULL);
2513 		assert(object == first_object);
2514 	}
2515 
2516 	thread_interrupt_level(interruptible_state);
2517 
2518 #if TRACEFAULTPAGE
2519 	dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS, 0);       /* (TEST/DEBUG) */
2520 #endif
2521 	return retval;
2522 
2523 backoff:
2524 	thread_interrupt_level(interruptible_state);
2525 
2526 	if (wait_result == THREAD_INTERRUPTED) {
2527 		ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_INTERRUPTED), 0 /* arg */);
2528 		return VM_FAULT_INTERRUPTED;
2529 	}
2530 	return VM_FAULT_RETRY;
2531 
2532 #undef  RELEASE_PAGE
2533 }
2534 
2535 #if MACH_ASSERT && (XNU_PLATFORM_WatchOS || __x86_64__)
2536 #define PANIC_ON_CS_KILLED_DEFAULT true
2537 #else
2538 #define PANIC_ON_CS_KILLED_DEFAULT false
2539 #endif
2540 static TUNABLE(bool, panic_on_cs_killed, "panic_on_cs_killed",
2541     PANIC_ON_CS_KILLED_DEFAULT);
2542 
2543 extern int proc_selfpid(void);
2544 extern char *proc_name_address(struct proc *p);
2545 extern char *proc_best_name(struct proc *);
2546 unsigned long cs_enter_tainted_rejected = 0;
2547 unsigned long cs_enter_tainted_accepted = 0;
2548 
2549 /*
2550  * CODE SIGNING:
2551  * When soft faulting a page, we have to validate the page if:
2552  * 1. the page is being mapped in user space
2553  * 2. the page hasn't already been found to be "tainted"
2554  * 3. the page belongs to a code-signed object
2555  * 4. the page has not been validated yet or has been mapped for write.
2556  */
2557 static bool
vm_fault_cs_need_validation(pmap_t pmap,vm_page_t page,vm_object_t page_obj,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset)2558 vm_fault_cs_need_validation(
2559 	pmap_t pmap,
2560 	vm_page_t page,
2561 	vm_object_t page_obj,
2562 	vm_map_size_t fault_page_size,
2563 	vm_map_offset_t fault_phys_offset)
2564 {
2565 	if (pmap == kernel_pmap) {
2566 		/* 1 - not user space */
2567 		return false;
2568 	}
2569 	if (!page_obj->code_signed) {
2570 		/* 3 - page does not belong to a code-signed object */
2571 		return false;
2572 	}
2573 	if (fault_page_size == PAGE_SIZE) {
2574 		/* looking at the whole page */
2575 		assertf(fault_phys_offset == 0,
2576 		    "fault_page_size 0x%llx fault_phys_offset 0x%llx\n",
2577 		    (uint64_t)fault_page_size,
2578 		    (uint64_t)fault_phys_offset);
2579 		if (page->vmp_cs_tainted == VMP_CS_ALL_TRUE) {
2580 			/* 2 - page is all tainted */
2581 			return false;
2582 		}
2583 		if (page->vmp_cs_validated == VMP_CS_ALL_TRUE &&
2584 		    !page->vmp_wpmapped) {
2585 			/* 4 - already fully validated and never mapped writable */
2586 			return false;
2587 		}
2588 	} else {
2589 		/* looking at a specific sub-page */
2590 		if (VMP_CS_TAINTED(page, fault_page_size, fault_phys_offset)) {
2591 			/* 2 - sub-page was already marked as tainted */
2592 			return false;
2593 		}
2594 		if (VMP_CS_VALIDATED(page, fault_page_size, fault_phys_offset) &&
2595 		    !page->vmp_wpmapped) {
2596 			/* 4 - already validated and never mapped writable */
2597 			return false;
2598 		}
2599 	}
2600 	/* page needs to be validated */
2601 	return true;
2602 }
2603 
2604 
2605 static bool
vm_fault_cs_page_immutable(vm_page_t m,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,vm_prot_t prot __unused)2606 vm_fault_cs_page_immutable(
2607 	vm_page_t m,
2608 	vm_map_size_t fault_page_size,
2609 	vm_map_offset_t fault_phys_offset,
2610 	vm_prot_t prot __unused)
2611 {
2612 	if (VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset)
2613 	    /*&& ((prot) & VM_PROT_EXECUTE)*/) {
2614 		return true;
2615 	}
2616 	return false;
2617 }
2618 
2619 static bool
vm_fault_cs_page_nx(vm_page_t m,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset)2620 vm_fault_cs_page_nx(
2621 	vm_page_t m,
2622 	vm_map_size_t fault_page_size,
2623 	vm_map_offset_t fault_phys_offset)
2624 {
2625 	return VMP_CS_NX(m, fault_page_size, fault_phys_offset);
2626 }
2627 
2628 /*
2629  * Check if the page being entered into the pmap violates code signing.
2630  */
2631 static kern_return_t
vm_fault_cs_check_violation(bool cs_bypass,vm_object_t object,vm_page_t m,pmap_t pmap,vm_prot_t prot,vm_prot_t caller_prot,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,vm_object_fault_info_t fault_info,bool map_is_switched,bool map_is_switch_protected,bool * cs_violation)2632 vm_fault_cs_check_violation(
2633 	bool cs_bypass,
2634 	vm_object_t object,
2635 	vm_page_t m,
2636 	pmap_t pmap,
2637 	vm_prot_t prot,
2638 	vm_prot_t caller_prot,
2639 	vm_map_size_t fault_page_size,
2640 	vm_map_offset_t fault_phys_offset,
2641 	vm_object_fault_info_t fault_info,
2642 	bool map_is_switched,
2643 	bool map_is_switch_protected,
2644 	bool *cs_violation)
2645 {
2646 #if !CODE_SIGNING_MONITOR
2647 #pragma unused(caller_prot)
2648 #pragma unused(fault_info)
2649 #endif /* !CODE_SIGNING_MONITOR */
2650 
2651 	int             cs_enforcement_enabled;
2652 	if (!cs_bypass &&
2653 	    vm_fault_cs_need_validation(pmap, m, object,
2654 	    fault_page_size, fault_phys_offset)) {
2655 		vm_object_lock_assert_exclusive(object);
2656 
2657 		if (VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset)) {
2658 			vm_cs_revalidates++;
2659 		}
2660 
2661 		/* VM map is locked, so 1 ref will remain on VM object -
2662 		 * so no harm if vm_page_validate_cs drops the object lock */
2663 
2664 #if CODE_SIGNING_MONITOR
2665 		if (fault_info->csm_associated &&
2666 		    csm_enabled() &&
2667 		    !VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) &&
2668 		    !VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset) &&
2669 		    !VMP_CS_NX(m, fault_page_size, fault_phys_offset) &&
2670 		    (prot & VM_PROT_EXECUTE) &&
2671 		    (caller_prot & VM_PROT_EXECUTE)) {
2672 			/*
2673 			 * When we have a code signing monitor, the monitor will evaluate the code signature
2674 			 * for any executable page mapping. No need for the VM to also validate the page.
2675 			 * In the code signing monitor we trust :)
2676 			 */
2677 			vm_cs_defer_to_csm++;
2678 		} else {
2679 			vm_cs_defer_to_csm_not++;
2680 			vm_page_validate_cs(m, fault_page_size, fault_phys_offset);
2681 		}
2682 #else /* CODE_SIGNING_MONITOR */
2683 		vm_page_validate_cs(m, fault_page_size, fault_phys_offset);
2684 #endif /* CODE_SIGNING_MONITOR */
2685 	}
2686 
2687 	/* If the map is switched, and is switch-protected, we must protect
2688 	 * some pages from being write-faulted: immutable pages because by
2689 	 * definition they may not be written, and executable pages because that
2690 	 * would provide a way to inject unsigned code.
2691 	 * If the page is immutable, we can simply return. However, we can't
2692 	 * immediately determine whether a page is executable anywhere. But,
2693 	 * we can disconnect it everywhere and remove the executable protection
2694 	 * from the current map. We do that below right before we do the
2695 	 * PMAP_ENTER.
2696 	 */
2697 	if (pmap == kernel_pmap) {
2698 		/* kernel fault: cs_enforcement does not apply */
2699 		cs_enforcement_enabled = 0;
2700 	} else {
2701 		cs_enforcement_enabled = pmap_get_vm_map_cs_enforced(pmap);
2702 	}
2703 
2704 	if (cs_enforcement_enabled && map_is_switched &&
2705 	    map_is_switch_protected &&
2706 	    vm_fault_cs_page_immutable(m, fault_page_size, fault_phys_offset, prot) &&
2707 	    (prot & VM_PROT_WRITE)) {
2708 		ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAILED_IMMUTABLE_PAGE_WRITE), 0 /* arg */);
2709 		return KERN_CODESIGN_ERROR;
2710 	}
2711 
2712 	if (cs_enforcement_enabled &&
2713 	    vm_fault_cs_page_nx(m, fault_page_size, fault_phys_offset) &&
2714 	    (prot & VM_PROT_EXECUTE)) {
2715 		if (cs_debug) {
2716 			printf("page marked to be NX, not letting it be mapped EXEC\n");
2717 		}
2718 		ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAILED_NX_PAGE_EXEC_MAPPING), 0 /* arg */);
2719 		return KERN_CODESIGN_ERROR;
2720 	}
2721 
2722 	/* A page could be tainted, or pose a risk of being tainted later.
2723 	 * Check whether the receiving process wants it, and make it feel
2724 	 * the consequences (that hapens in cs_invalid_page()).
2725 	 * For CS Enforcement, two other conditions will
2726 	 * cause that page to be tainted as well:
2727 	 * - pmapping an unsigned page executable - this means unsigned code;
2728 	 * - writeable mapping of a validated page - the content of that page
2729 	 *   can be changed without the kernel noticing, therefore unsigned
2730 	 *   code can be created
2731 	 */
2732 	if (cs_bypass) {
2733 		/* code-signing is bypassed */
2734 		*cs_violation = FALSE;
2735 	} else if (VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset)) {
2736 		/* tainted page */
2737 		*cs_violation = TRUE;
2738 	} else if (!cs_enforcement_enabled) {
2739 		/* no further code-signing enforcement */
2740 		*cs_violation = FALSE;
2741 	} else if (vm_fault_cs_page_immutable(m, fault_page_size, fault_phys_offset, prot) &&
2742 	    ((prot & VM_PROT_WRITE) ||
2743 	    m->vmp_wpmapped)) {
2744 		/*
2745 		 * The page should be immutable, but is in danger of being
2746 		 * modified.
2747 		 * This is the case where we want policy from the code
2748 		 * directory - is the page immutable or not? For now we have
2749 		 * to assume that code pages will be immutable, data pages not.
2750 		 * We'll assume a page is a code page if it has a code directory
2751 		 * and we fault for execution.
2752 		 * That is good enough since if we faulted the code page for
2753 		 * writing in another map before, it is wpmapped; if we fault
2754 		 * it for writing in this map later it will also be faulted for
2755 		 * executing at the same time; and if we fault for writing in
2756 		 * another map later, we will disconnect it from this pmap so
2757 		 * we'll notice the change.
2758 		 */
2759 		*cs_violation = TRUE;
2760 	} else if (!VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) &&
2761 	    (prot & VM_PROT_EXECUTE)
2762 #if CODE_SIGNING_MONITOR
2763 	    /*
2764 	     * Executable pages will be validated by the code signing monitor. If the
2765 	     * code signing monitor is turned off, then this is a code-signing violation.
2766 	     */
2767 	    && !csm_enabled()
2768 #endif /* CODE_SIGNING_MONITOR */
2769 	    ) {
2770 		*cs_violation = TRUE;
2771 	} else {
2772 		*cs_violation = FALSE;
2773 	}
2774 	return KERN_SUCCESS;
2775 }
2776 
2777 /*
2778  * Handles a code signing violation by either rejecting the page or forcing a disconnect.
2779  * @param must_disconnect This value will be set to true if the caller must disconnect
2780  * this page.
2781  * @return If this function does not return KERN_SUCCESS, the caller must abort the page fault.
2782  */
2783 static kern_return_t
vm_fault_cs_handle_violation(vm_object_t object,vm_page_t m,pmap_t pmap,vm_prot_t prot,vm_map_offset_t vaddr,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,bool map_is_switched,bool map_is_switch_protected,bool * must_disconnect)2784 vm_fault_cs_handle_violation(
2785 	vm_object_t object,
2786 	vm_page_t m,
2787 	pmap_t pmap,
2788 	vm_prot_t prot,
2789 	vm_map_offset_t vaddr,
2790 	vm_map_size_t fault_page_size,
2791 	vm_map_offset_t fault_phys_offset,
2792 	bool map_is_switched,
2793 	bool map_is_switch_protected,
2794 	bool *must_disconnect)
2795 {
2796 #if !MACH_ASSERT
2797 #pragma unused(pmap)
2798 #pragma unused(map_is_switch_protected)
2799 #endif /* !MACH_ASSERT */
2800 	/*
2801 	 * We will have a tainted page. Have to handle the special case
2802 	 * of a switched map now. If the map is not switched, standard
2803 	 * procedure applies - call cs_invalid_page().
2804 	 * If the map is switched, the real owner is invalid already.
2805 	 * There is no point in invalidating the switching process since
2806 	 * it will not be executing from the map. So we don't call
2807 	 * cs_invalid_page() in that case.
2808 	 */
2809 	boolean_t reject_page, cs_killed;
2810 	kern_return_t kr;
2811 	if (map_is_switched) {
2812 		assert(pmap == vm_map_pmap(current_thread()->map));
2813 		assert(!(prot & VM_PROT_WRITE) || (map_is_switch_protected == FALSE));
2814 		reject_page = FALSE;
2815 	} else {
2816 		if (cs_debug > 5) {
2817 			printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s prot: 0x%x\n",
2818 			    object->code_signed ? "yes" : "no",
2819 			    VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) ? "yes" : "no",
2820 			    VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset) ? "yes" : "no",
2821 			    m->vmp_wpmapped ? "yes" : "no",
2822 			    (int)prot);
2823 		}
2824 		reject_page = cs_invalid_page((addr64_t) vaddr, &cs_killed);
2825 	}
2826 
2827 	if (reject_page) {
2828 		/* reject the invalid page: abort the page fault */
2829 		int                     pid;
2830 		const char              *procname;
2831 		task_t                  task;
2832 		vm_object_t             file_object, shadow;
2833 		vm_object_offset_t      file_offset;
2834 		char                    *pathname, *filename;
2835 		vm_size_t               pathname_len, filename_len;
2836 		boolean_t               truncated_path;
2837 #define __PATH_MAX 1024
2838 		struct timespec         mtime, cs_mtime;
2839 		int                     shadow_depth;
2840 		os_reason_t             codesigning_exit_reason = OS_REASON_NULL;
2841 
2842 		kr = KERN_CODESIGN_ERROR;
2843 		cs_enter_tainted_rejected++;
2844 
2845 		/* get process name and pid */
2846 		procname = "?";
2847 		task = current_task();
2848 		pid = proc_selfpid();
2849 		if (get_bsdtask_info(task) != NULL) {
2850 			procname = proc_name_address(get_bsdtask_info(task));
2851 		}
2852 
2853 		/* get file's VM object */
2854 		file_object = object;
2855 		file_offset = m->vmp_offset;
2856 		for (shadow = file_object->shadow,
2857 		    shadow_depth = 0;
2858 		    shadow != VM_OBJECT_NULL;
2859 		    shadow = file_object->shadow,
2860 		    shadow_depth++) {
2861 			vm_object_lock_shared(shadow);
2862 			if (file_object != object) {
2863 				vm_object_unlock(file_object);
2864 			}
2865 			file_offset += file_object->vo_shadow_offset;
2866 			file_object = shadow;
2867 		}
2868 
2869 		mtime.tv_sec = 0;
2870 		mtime.tv_nsec = 0;
2871 		cs_mtime.tv_sec = 0;
2872 		cs_mtime.tv_nsec = 0;
2873 
2874 		/* get file's pathname and/or filename */
2875 		pathname = NULL;
2876 		filename = NULL;
2877 		pathname_len = 0;
2878 		filename_len = 0;
2879 		truncated_path = FALSE;
2880 		/* no pager -> no file -> no pathname, use "<nil>" in that case */
2881 		if (file_object->pager != NULL) {
2882 			pathname = kalloc_data(__PATH_MAX * 2, Z_WAITOK);
2883 			if (pathname) {
2884 				pathname[0] = '\0';
2885 				pathname_len = __PATH_MAX;
2886 				filename = pathname + pathname_len;
2887 				filename_len = __PATH_MAX;
2888 
2889 				if (vnode_pager_get_object_name(file_object->pager,
2890 				    pathname,
2891 				    pathname_len,
2892 				    filename,
2893 				    filename_len,
2894 				    &truncated_path) == KERN_SUCCESS) {
2895 					/* safety first... */
2896 					pathname[__PATH_MAX - 1] = '\0';
2897 					filename[__PATH_MAX - 1] = '\0';
2898 
2899 					vnode_pager_get_object_mtime(file_object->pager,
2900 					    &mtime,
2901 					    &cs_mtime);
2902 				} else {
2903 					kfree_data(pathname, __PATH_MAX * 2);
2904 					pathname = NULL;
2905 					filename = NULL;
2906 					pathname_len = 0;
2907 					filename_len = 0;
2908 					truncated_path = FALSE;
2909 				}
2910 			}
2911 		}
2912 		printf("CODE SIGNING: process %d[%s]: "
2913 		    "rejecting invalid page at address 0x%llx "
2914 		    "from offset 0x%llx in file \"%s%s%s\" "
2915 		    "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2916 		    "(signed:%d validated:%d tainted:%d nx:%d "
2917 		    "wpmapped:%d dirty:%d depth:%d)\n",
2918 		    pid, procname, (addr64_t) vaddr,
2919 		    file_offset,
2920 		    (pathname ? pathname : "<nil>"),
2921 		    (truncated_path ? "/.../" : ""),
2922 		    (truncated_path ? filename : ""),
2923 		    cs_mtime.tv_sec, cs_mtime.tv_nsec,
2924 		    ((cs_mtime.tv_sec == mtime.tv_sec &&
2925 		    cs_mtime.tv_nsec == mtime.tv_nsec)
2926 		    ? "=="
2927 		    : "!="),
2928 		    mtime.tv_sec, mtime.tv_nsec,
2929 		    object->code_signed,
2930 		    VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset),
2931 		    VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset),
2932 		    VMP_CS_NX(m, fault_page_size, fault_phys_offset),
2933 		    m->vmp_wpmapped,
2934 		    m->vmp_dirty,
2935 		    shadow_depth);
2936 
2937 		/*
2938 		 * We currently only generate an exit reason if cs_invalid_page directly killed a process. If cs_invalid_page
2939 		 * did not kill the process (more the case on desktop), vm_fault_enter will not satisfy the fault and whether the
2940 		 * process dies is dependent on whether there is a signal handler registered for SIGSEGV and how that handler
2941 		 * will deal with the segmentation fault.
2942 		 */
2943 		if (cs_killed) {
2944 			KDBG(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
2945 			    pid, OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_INVALID_PAGE);
2946 
2947 			codesigning_exit_reason = os_reason_create(OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_INVALID_PAGE);
2948 			if (codesigning_exit_reason == NULL) {
2949 				printf("vm_fault_enter: failed to allocate codesigning exit reason\n");
2950 			} else {
2951 				mach_vm_address_t data_addr = 0;
2952 				struct codesigning_exit_reason_info *ceri = NULL;
2953 				uint32_t reason_buffer_size_estimate = kcdata_estimate_required_buffer_size(1, sizeof(*ceri));
2954 
2955 				if (os_reason_alloc_buffer_noblock(codesigning_exit_reason, reason_buffer_size_estimate)) {
2956 					printf("vm_fault_enter: failed to allocate buffer for codesigning exit reason\n");
2957 				} else {
2958 					if (KERN_SUCCESS == kcdata_get_memory_addr(&codesigning_exit_reason->osr_kcd_descriptor,
2959 					    EXIT_REASON_CODESIGNING_INFO, sizeof(*ceri), &data_addr)) {
2960 						ceri = (struct codesigning_exit_reason_info *)data_addr;
2961 						static_assert(__PATH_MAX == sizeof(ceri->ceri_pathname));
2962 
2963 						ceri->ceri_virt_addr = vaddr;
2964 						ceri->ceri_file_offset = file_offset;
2965 						if (pathname) {
2966 							strncpy((char *)&ceri->ceri_pathname, pathname, sizeof(ceri->ceri_pathname));
2967 						} else {
2968 							ceri->ceri_pathname[0] = '\0';
2969 						}
2970 						if (filename) {
2971 							strncpy((char *)&ceri->ceri_filename, filename, sizeof(ceri->ceri_filename));
2972 						} else {
2973 							ceri->ceri_filename[0] = '\0';
2974 						}
2975 						ceri->ceri_path_truncated = (truncated_path ? 1 : 0);
2976 						ceri->ceri_codesig_modtime_secs = cs_mtime.tv_sec;
2977 						ceri->ceri_codesig_modtime_nsecs = cs_mtime.tv_nsec;
2978 						ceri->ceri_page_modtime_secs = mtime.tv_sec;
2979 						ceri->ceri_page_modtime_nsecs = mtime.tv_nsec;
2980 						ceri->ceri_object_codesigned = (object->code_signed);
2981 						ceri->ceri_page_codesig_validated = VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset);
2982 						ceri->ceri_page_codesig_tainted = VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset);
2983 						ceri->ceri_page_codesig_nx = VMP_CS_NX(m, fault_page_size, fault_phys_offset);
2984 						ceri->ceri_page_wpmapped = (m->vmp_wpmapped);
2985 						ceri->ceri_page_slid = 0;
2986 						ceri->ceri_page_dirty = (m->vmp_dirty);
2987 						ceri->ceri_page_shadow_depth = shadow_depth;
2988 					} else {
2989 #if DEBUG || DEVELOPMENT
2990 						panic("vm_fault_enter: failed to allocate kcdata for codesigning exit reason");
2991 #else
2992 						printf("vm_fault_enter: failed to allocate kcdata for codesigning exit reason\n");
2993 #endif /* DEBUG || DEVELOPMENT */
2994 						/* Free the buffer */
2995 						os_reason_alloc_buffer_noblock(codesigning_exit_reason, 0);
2996 					}
2997 				}
2998 			}
2999 
3000 			set_thread_exit_reason(current_thread(), codesigning_exit_reason, FALSE);
3001 		}
3002 		if (panic_on_cs_killed &&
3003 		    object->object_is_shared_cache) {
3004 			char *tainted_contents;
3005 			vm_map_offset_t src_vaddr;
3006 			src_vaddr = (vm_map_offset_t) phystokv((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m) << PAGE_SHIFT);
3007 			tainted_contents = kalloc_data(PAGE_SIZE, Z_WAITOK);
3008 			bcopy((const char *)src_vaddr, tainted_contents, PAGE_SIZE);
3009 			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);
3010 			panic("CODE SIGNING: process %d[%s]: "
3011 			    "rejecting invalid page (phys#0x%x) at address 0x%llx "
3012 			    "from offset 0x%llx in file \"%s%s%s\" "
3013 			    "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
3014 			    "(signed:%d validated:%d tainted:%d nx:%d"
3015 			    "wpmapped:%d dirty:%d depth:%d)\n",
3016 			    pid, procname,
3017 			    VM_PAGE_GET_PHYS_PAGE(m),
3018 			    (addr64_t) vaddr,
3019 			    file_offset,
3020 			    (pathname ? pathname : "<nil>"),
3021 			    (truncated_path ? "/.../" : ""),
3022 			    (truncated_path ? filename : ""),
3023 			    cs_mtime.tv_sec, cs_mtime.tv_nsec,
3024 			    ((cs_mtime.tv_sec == mtime.tv_sec &&
3025 			    cs_mtime.tv_nsec == mtime.tv_nsec)
3026 			    ? "=="
3027 			    : "!="),
3028 			    mtime.tv_sec, mtime.tv_nsec,
3029 			    object->code_signed,
3030 			    VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset),
3031 			    VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset),
3032 			    VMP_CS_NX(m, fault_page_size, fault_phys_offset),
3033 			    m->vmp_wpmapped,
3034 			    m->vmp_dirty,
3035 			    shadow_depth);
3036 		}
3037 
3038 		if (file_object != object) {
3039 			vm_object_unlock(file_object);
3040 		}
3041 		if (pathname_len != 0) {
3042 			kfree_data(pathname, __PATH_MAX * 2);
3043 			pathname = NULL;
3044 			filename = NULL;
3045 		}
3046 	} else {
3047 		/* proceed with the invalid page */
3048 		kr = KERN_SUCCESS;
3049 		if (!VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) &&
3050 		    !object->code_signed) {
3051 			/*
3052 			 * This page has not been (fully) validated but
3053 			 * does not belong to a code-signed object
3054 			 * so it should not be forcefully considered
3055 			 * as tainted.
3056 			 * We're just concerned about it here because
3057 			 * we've been asked to "execute" it but that
3058 			 * does not mean that it should cause other
3059 			 * accesses to fail.
3060 			 * This happens when a debugger sets a
3061 			 * breakpoint and we then execute code in
3062 			 * that page.  Marking the page as "tainted"
3063 			 * would cause any inspection tool ("leaks",
3064 			 * "vmmap", "CrashReporter", ...) to get killed
3065 			 * due to code-signing violation on that page,
3066 			 * even though they're just reading it and not
3067 			 * executing from it.
3068 			 */
3069 		} else {
3070 			/*
3071 			 * Page might have been tainted before or not;
3072 			 * now it definitively is. If the page wasn't
3073 			 * tainted, we must disconnect it from all
3074 			 * pmaps later, to force existing mappings
3075 			 * through that code path for re-consideration
3076 			 * of the validity of that page.
3077 			 */
3078 			if (!VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset)) {
3079 				*must_disconnect = TRUE;
3080 				VMP_CS_SET_TAINTED(m, fault_page_size, fault_phys_offset, TRUE);
3081 			}
3082 		}
3083 		cs_enter_tainted_accepted++;
3084 	}
3085 	if (kr != KERN_SUCCESS) {
3086 		if (cs_debug) {
3087 			printf("CODESIGNING: vm_fault_enter(0x%llx): "
3088 			    "*** INVALID PAGE ***\n",
3089 			    (long long)vaddr);
3090 		}
3091 #if !SECURE_KERNEL
3092 		if (cs_enforcement_panic) {
3093 			panic("CODESIGNING: panicking on invalid page");
3094 		}
3095 #endif
3096 	}
3097 	return kr;
3098 }
3099 
3100 /*
3101  * Check that the code signature is valid for the given page being inserted into
3102  * the pmap.
3103  *
3104  * @param must_disconnect This value will be set to true if the caller must disconnect
3105  * this page.
3106  * @return If this function does not return KERN_SUCCESS, the caller must abort the page fault.
3107  */
3108 static kern_return_t
vm_fault_validate_cs(bool cs_bypass,vm_object_t object,vm_page_t m,pmap_t pmap,vm_map_offset_t vaddr,vm_prot_t prot,vm_prot_t caller_prot,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,vm_object_fault_info_t fault_info,bool * must_disconnect)3109 vm_fault_validate_cs(
3110 	bool cs_bypass,
3111 	vm_object_t object,
3112 	vm_page_t m,
3113 	pmap_t pmap,
3114 	vm_map_offset_t vaddr,
3115 	vm_prot_t prot,
3116 	vm_prot_t caller_prot,
3117 	vm_map_size_t fault_page_size,
3118 	vm_map_offset_t fault_phys_offset,
3119 	vm_object_fault_info_t fault_info,
3120 	bool *must_disconnect)
3121 {
3122 	bool map_is_switched, map_is_switch_protected, cs_violation;
3123 	kern_return_t kr;
3124 	/* Validate code signature if necessary. */
3125 	map_is_switched = ((pmap != vm_map_pmap(current_task()->map)) &&
3126 	    (pmap == vm_map_pmap(current_thread()->map)));
3127 	map_is_switch_protected = current_thread()->map->switch_protect;
3128 	kr = vm_fault_cs_check_violation(cs_bypass, object, m, pmap,
3129 	    prot, caller_prot, fault_page_size, fault_phys_offset, fault_info,
3130 	    map_is_switched, map_is_switch_protected, &cs_violation);
3131 	if (kr != KERN_SUCCESS) {
3132 		return kr;
3133 	}
3134 	if (cs_violation) {
3135 		kr = vm_fault_cs_handle_violation(object, m, pmap, prot, vaddr,
3136 		    fault_page_size, fault_phys_offset,
3137 		    map_is_switched, map_is_switch_protected, must_disconnect);
3138 	}
3139 	return kr;
3140 }
3141 
3142 /*
3143  * Enqueue the page on the appropriate paging queue.
3144  */
3145 static void
vm_fault_enqueue_page(vm_object_t object,vm_page_t m,bool wired,bool change_wiring,vm_tag_t wire_tag,bool no_cache,int * type_of_fault,kern_return_t kr)3146 vm_fault_enqueue_page(
3147 	vm_object_t object,
3148 	vm_page_t m,
3149 	bool wired,
3150 	bool change_wiring,
3151 	vm_tag_t wire_tag,
3152 	bool no_cache,
3153 	int *type_of_fault,
3154 	kern_return_t kr)
3155 {
3156 	assert((m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) || object != compressor_object);
3157 	boolean_t       page_queues_locked = FALSE;
3158 	boolean_t       previously_pmapped = m->vmp_pmapped;
3159 #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED()   \
3160 MACRO_BEGIN                                     \
3161 	if (! page_queues_locked) {             \
3162 	        page_queues_locked = TRUE;      \
3163 	        vm_page_lockspin_queues();      \
3164 	}                                       \
3165 MACRO_END
3166 #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED()     \
3167 MACRO_BEGIN                                     \
3168 	if (page_queues_locked) {               \
3169 	        page_queues_locked = FALSE;     \
3170 	        vm_page_unlock_queues();        \
3171 	}                                       \
3172 MACRO_END
3173 
3174 	vm_page_update_special_state(m);
3175 	if (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) {
3176 		/*
3177 		 * Compressor pages are neither wired
3178 		 * nor pageable and should never change.
3179 		 */
3180 		assert(object == compressor_object);
3181 	} else if (change_wiring) {
3182 		__VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3183 
3184 		if (wired) {
3185 			if (kr == KERN_SUCCESS) {
3186 				vm_page_wire(m, wire_tag, TRUE);
3187 			}
3188 		} else {
3189 			vm_page_unwire(m, TRUE);
3190 		}
3191 		/* we keep the page queues lock, if we need it later */
3192 	} else {
3193 		if (object->internal == TRUE) {
3194 			/*
3195 			 * don't allow anonymous pages on
3196 			 * the speculative queues
3197 			 */
3198 			no_cache = FALSE;
3199 		}
3200 		if (kr != KERN_SUCCESS) {
3201 			__VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3202 			vm_page_deactivate(m);
3203 			/* we keep the page queues lock, if we need it later */
3204 		} else if (((m->vmp_q_state == VM_PAGE_NOT_ON_Q) ||
3205 		    (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) ||
3206 		    (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) ||
3207 		    ((m->vmp_q_state != VM_PAGE_ON_THROTTLED_Q) && no_cache)) &&
3208 		    !VM_PAGE_WIRED(m)) {
3209 			if (vm_page_local_q &&
3210 			    (*type_of_fault == DBG_COW_FAULT ||
3211 			    *type_of_fault == DBG_ZERO_FILL_FAULT)) {
3212 				struct vpl      *lq;
3213 				uint32_t        lid;
3214 
3215 				assert(m->vmp_q_state == VM_PAGE_NOT_ON_Q);
3216 
3217 				__VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3218 				vm_object_lock_assert_exclusive(object);
3219 
3220 				/*
3221 				 * we got a local queue to stuff this
3222 				 * new page on...
3223 				 * its safe to manipulate local and
3224 				 * local_id at this point since we're
3225 				 * behind an exclusive object lock and
3226 				 * the page is not on any global queue.
3227 				 *
3228 				 * we'll use the current cpu number to
3229 				 * select the queue note that we don't
3230 				 * need to disable preemption... we're
3231 				 * going to be behind the local queue's
3232 				 * lock to do the real work
3233 				 */
3234 				lid = cpu_number();
3235 
3236 				lq = zpercpu_get_cpu(vm_page_local_q, lid);
3237 
3238 				VPL_LOCK(&lq->vpl_lock);
3239 
3240 				vm_page_check_pageable_safe(m);
3241 				vm_page_queue_enter(&lq->vpl_queue, m, vmp_pageq);
3242 				m->vmp_q_state = VM_PAGE_ON_ACTIVE_LOCAL_Q;
3243 				m->vmp_local_id = lid;
3244 				lq->vpl_count++;
3245 
3246 				if (object->internal) {
3247 					lq->vpl_internal_count++;
3248 				} else {
3249 					lq->vpl_external_count++;
3250 				}
3251 
3252 				VPL_UNLOCK(&lq->vpl_lock);
3253 
3254 				if (lq->vpl_count > vm_page_local_q_soft_limit) {
3255 					/*
3256 					 * we're beyond the soft limit
3257 					 * for the local queue
3258 					 * vm_page_reactivate_local will
3259 					 * 'try' to take the global page
3260 					 * queue lock... if it can't
3261 					 * that's ok... we'll let the
3262 					 * queue continue to grow up
3263 					 * to the hard limit... at that
3264 					 * point we'll wait for the
3265 					 * lock... once we've got the
3266 					 * lock, we'll transfer all of
3267 					 * the pages from the local
3268 					 * queue to the global active
3269 					 * queue
3270 					 */
3271 					vm_page_reactivate_local(lid, FALSE, FALSE);
3272 				}
3273 			} else {
3274 				__VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3275 
3276 				/*
3277 				 * test again now that we hold the
3278 				 * page queue lock
3279 				 */
3280 				if (!VM_PAGE_WIRED(m)) {
3281 					if (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) {
3282 						vm_page_queues_remove(m, FALSE);
3283 
3284 						VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated, 1);
3285 						VM_PAGEOUT_DEBUG(vm_pageout_cleaned_fault_reactivated, 1);
3286 					}
3287 
3288 					if (!VM_PAGE_ACTIVE_OR_INACTIVE(m) ||
3289 					    no_cache) {
3290 						/*
3291 						 * If this is a no_cache mapping
3292 						 * and the page has never been
3293 						 * mapped before or was
3294 						 * previously a no_cache page,
3295 						 * then we want to leave pages
3296 						 * in the speculative state so
3297 						 * that they can be readily
3298 						 * recycled if free memory runs
3299 						 * low.  Otherwise the page is
3300 						 * activated as normal.
3301 						 */
3302 
3303 						if (no_cache &&
3304 						    (!previously_pmapped ||
3305 						    m->vmp_no_cache)) {
3306 							m->vmp_no_cache = TRUE;
3307 
3308 							if (m->vmp_q_state != VM_PAGE_ON_SPECULATIVE_Q) {
3309 								vm_page_speculate(m, FALSE);
3310 							}
3311 						} else if (!VM_PAGE_ACTIVE_OR_INACTIVE(m)) {
3312 							vm_page_activate(m);
3313 						}
3314 					}
3315 				}
3316 				/* we keep the page queues lock, if we need it later */
3317 			}
3318 		}
3319 	}
3320 	/* we're done with the page queues lock, if we ever took it */
3321 	__VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3322 }
3323 
3324 /*
3325  * Sets the pmmpped, xpmapped, and wpmapped bits on the vm_page_t and updates accounting.
3326  * @return true if the page needs to be sync'ed via pmap_sync-page_data_physo
3327  * before being inserted into the pmap.
3328  */
3329 static bool
vm_fault_enter_set_mapped(vm_object_t object,vm_page_t m,vm_prot_t prot,vm_prot_t fault_type)3330 vm_fault_enter_set_mapped(
3331 	vm_object_t object,
3332 	vm_page_t m,
3333 	vm_prot_t prot,
3334 	vm_prot_t fault_type)
3335 {
3336 	bool page_needs_sync = false;
3337 	/*
3338 	 * NOTE: we may only hold the vm_object lock SHARED
3339 	 * at this point, so we need the phys_page lock to
3340 	 * properly serialize updating the pmapped and
3341 	 * xpmapped bits
3342 	 */
3343 	if ((prot & VM_PROT_EXECUTE) && !m->vmp_xpmapped) {
3344 		ppnum_t phys_page = VM_PAGE_GET_PHYS_PAGE(m);
3345 
3346 		pmap_lock_phys_page(phys_page);
3347 		m->vmp_pmapped = TRUE;
3348 
3349 		if (!m->vmp_xpmapped) {
3350 			m->vmp_xpmapped = TRUE;
3351 
3352 			pmap_unlock_phys_page(phys_page);
3353 
3354 			if (!object->internal) {
3355 				OSAddAtomic(1, &vm_page_xpmapped_external_count);
3356 			}
3357 
3358 #if defined(__arm64__)
3359 			page_needs_sync = true;
3360 #else
3361 			if (object->internal &&
3362 			    object->pager != NULL) {
3363 				/*
3364 				 * This page could have been
3365 				 * uncompressed by the
3366 				 * compressor pager and its
3367 				 * contents might be only in
3368 				 * the data cache.
3369 				 * Since it's being mapped for
3370 				 * "execute" for the fist time,
3371 				 * make sure the icache is in
3372 				 * sync.
3373 				 */
3374 				assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);
3375 				page_needs_sync = true;
3376 			}
3377 #endif
3378 		} else {
3379 			pmap_unlock_phys_page(phys_page);
3380 		}
3381 	} else {
3382 		if (m->vmp_pmapped == FALSE) {
3383 			ppnum_t phys_page = VM_PAGE_GET_PHYS_PAGE(m);
3384 
3385 			pmap_lock_phys_page(phys_page);
3386 			m->vmp_pmapped = TRUE;
3387 			pmap_unlock_phys_page(phys_page);
3388 		}
3389 	}
3390 
3391 	if (fault_type & VM_PROT_WRITE) {
3392 		if (m->vmp_wpmapped == FALSE) {
3393 			vm_object_lock_assert_exclusive(object);
3394 			if (!object->internal && object->pager) {
3395 				task_update_logical_writes(current_task(), PAGE_SIZE, TASK_WRITE_DEFERRED, vnode_pager_lookup_vnode(object->pager));
3396 			}
3397 			m->vmp_wpmapped = TRUE;
3398 		}
3399 	}
3400 	return page_needs_sync;
3401 }
3402 
3403 #if CODE_SIGNING_MONITOR && !XNU_PLATFORM_MacOSX
3404 #define KILL_FOR_CSM_VIOLATION 1
3405 #else /* CODE_SIGNING_MONITOR && !XNU_PLATFORM_MacOSX */
3406 #define KILL_FOR_CSM_VIOLATION 0
3407 #endif /* CODE_SIGNING_MONITOR && !XNU_PLATFORM_MacOSX */
3408 
3409 #if KILL_FOR_CSM_VIOLATION
3410 static void
vm_fault_kill_for_csm_violation(pmap_t pmap,vm_map_offset_t vaddr,vm_prot_t prot,vm_prot_t fault_type)3411 vm_fault_kill_for_csm_violation(
3412 	pmap_t pmap,
3413 	vm_map_offset_t vaddr,
3414 	vm_prot_t prot,
3415 	vm_prot_t fault_type)
3416 {
3417 	void *p;
3418 	char *pname;
3419 
3420 	if (pmap_is_nested(pmap)) {
3421 		panic("code-signing violation for nested pmap %p vaddr 0x%llx prot 0x%x fault 0x%x", pmap, (uint64_t)vaddr, prot, fault_type);
3422 	}
3423 	p = get_bsdtask_info(current_task());
3424 	pname = p ? proc_best_name(p) : "?";
3425 	printf("CODESIGNING: killing %d[%s] for CSM violation at vaddr 0x%llx prot 0x%x fault 0x%x\n",
3426 	    proc_selfpid(), pname,
3427 	    (uint64_t)vaddr, prot, fault_type);
3428 	task_bsdtask_kill(current_task());
3429 }
3430 #endif /* KILL_FOR_CSM_VIOLATION */
3431 
3432 /*
3433  * wrapper for pmap_enter_options()
3434  */
3435 static kern_return_t
pmap_enter_options_check(pmap_t pmap,vm_map_address_t virtual_address,vm_map_offset_t fault_phys_offset,vm_page_t page,vm_prot_t protection,vm_prot_t fault_type,unsigned int flags,boolean_t wired,unsigned int options)3436 pmap_enter_options_check(
3437 	pmap_t           pmap,
3438 	vm_map_address_t virtual_address,
3439 	vm_map_offset_t  fault_phys_offset,
3440 	vm_page_t        page,
3441 	vm_prot_t        protection,
3442 	vm_prot_t        fault_type,
3443 	unsigned int     flags,
3444 	boolean_t        wired,
3445 	unsigned int     options)
3446 {
3447 	int             extra_options = 0;
3448 	vm_object_t     obj;
3449 
3450 	if (page->vmp_error) {
3451 		return KERN_MEMORY_FAILURE;
3452 	}
3453 	obj = VM_PAGE_OBJECT(page);
3454 	if (obj->internal) {
3455 		extra_options |= PMAP_OPTIONS_INTERNAL;
3456 	}
3457 	if (page->vmp_reusable || obj->all_reusable) {
3458 		extra_options |= PMAP_OPTIONS_REUSABLE;
3459 	}
3460 	return pmap_enter_options_addr(pmap,
3461 	           virtual_address,
3462 	           (pmap_paddr_t)ptoa(VM_PAGE_GET_PHYS_PAGE(page)) + fault_phys_offset,
3463 	           protection,
3464 	           fault_type,
3465 	           flags,
3466 	           wired,
3467 	           options | extra_options,
3468 	           NULL);
3469 }
3470 
3471 /*
3472  * Try to enter the given page into the pmap.
3473  * Will retry without execute permission if the code signing monitor is enabled and
3474  * we encounter a codesigning failure on a non-execute fault.
3475  */
3476 static kern_return_t
vm_fault_attempt_pmap_enter(pmap_t pmap,vm_map_offset_t vaddr,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,vm_page_t m,vm_prot_t * prot,vm_prot_t caller_prot,vm_prot_t fault_type,bool wired,int pmap_options)3477 vm_fault_attempt_pmap_enter(
3478 	pmap_t pmap,
3479 	vm_map_offset_t vaddr,
3480 	vm_map_size_t fault_page_size,
3481 	vm_map_offset_t fault_phys_offset,
3482 	vm_page_t m,
3483 	vm_prot_t *prot,
3484 	vm_prot_t caller_prot,
3485 	vm_prot_t fault_type,
3486 	bool wired,
3487 	int pmap_options)
3488 {
3489 #if !CODE_SIGNING_MONITOR
3490 #pragma unused(caller_prot)
3491 #endif /* !CODE_SIGNING_MONITOR */
3492 
3493 	kern_return_t kr;
3494 	if (fault_page_size != PAGE_SIZE) {
3495 		DEBUG4K_FAULT("pmap %p va 0x%llx pa 0x%llx (0x%llx+0x%llx) prot 0x%x fault_type 0x%x\n", pmap, (uint64_t)vaddr, (uint64_t)((((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT) + fault_phys_offset), (uint64_t)(((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT), (uint64_t)fault_phys_offset, *prot, fault_type);
3496 		assertf((!(fault_phys_offset & FOURK_PAGE_MASK) &&
3497 		    fault_phys_offset < PAGE_SIZE),
3498 		    "0x%llx\n", (uint64_t)fault_phys_offset);
3499 	} else {
3500 		assertf(fault_phys_offset == 0,
3501 		    "0x%llx\n", (uint64_t)fault_phys_offset);
3502 	}
3503 
3504 	kr = pmap_enter_options_check(pmap, vaddr,
3505 	    fault_phys_offset,
3506 	    m, *prot, fault_type, 0,
3507 	    wired,
3508 	    pmap_options);
3509 
3510 #if CODE_SIGNING_MONITOR
3511 	/*
3512 	 * Retry without execute permission if we encountered a codesigning
3513 	 * failure on a non-execute fault.  This allows applications which
3514 	 * don't actually need to execute code to still map it for read access.
3515 	 */
3516 	if (kr == KERN_CODESIGN_ERROR &&
3517 	    csm_enabled() &&
3518 	    (*prot & VM_PROT_EXECUTE) &&
3519 	    !(caller_prot & VM_PROT_EXECUTE)) {
3520 		*prot &= ~VM_PROT_EXECUTE;
3521 		kr = pmap_enter_options_check(pmap, vaddr,
3522 		    fault_phys_offset,
3523 		    m, *prot, fault_type, 0,
3524 		    wired,
3525 		    pmap_options);
3526 	}
3527 #if KILL_FOR_CSM_VIOLATION
3528 	if (kr == KERN_CODESIGN_ERROR && pmap != kernel_pmap) {
3529 		vm_fault_kill_for_csm_violation(pmap, vaddr, *prot, fault_type);
3530 	}
3531 #endif /* KILL_FOR_CSM_VIOLATION */
3532 #endif /* CODE_SIGNING_MONITOR */
3533 
3534 	return kr;
3535 }
3536 
3537 /*
3538  * Enter the given page into the pmap.
3539  * The map must be locked shared.
3540  * The vm object must NOT be locked.
3541  *
3542  * @param need_retry if not null, avoid making a (potentially) blocking call into
3543  * the pmap layer. When such a call would be necessary, return true in this boolean instead.
3544  */
3545 static kern_return_t
vm_fault_pmap_enter(pmap_t pmap,vm_map_offset_t vaddr,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,vm_page_t m,vm_prot_t * prot,vm_prot_t caller_prot,vm_prot_t fault_type,bool wired,int pmap_options,boolean_t * need_retry)3546 vm_fault_pmap_enter(
3547 	pmap_t pmap,
3548 	vm_map_offset_t vaddr,
3549 	vm_map_size_t fault_page_size,
3550 	vm_map_offset_t fault_phys_offset,
3551 	vm_page_t m,
3552 	vm_prot_t *prot,
3553 	vm_prot_t caller_prot,
3554 	vm_prot_t fault_type,
3555 	bool wired,
3556 	int pmap_options,
3557 	boolean_t *need_retry)
3558 {
3559 	kern_return_t kr;
3560 	if (need_retry != NULL) {
3561 		/*
3562 		 * Although we don't hold a lock on this object, we hold a lock
3563 		 * on the top object in the chain. To prevent a deadlock, we
3564 		 * can't allow the pmap layer to block.
3565 		 */
3566 		pmap_options |= PMAP_OPTIONS_NOWAIT;
3567 	}
3568 	kr = vm_fault_attempt_pmap_enter(pmap, vaddr,
3569 	    fault_page_size, fault_phys_offset,
3570 	    m, prot, caller_prot, fault_type, wired, pmap_options);
3571 	if (kr == KERN_RESOURCE_SHORTAGE) {
3572 		if (need_retry) {
3573 			/*
3574 			 * There's nothing we can do here since we hold the
3575 			 * lock on the top object in the chain. The caller
3576 			 * will need to deal with this by dropping that lock and retrying.
3577 			 */
3578 			*need_retry = TRUE;
3579 			vm_pmap_enter_retried++;
3580 		}
3581 	}
3582 	return kr;
3583 }
3584 
3585 /*
3586  * Enter the given page into the pmap.
3587  * The vm map must be locked shared.
3588  * The vm object must be locked exclusive, unless this is a soft fault.
3589  * For a soft fault, the object must be locked shared or exclusive.
3590  *
3591  * @param need_retry if not null, avoid making a (potentially) blocking call into
3592  * the pmap layer. When such a call would be necessary, return true in this boolean instead.
3593  */
3594 static kern_return_t
vm_fault_pmap_enter_with_object_lock(vm_object_t object,pmap_t pmap,vm_map_offset_t vaddr,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,vm_page_t m,vm_prot_t * prot,vm_prot_t caller_prot,vm_prot_t fault_type,bool wired,int pmap_options,boolean_t * need_retry,uint8_t * object_lock_type)3595 vm_fault_pmap_enter_with_object_lock(
3596 	vm_object_t object,
3597 	pmap_t pmap,
3598 	vm_map_offset_t vaddr,
3599 	vm_map_size_t fault_page_size,
3600 	vm_map_offset_t fault_phys_offset,
3601 	vm_page_t m,
3602 	vm_prot_t *prot,
3603 	vm_prot_t caller_prot,
3604 	vm_prot_t fault_type,
3605 	bool wired,
3606 	int pmap_options,
3607 	boolean_t *need_retry,
3608 	uint8_t *object_lock_type)
3609 {
3610 	kern_return_t kr;
3611 	/*
3612 	 * Prevent a deadlock by not
3613 	 * holding the object lock if we need to wait for a page in
3614 	 * pmap_enter() - <rdar://problem/7138958>
3615 	 */
3616 	kr = vm_fault_attempt_pmap_enter(pmap, vaddr,
3617 	    fault_page_size, fault_phys_offset,
3618 	    m, prot, caller_prot, fault_type, wired, pmap_options | PMAP_OPTIONS_NOWAIT);
3619 #if __x86_64__
3620 	if (kr == KERN_INVALID_ARGUMENT &&
3621 	    pmap == PMAP_NULL &&
3622 	    wired) {
3623 		/*
3624 		 * Wiring a page in a pmap-less VM map:
3625 		 * VMware's "vmmon" kernel extension does this
3626 		 * to grab pages.
3627 		 * Let it proceed even though the PMAP_ENTER() failed.
3628 		 */
3629 		kr = KERN_SUCCESS;
3630 	}
3631 #endif /* __x86_64__ */
3632 
3633 	if (kr == KERN_RESOURCE_SHORTAGE) {
3634 		if (need_retry) {
3635 			/*
3636 			 * this will be non-null in the case where we hold the lock
3637 			 * on the top-object in this chain... we can't just drop
3638 			 * the lock on the object we're inserting the page into
3639 			 * and recall the PMAP_ENTER since we can still cause
3640 			 * a deadlock if one of the critical paths tries to
3641 			 * acquire the lock on the top-object and we're blocked
3642 			 * in PMAP_ENTER waiting for memory... our only recourse
3643 			 * is to deal with it at a higher level where we can
3644 			 * drop both locks.
3645 			 */
3646 			*need_retry = TRUE;
3647 			vm_pmap_enter_retried++;
3648 			goto done;
3649 		}
3650 		/*
3651 		 * The nonblocking version of pmap_enter did not succeed.
3652 		 * and we don't need to drop other locks and retry
3653 		 * at the level above us, so
3654 		 * use the blocking version instead. Requires marking
3655 		 * the page busy and unlocking the object
3656 		 */
3657 		boolean_t was_busy = m->vmp_busy;
3658 
3659 		vm_object_lock_assert_exclusive(object);
3660 
3661 		m->vmp_busy = TRUE;
3662 		vm_object_unlock(object);
3663 
3664 		kr = pmap_enter_options_check(pmap, vaddr,
3665 		    fault_phys_offset,
3666 		    m, *prot, fault_type,
3667 		    0, wired,
3668 		    pmap_options);
3669 
3670 		assert(VM_PAGE_OBJECT(m) == object);
3671 
3672 #if KILL_FOR_CSM_VIOLATION
3673 		if (kr == KERN_CODESIGN_ERROR && pmap != kernel_pmap) {
3674 			vm_fault_kill_for_csm_violation(pmap, vaddr, *prot, fault_type);
3675 		}
3676 #endif /* KILL_FOR_CSM_VIOLATION */
3677 
3678 		/* Take the object lock again. */
3679 		vm_object_lock(object);
3680 
3681 		/* If the page was busy, someone else will wake it up.
3682 		 * Otherwise, we have to do it now. */
3683 		assert(m->vmp_busy);
3684 		if (!was_busy) {
3685 			PAGE_WAKEUP_DONE(m);
3686 		}
3687 		vm_pmap_enter_blocked++;
3688 	}
3689 
3690 #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES
3691 	if ((*prot & VM_PROT_WRITE) && m->vmp_unmodified_ro) {
3692 		if (*object_lock_type == OBJECT_LOCK_SHARED) {
3693 			boolean_t was_busy = m->vmp_busy;
3694 			m->vmp_busy = TRUE;
3695 
3696 			*object_lock_type = OBJECT_LOCK_EXCLUSIVE;
3697 
3698 			if (vm_object_lock_upgrade(object) == FALSE) {
3699 				vm_object_lock(object);
3700 			}
3701 
3702 			if (!was_busy) {
3703 				PAGE_WAKEUP_DONE(m);
3704 			}
3705 		}
3706 		vm_object_lock_assert_exclusive(object);
3707 		vm_page_lockspin_queues();
3708 		m->vmp_unmodified_ro = false;
3709 		vm_page_unlock_queues();
3710 		os_atomic_dec(&compressor_ro_uncompressed, relaxed);
3711 
3712 		VM_COMPRESSOR_PAGER_STATE_CLR(VM_PAGE_OBJECT(m), m->vmp_offset);
3713 	}
3714 #else /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */
3715 #pragma unused(object_lock_type)
3716 #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */
3717 
3718 done:
3719 	return kr;
3720 }
3721 
3722 /*
3723  * Prepare to enter a page into the pmap by checking CS, protection bits,
3724  * and setting mapped bits on the page_t.
3725  * Does not modify the page's paging queue.
3726  *
3727  * page queue lock must NOT be held
3728  * m->vmp_object must be locked
3729  *
3730  * NOTE: m->vmp_object could be locked "shared" only if we are called
3731  * from vm_fault() as part of a soft fault.
3732  */
3733 static kern_return_t
vm_fault_enter_prepare(vm_page_t m,pmap_t pmap,vm_map_offset_t vaddr,vm_prot_t * prot,vm_prot_t caller_prot,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,boolean_t change_wiring,vm_prot_t fault_type,vm_object_fault_info_t fault_info,int * type_of_fault,bool * page_needs_data_sync)3734 vm_fault_enter_prepare(
3735 	vm_page_t m,
3736 	pmap_t pmap,
3737 	vm_map_offset_t vaddr,
3738 	vm_prot_t *prot,
3739 	vm_prot_t caller_prot,
3740 	vm_map_size_t fault_page_size,
3741 	vm_map_offset_t fault_phys_offset,
3742 	boolean_t change_wiring,
3743 	vm_prot_t fault_type,
3744 	vm_object_fault_info_t fault_info,
3745 	int *type_of_fault,
3746 	bool *page_needs_data_sync)
3747 {
3748 	kern_return_t   kr;
3749 	bool            is_tainted = false;
3750 	vm_object_t     object;
3751 	boolean_t       cs_bypass = fault_info->cs_bypass;
3752 
3753 	object = VM_PAGE_OBJECT(m);
3754 
3755 	vm_object_lock_assert_held(object);
3756 
3757 #if KASAN
3758 	if (pmap == kernel_pmap) {
3759 		kasan_notify_address(vaddr, PAGE_SIZE);
3760 	}
3761 #endif
3762 
3763 #if CODE_SIGNING_MONITOR
3764 	if (csm_address_space_exempt(pmap) == KERN_SUCCESS) {
3765 		cs_bypass = TRUE;
3766 	}
3767 #endif
3768 
3769 	LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED);
3770 
3771 	if (*type_of_fault == DBG_ZERO_FILL_FAULT) {
3772 		vm_object_lock_assert_exclusive(object);
3773 	} else if ((fault_type & VM_PROT_WRITE) == 0 &&
3774 	    !change_wiring &&
3775 	    (!m->vmp_wpmapped
3776 #if VM_OBJECT_ACCESS_TRACKING
3777 	    || object->access_tracking
3778 #endif /* VM_OBJECT_ACCESS_TRACKING */
3779 	    )) {
3780 		/*
3781 		 * This is not a "write" fault, so we
3782 		 * might not have taken the object lock
3783 		 * exclusively and we might not be able
3784 		 * to update the "wpmapped" bit in
3785 		 * vm_fault_enter().
3786 		 * Let's just grant read access to
3787 		 * the page for now and we'll
3788 		 * soft-fault again if we need write
3789 		 * access later...
3790 		 */
3791 
3792 		/* This had better not be a JIT page. */
3793 		if (!pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, *prot)) {
3794 			*prot &= ~VM_PROT_WRITE;
3795 		} else {
3796 			assert(cs_bypass);
3797 		}
3798 	}
3799 	if (m->vmp_pmapped == FALSE) {
3800 		if (m->vmp_clustered) {
3801 			if (*type_of_fault == DBG_CACHE_HIT_FAULT) {
3802 				/*
3803 				 * found it in the cache, but this
3804 				 * is the first fault-in of the page (m->vmp_pmapped == FALSE)
3805 				 * so it must have come in as part of
3806 				 * a cluster... account 1 pagein against it
3807 				 */
3808 				if (object->internal) {
3809 					*type_of_fault = DBG_PAGEIND_FAULT;
3810 				} else {
3811 					*type_of_fault = DBG_PAGEINV_FAULT;
3812 				}
3813 
3814 				VM_PAGE_COUNT_AS_PAGEIN(m);
3815 			}
3816 			VM_PAGE_CONSUME_CLUSTERED(m);
3817 		}
3818 	}
3819 
3820 	if (*type_of_fault != DBG_COW_FAULT) {
3821 		DTRACE_VM2(as_fault, int, 1, (uint64_t *), NULL);
3822 
3823 		if (pmap == kernel_pmap) {
3824 			DTRACE_VM2(kernel_asflt, int, 1, (uint64_t *), NULL);
3825 		}
3826 	}
3827 
3828 	kr = vm_fault_validate_cs(cs_bypass, object, m, pmap, vaddr,
3829 	    *prot, caller_prot, fault_page_size, fault_phys_offset,
3830 	    fault_info, &is_tainted);
3831 	if (kr == KERN_SUCCESS) {
3832 		/*
3833 		 * We either have a good page, or a tainted page that has been accepted by the process.
3834 		 * In both cases the page will be entered into the pmap.
3835 		 */
3836 		*page_needs_data_sync = vm_fault_enter_set_mapped(object, m, *prot, fault_type);
3837 		if ((fault_type & VM_PROT_WRITE) && is_tainted) {
3838 			/*
3839 			 * This page is tainted but we're inserting it anyways.
3840 			 * Since it's writeable, we need to disconnect it from other pmaps
3841 			 * now so those processes can take note.
3842 			 */
3843 
3844 			/*
3845 			 * We can only get here
3846 			 * because of the CSE logic
3847 			 */
3848 			assert(pmap_get_vm_map_cs_enforced(pmap));
3849 			pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
3850 			/*
3851 			 * If we are faulting for a write, we can clear
3852 			 * the execute bit - that will ensure the page is
3853 			 * checked again before being executable, which
3854 			 * protects against a map switch.
3855 			 * This only happens the first time the page
3856 			 * gets tainted, so we won't get stuck here
3857 			 * to make an already writeable page executable.
3858 			 */
3859 			if (!cs_bypass) {
3860 				assert(!pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, *prot));
3861 				*prot &= ~VM_PROT_EXECUTE;
3862 			}
3863 		}
3864 		assert(VM_PAGE_OBJECT(m) == object);
3865 
3866 #if VM_OBJECT_ACCESS_TRACKING
3867 		if (object->access_tracking) {
3868 			DTRACE_VM2(access_tracking, vm_map_offset_t, vaddr, int, fault_type);
3869 			if (fault_type & VM_PROT_WRITE) {
3870 				object->access_tracking_writes++;
3871 				vm_object_access_tracking_writes++;
3872 			} else {
3873 				object->access_tracking_reads++;
3874 				vm_object_access_tracking_reads++;
3875 			}
3876 		}
3877 #endif /* VM_OBJECT_ACCESS_TRACKING */
3878 	}
3879 
3880 	return kr;
3881 }
3882 
3883 /*
3884  * page queue lock must NOT be held
3885  * m->vmp_object must be locked
3886  *
3887  * NOTE: m->vmp_object could be locked "shared" only if we are called
3888  * from vm_fault() as part of a soft fault.  If so, we must be
3889  * careful not to modify the VM object in any way that is not
3890  * legal under a shared lock...
3891  */
3892 kern_return_t
vm_fault_enter(vm_page_t m,pmap_t pmap,vm_map_offset_t vaddr,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,vm_prot_t prot,vm_prot_t caller_prot,boolean_t wired,boolean_t change_wiring,vm_tag_t wire_tag,vm_object_fault_info_t fault_info,boolean_t * need_retry,int * type_of_fault,uint8_t * object_lock_type)3893 vm_fault_enter(
3894 	vm_page_t m,
3895 	pmap_t pmap,
3896 	vm_map_offset_t vaddr,
3897 	vm_map_size_t fault_page_size,
3898 	vm_map_offset_t fault_phys_offset,
3899 	vm_prot_t prot,
3900 	vm_prot_t caller_prot,
3901 	boolean_t wired,
3902 	boolean_t change_wiring,
3903 	vm_tag_t  wire_tag,
3904 	vm_object_fault_info_t fault_info,
3905 	boolean_t *need_retry,
3906 	int *type_of_fault,
3907 	uint8_t *object_lock_type)
3908 {
3909 	kern_return_t   kr;
3910 	vm_object_t     object;
3911 	bool            page_needs_data_sync;
3912 	vm_prot_t       fault_type;
3913 	int             pmap_options = fault_info->pmap_options;
3914 
3915 	if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) {
3916 		assert(m->vmp_fictitious);
3917 		return KERN_SUCCESS;
3918 	}
3919 
3920 	fault_type = change_wiring ? VM_PROT_NONE : caller_prot;
3921 
3922 	assertf(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL, "m=%p", m);
3923 	kr = vm_fault_enter_prepare(m, pmap, vaddr, &prot, caller_prot,
3924 	    fault_page_size, fault_phys_offset, change_wiring, fault_type,
3925 	    fault_info, type_of_fault, &page_needs_data_sync);
3926 	object = VM_PAGE_OBJECT(m);
3927 
3928 	vm_fault_enqueue_page(object, m, wired, change_wiring, wire_tag, fault_info->no_cache, type_of_fault, kr);
3929 
3930 	if (kr == KERN_SUCCESS) {
3931 		if (page_needs_data_sync) {
3932 			pmap_sync_page_data_phys(VM_PAGE_GET_PHYS_PAGE(m));
3933 		}
3934 
3935 		if (fault_info->fi_xnu_user_debug && !object->code_signed) {
3936 			pmap_options |= PMAP_OPTIONS_XNU_USER_DEBUG;
3937 		}
3938 
3939 
3940 		kr = vm_fault_pmap_enter_with_object_lock(object, pmap, vaddr,
3941 		    fault_page_size, fault_phys_offset, m,
3942 		    &prot, caller_prot, fault_type, wired, pmap_options, need_retry, object_lock_type);
3943 	}
3944 
3945 	return kr;
3946 }
3947 
3948 void
vm_pre_fault(vm_map_offset_t vaddr,vm_prot_t prot)3949 vm_pre_fault(vm_map_offset_t vaddr, vm_prot_t prot)
3950 {
3951 	if (pmap_find_phys(current_map()->pmap, vaddr) == 0) {
3952 		vm_fault(current_map(),      /* map */
3953 		    vaddr,                   /* vaddr */
3954 		    prot,                    /* fault_type */
3955 		    FALSE,                   /* change_wiring */
3956 		    VM_KERN_MEMORY_NONE,     /* tag - not wiring */
3957 		    THREAD_UNINT,            /* interruptible */
3958 		    NULL,                    /* caller_pmap */
3959 		    0 /* caller_pmap_addr */);
3960 	}
3961 }
3962 
3963 
3964 /*
3965  *	Routine:	vm_fault
3966  *	Purpose:
3967  *		Handle page faults, including pseudo-faults
3968  *		used to change the wiring status of pages.
3969  *	Returns:
3970  *		Explicit continuations have been removed.
3971  *	Implementation:
3972  *		vm_fault and vm_fault_page save mucho state
3973  *		in the moral equivalent of a closure.  The state
3974  *		structure is allocated when first entering vm_fault
3975  *		and deallocated when leaving vm_fault.
3976  */
3977 
3978 extern uint64_t get_current_unique_pid(void);
3979 
3980 unsigned long vm_fault_collapse_total = 0;
3981 unsigned long vm_fault_collapse_skipped = 0;
3982 
3983 
3984 kern_return_t
vm_fault_external(vm_map_t map,vm_map_offset_t vaddr,vm_prot_t fault_type,boolean_t change_wiring,int interruptible,pmap_t caller_pmap,vm_map_offset_t caller_pmap_addr)3985 vm_fault_external(
3986 	vm_map_t        map,
3987 	vm_map_offset_t vaddr,
3988 	vm_prot_t       fault_type,
3989 	boolean_t       change_wiring,
3990 	int             interruptible,
3991 	pmap_t          caller_pmap,
3992 	vm_map_offset_t caller_pmap_addr)
3993 {
3994 	return vm_fault_internal(map, vaddr, fault_type, change_wiring,
3995 	           change_wiring ? vm_tag_bt() : VM_KERN_MEMORY_NONE,
3996 	           interruptible, caller_pmap, caller_pmap_addr,
3997 	           NULL);
3998 }
3999 
4000 kern_return_t
vm_fault(vm_map_t map,vm_map_offset_t vaddr,vm_prot_t fault_type,boolean_t change_wiring,vm_tag_t wire_tag,int interruptible,pmap_t caller_pmap,vm_map_offset_t caller_pmap_addr)4001 vm_fault(
4002 	vm_map_t        map,
4003 	vm_map_offset_t vaddr,
4004 	vm_prot_t       fault_type,
4005 	boolean_t       change_wiring,
4006 	vm_tag_t        wire_tag,               /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
4007 	int             interruptible,
4008 	pmap_t          caller_pmap,
4009 	vm_map_offset_t caller_pmap_addr)
4010 {
4011 	return vm_fault_internal(map, vaddr, fault_type, change_wiring, wire_tag,
4012 	           interruptible, caller_pmap, caller_pmap_addr,
4013 	           NULL);
4014 }
4015 
4016 static boolean_t
current_proc_is_privileged(void)4017 current_proc_is_privileged(void)
4018 {
4019 	return csproc_get_platform_binary(current_proc());
4020 }
4021 
4022 uint64_t vm_copied_on_read = 0;
4023 
4024 /*
4025  * Cleanup after a vm_fault_enter.
4026  * At this point, the fault should either have failed (kr != KERN_SUCCESS)
4027  * or the page should be in the pmap and on the correct paging queue.
4028  *
4029  * Precondition:
4030  * map must be locked shared.
4031  * m_object must be locked.
4032  * If top_object != VM_OBJECT_NULL, it must be locked.
4033  * real_map must be locked.
4034  *
4035  * Postcondition:
4036  * map will be unlocked
4037  * m_object will be unlocked
4038  * top_object will be unlocked
4039  * If real_map != map, it will be unlocked
4040  */
4041 static void
vm_fault_complete(vm_map_t map,vm_map_t real_map,vm_object_t object,vm_object_t m_object,vm_page_t m,vm_map_offset_t offset,vm_map_offset_t trace_real_vaddr,vm_object_fault_info_t fault_info,vm_prot_t caller_prot,vm_map_offset_t real_vaddr,int type_of_fault,boolean_t need_retry,kern_return_t kr,ppnum_t * physpage_p,vm_prot_t prot,vm_object_t top_object,boolean_t need_collapse,vm_map_offset_t cur_offset,vm_prot_t fault_type,vm_object_t * written_on_object,memory_object_t * written_on_pager,vm_object_offset_t * written_on_offset)4042 vm_fault_complete(
4043 	vm_map_t map,
4044 	vm_map_t real_map,
4045 	vm_object_t object,
4046 	vm_object_t m_object,
4047 	vm_page_t m,
4048 	vm_map_offset_t offset,
4049 	vm_map_offset_t trace_real_vaddr,
4050 	vm_object_fault_info_t fault_info,
4051 	vm_prot_t caller_prot,
4052 #if CONFIG_DTRACE
4053 	vm_map_offset_t real_vaddr,
4054 #else
4055 	__unused vm_map_offset_t real_vaddr,
4056 #endif /* CONFIG_DTRACE */
4057 	int type_of_fault,
4058 	boolean_t need_retry,
4059 	kern_return_t kr,
4060 	ppnum_t *physpage_p,
4061 	vm_prot_t prot,
4062 	vm_object_t top_object,
4063 	boolean_t need_collapse,
4064 	vm_map_offset_t cur_offset,
4065 	vm_prot_t fault_type,
4066 	vm_object_t *written_on_object,
4067 	memory_object_t *written_on_pager,
4068 	vm_object_offset_t *written_on_offset)
4069 {
4070 	int     event_code = 0;
4071 	vm_map_lock_assert_shared(map);
4072 	vm_object_lock_assert_held(m_object);
4073 	if (top_object != VM_OBJECT_NULL) {
4074 		vm_object_lock_assert_held(top_object);
4075 	}
4076 	vm_map_lock_assert_held(real_map);
4077 
4078 	if (m_object->internal) {
4079 		event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_INTERNAL));
4080 	} else if (m_object->object_is_shared_cache) {
4081 		event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_SHAREDCACHE));
4082 	} else {
4083 		event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_EXTERNAL));
4084 	}
4085 	KDBG_RELEASE(event_code | DBG_FUNC_NONE, trace_real_vaddr, (fault_info->user_tag << 16) | (caller_prot << 8) | type_of_fault, m->vmp_offset, get_current_unique_pid());
4086 	if (need_retry == FALSE) {
4087 		KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_FAST), get_current_unique_pid());
4088 	}
4089 	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);
4090 	if (kr == KERN_SUCCESS &&
4091 	    physpage_p != NULL) {
4092 		/* for vm_map_wire_and_extract() */
4093 		*physpage_p = VM_PAGE_GET_PHYS_PAGE(m);
4094 		if (prot & VM_PROT_WRITE) {
4095 			vm_object_lock_assert_exclusive(m_object);
4096 			m->vmp_dirty = TRUE;
4097 		}
4098 	}
4099 
4100 	if (top_object != VM_OBJECT_NULL) {
4101 		/*
4102 		 * It's safe to drop the top object
4103 		 * now that we've done our
4104 		 * vm_fault_enter().  Any other fault
4105 		 * in progress for that virtual
4106 		 * address will either find our page
4107 		 * and translation or put in a new page
4108 		 * and translation.
4109 		 */
4110 		vm_object_unlock(top_object);
4111 		top_object = VM_OBJECT_NULL;
4112 	}
4113 
4114 	if (need_collapse == TRUE) {
4115 		vm_object_collapse(object, vm_object_trunc_page(offset), TRUE);
4116 	}
4117 
4118 	if (need_retry == FALSE &&
4119 	    (type_of_fault == DBG_PAGEIND_FAULT || type_of_fault == DBG_PAGEINV_FAULT || type_of_fault == DBG_CACHE_HIT_FAULT)) {
4120 		/*
4121 		 * evaluate access pattern and update state
4122 		 * vm_fault_deactivate_behind depends on the
4123 		 * state being up to date
4124 		 */
4125 		vm_fault_is_sequential(m_object, cur_offset, fault_info->behavior);
4126 
4127 		vm_fault_deactivate_behind(m_object, cur_offset, fault_info->behavior);
4128 	}
4129 	/*
4130 	 * That's it, clean up and return.
4131 	 */
4132 	if (m->vmp_busy) {
4133 		vm_object_lock_assert_exclusive(m_object);
4134 		PAGE_WAKEUP_DONE(m);
4135 	}
4136 
4137 	if (need_retry == FALSE && !m_object->internal && (fault_type & VM_PROT_WRITE)) {
4138 		vm_object_paging_begin(m_object);
4139 
4140 		assert(*written_on_object == VM_OBJECT_NULL);
4141 		*written_on_object = m_object;
4142 		*written_on_pager = m_object->pager;
4143 		*written_on_offset = m_object->paging_offset + m->vmp_offset;
4144 	}
4145 	vm_object_unlock(object);
4146 
4147 	vm_map_unlock_read(map);
4148 	if (real_map != map) {
4149 		vm_map_unlock(real_map);
4150 	}
4151 }
4152 
4153 static inline int
vm_fault_type_for_tracing(boolean_t need_copy_on_read,int type_of_fault)4154 vm_fault_type_for_tracing(boolean_t need_copy_on_read, int type_of_fault)
4155 {
4156 	if (need_copy_on_read && type_of_fault == DBG_COW_FAULT) {
4157 		return DBG_COR_FAULT;
4158 	}
4159 	return type_of_fault;
4160 }
4161 
4162 uint64_t vm_fault_resilient_media_initiate = 0;
4163 uint64_t vm_fault_resilient_media_retry = 0;
4164 uint64_t vm_fault_resilient_media_proceed = 0;
4165 uint64_t vm_fault_resilient_media_release = 0;
4166 uint64_t vm_fault_resilient_media_abort1 = 0;
4167 uint64_t vm_fault_resilient_media_abort2 = 0;
4168 
4169 #if MACH_ASSERT
4170 int vm_fault_resilient_media_inject_error1_rate = 0;
4171 int vm_fault_resilient_media_inject_error1 = 0;
4172 int vm_fault_resilient_media_inject_error2_rate = 0;
4173 int vm_fault_resilient_media_inject_error2 = 0;
4174 int vm_fault_resilient_media_inject_error3_rate = 0;
4175 int vm_fault_resilient_media_inject_error3 = 0;
4176 #endif /* MACH_ASSERT */
4177 
4178 kern_return_t
vm_fault_internal(vm_map_t map,vm_map_offset_t vaddr,vm_prot_t caller_prot,boolean_t change_wiring,vm_tag_t wire_tag,int interruptible,pmap_t caller_pmap,vm_map_offset_t caller_pmap_addr,ppnum_t * physpage_p)4179 vm_fault_internal(
4180 	vm_map_t        map,
4181 	vm_map_offset_t vaddr,
4182 	vm_prot_t       caller_prot,
4183 	boolean_t       change_wiring,
4184 	vm_tag_t        wire_tag,               /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
4185 	int             interruptible,
4186 	pmap_t          caller_pmap,
4187 	vm_map_offset_t caller_pmap_addr,
4188 	ppnum_t         *physpage_p)
4189 {
4190 	vm_map_version_t        version;        /* Map version for verificiation */
4191 	boolean_t               wired;          /* Should mapping be wired down? */
4192 	vm_object_t             object;         /* Top-level object */
4193 	vm_object_offset_t      offset;         /* Top-level offset */
4194 	vm_prot_t               prot;           /* Protection for mapping */
4195 	vm_object_t             old_copy_object; /* Saved copy object */
4196 	uint32_t                old_copy_version;
4197 	vm_page_t               result_page;    /* Result of vm_fault_page */
4198 	vm_page_t               top_page;       /* Placeholder page */
4199 	kern_return_t           kr;
4200 
4201 	vm_page_t               m;      /* Fast access to result_page */
4202 	kern_return_t           error_code;
4203 	vm_object_t             cur_object;
4204 	vm_object_t             m_object = NULL;
4205 	vm_object_offset_t      cur_offset;
4206 	vm_page_t               cur_m;
4207 	vm_object_t             new_object;
4208 	int                     type_of_fault;
4209 	pmap_t                  pmap;
4210 	wait_interrupt_t        interruptible_state;
4211 	vm_map_t                real_map = map;
4212 	vm_map_t                original_map = map;
4213 	bool                    object_locks_dropped = FALSE;
4214 	vm_prot_t               fault_type;
4215 	vm_prot_t               original_fault_type;
4216 	struct vm_object_fault_info fault_info = {};
4217 	bool                    need_collapse = FALSE;
4218 	boolean_t               need_retry = FALSE;
4219 	boolean_t               *need_retry_ptr = NULL;
4220 	uint8_t                 object_lock_type = 0;
4221 	uint8_t                 cur_object_lock_type;
4222 	vm_object_t             top_object = VM_OBJECT_NULL;
4223 	vm_object_t             written_on_object = VM_OBJECT_NULL;
4224 	memory_object_t         written_on_pager = NULL;
4225 	vm_object_offset_t      written_on_offset = 0;
4226 	int                     throttle_delay;
4227 	int                     compressed_count_delta;
4228 	uint8_t                 grab_options;
4229 	bool                    need_copy;
4230 	bool                    need_copy_on_read;
4231 	vm_map_offset_t         trace_vaddr;
4232 	vm_map_offset_t         trace_real_vaddr;
4233 	vm_map_size_t           fault_page_size;
4234 	vm_map_size_t           fault_page_mask;
4235 	int                     fault_page_shift;
4236 	vm_map_offset_t         fault_phys_offset;
4237 	vm_map_offset_t         real_vaddr;
4238 	bool                    resilient_media_retry = false;
4239 	bool                    resilient_media_ref_transfer = false;
4240 	vm_object_t             resilient_media_object = VM_OBJECT_NULL;
4241 	vm_object_offset_t      resilient_media_offset = (vm_object_offset_t)-1;
4242 	bool                    page_needs_data_sync = false;
4243 	/*
4244 	 * Was the VM object contended when vm_map_lookup_and_lock_object locked it?
4245 	 * If so, the zero fill path will drop the lock
4246 	 * NB: Ideally we would always drop the lock rather than rely on
4247 	 * this heuristic, but vm_object_unlock currently takes > 30 cycles.
4248 	 */
4249 	bool                    object_is_contended = false;
4250 
4251 	real_vaddr = vaddr;
4252 	trace_real_vaddr = vaddr;
4253 
4254 	/*
4255 	 * Some (kernel) submaps are marked with "should never fault".
4256 	 *
4257 	 * We do this for two reasons:
4258 	 * - PGZ which is inside the zone map range can't go down the normal
4259 	 *   lookup path (vm_map_lookup_entry() would panic).
4260 	 *
4261 	 * - we want for guard pages to not have to use fictitious pages at all
4262 	 *   to prevent from ZFOD pages to be made.
4263 	 *
4264 	 * We also want capture the fault address easily so that the zone
4265 	 * allocator might present an enhanced panic log.
4266 	 */
4267 	if (map->never_faults || (pgz_owned(vaddr) && map->pmap == kernel_pmap)) {
4268 		assert(map->pmap == kernel_pmap);
4269 		return KERN_INVALID_ADDRESS;
4270 	}
4271 
4272 	if (VM_MAP_PAGE_SIZE(original_map) < PAGE_SIZE) {
4273 		fault_phys_offset = (vm_map_offset_t)-1;
4274 		fault_page_size = VM_MAP_PAGE_SIZE(original_map);
4275 		fault_page_mask = VM_MAP_PAGE_MASK(original_map);
4276 		fault_page_shift = VM_MAP_PAGE_SHIFT(original_map);
4277 		if (fault_page_size < PAGE_SIZE) {
4278 			DEBUG4K_FAULT("map %p vaddr 0x%llx caller_prot 0x%x\n", map, (uint64_t)trace_real_vaddr, caller_prot);
4279 			vaddr = vm_map_trunc_page(vaddr, fault_page_mask);
4280 		}
4281 	} else {
4282 		fault_phys_offset = 0;
4283 		fault_page_size = PAGE_SIZE;
4284 		fault_page_mask = PAGE_MASK;
4285 		fault_page_shift = PAGE_SHIFT;
4286 		vaddr = vm_map_trunc_page(vaddr, PAGE_MASK);
4287 	}
4288 
4289 	if (map == kernel_map) {
4290 		trace_vaddr = VM_KERNEL_ADDRHIDE(vaddr);
4291 		trace_real_vaddr = VM_KERNEL_ADDRHIDE(trace_real_vaddr);
4292 	} else {
4293 		trace_vaddr = vaddr;
4294 	}
4295 
4296 	KDBG_RELEASE(
4297 		(MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_START,
4298 		((uint64_t)trace_vaddr >> 32),
4299 		trace_vaddr,
4300 		(map == kernel_map));
4301 
4302 	if (get_preemption_level() != 0) {
4303 		KDBG_RELEASE(
4304 			(MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END,
4305 			((uint64_t)trace_vaddr >> 32),
4306 			trace_vaddr,
4307 			KERN_FAILURE);
4308 
4309 		ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_NONZERO_PREEMPTION_LEVEL), 0 /* arg */);
4310 		return KERN_FAILURE;
4311 	}
4312 
4313 	thread_t cthread = current_thread();
4314 	bool      rtfault = (cthread->sched_mode == TH_MODE_REALTIME);
4315 	uint64_t fstart = 0;
4316 
4317 	if (rtfault) {
4318 		fstart = mach_continuous_time();
4319 	}
4320 
4321 	interruptible_state = thread_interrupt_level(interruptible);
4322 
4323 	fault_type = (change_wiring ? VM_PROT_NONE : caller_prot);
4324 
4325 	counter_inc(&vm_statistics_faults);
4326 	counter_inc(&current_task()->faults);
4327 	original_fault_type = fault_type;
4328 
4329 	need_copy = FALSE;
4330 	if (fault_type & VM_PROT_WRITE) {
4331 		need_copy = TRUE;
4332 	}
4333 
4334 	if (need_copy || change_wiring) {
4335 		object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4336 	} else {
4337 		object_lock_type = OBJECT_LOCK_SHARED;
4338 	}
4339 
4340 	cur_object_lock_type = OBJECT_LOCK_SHARED;
4341 
4342 	if ((map == kernel_map) && (caller_prot & VM_PROT_WRITE)) {
4343 		if (compressor_map) {
4344 			if ((vaddr >= vm_map_min(compressor_map)) && (vaddr < vm_map_max(compressor_map))) {
4345 				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));
4346 			}
4347 		}
4348 	}
4349 RetryFault:
4350 	assert(written_on_object == VM_OBJECT_NULL);
4351 
4352 	/*
4353 	 * assume we will hit a page in the cache
4354 	 * otherwise, explicitly override with
4355 	 * the real fault type once we determine it
4356 	 */
4357 	type_of_fault = DBG_CACHE_HIT_FAULT;
4358 
4359 	/*
4360 	 *	Find the backing store object and offset into
4361 	 *	it to begin the search.
4362 	 */
4363 	fault_type = original_fault_type;
4364 	map = original_map;
4365 	vm_map_lock_read(map);
4366 
4367 	if (resilient_media_retry) {
4368 		/*
4369 		 * If we have to insert a fake zero-filled page to hide
4370 		 * a media failure to provide the real page, we need to
4371 		 * resolve any pending copy-on-write on this mapping.
4372 		 * VM_PROT_COPY tells vm_map_lookup_and_lock_object() to deal
4373 		 * with that even if this is not a "write" fault.
4374 		 */
4375 		need_copy = TRUE;
4376 		object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4377 		vm_fault_resilient_media_retry++;
4378 	}
4379 
4380 	kr = vm_map_lookup_and_lock_object(&map, vaddr,
4381 	    (fault_type | (need_copy ? VM_PROT_COPY : 0)),
4382 	    object_lock_type, &version,
4383 	    &object, &offset, &prot, &wired,
4384 	    &fault_info,
4385 	    &real_map,
4386 	    &object_is_contended);
4387 	object_is_contended = false; /* avoid unsafe optimization */
4388 
4389 	if (kr != KERN_SUCCESS) {
4390 		vm_map_unlock_read(map);
4391 		/*
4392 		 * This can be seen in a crash report if indeed the
4393 		 * thread is crashing due to an invalid access in a non-existent
4394 		 * range.
4395 		 * Turning this OFF for now because it is noisy and not always fatal
4396 		 * eg prefaulting.
4397 		 *
4398 		 * if (kr == KERN_INVALID_ADDRESS) {
4399 		 *	ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_ADDRESS_NOT_FOUND), 0);
4400 		 * }
4401 		 */
4402 		goto done;
4403 	}
4404 
4405 
4406 	pmap = real_map->pmap;
4407 	fault_info.interruptible = interruptible;
4408 	fault_info.stealth = FALSE;
4409 	fault_info.io_sync = FALSE;
4410 	fault_info.mark_zf_absent = FALSE;
4411 	fault_info.batch_pmap_op = FALSE;
4412 
4413 	if (resilient_media_retry) {
4414 		/*
4415 		 * We're retrying this fault after having detected a media
4416 		 * failure from a "resilient_media" mapping.
4417 		 * Check that the mapping is still pointing at the object
4418 		 * that just failed to provide a page.
4419 		 */
4420 		assert(resilient_media_object != VM_OBJECT_NULL);
4421 		assert(resilient_media_offset != (vm_object_offset_t)-1);
4422 		if ((object != VM_OBJECT_NULL &&
4423 		    object == resilient_media_object &&
4424 		    offset == resilient_media_offset &&
4425 		    fault_info.resilient_media)
4426 #if MACH_ASSERT
4427 		    && (vm_fault_resilient_media_inject_error1_rate == 0 ||
4428 		    (++vm_fault_resilient_media_inject_error1 % vm_fault_resilient_media_inject_error1_rate) != 0)
4429 #endif /* MACH_ASSERT */
4430 		    ) {
4431 			/*
4432 			 * This mapping still points at the same object
4433 			 * and is still "resilient_media": proceed in
4434 			 * "recovery-from-media-failure" mode, where we'll
4435 			 * insert a zero-filled page in the top object.
4436 			 */
4437 //                     printf("RESILIENT_MEDIA %s:%d recovering for object %p offset 0x%llx\n", __FUNCTION__, __LINE__, object, offset);
4438 			vm_fault_resilient_media_proceed++;
4439 		} else {
4440 			/* not recovering: reset state and retry fault */
4441 //                     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);
4442 			vm_object_unlock(object);
4443 			if (real_map != map) {
4444 				vm_map_unlock(real_map);
4445 			}
4446 			vm_map_unlock_read(map);
4447 			/* release our extra reference on failed object */
4448 //                     printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
4449 			vm_object_lock_assert_notheld(resilient_media_object);
4450 			vm_object_deallocate(resilient_media_object);
4451 			resilient_media_object = VM_OBJECT_NULL;
4452 			resilient_media_offset = (vm_object_offset_t)-1;
4453 			resilient_media_retry = false;
4454 			vm_fault_resilient_media_abort1++;
4455 			goto RetryFault;
4456 		}
4457 	} else {
4458 		assert(resilient_media_object == VM_OBJECT_NULL);
4459 		resilient_media_offset = (vm_object_offset_t)-1;
4460 	}
4461 
4462 	/*
4463 	 * If the page is wired, we must fault for the current protection
4464 	 * value, to avoid further faults.
4465 	 */
4466 	if (wired) {
4467 		fault_type = prot | VM_PROT_WRITE;
4468 	}
4469 	if (wired || need_copy) {
4470 		/*
4471 		 * since we're treating this fault as a 'write'
4472 		 * we must hold the top object lock exclusively
4473 		 */
4474 		if (object_lock_type == OBJECT_LOCK_SHARED) {
4475 			object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4476 
4477 			if (vm_object_lock_upgrade(object) == FALSE) {
4478 				/*
4479 				 * couldn't upgrade, so explictly
4480 				 * take the lock exclusively
4481 				 */
4482 				vm_object_lock(object);
4483 			}
4484 		}
4485 	}
4486 
4487 #if     VM_FAULT_CLASSIFY
4488 	/*
4489 	 *	Temporary data gathering code
4490 	 */
4491 	vm_fault_classify(object, offset, fault_type);
4492 #endif
4493 	/*
4494 	 *	Fast fault code.  The basic idea is to do as much as
4495 	 *	possible while holding the map lock and object locks.
4496 	 *      Busy pages are not used until the object lock has to
4497 	 *	be dropped to do something (copy, zero fill, pmap enter).
4498 	 *	Similarly, paging references aren't acquired until that
4499 	 *	point, and object references aren't used.
4500 	 *
4501 	 *	If we can figure out what to do
4502 	 *	(zero fill, copy on write, pmap enter) while holding
4503 	 *	the locks, then it gets done.  Otherwise, we give up,
4504 	 *	and use the original fault path (which doesn't hold
4505 	 *	the map lock, and relies on busy pages).
4506 	 *	The give up cases include:
4507 	 *              - Have to talk to pager.
4508 	 *		- Page is busy, absent or in error.
4509 	 *		- Pager has locked out desired access.
4510 	 *		- Fault needs to be restarted.
4511 	 *		- Have to push page into copy object.
4512 	 *
4513 	 *	The code is an infinite loop that moves one level down
4514 	 *	the shadow chain each time.  cur_object and cur_offset
4515 	 *      refer to the current object being examined. object and offset
4516 	 *	are the original object from the map.  The loop is at the
4517 	 *	top level if and only if object and cur_object are the same.
4518 	 *
4519 	 *	Invariants:  Map lock is held throughout.  Lock is held on
4520 	 *		original object and cur_object (if different) when
4521 	 *		continuing or exiting loop.
4522 	 *
4523 	 */
4524 
4525 #if defined(__arm64__)
4526 	/*
4527 	 * Fail if reading an execute-only page in a
4528 	 * pmap that enforces execute-only protection.
4529 	 */
4530 	if (fault_type == VM_PROT_READ &&
4531 	    (prot & VM_PROT_EXECUTE) &&
4532 	    !(prot & VM_PROT_READ) &&
4533 	    pmap_enforces_execute_only(pmap)) {
4534 		vm_object_unlock(object);
4535 		vm_map_unlock_read(map);
4536 		if (real_map != map) {
4537 			vm_map_unlock(real_map);
4538 		}
4539 		kr = KERN_PROTECTION_FAILURE;
4540 		goto done;
4541 	}
4542 #endif
4543 
4544 	fault_phys_offset = (vm_map_offset_t)offset - vm_map_trunc_page((vm_map_offset_t)offset, PAGE_MASK);
4545 
4546 	/*
4547 	 * If this page is to be inserted in a copy delay object
4548 	 * for writing, and if the object has a copy, then the
4549 	 * copy delay strategy is implemented in the slow fault page.
4550 	 */
4551 	if ((object->copy_strategy == MEMORY_OBJECT_COPY_DELAY ||
4552 	    object->copy_strategy == MEMORY_OBJECT_COPY_DELAY_FORK) &&
4553 	    object->vo_copy != VM_OBJECT_NULL && (fault_type & VM_PROT_WRITE)) {
4554 		goto handle_copy_delay;
4555 	}
4556 
4557 	cur_object = object;
4558 	cur_offset = offset;
4559 
4560 	grab_options = 0;
4561 #if CONFIG_SECLUDED_MEMORY
4562 	if (object->can_grab_secluded) {
4563 		grab_options |= VM_PAGE_GRAB_SECLUDED;
4564 	}
4565 #endif /* CONFIG_SECLUDED_MEMORY */
4566 
4567 	while (TRUE) {
4568 		if (!cur_object->pager_created &&
4569 		    cur_object->phys_contiguous) { /* superpage */
4570 			break;
4571 		}
4572 
4573 		if (cur_object->blocked_access) {
4574 			/*
4575 			 * Access to this VM object has been blocked.
4576 			 * Let the slow path handle it.
4577 			 */
4578 			break;
4579 		}
4580 
4581 		m = vm_page_lookup(cur_object, vm_object_trunc_page(cur_offset));
4582 		m_object = NULL;
4583 
4584 		if (m != VM_PAGE_NULL) {
4585 			m_object = cur_object;
4586 
4587 			if (m->vmp_busy) {
4588 				wait_result_t   result;
4589 
4590 				/*
4591 				 * in order to do the PAGE_ASSERT_WAIT, we must
4592 				 * have object that 'm' belongs to locked exclusively
4593 				 */
4594 				if (object != cur_object) {
4595 					if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
4596 						cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4597 
4598 						if (vm_object_lock_upgrade(cur_object) == FALSE) {
4599 							/*
4600 							 * couldn't upgrade so go do a full retry
4601 							 * immediately since we can no longer be
4602 							 * certain about cur_object (since we
4603 							 * don't hold a reference on it)...
4604 							 * first drop the top object lock
4605 							 */
4606 							vm_object_unlock(object);
4607 
4608 							vm_map_unlock_read(map);
4609 							if (real_map != map) {
4610 								vm_map_unlock(real_map);
4611 							}
4612 
4613 							goto RetryFault;
4614 						}
4615 					}
4616 				} else if (object_lock_type == OBJECT_LOCK_SHARED) {
4617 					object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4618 
4619 					if (vm_object_lock_upgrade(object) == FALSE) {
4620 						/*
4621 						 * couldn't upgrade, so explictly take the lock
4622 						 * exclusively and go relookup the page since we
4623 						 * will have dropped the object lock and
4624 						 * a different thread could have inserted
4625 						 * a page at this offset
4626 						 * no need for a full retry since we're
4627 						 * at the top level of the object chain
4628 						 */
4629 						vm_object_lock(object);
4630 
4631 						continue;
4632 					}
4633 				}
4634 				if ((m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) && m_object->internal) {
4635 					/*
4636 					 * m->vmp_busy == TRUE and the object is locked exclusively
4637 					 * if m->pageout_queue == TRUE after we acquire the
4638 					 * queues lock, we are guaranteed that it is stable on
4639 					 * the pageout queue and therefore reclaimable
4640 					 *
4641 					 * NOTE: this is only true for the internal pageout queue
4642 					 * in the compressor world
4643 					 */
4644 					assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);
4645 
4646 					vm_page_lock_queues();
4647 
4648 					if (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) {
4649 						vm_pageout_throttle_up(m);
4650 						vm_page_unlock_queues();
4651 
4652 						PAGE_WAKEUP_DONE(m);
4653 						goto reclaimed_from_pageout;
4654 					}
4655 					vm_page_unlock_queues();
4656 				}
4657 				if (object != cur_object) {
4658 					vm_object_unlock(object);
4659 				}
4660 
4661 				vm_map_unlock_read(map);
4662 				if (real_map != map) {
4663 					vm_map_unlock(real_map);
4664 				}
4665 
4666 				result = PAGE_ASSERT_WAIT(m, interruptible);
4667 
4668 				vm_object_unlock(cur_object);
4669 
4670 				if (result == THREAD_WAITING) {
4671 					result = thread_block(THREAD_CONTINUE_NULL);
4672 				}
4673 				if (result == THREAD_AWAKENED || result == THREAD_RESTART) {
4674 					goto RetryFault;
4675 				}
4676 
4677 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_BUSYPAGE_WAIT_INTERRUPTED), 0 /* arg */);
4678 				kr = KERN_ABORTED;
4679 				goto done;
4680 			}
4681 reclaimed_from_pageout:
4682 			if (m->vmp_laundry) {
4683 				if (object != cur_object) {
4684 					if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
4685 						cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4686 
4687 						vm_object_unlock(object);
4688 						vm_object_unlock(cur_object);
4689 
4690 						vm_map_unlock_read(map);
4691 						if (real_map != map) {
4692 							vm_map_unlock(real_map);
4693 						}
4694 
4695 						goto RetryFault;
4696 					}
4697 				} else if (object_lock_type == OBJECT_LOCK_SHARED) {
4698 					object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4699 
4700 					if (vm_object_lock_upgrade(object) == FALSE) {
4701 						/*
4702 						 * couldn't upgrade, so explictly take the lock
4703 						 * exclusively and go relookup the page since we
4704 						 * will have dropped the object lock and
4705 						 * a different thread could have inserted
4706 						 * a page at this offset
4707 						 * no need for a full retry since we're
4708 						 * at the top level of the object chain
4709 						 */
4710 						vm_object_lock(object);
4711 
4712 						continue;
4713 					}
4714 				}
4715 				vm_object_lock_assert_exclusive(VM_PAGE_OBJECT(m));
4716 				vm_pageout_steal_laundry(m, FALSE);
4717 			}
4718 
4719 
4720 			if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) {
4721 				/*
4722 				 * Guard page: let the slow path deal with it
4723 				 */
4724 				break;
4725 			}
4726 			if (m->vmp_unusual && (m->vmp_error || m->vmp_restart || m->vmp_private || m->vmp_absent)) {
4727 				/*
4728 				 * Unusual case... let the slow path deal with it
4729 				 */
4730 				break;
4731 			}
4732 			if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object)) {
4733 				if (object != cur_object) {
4734 					vm_object_unlock(object);
4735 				}
4736 				vm_map_unlock_read(map);
4737 				if (real_map != map) {
4738 					vm_map_unlock(real_map);
4739 				}
4740 				vm_object_unlock(cur_object);
4741 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PURGEABLE_FAULT_ERROR), 0 /* arg */);
4742 				kr = KERN_MEMORY_ERROR;
4743 				goto done;
4744 			}
4745 			assert(m_object == VM_PAGE_OBJECT(m));
4746 
4747 			if (vm_fault_cs_need_validation(map->pmap, m, m_object,
4748 			    PAGE_SIZE, 0) ||
4749 			    (physpage_p != NULL && (prot & VM_PROT_WRITE))) {
4750 upgrade_lock_and_retry:
4751 				/*
4752 				 * We might need to validate this page
4753 				 * against its code signature, so we
4754 				 * want to hold the VM object exclusively.
4755 				 */
4756 				if (object != cur_object) {
4757 					if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
4758 						vm_object_unlock(object);
4759 						vm_object_unlock(cur_object);
4760 
4761 						cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4762 
4763 						vm_map_unlock_read(map);
4764 						if (real_map != map) {
4765 							vm_map_unlock(real_map);
4766 						}
4767 
4768 						goto RetryFault;
4769 					}
4770 				} else if (object_lock_type == OBJECT_LOCK_SHARED) {
4771 					object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4772 
4773 					if (vm_object_lock_upgrade(object) == FALSE) {
4774 						/*
4775 						 * couldn't upgrade, so explictly take the lock
4776 						 * exclusively and go relookup the page since we
4777 						 * will have dropped the object lock and
4778 						 * a different thread could have inserted
4779 						 * a page at this offset
4780 						 * no need for a full retry since we're
4781 						 * at the top level of the object chain
4782 						 */
4783 						vm_object_lock(object);
4784 
4785 						continue;
4786 					}
4787 				}
4788 			}
4789 			/*
4790 			 *	Two cases of map in faults:
4791 			 *	    - At top level w/o copy object.
4792 			 *	    - Read fault anywhere.
4793 			 *		--> must disallow write.
4794 			 */
4795 
4796 			if (object == cur_object && object->vo_copy == VM_OBJECT_NULL) {
4797 #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES
4798 				if ((fault_type & VM_PROT_WRITE) && m->vmp_unmodified_ro) {
4799 					assert(cur_object == VM_PAGE_OBJECT(m));
4800 					assert(cur_object->internal);
4801 					vm_object_lock_assert_exclusive(cur_object);
4802 					vm_page_lockspin_queues();
4803 					m->vmp_unmodified_ro = false;
4804 					vm_page_unlock_queues();
4805 					os_atomic_dec(&compressor_ro_uncompressed, relaxed);
4806 					VM_COMPRESSOR_PAGER_STATE_CLR(cur_object, m->vmp_offset);
4807 				}
4808 #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */
4809 				goto FastPmapEnter;
4810 			}
4811 
4812 			if (!need_copy &&
4813 			    !fault_info.no_copy_on_read &&
4814 			    cur_object != object &&
4815 			    !cur_object->internal &&
4816 			    !cur_object->pager_trusted &&
4817 			    vm_protect_privileged_from_untrusted &&
4818 			    !cur_object->code_signed &&
4819 			    current_proc_is_privileged()) {
4820 				/*
4821 				 * We're faulting on a page in "object" and
4822 				 * went down the shadow chain to "cur_object"
4823 				 * to find out that "cur_object"'s pager
4824 				 * is not "trusted", i.e. we can not trust it
4825 				 * to always return the same contents.
4826 				 * Since the target is a "privileged" process,
4827 				 * let's treat this as a copy-on-read fault, as
4828 				 * if it was a copy-on-write fault.
4829 				 * Once "object" gets a copy of this page, it
4830 				 * won't have to rely on "cur_object" to
4831 				 * provide the contents again.
4832 				 *
4833 				 * This is done by setting "need_copy" and
4834 				 * retrying the fault from the top with the
4835 				 * appropriate locking.
4836 				 *
4837 				 * Special case: if the mapping is executable
4838 				 * and the untrusted object is code-signed and
4839 				 * the process is "cs_enforced", we do not
4840 				 * copy-on-read because that would break
4841 				 * code-signing enforcement expectations (an
4842 				 * executable page must belong to a code-signed
4843 				 * object) and we can rely on code-signing
4844 				 * to re-validate the page if it gets evicted
4845 				 * and paged back in.
4846 				 */
4847 //				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);
4848 				vm_copied_on_read++;
4849 				need_copy = TRUE;
4850 
4851 				vm_object_unlock(object);
4852 				vm_object_unlock(cur_object);
4853 				object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4854 				vm_map_unlock_read(map);
4855 				if (real_map != map) {
4856 					vm_map_unlock(real_map);
4857 				}
4858 				goto RetryFault;
4859 			}
4860 
4861 			if (!(fault_type & VM_PROT_WRITE) && !need_copy) {
4862 				if (!pmap_has_prot_policy(pmap, fault_info.pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, prot)) {
4863 					prot &= ~VM_PROT_WRITE;
4864 				} else {
4865 					/*
4866 					 * For a protection that the pmap cares
4867 					 * about, we must hand over the full
4868 					 * set of protections (so that the pmap
4869 					 * layer can apply any desired policy).
4870 					 * This means that cs_bypass must be
4871 					 * set, as this can force us to pass
4872 					 * RWX.
4873 					 */
4874 					assert(fault_info.cs_bypass);
4875 				}
4876 
4877 				if (object != cur_object) {
4878 					/*
4879 					 * We still need to hold the top object
4880 					 * lock here to prevent a race between
4881 					 * a read fault (taking only "shared"
4882 					 * locks) and a write fault (taking
4883 					 * an "exclusive" lock on the top
4884 					 * object.
4885 					 * Otherwise, as soon as we release the
4886 					 * top lock, the write fault could
4887 					 * proceed and actually complete before
4888 					 * the read fault, and the copied page's
4889 					 * translation could then be overwritten
4890 					 * by the read fault's translation for
4891 					 * the original page.
4892 					 *
4893 					 * Let's just record what the top object
4894 					 * is and we'll release it later.
4895 					 */
4896 					top_object = object;
4897 
4898 					/*
4899 					 * switch to the object that has the new page
4900 					 */
4901 					object = cur_object;
4902 					object_lock_type = cur_object_lock_type;
4903 				}
4904 FastPmapEnter:
4905 				assert(m_object == VM_PAGE_OBJECT(m));
4906 
4907 				/*
4908 				 * prepare for the pmap_enter...
4909 				 * object and map are both locked
4910 				 * m contains valid data
4911 				 * object == m->vmp_object
4912 				 * cur_object == NULL or it's been unlocked
4913 				 * no paging references on either object or cur_object
4914 				 */
4915 				if (top_object != VM_OBJECT_NULL || object_lock_type != OBJECT_LOCK_EXCLUSIVE) {
4916 					need_retry_ptr = &need_retry;
4917 				} else {
4918 					need_retry_ptr = NULL;
4919 				}
4920 
4921 				if (fault_page_size < PAGE_SIZE) {
4922 					DEBUG4K_FAULT("map %p original %p pmap %p va 0x%llx caller pmap %p va 0x%llx pa 0x%llx (0x%llx+0x%llx) prot 0x%x caller_prot 0x%x\n", map, original_map, pmap, (uint64_t)vaddr, caller_pmap, (uint64_t)caller_pmap_addr, (uint64_t)((((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT) + fault_phys_offset), (uint64_t)(((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT), (uint64_t)fault_phys_offset, prot, caller_prot);
4923 					assertf((!(fault_phys_offset & FOURK_PAGE_MASK) &&
4924 					    fault_phys_offset < PAGE_SIZE),
4925 					    "0x%llx\n", (uint64_t)fault_phys_offset);
4926 				} else {
4927 					assertf(fault_phys_offset == 0,
4928 					    "0x%llx\n", (uint64_t)fault_phys_offset);
4929 				}
4930 
4931 				if (__improbable(rtfault &&
4932 				    !m->vmp_realtime &&
4933 				    vm_pageout_protect_realtime)) {
4934 					vm_page_lock_queues();
4935 					if (!m->vmp_realtime) {
4936 						m->vmp_realtime = true;
4937 						vm_page_realtime_count++;
4938 					}
4939 					vm_page_unlock_queues();
4940 				}
4941 				assertf(VM_PAGE_OBJECT(m) == m_object, "m=%p m_object=%p object=%p", m, m_object, object);
4942 				assert(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL);
4943 				if (caller_pmap) {
4944 					kr = vm_fault_enter(m,
4945 					    caller_pmap,
4946 					    caller_pmap_addr,
4947 					    fault_page_size,
4948 					    fault_phys_offset,
4949 					    prot,
4950 					    caller_prot,
4951 					    wired,
4952 					    change_wiring,
4953 					    wire_tag,
4954 					    &fault_info,
4955 					    need_retry_ptr,
4956 					    &type_of_fault,
4957 					    &object_lock_type);
4958 				} else {
4959 					kr = vm_fault_enter(m,
4960 					    pmap,
4961 					    vaddr,
4962 					    fault_page_size,
4963 					    fault_phys_offset,
4964 					    prot,
4965 					    caller_prot,
4966 					    wired,
4967 					    change_wiring,
4968 					    wire_tag,
4969 					    &fault_info,
4970 					    need_retry_ptr,
4971 					    &type_of_fault,
4972 					    &object_lock_type);
4973 				}
4974 
4975 				vm_fault_complete(
4976 					map,
4977 					real_map,
4978 					object,
4979 					m_object,
4980 					m,
4981 					offset,
4982 					trace_real_vaddr,
4983 					&fault_info,
4984 					caller_prot,
4985 					real_vaddr,
4986 					vm_fault_type_for_tracing(need_copy_on_read, type_of_fault),
4987 					need_retry,
4988 					kr,
4989 					physpage_p,
4990 					prot,
4991 					top_object,
4992 					need_collapse,
4993 					cur_offset,
4994 					fault_type,
4995 					&written_on_object,
4996 					&written_on_pager,
4997 					&written_on_offset);
4998 				top_object = VM_OBJECT_NULL;
4999 				if (need_retry == TRUE) {
5000 					/*
5001 					 * vm_fault_enter couldn't complete the PMAP_ENTER...
5002 					 * at this point we don't hold any locks so it's safe
5003 					 * to ask the pmap layer to expand the page table to
5004 					 * accommodate this mapping... once expanded, we'll
5005 					 * re-drive the fault which should result in vm_fault_enter
5006 					 * being able to successfully enter the mapping this time around
5007 					 */
5008 					(void)pmap_enter_options(
5009 						pmap, vaddr, 0, 0, 0, 0, 0,
5010 						PMAP_OPTIONS_NOENTER, NULL);
5011 
5012 					need_retry = FALSE;
5013 					goto RetryFault;
5014 				}
5015 				goto done;
5016 			}
5017 			/*
5018 			 * COPY ON WRITE FAULT
5019 			 */
5020 			assert(object_lock_type == OBJECT_LOCK_EXCLUSIVE);
5021 
5022 			/*
5023 			 * If objects match, then
5024 			 * object->vo_copy must not be NULL (else control
5025 			 * would be in previous code block), and we
5026 			 * have a potential push into the copy object
5027 			 * with which we can't cope with here.
5028 			 */
5029 			if (cur_object == object) {
5030 				/*
5031 				 * must take the slow path to
5032 				 * deal with the copy push
5033 				 */
5034 				break;
5035 			}
5036 
5037 			/*
5038 			 * This is now a shadow based copy on write
5039 			 * fault -- it requires a copy up the shadow
5040 			 * chain.
5041 			 */
5042 			assert(m_object == VM_PAGE_OBJECT(m));
5043 
5044 			if ((cur_object_lock_type == OBJECT_LOCK_SHARED) &&
5045 			    vm_fault_cs_need_validation(NULL, m, m_object,
5046 			    PAGE_SIZE, 0)) {
5047 				goto upgrade_lock_and_retry;
5048 			}
5049 
5050 #if MACH_ASSERT
5051 			if (resilient_media_retry &&
5052 			    vm_fault_resilient_media_inject_error2_rate != 0 &&
5053 			    (++vm_fault_resilient_media_inject_error2 % vm_fault_resilient_media_inject_error2_rate) == 0) {
5054 				/* inject an error */
5055 				cur_m = m;
5056 				m = VM_PAGE_NULL;
5057 				m_object = VM_OBJECT_NULL;
5058 				break;
5059 			}
5060 #endif /* MACH_ASSERT */
5061 			/*
5062 			 * Allocate a page in the original top level
5063 			 * object. Give up if allocate fails.  Also
5064 			 * need to remember current page, as it's the
5065 			 * source of the copy.
5066 			 *
5067 			 * at this point we hold locks on both
5068 			 * object and cur_object... no need to take
5069 			 * paging refs or mark pages BUSY since
5070 			 * we don't drop either object lock until
5071 			 * the page has been copied and inserted
5072 			 */
5073 			cur_m = m;
5074 			m = vm_page_grab_options(grab_options);
5075 			m_object = NULL;
5076 
5077 			if (m == VM_PAGE_NULL) {
5078 				/*
5079 				 * no free page currently available...
5080 				 * must take the slow path
5081 				 */
5082 				break;
5083 			}
5084 
5085 			/*
5086 			 * Now do the copy.  Mark the source page busy...
5087 			 *
5088 			 *	NOTE: This code holds the map lock across
5089 			 *	the page copy.
5090 			 */
5091 			vm_page_copy(cur_m, m);
5092 			vm_page_insert(m, object, vm_object_trunc_page(offset));
5093 			if (VM_MAP_PAGE_MASK(map) != PAGE_MASK) {
5094 				DEBUG4K_FAULT("map %p vaddr 0x%llx page %p [%p 0x%llx] copied to %p [%p 0x%llx]\n", map, (uint64_t)vaddr, cur_m, VM_PAGE_OBJECT(cur_m), cur_m->vmp_offset, m, VM_PAGE_OBJECT(m), m->vmp_offset);
5095 			}
5096 			m_object = object;
5097 			SET_PAGE_DIRTY(m, FALSE);
5098 
5099 			/*
5100 			 * Now cope with the source page and object
5101 			 */
5102 			if (object->ref_count > 1 && cur_m->vmp_pmapped) {
5103 				pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m));
5104 			} else if (VM_MAP_PAGE_SIZE(map) < PAGE_SIZE) {
5105 				/*
5106 				 * We've copied the full 16K page but we're
5107 				 * about to call vm_fault_enter() only for
5108 				 * the 4K chunk we're faulting on.  The other
5109 				 * three 4K chunks in that page could still
5110 				 * be pmapped in this pmap.
5111 				 * Since the VM object layer thinks that the
5112 				 * entire page has been dealt with and the
5113 				 * original page might no longer be needed,
5114 				 * it might collapse/bypass the original VM
5115 				 * object and free its pages, which would be
5116 				 * bad (and would trigger pmap_verify_free()
5117 				 * assertions) if the other 4K chunks are still
5118 				 * pmapped.
5119 				 */
5120 				/*
5121 				 * XXX FBDP TODO4K: to be revisisted
5122 				 * Technically, we need to pmap_disconnect()
5123 				 * only the target pmap's mappings for the 4K
5124 				 * chunks of this 16K VM page.  If other pmaps
5125 				 * have PTEs on these chunks, that means that
5126 				 * the associated VM map must have a reference
5127 				 * on the VM object, so no need to worry about
5128 				 * those.
5129 				 * pmap_protect() for each 4K chunk would be
5130 				 * better but we'd have to check which chunks
5131 				 * are actually mapped before and after this
5132 				 * one.
5133 				 * A full-blown pmap_disconnect() is easier
5134 				 * for now but not efficient.
5135 				 */
5136 				DEBUG4K_FAULT("pmap_disconnect() page %p object %p offset 0x%llx phys 0x%x\n", cur_m, VM_PAGE_OBJECT(cur_m), cur_m->vmp_offset, VM_PAGE_GET_PHYS_PAGE(cur_m));
5137 				pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m));
5138 			}
5139 
5140 			if (cur_m->vmp_clustered) {
5141 				VM_PAGE_COUNT_AS_PAGEIN(cur_m);
5142 				VM_PAGE_CONSUME_CLUSTERED(cur_m);
5143 				vm_fault_is_sequential(cur_object, cur_offset, fault_info.behavior);
5144 			}
5145 			need_collapse = TRUE;
5146 
5147 			if (!cur_object->internal &&
5148 			    cur_object->copy_strategy == MEMORY_OBJECT_COPY_DELAY) {
5149 				/*
5150 				 * The object from which we've just
5151 				 * copied a page is most probably backed
5152 				 * by a vnode.  We don't want to waste too
5153 				 * much time trying to collapse the VM objects
5154 				 * and create a bottleneck when several tasks
5155 				 * map the same file.
5156 				 */
5157 				if (cur_object->vo_copy == object) {
5158 					/*
5159 					 * Shared mapping or no COW yet.
5160 					 * We can never collapse a copy
5161 					 * object into its backing object.
5162 					 */
5163 					need_collapse = FALSE;
5164 				} else if (cur_object->vo_copy == object->shadow &&
5165 				    object->shadow->resident_page_count == 0) {
5166 					/*
5167 					 * Shared mapping after a COW occurred.
5168 					 */
5169 					need_collapse = FALSE;
5170 				}
5171 			}
5172 			vm_object_unlock(cur_object);
5173 
5174 			if (need_collapse == FALSE) {
5175 				vm_fault_collapse_skipped++;
5176 			}
5177 			vm_fault_collapse_total++;
5178 
5179 			type_of_fault = DBG_COW_FAULT;
5180 			counter_inc(&vm_statistics_cow_faults);
5181 			DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL);
5182 			counter_inc(&current_task()->cow_faults);
5183 
5184 			goto FastPmapEnter;
5185 		} else {
5186 			/*
5187 			 * No page at cur_object, cur_offset... m == NULL
5188 			 */
5189 			if (cur_object->pager_created) {
5190 				vm_external_state_t compressor_external_state = VM_EXTERNAL_STATE_UNKNOWN;
5191 
5192 				if (MUST_ASK_PAGER(cur_object, cur_offset, compressor_external_state) == TRUE) {
5193 					int             my_fault_type;
5194 					vm_compressor_options_t         c_flags = C_DONT_BLOCK;
5195 					bool            insert_cur_object = FALSE;
5196 
5197 					/*
5198 					 * May have to talk to a pager...
5199 					 * if so, take the slow path by
5200 					 * doing a 'break' from the while (TRUE) loop
5201 					 *
5202 					 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
5203 					 * if the compressor is active and the page exists there
5204 					 */
5205 					if (compressor_external_state != VM_EXTERNAL_STATE_EXISTS) {
5206 						break;
5207 					}
5208 
5209 					if (map == kernel_map || real_map == kernel_map) {
5210 						/*
5211 						 * can't call into the compressor with the kernel_map
5212 						 * lock held, since the compressor may try to operate
5213 						 * on the kernel map in order to return an empty c_segment
5214 						 */
5215 						break;
5216 					}
5217 					if (object != cur_object) {
5218 						if (fault_type & VM_PROT_WRITE) {
5219 							c_flags |= C_KEEP;
5220 						} else {
5221 							insert_cur_object = TRUE;
5222 						}
5223 					}
5224 					if (insert_cur_object == TRUE) {
5225 						if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
5226 							cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
5227 
5228 							if (vm_object_lock_upgrade(cur_object) == FALSE) {
5229 								/*
5230 								 * couldn't upgrade so go do a full retry
5231 								 * immediately since we can no longer be
5232 								 * certain about cur_object (since we
5233 								 * don't hold a reference on it)...
5234 								 * first drop the top object lock
5235 								 */
5236 								vm_object_unlock(object);
5237 
5238 								vm_map_unlock_read(map);
5239 								if (real_map != map) {
5240 									vm_map_unlock(real_map);
5241 								}
5242 
5243 								goto RetryFault;
5244 							}
5245 						}
5246 					} else if (object_lock_type == OBJECT_LOCK_SHARED) {
5247 						object_lock_type = OBJECT_LOCK_EXCLUSIVE;
5248 
5249 						if (object != cur_object) {
5250 							/*
5251 							 * we can't go for the upgrade on the top
5252 							 * lock since the upgrade may block waiting
5253 							 * for readers to drain... since we hold
5254 							 * cur_object locked at this point, waiting
5255 							 * for the readers to drain would represent
5256 							 * a lock order inversion since the lock order
5257 							 * for objects is the reference order in the
5258 							 * shadown chain
5259 							 */
5260 							vm_object_unlock(object);
5261 							vm_object_unlock(cur_object);
5262 
5263 							vm_map_unlock_read(map);
5264 							if (real_map != map) {
5265 								vm_map_unlock(real_map);
5266 							}
5267 
5268 							goto RetryFault;
5269 						}
5270 						if (vm_object_lock_upgrade(object) == FALSE) {
5271 							/*
5272 							 * couldn't upgrade, so explictly take the lock
5273 							 * exclusively and go relookup the page since we
5274 							 * will have dropped the object lock and
5275 							 * a different thread could have inserted
5276 							 * a page at this offset
5277 							 * no need for a full retry since we're
5278 							 * at the top level of the object chain
5279 							 */
5280 							vm_object_lock(object);
5281 
5282 							continue;
5283 						}
5284 					}
5285 					m = vm_page_grab_options(grab_options);
5286 					m_object = NULL;
5287 
5288 					if (m == VM_PAGE_NULL) {
5289 						/*
5290 						 * no free page currently available...
5291 						 * must take the slow path
5292 						 */
5293 						break;
5294 					}
5295 
5296 					/*
5297 					 * The object is and remains locked
5298 					 * so no need to take a
5299 					 * "paging_in_progress" reference.
5300 					 */
5301 					bool      shared_lock;
5302 					if ((object == cur_object &&
5303 					    object_lock_type == OBJECT_LOCK_EXCLUSIVE) ||
5304 					    (object != cur_object &&
5305 					    cur_object_lock_type == OBJECT_LOCK_EXCLUSIVE)) {
5306 						shared_lock = FALSE;
5307 					} else {
5308 						shared_lock = TRUE;
5309 					}
5310 
5311 					kr = vm_compressor_pager_get(
5312 						cur_object->pager,
5313 						(vm_object_trunc_page(cur_offset)
5314 						+ cur_object->paging_offset),
5315 						VM_PAGE_GET_PHYS_PAGE(m),
5316 						&my_fault_type,
5317 						c_flags,
5318 						&compressed_count_delta);
5319 
5320 					vm_compressor_pager_count(
5321 						cur_object->pager,
5322 						compressed_count_delta,
5323 						shared_lock,
5324 						cur_object);
5325 
5326 					if (kr != KERN_SUCCESS) {
5327 						vm_page_release(m, FALSE);
5328 						m = VM_PAGE_NULL;
5329 					}
5330 					/*
5331 					 * If vm_compressor_pager_get() returns
5332 					 * KERN_MEMORY_FAILURE, then the
5333 					 * compressed data is permanently lost,
5334 					 * so return this error immediately.
5335 					 */
5336 					if (kr == KERN_MEMORY_FAILURE) {
5337 						if (object != cur_object) {
5338 							vm_object_unlock(cur_object);
5339 						}
5340 						vm_object_unlock(object);
5341 						vm_map_unlock_read(map);
5342 						if (real_map != map) {
5343 							vm_map_unlock(real_map);
5344 						}
5345 
5346 						goto done;
5347 					} else if (kr != KERN_SUCCESS) {
5348 						break;
5349 					}
5350 					m->vmp_dirty = TRUE;
5351 #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES
5352 					if ((fault_type & VM_PROT_WRITE) == 0) {
5353 						prot &= ~VM_PROT_WRITE;
5354 						/*
5355 						 * The page, m, has yet to be inserted
5356 						 * into an object. So we are fine with
5357 						 * the object/cur_object lock being held
5358 						 * shared.
5359 						 */
5360 						vm_page_lockspin_queues();
5361 						m->vmp_unmodified_ro = true;
5362 						vm_page_unlock_queues();
5363 						os_atomic_inc(&compressor_ro_uncompressed, relaxed);
5364 					}
5365 #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */
5366 
5367 					/*
5368 					 * If the object is purgeable, its
5369 					 * owner's purgeable ledgers will be
5370 					 * updated in vm_page_insert() but the
5371 					 * page was also accounted for in a
5372 					 * "compressed purgeable" ledger, so
5373 					 * update that now.
5374 					 */
5375 					if (object != cur_object &&
5376 					    !insert_cur_object) {
5377 						/*
5378 						 * We're not going to insert
5379 						 * the decompressed page into
5380 						 * the object it came from.
5381 						 *
5382 						 * We're dealing with a
5383 						 * copy-on-write fault on
5384 						 * "object".
5385 						 * We're going to decompress
5386 						 * the page directly into the
5387 						 * target "object" while
5388 						 * keepin the compressed
5389 						 * page for "cur_object", so
5390 						 * no ledger update in that
5391 						 * case.
5392 						 */
5393 					} else if (((cur_object->purgable ==
5394 					    VM_PURGABLE_DENY) &&
5395 					    (!cur_object->vo_ledger_tag)) ||
5396 					    (cur_object->vo_owner ==
5397 					    NULL)) {
5398 						/*
5399 						 * "cur_object" is not purgeable
5400 						 * and is not ledger-taged, or
5401 						 * there's no owner for it,
5402 						 * so no owner's ledgers to
5403 						 * update.
5404 						 */
5405 					} else {
5406 						/*
5407 						 * One less compressed
5408 						 * purgeable/tagged page for
5409 						 * cur_object's owner.
5410 						 */
5411 						if (compressed_count_delta) {
5412 							vm_object_owner_compressed_update(
5413 								cur_object,
5414 								-1);
5415 						}
5416 					}
5417 
5418 					if (insert_cur_object) {
5419 						vm_page_insert(m, cur_object, vm_object_trunc_page(cur_offset));
5420 						m_object = cur_object;
5421 					} else {
5422 						vm_page_insert(m, object, vm_object_trunc_page(offset));
5423 						m_object = object;
5424 					}
5425 
5426 					if ((m_object->wimg_bits & VM_WIMG_MASK) != VM_WIMG_USE_DEFAULT) {
5427 						/*
5428 						 * If the page is not cacheable,
5429 						 * we can't let its contents
5430 						 * linger in the data cache
5431 						 * after the decompression.
5432 						 */
5433 						pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m));
5434 					}
5435 
5436 					type_of_fault = my_fault_type;
5437 
5438 					VM_STAT_DECOMPRESSIONS();
5439 
5440 					if (cur_object != object) {
5441 						if (insert_cur_object) {
5442 							top_object = object;
5443 							/*
5444 							 * switch to the object that has the new page
5445 							 */
5446 							object = cur_object;
5447 							object_lock_type = cur_object_lock_type;
5448 						} else {
5449 							vm_object_unlock(cur_object);
5450 							cur_object = object;
5451 						}
5452 					}
5453 					goto FastPmapEnter;
5454 				}
5455 				/*
5456 				 * existence map present and indicates
5457 				 * that the pager doesn't have this page
5458 				 */
5459 			}
5460 			if (cur_object->shadow == VM_OBJECT_NULL ||
5461 			    resilient_media_retry) {
5462 				/*
5463 				 * Zero fill fault.  Page gets
5464 				 * inserted into the original object.
5465 				 */
5466 				if (cur_object->shadow_severed ||
5467 				    VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object) ||
5468 				    cur_object == compressor_object ||
5469 				    is_kernel_object(cur_object)) {
5470 					if (object != cur_object) {
5471 						vm_object_unlock(cur_object);
5472 					}
5473 					vm_object_unlock(object);
5474 
5475 					vm_map_unlock_read(map);
5476 					if (real_map != map) {
5477 						vm_map_unlock(real_map);
5478 					}
5479 					if (VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object)) {
5480 						ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PURGEABLE_FAULT_ERROR), 0 /* arg */);
5481 					}
5482 
5483 					if (cur_object->shadow_severed) {
5484 						ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_OBJECT_SHADOW_SEVERED), 0 /* arg */);
5485 					}
5486 
5487 					kr = KERN_MEMORY_ERROR;
5488 					goto done;
5489 				}
5490 				if (cur_object != object) {
5491 					vm_object_unlock(cur_object);
5492 
5493 					cur_object = object;
5494 				}
5495 				if (object_lock_type == OBJECT_LOCK_SHARED) {
5496 					object_lock_type = OBJECT_LOCK_EXCLUSIVE;
5497 
5498 					if (vm_object_lock_upgrade(object) == FALSE) {
5499 						/*
5500 						 * couldn't upgrade so do a full retry on the fault
5501 						 * since we dropped the object lock which
5502 						 * could allow another thread to insert
5503 						 * a page at this offset
5504 						 */
5505 						vm_map_unlock_read(map);
5506 						if (real_map != map) {
5507 							vm_map_unlock(real_map);
5508 						}
5509 
5510 						goto RetryFault;
5511 					}
5512 				}
5513 				if (!object->internal) {
5514 					panic("%s:%d should not zero-fill page at offset 0x%llx in external object %p", __FUNCTION__, __LINE__, (uint64_t)offset, object);
5515 				}
5516 #if MACH_ASSERT
5517 				if (resilient_media_retry &&
5518 				    vm_fault_resilient_media_inject_error3_rate != 0 &&
5519 				    (++vm_fault_resilient_media_inject_error3 % vm_fault_resilient_media_inject_error3_rate) == 0) {
5520 					/* inject an error */
5521 					m_object = NULL;
5522 					break;
5523 				}
5524 #endif /* MACH_ASSERT */
5525 				m = vm_page_alloc(object, vm_object_trunc_page(offset));
5526 				m_object = NULL;
5527 
5528 				if (m == VM_PAGE_NULL) {
5529 					/*
5530 					 * no free page currently available...
5531 					 * must take the slow path
5532 					 */
5533 					break;
5534 				}
5535 				m_object = object;
5536 
5537 				if ((prot & VM_PROT_WRITE) &&
5538 				    !(fault_type & VM_PROT_WRITE) &&
5539 				    object->vo_copy != VM_OBJECT_NULL) {
5540 					/*
5541 					 * This is not a write fault and
5542 					 * we might have a copy-on-write
5543 					 * obligation to honor (copy object or
5544 					 * "needs_copy" map entry), so do not
5545 					 * give write access yet.
5546 					 * We'll need to catch the first write
5547 					 * to resolve the copy-on-write by
5548 					 * pushing this page to a copy object
5549 					 * or making a shadow object.
5550 					 */
5551 					if (!pmap_has_prot_policy(pmap, fault_info.pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, prot)) {
5552 						prot &= ~VM_PROT_WRITE;
5553 					} else {
5554 						assert(fault_info.cs_bypass);
5555 					}
5556 				}
5557 				assertf(!((fault_type & VM_PROT_WRITE) && object->vo_copy),
5558 				    "map %p va 0x%llx wrong path for write fault (fault_type 0x%x) on object %p with copy %p\n",
5559 				    map, (uint64_t)vaddr, fault_type, object, object->vo_copy);
5560 
5561 				vm_object_t saved_copy_object;
5562 				uint32_t saved_copy_version;
5563 				saved_copy_object = object->vo_copy;
5564 				saved_copy_version = object->vo_copy_version;
5565 
5566 				/*
5567 				 * Zeroing the page and entering into it into the pmap
5568 				 * represents a significant amount of the zero fill fault handler's work.
5569 				 *
5570 				 * To improve fault scalability, we'll drop the object lock, if it appears contended,
5571 				 * now that we've inserted the page into the vm object.
5572 				 * Before dropping the lock, we need to check protection bits and set the
5573 				 * mapped bits on the page. Then we can mark the page busy, drop the lock,
5574 				 * zero it, and do the pmap enter. We'll need to reacquire the lock
5575 				 * to clear the busy bit and wake up any waiters.
5576 				 */
5577 				vm_fault_cs_clear(m);
5578 				m->vmp_pmapped = TRUE;
5579 				if (map->no_zero_fill) {
5580 					type_of_fault = DBG_NZF_PAGE_FAULT;
5581 				} else {
5582 					type_of_fault = DBG_ZERO_FILL_FAULT;
5583 				}
5584 				{
5585 					pmap_t destination_pmap;
5586 					vm_map_offset_t destination_pmap_vaddr;
5587 					vm_prot_t enter_fault_type;
5588 					if (caller_pmap) {
5589 						destination_pmap = caller_pmap;
5590 						destination_pmap_vaddr = caller_pmap_addr;
5591 					} else {
5592 						destination_pmap = pmap;
5593 						destination_pmap_vaddr = vaddr;
5594 					}
5595 					if (change_wiring) {
5596 						enter_fault_type = VM_PROT_NONE;
5597 					} else {
5598 						enter_fault_type = caller_prot;
5599 					}
5600 					assertf(VM_PAGE_OBJECT(m) == object, "m=%p object=%p", m, object);
5601 					kr = vm_fault_enter_prepare(m,
5602 					    destination_pmap,
5603 					    destination_pmap_vaddr,
5604 					    &prot,
5605 					    caller_prot,
5606 					    fault_page_size,
5607 					    fault_phys_offset,
5608 					    change_wiring,
5609 					    enter_fault_type,
5610 					    &fault_info,
5611 					    &type_of_fault,
5612 					    &page_needs_data_sync);
5613 					if (kr != KERN_SUCCESS) {
5614 						goto zero_fill_cleanup;
5615 					}
5616 
5617 					if (object_is_contended) {
5618 						/*
5619 						 * At this point the page is in the vm object, but not on a paging queue.
5620 						 * Since it's accessible to another thread but its contents are invalid
5621 						 * (it hasn't been zeroed) mark it busy before dropping the object lock.
5622 						 */
5623 						m->vmp_busy = TRUE;
5624 						vm_object_paging_begin(object); /* keep object alive */
5625 						vm_object_unlock(object);
5626 					}
5627 					if (type_of_fault == DBG_ZERO_FILL_FAULT) {
5628 						/*
5629 						 * Now zero fill page...
5630 						 * the page is probably going to
5631 						 * be written soon, so don't bother
5632 						 * to clear the modified bit
5633 						 *
5634 						 *   NOTE: This code holds the map
5635 						 *   lock across the zero fill.
5636 						 */
5637 						vm_page_zero_fill(m);
5638 						counter_inc(&vm_statistics_zero_fill_count);
5639 						DTRACE_VM2(zfod, int, 1, (uint64_t *), NULL);
5640 					}
5641 
5642 					if (object_is_contended) {
5643 						/*
5644 						 * It's not safe to do the pmap_enter() without holding
5645 						 * the object lock because its "vo_copy" could change.
5646 						 */
5647 						object_is_contended = false; /* get out of that code path */
5648 
5649 						vm_object_lock(object);
5650 						vm_object_paging_end(object);
5651 						if (object->vo_copy != saved_copy_object ||
5652 						    object->vo_copy_version != saved_copy_version) {
5653 							/*
5654 							 * The COPY_DELAY copy-on-write situation for
5655 							 * this VM object has changed while it was
5656 							 * unlocked, so do not grant write access to
5657 							 * this page.
5658 							 * The write access will fault again and we'll
5659 							 * resolve the copy-on-write then.
5660 							 */
5661 							if (!pmap_has_prot_policy(pmap,
5662 							    fault_info.pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE,
5663 							    prot)) {
5664 								/* the pmap layer is OK with changing the PTE's prot */
5665 								prot &= ~VM_PROT_WRITE;
5666 							} else {
5667 								/* we should not do CoW on pmap_has_prot_policy mappings */
5668 								panic("map %p va 0x%llx obj %p,%u saved %p,%u: unexpected CoW",
5669 								    map, (uint64_t)vaddr,
5670 								    object, object->vo_copy_version,
5671 								    saved_copy_object, saved_copy_version);
5672 							}
5673 						}
5674 					}
5675 
5676 					if (page_needs_data_sync) {
5677 						pmap_sync_page_data_phys(VM_PAGE_GET_PHYS_PAGE(m));
5678 					}
5679 
5680 					if (top_object != VM_OBJECT_NULL) {
5681 						need_retry_ptr = &need_retry;
5682 					} else {
5683 						need_retry_ptr = NULL;
5684 					}
5685 					if (fault_info.fi_xnu_user_debug &&
5686 					    !object->code_signed) {
5687 						fault_info.pmap_options |= PMAP_OPTIONS_XNU_USER_DEBUG;
5688 					}
5689 					if (object_is_contended) {
5690 						panic("object_is_contended");
5691 						kr = vm_fault_pmap_enter(destination_pmap, destination_pmap_vaddr,
5692 						    fault_page_size, fault_phys_offset,
5693 						    m, &prot, caller_prot, enter_fault_type, wired,
5694 						    fault_info.pmap_options, need_retry_ptr);
5695 						vm_object_lock(object);
5696 						assertf(!((prot & VM_PROT_WRITE) && object->vo_copy),
5697 						    "prot 0x%x object %p copy %p\n",
5698 						    prot, object, object->vo_copy);
5699 					} else {
5700 						kr = vm_fault_pmap_enter_with_object_lock(object, destination_pmap, destination_pmap_vaddr,
5701 						    fault_page_size, fault_phys_offset,
5702 						    m, &prot, caller_prot, enter_fault_type, wired,
5703 						    fault_info.pmap_options, need_retry_ptr, &object_lock_type);
5704 					}
5705 				}
5706 zero_fill_cleanup:
5707 				if (!VM_DYNAMIC_PAGING_ENABLED() &&
5708 				    (object->purgable == VM_PURGABLE_DENY ||
5709 				    object->purgable == VM_PURGABLE_NONVOLATILE ||
5710 				    object->purgable == VM_PURGABLE_VOLATILE)) {
5711 					vm_page_lockspin_queues();
5712 					if (!VM_DYNAMIC_PAGING_ENABLED()) {
5713 						vm_fault_enqueue_throttled_locked(m);
5714 					}
5715 					vm_page_unlock_queues();
5716 				}
5717 				vm_fault_enqueue_page(object, m, wired, change_wiring, wire_tag, fault_info.no_cache, &type_of_fault, kr);
5718 
5719 				if (__improbable(rtfault &&
5720 				    !m->vmp_realtime &&
5721 				    vm_pageout_protect_realtime)) {
5722 					vm_page_lock_queues();
5723 					if (!m->vmp_realtime) {
5724 						m->vmp_realtime = true;
5725 						vm_page_realtime_count++;
5726 					}
5727 					vm_page_unlock_queues();
5728 				}
5729 				vm_fault_complete(
5730 					map,
5731 					real_map,
5732 					object,
5733 					m_object,
5734 					m,
5735 					offset,
5736 					trace_real_vaddr,
5737 					&fault_info,
5738 					caller_prot,
5739 					real_vaddr,
5740 					type_of_fault,
5741 					need_retry,
5742 					kr,
5743 					physpage_p,
5744 					prot,
5745 					top_object,
5746 					need_collapse,
5747 					cur_offset,
5748 					fault_type,
5749 					&written_on_object,
5750 					&written_on_pager,
5751 					&written_on_offset);
5752 				top_object = VM_OBJECT_NULL;
5753 				if (need_retry == TRUE) {
5754 					/*
5755 					 * vm_fault_enter couldn't complete the PMAP_ENTER...
5756 					 * at this point we don't hold any locks so it's safe
5757 					 * to ask the pmap layer to expand the page table to
5758 					 * accommodate this mapping... once expanded, we'll
5759 					 * re-drive the fault which should result in vm_fault_enter
5760 					 * being able to successfully enter the mapping this time around
5761 					 */
5762 					(void)pmap_enter_options(
5763 						pmap, vaddr, 0, 0, 0, 0, 0,
5764 						PMAP_OPTIONS_NOENTER, NULL);
5765 
5766 					need_retry = FALSE;
5767 					goto RetryFault;
5768 				}
5769 				goto done;
5770 			}
5771 			/*
5772 			 * On to the next level in the shadow chain
5773 			 */
5774 			cur_offset += cur_object->vo_shadow_offset;
5775 			new_object = cur_object->shadow;
5776 			fault_phys_offset = cur_offset - vm_object_trunc_page(cur_offset);
5777 
5778 			/*
5779 			 * take the new_object's lock with the indicated state
5780 			 */
5781 			if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
5782 				vm_object_lock_shared(new_object);
5783 			} else {
5784 				vm_object_lock(new_object);
5785 			}
5786 
5787 			if (cur_object != object) {
5788 				vm_object_unlock(cur_object);
5789 			}
5790 
5791 			cur_object = new_object;
5792 
5793 			continue;
5794 		}
5795 	}
5796 	/*
5797 	 * Cleanup from fast fault failure.  Drop any object
5798 	 * lock other than original and drop map lock.
5799 	 */
5800 	if (object != cur_object) {
5801 		vm_object_unlock(cur_object);
5802 	}
5803 
5804 	/*
5805 	 * must own the object lock exclusively at this point
5806 	 */
5807 	if (object_lock_type == OBJECT_LOCK_SHARED) {
5808 		object_lock_type = OBJECT_LOCK_EXCLUSIVE;
5809 
5810 		if (vm_object_lock_upgrade(object) == FALSE) {
5811 			/*
5812 			 * couldn't upgrade, so explictly
5813 			 * take the lock exclusively
5814 			 * no need to retry the fault at this
5815 			 * point since "vm_fault_page" will
5816 			 * completely re-evaluate the state
5817 			 */
5818 			vm_object_lock(object);
5819 		}
5820 	}
5821 
5822 handle_copy_delay:
5823 	vm_map_unlock_read(map);
5824 	if (real_map != map) {
5825 		vm_map_unlock(real_map);
5826 	}
5827 
5828 	if (__improbable(object == compressor_object ||
5829 	    is_kernel_object(object))) {
5830 		/*
5831 		 * These objects are explicitly managed and populated by the
5832 		 * kernel.  The virtual ranges backed by these objects should
5833 		 * either have wired pages or "holes" that are not supposed to
5834 		 * be accessed at all until they get explicitly populated.
5835 		 * We should never have to resolve a fault on a mapping backed
5836 		 * by one of these VM objects and providing a zero-filled page
5837 		 * would be wrong here, so let's fail the fault and let the
5838 		 * caller crash or recover.
5839 		 */
5840 		vm_object_unlock(object);
5841 		kr = KERN_MEMORY_ERROR;
5842 		goto done;
5843 	}
5844 
5845 	resilient_media_ref_transfer = false;
5846 	if (resilient_media_retry) {
5847 		/*
5848 		 * We could get here if we failed to get a free page
5849 		 * to zero-fill and had to take the slow path again.
5850 		 * Reset our "recovery-from-failed-media" state.
5851 		 */
5852 		assert(resilient_media_object != VM_OBJECT_NULL);
5853 		assert(resilient_media_offset != (vm_object_offset_t)-1);
5854 		/* release our extra reference on failed object */
5855 //             printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
5856 		if (object == resilient_media_object) {
5857 			/*
5858 			 * We're holding "object"'s lock, so we can't release
5859 			 * our extra reference at this point.
5860 			 * We need an extra reference on "object" anyway
5861 			 * (see below), so let's just transfer this reference.
5862 			 */
5863 			resilient_media_ref_transfer = true;
5864 		} else {
5865 			vm_object_lock_assert_notheld(resilient_media_object);
5866 			vm_object_deallocate(resilient_media_object);
5867 		}
5868 		resilient_media_object = VM_OBJECT_NULL;
5869 		resilient_media_offset = (vm_object_offset_t)-1;
5870 		resilient_media_retry = false;
5871 		vm_fault_resilient_media_abort2++;
5872 	}
5873 
5874 	/*
5875 	 * Make a reference to this object to
5876 	 * prevent its disposal while we are messing with
5877 	 * it.  Once we have the reference, the map is free
5878 	 * to be diddled.  Since objects reference their
5879 	 * shadows (and copies), they will stay around as well.
5880 	 */
5881 	if (resilient_media_ref_transfer) {
5882 		/* we already have an extra reference on this object */
5883 		resilient_media_ref_transfer = false;
5884 	} else {
5885 		vm_object_reference_locked(object);
5886 	}
5887 	vm_object_paging_begin(object);
5888 
5889 	set_thread_pagein_error(cthread, 0);
5890 	error_code = 0;
5891 
5892 	result_page = VM_PAGE_NULL;
5893 	kr = vm_fault_page(object, offset, fault_type,
5894 	    (change_wiring && !wired),
5895 	    FALSE,                /* page not looked up */
5896 	    &prot, &result_page, &top_page,
5897 	    &type_of_fault,
5898 	    &error_code, map->no_zero_fill,
5899 	    &fault_info);
5900 
5901 	/*
5902 	 * if kr != VM_FAULT_SUCCESS, then the paging reference
5903 	 * has been dropped and the object unlocked... the ref_count
5904 	 * is still held
5905 	 *
5906 	 * if kr == VM_FAULT_SUCCESS, then the paging reference
5907 	 * is still held along with the ref_count on the original object
5908 	 *
5909 	 *	the object is returned locked with a paging reference
5910 	 *
5911 	 *	if top_page != NULL, then it's BUSY and the
5912 	 *	object it belongs to has a paging reference
5913 	 *	but is returned unlocked
5914 	 */
5915 	if (kr != VM_FAULT_SUCCESS &&
5916 	    kr != VM_FAULT_SUCCESS_NO_VM_PAGE) {
5917 		if (kr == VM_FAULT_MEMORY_ERROR &&
5918 		    fault_info.resilient_media) {
5919 			assertf(object->internal, "object %p", object);
5920 			/*
5921 			 * This fault failed but the mapping was
5922 			 * "media resilient", so we'll retry the fault in
5923 			 * recovery mode to get a zero-filled page in the
5924 			 * top object.
5925 			 * Keep the reference on the failing object so
5926 			 * that we can check that the mapping is still
5927 			 * pointing to it when we retry the fault.
5928 			 */
5929 //                     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);
5930 			assert(!resilient_media_retry); /* no double retry */
5931 			assert(resilient_media_object == VM_OBJECT_NULL);
5932 			assert(resilient_media_offset == (vm_object_offset_t)-1);
5933 			resilient_media_retry = true;
5934 			resilient_media_object = object;
5935 			resilient_media_offset = offset;
5936 //                     printf("FBDP %s:%d resilient_media_object %p offset 0x%llx kept reference\n", __FUNCTION__, __LINE__, resilient_media_object, resilient_mmedia_offset);
5937 			vm_fault_resilient_media_initiate++;
5938 			goto RetryFault;
5939 		} else {
5940 			/*
5941 			 * we didn't succeed, lose the object reference
5942 			 * immediately.
5943 			 */
5944 			vm_object_deallocate(object);
5945 			object = VM_OBJECT_NULL; /* no longer valid */
5946 		}
5947 
5948 		/*
5949 		 * See why we failed, and take corrective action.
5950 		 */
5951 		switch (kr) {
5952 		case VM_FAULT_MEMORY_SHORTAGE:
5953 			if (vm_page_wait((change_wiring) ?
5954 			    THREAD_UNINT :
5955 			    THREAD_ABORTSAFE)) {
5956 				goto RetryFault;
5957 			}
5958 			ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_MEMORY_SHORTAGE), 0 /* arg */);
5959 			OS_FALLTHROUGH;
5960 		case VM_FAULT_INTERRUPTED:
5961 			ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_INTERRUPTED), 0 /* arg */);
5962 			kr = KERN_ABORTED;
5963 			goto done;
5964 		case VM_FAULT_RETRY:
5965 			goto RetryFault;
5966 		case VM_FAULT_MEMORY_ERROR:
5967 			if (error_code) {
5968 				kr = error_code;
5969 			} else {
5970 				kr = KERN_MEMORY_ERROR;
5971 			}
5972 			goto done;
5973 		default:
5974 			panic("vm_fault: unexpected error 0x%x from "
5975 			    "vm_fault_page()\n", kr);
5976 		}
5977 	}
5978 	m = result_page;
5979 	m_object = NULL;
5980 
5981 	if (m != VM_PAGE_NULL) {
5982 		m_object = VM_PAGE_OBJECT(m);
5983 		assert((change_wiring && !wired) ?
5984 		    (top_page == VM_PAGE_NULL) :
5985 		    ((top_page == VM_PAGE_NULL) == (m_object == object)));
5986 	}
5987 
5988 	/*
5989 	 * What to do with the resulting page from vm_fault_page
5990 	 * if it doesn't get entered into the physical map:
5991 	 */
5992 #define RELEASE_PAGE(m)                                 \
5993 	MACRO_BEGIN                                     \
5994 	PAGE_WAKEUP_DONE(m);                            \
5995 	if ( !VM_PAGE_PAGEABLE(m)) {                    \
5996 	        vm_page_lockspin_queues();              \
5997 	        if ( !VM_PAGE_PAGEABLE(m))              \
5998 	                vm_page_activate(m);            \
5999 	        vm_page_unlock_queues();                \
6000 	}                                               \
6001 	MACRO_END
6002 
6003 
6004 	object_locks_dropped = FALSE;
6005 	/*
6006 	 * We must verify that the maps have not changed
6007 	 * since our last lookup. vm_map_verify() needs the
6008 	 * map lock (shared) but we are holding object locks.
6009 	 * So we do a try_lock() first and, if that fails, we
6010 	 * drop the object locks and go in for the map lock again.
6011 	 */
6012 	if (m != VM_PAGE_NULL) {
6013 		old_copy_object = m_object->vo_copy;
6014 		old_copy_version = m_object->vo_copy_version;
6015 	} else {
6016 		old_copy_object = VM_OBJECT_NULL;
6017 		old_copy_version = 0;
6018 	}
6019 	if (!vm_map_try_lock_read(original_map)) {
6020 		if (m != VM_PAGE_NULL) {
6021 			vm_object_unlock(m_object);
6022 		} else {
6023 			vm_object_unlock(object);
6024 		}
6025 
6026 		object_locks_dropped = TRUE;
6027 
6028 		vm_map_lock_read(original_map);
6029 	}
6030 
6031 	if ((map != original_map) || !vm_map_verify(map, &version)) {
6032 		if (object_locks_dropped == FALSE) {
6033 			if (m != VM_PAGE_NULL) {
6034 				vm_object_unlock(m_object);
6035 			} else {
6036 				vm_object_unlock(object);
6037 			}
6038 
6039 			object_locks_dropped = TRUE;
6040 		}
6041 
6042 		/*
6043 		 * no object locks are held at this point
6044 		 */
6045 		vm_object_t             retry_object;
6046 		vm_object_offset_t      retry_offset;
6047 		vm_prot_t               retry_prot;
6048 
6049 		/*
6050 		 * To avoid trying to write_lock the map while another
6051 		 * thread has it read_locked (in vm_map_pageable), we
6052 		 * do not try for write permission.  If the page is
6053 		 * still writable, we will get write permission.  If it
6054 		 * is not, or has been marked needs_copy, we enter the
6055 		 * mapping without write permission, and will merely
6056 		 * take another fault.
6057 		 */
6058 		map = original_map;
6059 
6060 		kr = vm_map_lookup_and_lock_object(&map, vaddr,
6061 		    fault_type & ~VM_PROT_WRITE,
6062 		    OBJECT_LOCK_EXCLUSIVE, &version,
6063 		    &retry_object, &retry_offset, &retry_prot,
6064 		    &wired,
6065 		    &fault_info,
6066 		    &real_map,
6067 		    NULL);
6068 		pmap = real_map->pmap;
6069 
6070 		if (kr != KERN_SUCCESS) {
6071 			vm_map_unlock_read(map);
6072 
6073 			if (m != VM_PAGE_NULL) {
6074 				assert(VM_PAGE_OBJECT(m) == m_object);
6075 
6076 				/*
6077 				 * retake the lock so that
6078 				 * we can drop the paging reference
6079 				 * in vm_fault_cleanup and do the
6080 				 * PAGE_WAKEUP_DONE in RELEASE_PAGE
6081 				 */
6082 				vm_object_lock(m_object);
6083 
6084 				RELEASE_PAGE(m);
6085 
6086 				vm_fault_cleanup(m_object, top_page);
6087 			} else {
6088 				/*
6089 				 * retake the lock so that
6090 				 * we can drop the paging reference
6091 				 * in vm_fault_cleanup
6092 				 */
6093 				vm_object_lock(object);
6094 
6095 				vm_fault_cleanup(object, top_page);
6096 			}
6097 			vm_object_deallocate(object);
6098 
6099 			if (kr == KERN_INVALID_ADDRESS) {
6100 				ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_ADDRESS_NOT_FOUND), 0 /* arg */);
6101 			}
6102 			goto done;
6103 		}
6104 		vm_object_unlock(retry_object);
6105 
6106 		if ((retry_object != object) || (retry_offset != offset)) {
6107 			vm_map_unlock_read(map);
6108 			if (real_map != map) {
6109 				vm_map_unlock(real_map);
6110 			}
6111 
6112 			if (m != VM_PAGE_NULL) {
6113 				assert(VM_PAGE_OBJECT(m) == m_object);
6114 
6115 				/*
6116 				 * retake the lock so that
6117 				 * we can drop the paging reference
6118 				 * in vm_fault_cleanup and do the
6119 				 * PAGE_WAKEUP_DONE in RELEASE_PAGE
6120 				 */
6121 				vm_object_lock(m_object);
6122 
6123 				RELEASE_PAGE(m);
6124 
6125 				vm_fault_cleanup(m_object, top_page);
6126 			} else {
6127 				/*
6128 				 * retake the lock so that
6129 				 * we can drop the paging reference
6130 				 * in vm_fault_cleanup
6131 				 */
6132 				vm_object_lock(object);
6133 
6134 				vm_fault_cleanup(object, top_page);
6135 			}
6136 			vm_object_deallocate(object);
6137 
6138 			goto RetryFault;
6139 		}
6140 		/*
6141 		 * Check whether the protection has changed or the object
6142 		 * has been copied while we left the map unlocked.
6143 		 */
6144 		if (pmap_has_prot_policy(pmap, fault_info.pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, retry_prot)) {
6145 			/* If the pmap layer cares, pass the full set. */
6146 			prot = retry_prot;
6147 		} else {
6148 			prot &= retry_prot;
6149 		}
6150 	}
6151 
6152 	if (object_locks_dropped == TRUE) {
6153 		if (m != VM_PAGE_NULL) {
6154 			assertf(VM_PAGE_OBJECT(m) == m_object, "m=%p m_object=%p", m, m_object);
6155 			assert(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL);
6156 			vm_object_lock(m_object);
6157 		} else {
6158 			vm_object_lock(object);
6159 		}
6160 
6161 		object_locks_dropped = FALSE;
6162 	}
6163 
6164 	if ((prot & VM_PROT_WRITE) &&
6165 	    m != VM_PAGE_NULL &&
6166 	    (m_object->vo_copy != old_copy_object ||
6167 	    m_object->vo_copy_version != old_copy_version)) {
6168 		/*
6169 		 * The copy object changed while the top-level object
6170 		 * was unlocked, so take away write permission.
6171 		 */
6172 		assert(!pmap_has_prot_policy(pmap, fault_info.pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, prot));
6173 		prot &= ~VM_PROT_WRITE;
6174 	}
6175 
6176 	if (!need_copy &&
6177 	    !fault_info.no_copy_on_read &&
6178 	    m != VM_PAGE_NULL &&
6179 	    VM_PAGE_OBJECT(m) != object &&
6180 	    !VM_PAGE_OBJECT(m)->pager_trusted &&
6181 	    vm_protect_privileged_from_untrusted &&
6182 	    !VM_PAGE_OBJECT(m)->code_signed &&
6183 	    current_proc_is_privileged()) {
6184 		/*
6185 		 * We found the page we want in an "untrusted" VM object
6186 		 * down the shadow chain.  Since the target is "privileged"
6187 		 * we want to perform a copy-on-read of that page, so that the
6188 		 * mapped object gets a stable copy and does not have to
6189 		 * rely on the "untrusted" object to provide the same
6190 		 * contents if the page gets reclaimed and has to be paged
6191 		 * in again later on.
6192 		 *
6193 		 * Special case: if the mapping is executable and the untrusted
6194 		 * object is code-signed and the process is "cs_enforced", we
6195 		 * do not copy-on-read because that would break code-signing
6196 		 * enforcement expectations (an executable page must belong
6197 		 * to a code-signed object) and we can rely on code-signing
6198 		 * to re-validate the page if it gets evicted and paged back in.
6199 		 */
6200 //		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);
6201 		vm_copied_on_read++;
6202 		need_copy_on_read = TRUE;
6203 		need_copy = TRUE;
6204 	} else {
6205 		need_copy_on_read = FALSE;
6206 	}
6207 
6208 	/*
6209 	 * If we want to wire down this page, but no longer have
6210 	 * adequate permissions, we must start all over.
6211 	 * If we decided to copy-on-read, we must also start all over.
6212 	 */
6213 	if ((wired && (fault_type != (prot | VM_PROT_WRITE))) ||
6214 	    need_copy_on_read) {
6215 		vm_map_unlock_read(map);
6216 		if (real_map != map) {
6217 			vm_map_unlock(real_map);
6218 		}
6219 
6220 		if (m != VM_PAGE_NULL) {
6221 			assert(VM_PAGE_OBJECT(m) == m_object);
6222 
6223 			RELEASE_PAGE(m);
6224 
6225 			vm_fault_cleanup(m_object, top_page);
6226 		} else {
6227 			vm_fault_cleanup(object, top_page);
6228 		}
6229 
6230 		vm_object_deallocate(object);
6231 
6232 		goto RetryFault;
6233 	}
6234 	if (m != VM_PAGE_NULL) {
6235 		/*
6236 		 * Put this page into the physical map.
6237 		 * We had to do the unlock above because pmap_enter
6238 		 * may cause other faults.  The page may be on
6239 		 * the pageout queues.  If the pageout daemon comes
6240 		 * across the page, it will remove it from the queues.
6241 		 */
6242 		if (fault_page_size < PAGE_SIZE) {
6243 			DEBUG4K_FAULT("map %p original %p pmap %p va 0x%llx pa 0x%llx(0x%llx+0x%llx) prot 0x%x caller_prot 0x%x\n", map, original_map, pmap, (uint64_t)vaddr, (uint64_t)((((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT) + fault_phys_offset), (uint64_t)(((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT), (uint64_t)fault_phys_offset, prot, caller_prot);
6244 			assertf((!(fault_phys_offset & FOURK_PAGE_MASK) &&
6245 			    fault_phys_offset < PAGE_SIZE),
6246 			    "0x%llx\n", (uint64_t)fault_phys_offset);
6247 		} else {
6248 			assertf(fault_phys_offset == 0,
6249 			    "0x%llx\n", (uint64_t)fault_phys_offset);
6250 		}
6251 		assertf(VM_PAGE_OBJECT(m) == m_object, "m=%p m_object=%p", m, m_object);
6252 		assert(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL);
6253 		if (caller_pmap) {
6254 			kr = vm_fault_enter(m,
6255 			    caller_pmap,
6256 			    caller_pmap_addr,
6257 			    fault_page_size,
6258 			    fault_phys_offset,
6259 			    prot,
6260 			    caller_prot,
6261 			    wired,
6262 			    change_wiring,
6263 			    wire_tag,
6264 			    &fault_info,
6265 			    NULL,
6266 			    &type_of_fault,
6267 			    &object_lock_type);
6268 		} else {
6269 			kr = vm_fault_enter(m,
6270 			    pmap,
6271 			    vaddr,
6272 			    fault_page_size,
6273 			    fault_phys_offset,
6274 			    prot,
6275 			    caller_prot,
6276 			    wired,
6277 			    change_wiring,
6278 			    wire_tag,
6279 			    &fault_info,
6280 			    NULL,
6281 			    &type_of_fault,
6282 			    &object_lock_type);
6283 		}
6284 		assert(VM_PAGE_OBJECT(m) == m_object);
6285 
6286 		{
6287 			int     event_code = 0;
6288 
6289 			if (m_object->internal) {
6290 				event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_INTERNAL));
6291 			} else if (m_object->object_is_shared_cache) {
6292 				event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_SHAREDCACHE));
6293 			} else {
6294 				event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_EXTERNAL));
6295 			}
6296 
6297 			KDBG_RELEASE(event_code | DBG_FUNC_NONE, trace_real_vaddr, (fault_info.user_tag << 16) | (caller_prot << 8) | vm_fault_type_for_tracing(need_copy_on_read, type_of_fault), m->vmp_offset, get_current_unique_pid());
6298 			KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_SLOW), get_current_unique_pid());
6299 
6300 			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);
6301 		}
6302 		if (kr != KERN_SUCCESS) {
6303 			/* abort this page fault */
6304 			vm_map_unlock_read(map);
6305 			if (real_map != map) {
6306 				vm_map_unlock(real_map);
6307 			}
6308 			PAGE_WAKEUP_DONE(m);
6309 			vm_fault_cleanup(m_object, top_page);
6310 			vm_object_deallocate(object);
6311 			goto done;
6312 		}
6313 		if (physpage_p != NULL) {
6314 			/* for vm_map_wire_and_extract() */
6315 			*physpage_p = VM_PAGE_GET_PHYS_PAGE(m);
6316 			if (prot & VM_PROT_WRITE) {
6317 				vm_object_lock_assert_exclusive(m_object);
6318 				m->vmp_dirty = TRUE;
6319 			}
6320 		}
6321 	} else {
6322 		vm_map_entry_t          entry;
6323 		vm_map_offset_t         laddr;
6324 		vm_map_offset_t         ldelta, hdelta;
6325 
6326 		/*
6327 		 * do a pmap block mapping from the physical address
6328 		 * in the object
6329 		 */
6330 
6331 		if (real_map != map) {
6332 			vm_map_unlock(real_map);
6333 		}
6334 
6335 		if (original_map != map) {
6336 			vm_map_unlock_read(map);
6337 			vm_map_lock_read(original_map);
6338 			map = original_map;
6339 		}
6340 		real_map = map;
6341 
6342 		laddr = vaddr;
6343 		hdelta = ldelta = (vm_map_offset_t)0xFFFFFFFFFFFFF000ULL;
6344 
6345 		while (vm_map_lookup_entry(map, laddr, &entry)) {
6346 			if (ldelta > (laddr - entry->vme_start)) {
6347 				ldelta = laddr - entry->vme_start;
6348 			}
6349 			if (hdelta > (entry->vme_end - laddr)) {
6350 				hdelta = entry->vme_end - laddr;
6351 			}
6352 			if (entry->is_sub_map) {
6353 				laddr = ((laddr - entry->vme_start)
6354 				    + VME_OFFSET(entry));
6355 				vm_map_lock_read(VME_SUBMAP(entry));
6356 
6357 				if (map != real_map) {
6358 					vm_map_unlock_read(map);
6359 				}
6360 				if (entry->use_pmap) {
6361 					vm_map_unlock_read(real_map);
6362 					real_map = VME_SUBMAP(entry);
6363 				}
6364 				map = VME_SUBMAP(entry);
6365 			} else {
6366 				break;
6367 			}
6368 		}
6369 
6370 		if (vm_map_lookup_entry(map, laddr, &entry) &&
6371 		    (!entry->is_sub_map) &&
6372 		    (object != VM_OBJECT_NULL) &&
6373 		    (VME_OBJECT(entry) == object)) {
6374 			uint16_t superpage;
6375 
6376 			if (!object->pager_created &&
6377 			    object->phys_contiguous &&
6378 			    VME_OFFSET(entry) == 0 &&
6379 			    (entry->vme_end - entry->vme_start == object->vo_size) &&
6380 			    VM_MAP_PAGE_ALIGNED(entry->vme_start, (object->vo_size - 1))) {
6381 				superpage = VM_MEM_SUPERPAGE;
6382 			} else {
6383 				superpage = 0;
6384 			}
6385 
6386 			if (superpage && physpage_p) {
6387 				/* for vm_map_wire_and_extract() */
6388 				*physpage_p = (ppnum_t)
6389 				    ((((vm_map_offset_t)
6390 				    object->vo_shadow_offset)
6391 				    + VME_OFFSET(entry)
6392 				    + (laddr - entry->vme_start))
6393 				    >> PAGE_SHIFT);
6394 			}
6395 
6396 			if (caller_pmap) {
6397 				/*
6398 				 * Set up a block mapped area
6399 				 */
6400 				assert((uint32_t)((ldelta + hdelta) >> fault_page_shift) == ((ldelta + hdelta) >> fault_page_shift));
6401 				kr = pmap_map_block_addr(caller_pmap,
6402 				    (addr64_t)(caller_pmap_addr - ldelta),
6403 				    (pmap_paddr_t)(((vm_map_offset_t) (object->vo_shadow_offset)) +
6404 				    VME_OFFSET(entry) + (laddr - entry->vme_start) - ldelta),
6405 				    (uint32_t)((ldelta + hdelta) >> fault_page_shift), prot,
6406 				    (VM_WIMG_MASK & (int)object->wimg_bits) | superpage, 0);
6407 
6408 				if (kr != KERN_SUCCESS) {
6409 					goto cleanup;
6410 				}
6411 			} else {
6412 				/*
6413 				 * Set up a block mapped area
6414 				 */
6415 				assert((uint32_t)((ldelta + hdelta) >> fault_page_shift) == ((ldelta + hdelta) >> fault_page_shift));
6416 				kr = pmap_map_block_addr(real_map->pmap,
6417 				    (addr64_t)(vaddr - ldelta),
6418 				    (pmap_paddr_t)(((vm_map_offset_t)(object->vo_shadow_offset)) +
6419 				    VME_OFFSET(entry) + (laddr - entry->vme_start) - ldelta),
6420 				    (uint32_t)((ldelta + hdelta) >> fault_page_shift), prot,
6421 				    (VM_WIMG_MASK & (int)object->wimg_bits) | superpage, 0);
6422 
6423 				if (kr != KERN_SUCCESS) {
6424 					goto cleanup;
6425 				}
6426 			}
6427 		}
6428 	}
6429 
6430 	/*
6431 	 * Success
6432 	 */
6433 	kr = KERN_SUCCESS;
6434 
6435 	/*
6436 	 * TODO: could most of the done cases just use cleanup?
6437 	 */
6438 cleanup:
6439 	/*
6440 	 * Unlock everything, and return
6441 	 */
6442 	vm_map_unlock_read(map);
6443 	if (real_map != map) {
6444 		vm_map_unlock(real_map);
6445 	}
6446 
6447 	if (m != VM_PAGE_NULL) {
6448 		if (__improbable(rtfault &&
6449 		    !m->vmp_realtime &&
6450 		    vm_pageout_protect_realtime)) {
6451 			vm_page_lock_queues();
6452 			if (!m->vmp_realtime) {
6453 				m->vmp_realtime = true;
6454 				vm_page_realtime_count++;
6455 			}
6456 			vm_page_unlock_queues();
6457 		}
6458 		assert(VM_PAGE_OBJECT(m) == m_object);
6459 
6460 		if (!m_object->internal && (fault_type & VM_PROT_WRITE)) {
6461 			vm_object_paging_begin(m_object);
6462 
6463 			assert(written_on_object == VM_OBJECT_NULL);
6464 			written_on_object = m_object;
6465 			written_on_pager = m_object->pager;
6466 			written_on_offset = m_object->paging_offset + m->vmp_offset;
6467 		}
6468 		PAGE_WAKEUP_DONE(m);
6469 
6470 		vm_fault_cleanup(m_object, top_page);
6471 	} else {
6472 		vm_fault_cleanup(object, top_page);
6473 	}
6474 
6475 	vm_object_deallocate(object);
6476 
6477 #undef  RELEASE_PAGE
6478 
6479 done:
6480 	thread_interrupt_level(interruptible_state);
6481 
6482 	if (resilient_media_object != VM_OBJECT_NULL) {
6483 		assert(resilient_media_retry);
6484 		assert(resilient_media_offset != (vm_object_offset_t)-1);
6485 		/* release extra reference on failed object */
6486 //             printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object);
6487 		vm_object_lock_assert_notheld(resilient_media_object);
6488 		vm_object_deallocate(resilient_media_object);
6489 		resilient_media_object = VM_OBJECT_NULL;
6490 		resilient_media_offset = (vm_object_offset_t)-1;
6491 		resilient_media_retry = false;
6492 		vm_fault_resilient_media_release++;
6493 	}
6494 	assert(!resilient_media_retry);
6495 
6496 	/*
6497 	 * Only I/O throttle on faults which cause a pagein/swapin.
6498 	 */
6499 	if ((type_of_fault == DBG_PAGEIND_FAULT) || (type_of_fault == DBG_PAGEINV_FAULT) || (type_of_fault == DBG_COMPRESSOR_SWAPIN_FAULT)) {
6500 		throttle_lowpri_io(1);
6501 	} else {
6502 		if (kr == KERN_SUCCESS && type_of_fault != DBG_CACHE_HIT_FAULT && type_of_fault != DBG_GUARD_FAULT) {
6503 			if ((throttle_delay = vm_page_throttled(TRUE))) {
6504 				if (vm_debug_events) {
6505 					if (type_of_fault == DBG_COMPRESSOR_FAULT) {
6506 						VM_DEBUG_EVENT(vmf_compressordelay, VMF_COMPRESSORDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0);
6507 					} else if (type_of_fault == DBG_COW_FAULT) {
6508 						VM_DEBUG_EVENT(vmf_cowdelay, VMF_COWDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0);
6509 					} else {
6510 						VM_DEBUG_EVENT(vmf_zfdelay, VMF_ZFDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0);
6511 					}
6512 				}
6513 				__VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__(throttle_delay);
6514 			}
6515 		}
6516 	}
6517 
6518 	if (written_on_object) {
6519 		vnode_pager_dirtied(written_on_pager, written_on_offset, written_on_offset + PAGE_SIZE_64);
6520 
6521 		vm_object_lock(written_on_object);
6522 		vm_object_paging_end(written_on_object);
6523 		vm_object_unlock(written_on_object);
6524 
6525 		written_on_object = VM_OBJECT_NULL;
6526 	}
6527 
6528 	if (rtfault) {
6529 		vm_record_rtfault(cthread, fstart, trace_vaddr, type_of_fault);
6530 	}
6531 
6532 	KDBG_RELEASE(
6533 		(MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END,
6534 		((uint64_t)trace_vaddr >> 32),
6535 		trace_vaddr,
6536 		kr,
6537 		vm_fault_type_for_tracing(need_copy_on_read, type_of_fault));
6538 
6539 	if (fault_page_size < PAGE_SIZE && kr != KERN_SUCCESS) {
6540 		DEBUG4K_FAULT("map %p original %p vaddr 0x%llx -> 0x%x\n", map, original_map, (uint64_t)trace_real_vaddr, kr);
6541 	}
6542 
6543 	return kr;
6544 }
6545 
6546 /*
6547  *	vm_fault_wire:
6548  *
6549  *	Wire down a range of virtual addresses in a map.
6550  */
6551 kern_return_t
vm_fault_wire(vm_map_t map,vm_map_entry_t entry,vm_prot_t prot,vm_tag_t wire_tag,pmap_t pmap,vm_map_offset_t pmap_addr,ppnum_t * physpage_p)6552 vm_fault_wire(
6553 	vm_map_t        map,
6554 	vm_map_entry_t  entry,
6555 	vm_prot_t       prot,
6556 	vm_tag_t        wire_tag,
6557 	pmap_t          pmap,
6558 	vm_map_offset_t pmap_addr,
6559 	ppnum_t         *physpage_p)
6560 {
6561 	vm_map_offset_t va;
6562 	vm_map_offset_t end_addr = entry->vme_end;
6563 	kern_return_t   rc;
6564 	vm_map_size_t   effective_page_size;
6565 
6566 	assert(entry->in_transition);
6567 
6568 	if (!entry->is_sub_map &&
6569 	    VME_OBJECT(entry) != VM_OBJECT_NULL &&
6570 	    VME_OBJECT(entry)->phys_contiguous) {
6571 		return KERN_SUCCESS;
6572 	}
6573 
6574 	/*
6575 	 *	Inform the physical mapping system that the
6576 	 *	range of addresses may not fault, so that
6577 	 *	page tables and such can be locked down as well.
6578 	 */
6579 
6580 	pmap_pageable(pmap, pmap_addr,
6581 	    pmap_addr + (end_addr - entry->vme_start), FALSE);
6582 
6583 	/*
6584 	 *	We simulate a fault to get the page and enter it
6585 	 *	in the physical map.
6586 	 */
6587 
6588 	effective_page_size = MIN(VM_MAP_PAGE_SIZE(map), PAGE_SIZE);
6589 	for (va = entry->vme_start;
6590 	    va < end_addr;
6591 	    va += effective_page_size) {
6592 		rc = vm_fault_wire_fast(map, va, prot, wire_tag, entry, pmap,
6593 		    pmap_addr + (va - entry->vme_start),
6594 		    physpage_p);
6595 		if (rc != KERN_SUCCESS) {
6596 			rc = vm_fault_internal(map, va, prot, TRUE, wire_tag,
6597 			    ((pmap == kernel_pmap)
6598 			    ? THREAD_UNINT
6599 			    : THREAD_ABORTSAFE),
6600 			    pmap,
6601 			    (pmap_addr +
6602 			    (va - entry->vme_start)),
6603 			    physpage_p);
6604 			DTRACE_VM2(softlock, int, 1, (uint64_t *), NULL);
6605 		}
6606 
6607 		if (rc != KERN_SUCCESS) {
6608 			struct vm_map_entry     tmp_entry = *entry;
6609 
6610 			/* unwire wired pages */
6611 			tmp_entry.vme_end = va;
6612 			vm_fault_unwire(map, &tmp_entry, FALSE,
6613 			    pmap, pmap_addr, tmp_entry.vme_end);
6614 
6615 			return rc;
6616 		}
6617 	}
6618 	return KERN_SUCCESS;
6619 }
6620 
6621 /*
6622  *	vm_fault_unwire:
6623  *
6624  *	Unwire a range of virtual addresses in a map.
6625  */
6626 void
vm_fault_unwire(vm_map_t map,vm_map_entry_t entry,boolean_t deallocate,pmap_t pmap,vm_map_offset_t pmap_addr,vm_map_offset_t end_addr)6627 vm_fault_unwire(
6628 	vm_map_t        map,
6629 	vm_map_entry_t  entry,
6630 	boolean_t       deallocate,
6631 	pmap_t          pmap,
6632 	vm_map_offset_t pmap_addr,
6633 	vm_map_offset_t end_addr)
6634 {
6635 	vm_map_offset_t va;
6636 	vm_object_t     object;
6637 	struct vm_object_fault_info fault_info = {};
6638 	unsigned int    unwired_pages;
6639 	vm_map_size_t   effective_page_size;
6640 
6641 	object = (entry->is_sub_map) ? VM_OBJECT_NULL : VME_OBJECT(entry);
6642 
6643 	/*
6644 	 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
6645 	 * do anything since such memory is wired by default.  So we don't have
6646 	 * anything to undo here.
6647 	 */
6648 
6649 	if (object != VM_OBJECT_NULL && object->phys_contiguous) {
6650 		return;
6651 	}
6652 
6653 	fault_info.interruptible = THREAD_UNINT;
6654 	fault_info.behavior = entry->behavior;
6655 	fault_info.user_tag = VME_ALIAS(entry);
6656 	if (entry->iokit_acct ||
6657 	    (!entry->is_sub_map && !entry->use_pmap)) {
6658 		fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT;
6659 	}
6660 	fault_info.lo_offset = VME_OFFSET(entry);
6661 	fault_info.hi_offset = (entry->vme_end - entry->vme_start) + VME_OFFSET(entry);
6662 	fault_info.no_cache = entry->no_cache;
6663 	fault_info.stealth = TRUE;
6664 	if (entry->vme_xnu_user_debug) {
6665 		/*
6666 		 * Modified code-signed executable region: wired pages must
6667 		 * have been copied, so they should be XNU_USER_DEBUG rather
6668 		 * than XNU_USER_EXEC.
6669 		 */
6670 		fault_info.pmap_options |= PMAP_OPTIONS_XNU_USER_DEBUG;
6671 	}
6672 
6673 	unwired_pages = 0;
6674 
6675 	/*
6676 	 *	Since the pages are wired down, we must be able to
6677 	 *	get their mappings from the physical map system.
6678 	 */
6679 
6680 	effective_page_size = MIN(VM_MAP_PAGE_SIZE(map), PAGE_SIZE);
6681 	for (va = entry->vme_start;
6682 	    va < end_addr;
6683 	    va += effective_page_size) {
6684 		if (object == VM_OBJECT_NULL) {
6685 			if (pmap) {
6686 				pmap_change_wiring(pmap,
6687 				    pmap_addr + (va - entry->vme_start), FALSE);
6688 			}
6689 			(void) vm_fault(map, va, VM_PROT_NONE,
6690 			    TRUE, VM_KERN_MEMORY_NONE, THREAD_UNINT, pmap, pmap_addr);
6691 		} else {
6692 			vm_prot_t       prot;
6693 			vm_page_t       result_page;
6694 			vm_page_t       top_page;
6695 			vm_object_t     result_object;
6696 			vm_fault_return_t result;
6697 
6698 			/* cap cluster size at maximum UPL size */
6699 			upl_size_t cluster_size;
6700 			if (os_sub_overflow(end_addr, va, &cluster_size)) {
6701 				cluster_size = 0 - (upl_size_t)PAGE_SIZE;
6702 			}
6703 			fault_info.cluster_size = cluster_size;
6704 
6705 			do {
6706 				prot = VM_PROT_NONE;
6707 
6708 				vm_object_lock(object);
6709 				vm_object_paging_begin(object);
6710 				result_page = VM_PAGE_NULL;
6711 				result = vm_fault_page(
6712 					object,
6713 					(VME_OFFSET(entry) +
6714 					(va - entry->vme_start)),
6715 					VM_PROT_NONE, TRUE,
6716 					FALSE, /* page not looked up */
6717 					&prot, &result_page, &top_page,
6718 					(int *)0,
6719 					NULL, map->no_zero_fill,
6720 					&fault_info);
6721 			} while (result == VM_FAULT_RETRY);
6722 
6723 			/*
6724 			 * If this was a mapping to a file on a device that has been forcibly
6725 			 * unmounted, then we won't get a page back from vm_fault_page().  Just
6726 			 * move on to the next one in case the remaining pages are mapped from
6727 			 * different objects.  During a forced unmount, the object is terminated
6728 			 * so the alive flag will be false if this happens.  A forced unmount will
6729 			 * will occur when an external disk is unplugged before the user does an
6730 			 * eject, so we don't want to panic in that situation.
6731 			 */
6732 
6733 			if (result == VM_FAULT_MEMORY_ERROR) {
6734 				if (!object->alive) {
6735 					continue;
6736 				}
6737 				if (!object->internal && object->pager == NULL) {
6738 					continue;
6739 				}
6740 			}
6741 
6742 			if (result == VM_FAULT_MEMORY_ERROR &&
6743 			    is_kernel_object(object)) {
6744 				/*
6745 				 * This must have been allocated with
6746 				 * KMA_KOBJECT and KMA_VAONLY and there's
6747 				 * no physical page at this offset.
6748 				 * We're done (no page to free).
6749 				 */
6750 				assert(deallocate);
6751 				continue;
6752 			}
6753 
6754 			if (result != VM_FAULT_SUCCESS) {
6755 				panic("vm_fault_unwire: failure");
6756 			}
6757 
6758 			result_object = VM_PAGE_OBJECT(result_page);
6759 
6760 			if (deallocate) {
6761 				assert(VM_PAGE_GET_PHYS_PAGE(result_page) !=
6762 				    vm_page_fictitious_addr);
6763 				pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page));
6764 				if (VM_PAGE_WIRED(result_page)) {
6765 					unwired_pages++;
6766 				}
6767 				VM_PAGE_FREE(result_page);
6768 			} else {
6769 				if ((pmap) && (VM_PAGE_GET_PHYS_PAGE(result_page) != vm_page_guard_addr)) {
6770 					pmap_change_wiring(pmap,
6771 					    pmap_addr + (va - entry->vme_start), FALSE);
6772 				}
6773 
6774 
6775 				if (VM_PAGE_WIRED(result_page)) {
6776 					vm_page_lockspin_queues();
6777 					vm_page_unwire(result_page, TRUE);
6778 					vm_page_unlock_queues();
6779 					unwired_pages++;
6780 				}
6781 				if (entry->zero_wired_pages) {
6782 					pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page));
6783 					entry->zero_wired_pages = FALSE;
6784 				}
6785 
6786 				PAGE_WAKEUP_DONE(result_page);
6787 			}
6788 			vm_fault_cleanup(result_object, top_page);
6789 		}
6790 	}
6791 
6792 	/*
6793 	 *	Inform the physical mapping system that the range
6794 	 *	of addresses may fault, so that page tables and
6795 	 *	such may be unwired themselves.
6796 	 */
6797 
6798 	pmap_pageable(pmap, pmap_addr,
6799 	    pmap_addr + (end_addr - entry->vme_start), TRUE);
6800 
6801 	if (is_kernel_object(object)) {
6802 		/*
6803 		 * Would like to make user_tag in vm_object_fault_info
6804 		 * vm_tag_t (unsigned short) but user_tag derives its value from
6805 		 * VME_ALIAS(entry) at a few places and VME_ALIAS, in turn, casts
6806 		 * to an _unsigned int_ which is used by non-fault_info paths throughout the
6807 		 * code at many places.
6808 		 *
6809 		 * So, for now, an explicit truncation to unsigned short (vm_tag_t).
6810 		 */
6811 		assertf((fault_info.user_tag & VME_ALIAS_MASK) == fault_info.user_tag,
6812 		    "VM Tag truncated from 0x%x to 0x%x\n", fault_info.user_tag, (fault_info.user_tag & VME_ALIAS_MASK));
6813 		vm_tag_update_size((vm_tag_t) fault_info.user_tag, -ptoa_64(unwired_pages), NULL);
6814 	}
6815 }
6816 
6817 /*
6818  *	vm_fault_wire_fast:
6819  *
6820  *	Handle common case of a wire down page fault at the given address.
6821  *	If successful, the page is inserted into the associated physical map.
6822  *	The map entry is passed in to avoid the overhead of a map lookup.
6823  *
6824  *	NOTE: the given address should be truncated to the
6825  *	proper page address.
6826  *
6827  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
6828  *	a standard error specifying why the fault is fatal is returned.
6829  *
6830  *	The map in question must be referenced, and remains so.
6831  *	Caller has a read lock on the map.
6832  *
6833  *	This is a stripped version of vm_fault() for wiring pages.  Anything
6834  *	other than the common case will return KERN_FAILURE, and the caller
6835  *	is expected to call vm_fault().
6836  */
6837 static kern_return_t
vm_fault_wire_fast(__unused vm_map_t map,vm_map_offset_t va,__unused vm_prot_t caller_prot,vm_tag_t wire_tag,vm_map_entry_t entry,pmap_t pmap,vm_map_offset_t pmap_addr,ppnum_t * physpage_p)6838 vm_fault_wire_fast(
6839 	__unused vm_map_t       map,
6840 	vm_map_offset_t va,
6841 	__unused vm_prot_t       caller_prot,
6842 	vm_tag_t        wire_tag,
6843 	vm_map_entry_t  entry,
6844 	pmap_t          pmap,
6845 	vm_map_offset_t pmap_addr,
6846 	ppnum_t         *physpage_p)
6847 {
6848 	vm_object_t             object;
6849 	vm_object_offset_t      offset;
6850 	vm_page_t               m;
6851 	vm_prot_t               prot;
6852 	thread_t                thread = current_thread();
6853 	int                     type_of_fault;
6854 	kern_return_t           kr;
6855 	vm_map_size_t           fault_page_size;
6856 	vm_map_offset_t         fault_phys_offset;
6857 	struct vm_object_fault_info fault_info = {};
6858 	uint8_t                 object_lock_type = 0;
6859 
6860 	counter_inc(&vm_statistics_faults);
6861 
6862 	if (thread != THREAD_NULL) {
6863 		counter_inc(&get_threadtask(thread)->faults);
6864 	}
6865 
6866 /*
6867  *	Recovery actions
6868  */
6869 
6870 #undef  RELEASE_PAGE
6871 #define RELEASE_PAGE(m) {                               \
6872 	PAGE_WAKEUP_DONE(m);                            \
6873 	vm_page_lockspin_queues();                      \
6874 	vm_page_unwire(m, TRUE);                        \
6875 	vm_page_unlock_queues();                        \
6876 }
6877 
6878 
6879 #undef  UNLOCK_THINGS
6880 #define UNLOCK_THINGS   {                               \
6881 	vm_object_paging_end(object);                      \
6882 	vm_object_unlock(object);                          \
6883 }
6884 
6885 #undef  UNLOCK_AND_DEALLOCATE
6886 #define UNLOCK_AND_DEALLOCATE   {                       \
6887 	UNLOCK_THINGS;                                  \
6888 	vm_object_deallocate(object);                   \
6889 }
6890 /*
6891  *	Give up and have caller do things the hard way.
6892  */
6893 
6894 #define GIVE_UP {                                       \
6895 	UNLOCK_AND_DEALLOCATE;                          \
6896 	return(KERN_FAILURE);                           \
6897 }
6898 
6899 
6900 	/*
6901 	 *	If this entry is not directly to a vm_object, bail out.
6902 	 */
6903 	if (entry->is_sub_map) {
6904 		assert(physpage_p == NULL);
6905 		return KERN_FAILURE;
6906 	}
6907 
6908 	/*
6909 	 *	Find the backing store object and offset into it.
6910 	 */
6911 
6912 	object = VME_OBJECT(entry);
6913 	offset = (va - entry->vme_start) + VME_OFFSET(entry);
6914 	prot = entry->protection;
6915 
6916 	/*
6917 	 *	Make a reference to this object to prevent its
6918 	 *	disposal while we are messing with it.
6919 	 */
6920 
6921 	object_lock_type = OBJECT_LOCK_EXCLUSIVE;
6922 	vm_object_lock(object);
6923 	vm_object_reference_locked(object);
6924 	vm_object_paging_begin(object);
6925 
6926 	/*
6927 	 *	INVARIANTS (through entire routine):
6928 	 *
6929 	 *	1)	At all times, we must either have the object
6930 	 *		lock or a busy page in some object to prevent
6931 	 *		some other thread from trying to bring in
6932 	 *		the same page.
6933 	 *
6934 	 *	2)	Once we have a busy page, we must remove it from
6935 	 *		the pageout queues, so that the pageout daemon
6936 	 *		will not grab it away.
6937 	 *
6938 	 */
6939 
6940 	/*
6941 	 *	Look for page in top-level object.  If it's not there or
6942 	 *	there's something going on, give up.
6943 	 */
6944 	m = vm_page_lookup(object, vm_object_trunc_page(offset));
6945 	if ((m == VM_PAGE_NULL) || (m->vmp_busy) ||
6946 	    (m->vmp_unusual && (m->vmp_error || m->vmp_restart || m->vmp_absent))) {
6947 		GIVE_UP;
6948 	}
6949 	if (m->vmp_fictitious &&
6950 	    VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) {
6951 		/*
6952 		 * Guard pages are fictitious pages and are never
6953 		 * entered into a pmap, so let's say it's been wired...
6954 		 */
6955 		kr = KERN_SUCCESS;
6956 		goto done;
6957 	}
6958 
6959 	/*
6960 	 *	Wire the page down now.  All bail outs beyond this
6961 	 *	point must unwire the page.
6962 	 */
6963 
6964 	vm_page_lockspin_queues();
6965 	vm_page_wire(m, wire_tag, TRUE);
6966 	vm_page_unlock_queues();
6967 
6968 	/*
6969 	 *	Mark page busy for other threads.
6970 	 */
6971 	assert(!m->vmp_busy);
6972 	m->vmp_busy = TRUE;
6973 	assert(!m->vmp_absent);
6974 
6975 	/*
6976 	 *	Give up if the page is being written and there's a copy object
6977 	 */
6978 	if ((object->vo_copy != VM_OBJECT_NULL) && (prot & VM_PROT_WRITE)) {
6979 		RELEASE_PAGE(m);
6980 		GIVE_UP;
6981 	}
6982 
6983 	fault_info.user_tag = VME_ALIAS(entry);
6984 	fault_info.pmap_options = 0;
6985 	if (entry->iokit_acct ||
6986 	    (!entry->is_sub_map && !entry->use_pmap)) {
6987 		fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT;
6988 	}
6989 	if (entry->vme_xnu_user_debug) {
6990 		/*
6991 		 * Modified code-signed executable region: wiring will
6992 		 * copy the pages, so they should be XNU_USER_DEBUG rather
6993 		 * than XNU_USER_EXEC.
6994 		 */
6995 		fault_info.pmap_options |= PMAP_OPTIONS_XNU_USER_DEBUG;
6996 	}
6997 
6998 	fault_page_size = MIN(VM_MAP_PAGE_SIZE(map), PAGE_SIZE);
6999 	fault_phys_offset = offset - vm_object_trunc_page(offset);
7000 
7001 	/*
7002 	 *	Put this page into the physical map.
7003 	 */
7004 	type_of_fault = DBG_CACHE_HIT_FAULT;
7005 	assertf(VM_PAGE_OBJECT(m) == object, "m=%p object=%p", m, object);
7006 	assert(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL);
7007 	kr = vm_fault_enter(m,
7008 	    pmap,
7009 	    pmap_addr,
7010 	    fault_page_size,
7011 	    fault_phys_offset,
7012 	    prot,
7013 	    prot,
7014 	    TRUE,                  /* wired */
7015 	    FALSE,                 /* change_wiring */
7016 	    wire_tag,
7017 	    &fault_info,
7018 	    NULL,
7019 	    &type_of_fault,
7020 	    &object_lock_type); /* Exclusive lock mode. Will remain unchanged.*/
7021 	if (kr != KERN_SUCCESS) {
7022 		RELEASE_PAGE(m);
7023 		GIVE_UP;
7024 	}
7025 
7026 done:
7027 	/*
7028 	 *	Unlock everything, and return
7029 	 */
7030 
7031 	if (physpage_p) {
7032 		/* for vm_map_wire_and_extract() */
7033 		if (kr == KERN_SUCCESS) {
7034 			assert(object == VM_PAGE_OBJECT(m));
7035 			*physpage_p = VM_PAGE_GET_PHYS_PAGE(m);
7036 			if (prot & VM_PROT_WRITE) {
7037 				vm_object_lock_assert_exclusive(object);
7038 				m->vmp_dirty = TRUE;
7039 			}
7040 		} else {
7041 			*physpage_p = 0;
7042 		}
7043 	}
7044 
7045 	PAGE_WAKEUP_DONE(m);
7046 	UNLOCK_AND_DEALLOCATE;
7047 
7048 	return kr;
7049 }
7050 
7051 /*
7052  *	Routine:	vm_fault_copy_cleanup
7053  *	Purpose:
7054  *		Release a page used by vm_fault_copy.
7055  */
7056 
7057 static void
vm_fault_copy_cleanup(vm_page_t page,vm_page_t top_page)7058 vm_fault_copy_cleanup(
7059 	vm_page_t       page,
7060 	vm_page_t       top_page)
7061 {
7062 	vm_object_t     object = VM_PAGE_OBJECT(page);
7063 
7064 	vm_object_lock(object);
7065 	PAGE_WAKEUP_DONE(page);
7066 	if (!VM_PAGE_PAGEABLE(page)) {
7067 		vm_page_lockspin_queues();
7068 		if (!VM_PAGE_PAGEABLE(page)) {
7069 			vm_page_activate(page);
7070 		}
7071 		vm_page_unlock_queues();
7072 	}
7073 	vm_fault_cleanup(object, top_page);
7074 }
7075 
7076 static void
vm_fault_copy_dst_cleanup(vm_page_t page)7077 vm_fault_copy_dst_cleanup(
7078 	vm_page_t       page)
7079 {
7080 	vm_object_t     object;
7081 
7082 	if (page != VM_PAGE_NULL) {
7083 		object = VM_PAGE_OBJECT(page);
7084 		vm_object_lock(object);
7085 		vm_page_lockspin_queues();
7086 		vm_page_unwire(page, TRUE);
7087 		vm_page_unlock_queues();
7088 		vm_object_paging_end(object);
7089 		vm_object_unlock(object);
7090 	}
7091 }
7092 
7093 /*
7094  *	Routine:	vm_fault_copy
7095  *
7096  *	Purpose:
7097  *		Copy pages from one virtual memory object to another --
7098  *		neither the source nor destination pages need be resident.
7099  *
7100  *		Before actually copying a page, the version associated with
7101  *		the destination address map wil be verified.
7102  *
7103  *	In/out conditions:
7104  *		The caller must hold a reference, but not a lock, to
7105  *		each of the source and destination objects and to the
7106  *		destination map.
7107  *
7108  *	Results:
7109  *		Returns KERN_SUCCESS if no errors were encountered in
7110  *		reading or writing the data.  Returns KERN_INTERRUPTED if
7111  *		the operation was interrupted (only possible if the
7112  *		"interruptible" argument is asserted).  Other return values
7113  *		indicate a permanent error in copying the data.
7114  *
7115  *		The actual amount of data copied will be returned in the
7116  *		"copy_size" argument.  In the event that the destination map
7117  *		verification failed, this amount may be less than the amount
7118  *		requested.
7119  */
7120 kern_return_t
vm_fault_copy(vm_object_t src_object,vm_object_offset_t src_offset,vm_map_size_t * copy_size,vm_object_t dst_object,vm_object_offset_t dst_offset,vm_map_t dst_map,vm_map_version_t * dst_version,int interruptible)7121 vm_fault_copy(
7122 	vm_object_t             src_object,
7123 	vm_object_offset_t      src_offset,
7124 	vm_map_size_t           *copy_size,             /* INOUT */
7125 	vm_object_t             dst_object,
7126 	vm_object_offset_t      dst_offset,
7127 	vm_map_t                dst_map,
7128 	vm_map_version_t         *dst_version,
7129 	int                     interruptible)
7130 {
7131 	vm_page_t               result_page;
7132 
7133 	vm_page_t               src_page;
7134 	vm_page_t               src_top_page;
7135 	vm_prot_t               src_prot;
7136 
7137 	vm_page_t               dst_page;
7138 	vm_page_t               dst_top_page;
7139 	vm_prot_t               dst_prot;
7140 
7141 	vm_map_size_t           amount_left;
7142 	vm_object_t             old_copy_object;
7143 	uint32_t                old_copy_version;
7144 	vm_object_t             result_page_object = NULL;
7145 	kern_return_t           error = 0;
7146 	vm_fault_return_t       result;
7147 
7148 	vm_map_size_t           part_size;
7149 	struct vm_object_fault_info fault_info_src = {};
7150 	struct vm_object_fault_info fault_info_dst = {};
7151 
7152 	/*
7153 	 * In order not to confuse the clustered pageins, align
7154 	 * the different offsets on a page boundary.
7155 	 */
7156 
7157 #define RETURN(x)                                       \
7158 	MACRO_BEGIN                                     \
7159 	*copy_size -= amount_left;                      \
7160 	MACRO_RETURN(x);                                \
7161 	MACRO_END
7162 
7163 	amount_left = *copy_size;
7164 
7165 	fault_info_src.interruptible = interruptible;
7166 	fault_info_src.behavior = VM_BEHAVIOR_SEQUENTIAL;
7167 	fault_info_src.lo_offset = vm_object_trunc_page(src_offset);
7168 	fault_info_src.hi_offset = fault_info_src.lo_offset + amount_left;
7169 	fault_info_src.stealth = TRUE;
7170 
7171 	fault_info_dst.interruptible = interruptible;
7172 	fault_info_dst.behavior = VM_BEHAVIOR_SEQUENTIAL;
7173 	fault_info_dst.lo_offset = vm_object_trunc_page(dst_offset);
7174 	fault_info_dst.hi_offset = fault_info_dst.lo_offset + amount_left;
7175 	fault_info_dst.stealth = TRUE;
7176 
7177 	do { /* while (amount_left > 0) */
7178 		/*
7179 		 * There may be a deadlock if both source and destination
7180 		 * pages are the same. To avoid this deadlock, the copy must
7181 		 * start by getting the destination page in order to apply
7182 		 * COW semantics if any.
7183 		 */
7184 
7185 RetryDestinationFault:;
7186 
7187 		dst_prot = VM_PROT_WRITE | VM_PROT_READ;
7188 
7189 		vm_object_lock(dst_object);
7190 		vm_object_paging_begin(dst_object);
7191 
7192 		/* cap cluster size at maximum UPL size */
7193 		upl_size_t cluster_size;
7194 		if (os_convert_overflow(amount_left, &cluster_size)) {
7195 			cluster_size = 0 - (upl_size_t)PAGE_SIZE;
7196 		}
7197 		fault_info_dst.cluster_size = cluster_size;
7198 
7199 		dst_page = VM_PAGE_NULL;
7200 		result = vm_fault_page(dst_object,
7201 		    vm_object_trunc_page(dst_offset),
7202 		    VM_PROT_WRITE | VM_PROT_READ,
7203 		    FALSE,
7204 		    FALSE,                    /* page not looked up */
7205 		    &dst_prot, &dst_page, &dst_top_page,
7206 		    (int *)0,
7207 		    &error,
7208 		    dst_map->no_zero_fill,
7209 		    &fault_info_dst);
7210 		switch (result) {
7211 		case VM_FAULT_SUCCESS:
7212 			break;
7213 		case VM_FAULT_RETRY:
7214 			goto RetryDestinationFault;
7215 		case VM_FAULT_MEMORY_SHORTAGE:
7216 			if (vm_page_wait(interruptible)) {
7217 				goto RetryDestinationFault;
7218 			}
7219 			ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_COPY_MEMORY_SHORTAGE), 0 /* arg */);
7220 			OS_FALLTHROUGH;
7221 		case VM_FAULT_INTERRUPTED:
7222 			RETURN(MACH_SEND_INTERRUPTED);
7223 		case VM_FAULT_SUCCESS_NO_VM_PAGE:
7224 			/* success but no VM page: fail the copy */
7225 			vm_object_paging_end(dst_object);
7226 			vm_object_unlock(dst_object);
7227 			OS_FALLTHROUGH;
7228 		case VM_FAULT_MEMORY_ERROR:
7229 			if (error) {
7230 				return error;
7231 			} else {
7232 				return KERN_MEMORY_ERROR;
7233 			}
7234 		default:
7235 			panic("vm_fault_copy: unexpected error 0x%x from "
7236 			    "vm_fault_page()\n", result);
7237 		}
7238 		assert((dst_prot & VM_PROT_WRITE) != VM_PROT_NONE);
7239 
7240 		assert(dst_object == VM_PAGE_OBJECT(dst_page));
7241 		old_copy_object = dst_object->vo_copy;
7242 		old_copy_version = dst_object->vo_copy_version;
7243 
7244 		/*
7245 		 * There exists the possiblity that the source and
7246 		 * destination page are the same.  But we can't
7247 		 * easily determine that now.  If they are the
7248 		 * same, the call to vm_fault_page() for the
7249 		 * destination page will deadlock.  To prevent this we
7250 		 * wire the page so we can drop busy without having
7251 		 * the page daemon steal the page.  We clean up the
7252 		 * top page  but keep the paging reference on the object
7253 		 * holding the dest page so it doesn't go away.
7254 		 */
7255 
7256 		vm_page_lockspin_queues();
7257 		vm_page_wire(dst_page, VM_KERN_MEMORY_OSFMK, TRUE);
7258 		vm_page_unlock_queues();
7259 		PAGE_WAKEUP_DONE(dst_page);
7260 		vm_object_unlock(dst_object);
7261 
7262 		if (dst_top_page != VM_PAGE_NULL) {
7263 			vm_object_lock(dst_object);
7264 			VM_PAGE_FREE(dst_top_page);
7265 			vm_object_paging_end(dst_object);
7266 			vm_object_unlock(dst_object);
7267 		}
7268 
7269 RetrySourceFault:;
7270 
7271 		if (src_object == VM_OBJECT_NULL) {
7272 			/*
7273 			 *	No source object.  We will just
7274 			 *	zero-fill the page in dst_object.
7275 			 */
7276 			src_page = VM_PAGE_NULL;
7277 			result_page = VM_PAGE_NULL;
7278 		} else {
7279 			vm_object_lock(src_object);
7280 			src_page = vm_page_lookup(src_object,
7281 			    vm_object_trunc_page(src_offset));
7282 			if (src_page == dst_page) {
7283 				src_prot = dst_prot;
7284 				result_page = VM_PAGE_NULL;
7285 			} else {
7286 				src_prot = VM_PROT_READ;
7287 				vm_object_paging_begin(src_object);
7288 
7289 				/* cap cluster size at maximum UPL size */
7290 				if (os_convert_overflow(amount_left, &cluster_size)) {
7291 					cluster_size = 0 - (upl_size_t)PAGE_SIZE;
7292 				}
7293 				fault_info_src.cluster_size = cluster_size;
7294 
7295 				result_page = VM_PAGE_NULL;
7296 				result = vm_fault_page(
7297 					src_object,
7298 					vm_object_trunc_page(src_offset),
7299 					VM_PROT_READ, FALSE,
7300 					FALSE, /* page not looked up */
7301 					&src_prot,
7302 					&result_page, &src_top_page,
7303 					(int *)0, &error, FALSE,
7304 					&fault_info_src);
7305 
7306 				switch (result) {
7307 				case VM_FAULT_SUCCESS:
7308 					break;
7309 				case VM_FAULT_RETRY:
7310 					goto RetrySourceFault;
7311 				case VM_FAULT_MEMORY_SHORTAGE:
7312 					if (vm_page_wait(interruptible)) {
7313 						goto RetrySourceFault;
7314 					}
7315 					OS_FALLTHROUGH;
7316 				case VM_FAULT_INTERRUPTED:
7317 					vm_fault_copy_dst_cleanup(dst_page);
7318 					RETURN(MACH_SEND_INTERRUPTED);
7319 				case VM_FAULT_SUCCESS_NO_VM_PAGE:
7320 					/* success but no VM page: fail */
7321 					vm_object_paging_end(src_object);
7322 					vm_object_unlock(src_object);
7323 					OS_FALLTHROUGH;
7324 				case VM_FAULT_MEMORY_ERROR:
7325 					vm_fault_copy_dst_cleanup(dst_page);
7326 					if (error) {
7327 						return error;
7328 					} else {
7329 						return KERN_MEMORY_ERROR;
7330 					}
7331 				default:
7332 					panic("vm_fault_copy(2): unexpected "
7333 					    "error 0x%x from "
7334 					    "vm_fault_page()\n", result);
7335 				}
7336 
7337 				result_page_object = VM_PAGE_OBJECT(result_page);
7338 				assert((src_top_page == VM_PAGE_NULL) ==
7339 				    (result_page_object == src_object));
7340 			}
7341 			assert((src_prot & VM_PROT_READ) != VM_PROT_NONE);
7342 			vm_object_unlock(result_page_object);
7343 		}
7344 
7345 		vm_map_lock_read(dst_map);
7346 
7347 		if (!vm_map_verify(dst_map, dst_version)) {
7348 			vm_map_unlock_read(dst_map);
7349 			if (result_page != VM_PAGE_NULL && src_page != dst_page) {
7350 				vm_fault_copy_cleanup(result_page, src_top_page);
7351 			}
7352 			vm_fault_copy_dst_cleanup(dst_page);
7353 			break;
7354 		}
7355 		assert(dst_object == VM_PAGE_OBJECT(dst_page));
7356 
7357 		vm_object_lock(dst_object);
7358 
7359 		if ((dst_object->vo_copy != old_copy_object ||
7360 		    dst_object->vo_copy_version != old_copy_version)) {
7361 			vm_object_unlock(dst_object);
7362 			vm_map_unlock_read(dst_map);
7363 			if (result_page != VM_PAGE_NULL && src_page != dst_page) {
7364 				vm_fault_copy_cleanup(result_page, src_top_page);
7365 			}
7366 			vm_fault_copy_dst_cleanup(dst_page);
7367 			break;
7368 		}
7369 		vm_object_unlock(dst_object);
7370 
7371 		/*
7372 		 *	Copy the page, and note that it is dirty
7373 		 *	immediately.
7374 		 */
7375 
7376 		if (!page_aligned(src_offset) ||
7377 		    !page_aligned(dst_offset) ||
7378 		    !page_aligned(amount_left)) {
7379 			vm_object_offset_t      src_po,
7380 			    dst_po;
7381 
7382 			src_po = src_offset - vm_object_trunc_page(src_offset);
7383 			dst_po = dst_offset - vm_object_trunc_page(dst_offset);
7384 
7385 			if (dst_po > src_po) {
7386 				part_size = PAGE_SIZE - dst_po;
7387 			} else {
7388 				part_size = PAGE_SIZE - src_po;
7389 			}
7390 			if (part_size > (amount_left)) {
7391 				part_size = amount_left;
7392 			}
7393 
7394 			if (result_page == VM_PAGE_NULL) {
7395 				assert((vm_offset_t) dst_po == dst_po);
7396 				assert((vm_size_t) part_size == part_size);
7397 				vm_page_part_zero_fill(dst_page,
7398 				    (vm_offset_t) dst_po,
7399 				    (vm_size_t) part_size);
7400 			} else {
7401 				assert((vm_offset_t) src_po == src_po);
7402 				assert((vm_offset_t) dst_po == dst_po);
7403 				assert((vm_size_t) part_size == part_size);
7404 				vm_page_part_copy(result_page,
7405 				    (vm_offset_t) src_po,
7406 				    dst_page,
7407 				    (vm_offset_t) dst_po,
7408 				    (vm_size_t)part_size);
7409 				if (!dst_page->vmp_dirty) {
7410 					vm_object_lock(dst_object);
7411 					SET_PAGE_DIRTY(dst_page, TRUE);
7412 					vm_object_unlock(dst_object);
7413 				}
7414 			}
7415 		} else {
7416 			part_size = PAGE_SIZE;
7417 
7418 			if (result_page == VM_PAGE_NULL) {
7419 				vm_page_zero_fill(dst_page);
7420 			} else {
7421 				vm_object_lock(result_page_object);
7422 				vm_page_copy(result_page, dst_page);
7423 				vm_object_unlock(result_page_object);
7424 
7425 				if (!dst_page->vmp_dirty) {
7426 					vm_object_lock(dst_object);
7427 					SET_PAGE_DIRTY(dst_page, TRUE);
7428 					vm_object_unlock(dst_object);
7429 				}
7430 			}
7431 		}
7432 
7433 		/*
7434 		 *	Unlock everything, and return
7435 		 */
7436 
7437 		vm_map_unlock_read(dst_map);
7438 
7439 		if (result_page != VM_PAGE_NULL && src_page != dst_page) {
7440 			vm_fault_copy_cleanup(result_page, src_top_page);
7441 		}
7442 		vm_fault_copy_dst_cleanup(dst_page);
7443 
7444 		amount_left -= part_size;
7445 		src_offset += part_size;
7446 		dst_offset += part_size;
7447 	} while (amount_left > 0);
7448 
7449 	RETURN(KERN_SUCCESS);
7450 #undef  RETURN
7451 
7452 	/*NOTREACHED*/
7453 }
7454 
7455 #if     VM_FAULT_CLASSIFY
7456 /*
7457  *	Temporary statistics gathering support.
7458  */
7459 
7460 /*
7461  *	Statistics arrays:
7462  */
7463 #define VM_FAULT_TYPES_MAX      5
7464 #define VM_FAULT_LEVEL_MAX      8
7465 
7466 int     vm_fault_stats[VM_FAULT_TYPES_MAX][VM_FAULT_LEVEL_MAX];
7467 
7468 #define VM_FAULT_TYPE_ZERO_FILL 0
7469 #define VM_FAULT_TYPE_MAP_IN    1
7470 #define VM_FAULT_TYPE_PAGER     2
7471 #define VM_FAULT_TYPE_COPY      3
7472 #define VM_FAULT_TYPE_OTHER     4
7473 
7474 
7475 void
vm_fault_classify(vm_object_t object,vm_object_offset_t offset,vm_prot_t fault_type)7476 vm_fault_classify(vm_object_t           object,
7477     vm_object_offset_t    offset,
7478     vm_prot_t             fault_type)
7479 {
7480 	int             type, level = 0;
7481 	vm_page_t       m;
7482 
7483 	while (TRUE) {
7484 		m = vm_page_lookup(object, offset);
7485 		if (m != VM_PAGE_NULL) {
7486 			if (m->vmp_busy || m->vmp_error || m->vmp_restart || m->vmp_absent) {
7487 				type = VM_FAULT_TYPE_OTHER;
7488 				break;
7489 			}
7490 			if (((fault_type & VM_PROT_WRITE) == 0) ||
7491 			    ((level == 0) && object->vo_copy == VM_OBJECT_NULL)) {
7492 				type = VM_FAULT_TYPE_MAP_IN;
7493 				break;
7494 			}
7495 			type = VM_FAULT_TYPE_COPY;
7496 			break;
7497 		} else {
7498 			if (object->pager_created) {
7499 				type = VM_FAULT_TYPE_PAGER;
7500 				break;
7501 			}
7502 			if (object->shadow == VM_OBJECT_NULL) {
7503 				type = VM_FAULT_TYPE_ZERO_FILL;
7504 				break;
7505 			}
7506 
7507 			offset += object->vo_shadow_offset;
7508 			object = object->shadow;
7509 			level++;
7510 			continue;
7511 		}
7512 	}
7513 
7514 	if (level > VM_FAULT_LEVEL_MAX) {
7515 		level = VM_FAULT_LEVEL_MAX;
7516 	}
7517 
7518 	vm_fault_stats[type][level] += 1;
7519 
7520 	return;
7521 }
7522 
7523 /* cleanup routine to call from debugger */
7524 
7525 void
vm_fault_classify_init(void)7526 vm_fault_classify_init(void)
7527 {
7528 	int type, level;
7529 
7530 	for (type = 0; type < VM_FAULT_TYPES_MAX; type++) {
7531 		for (level = 0; level < VM_FAULT_LEVEL_MAX; level++) {
7532 			vm_fault_stats[type][level] = 0;
7533 		}
7534 	}
7535 
7536 	return;
7537 }
7538 #endif  /* VM_FAULT_CLASSIFY */
7539 
7540 vm_offset_t
kdp_lightweight_fault(vm_map_t map,vm_offset_t cur_target_addr)7541 kdp_lightweight_fault(vm_map_t map, vm_offset_t cur_target_addr)
7542 {
7543 	vm_map_entry_t  entry;
7544 	vm_object_t     object;
7545 	vm_offset_t     object_offset;
7546 	vm_page_t       m;
7547 	int             compressor_external_state, compressed_count_delta;
7548 	vm_compressor_options_t             compressor_flags = (C_DONT_BLOCK | C_KEEP | C_KDP);
7549 	int             my_fault_type = VM_PROT_READ;
7550 	kern_return_t   kr;
7551 	int effective_page_mask, effective_page_size;
7552 
7553 	if (VM_MAP_PAGE_SHIFT(map) < PAGE_SHIFT) {
7554 		effective_page_mask = VM_MAP_PAGE_MASK(map);
7555 		effective_page_size = VM_MAP_PAGE_SIZE(map);
7556 	} else {
7557 		effective_page_mask = PAGE_MASK;
7558 		effective_page_size = PAGE_SIZE;
7559 	}
7560 
7561 	if (not_in_kdp) {
7562 		panic("kdp_lightweight_fault called from outside of debugger context");
7563 	}
7564 
7565 	assert(map != VM_MAP_NULL);
7566 
7567 	assert((cur_target_addr & effective_page_mask) == 0);
7568 	if ((cur_target_addr & effective_page_mask) != 0) {
7569 		return 0;
7570 	}
7571 
7572 	if (kdp_lck_rw_lock_is_acquired_exclusive(&map->lock)) {
7573 		return 0;
7574 	}
7575 
7576 	if (!vm_map_lookup_entry(map, cur_target_addr, &entry)) {
7577 		return 0;
7578 	}
7579 
7580 	if (entry->is_sub_map) {
7581 		return 0;
7582 	}
7583 
7584 	object = VME_OBJECT(entry);
7585 	if (object == VM_OBJECT_NULL) {
7586 		return 0;
7587 	}
7588 
7589 	object_offset = cur_target_addr - entry->vme_start + VME_OFFSET(entry);
7590 
7591 	while (TRUE) {
7592 		if (kdp_lck_rw_lock_is_acquired_exclusive(&object->Lock)) {
7593 			return 0;
7594 		}
7595 
7596 		if (object->pager_created && (object->paging_in_progress ||
7597 		    object->activity_in_progress)) {
7598 			return 0;
7599 		}
7600 
7601 		m = kdp_vm_page_lookup(object, vm_object_trunc_page(object_offset));
7602 
7603 		if (m != VM_PAGE_NULL) {
7604 			if ((object->wimg_bits & VM_WIMG_MASK) != VM_WIMG_DEFAULT) {
7605 				return 0;
7606 			}
7607 
7608 			if (m->vmp_laundry || m->vmp_busy || m->vmp_free_when_done || m->vmp_absent || VMP_ERROR_GET(m) || m->vmp_cleaning ||
7609 			    m->vmp_overwriting || m->vmp_restart || m->vmp_unusual) {
7610 				return 0;
7611 			}
7612 
7613 			assert(!m->vmp_private);
7614 			if (m->vmp_private) {
7615 				return 0;
7616 			}
7617 
7618 			assert(!m->vmp_fictitious);
7619 			if (m->vmp_fictitious) {
7620 				return 0;
7621 			}
7622 
7623 			assert(m->vmp_q_state != VM_PAGE_USED_BY_COMPRESSOR);
7624 			if (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) {
7625 				return 0;
7626 			}
7627 
7628 			return ptoa(VM_PAGE_GET_PHYS_PAGE(m));
7629 		}
7630 
7631 		compressor_external_state = VM_EXTERNAL_STATE_UNKNOWN;
7632 
7633 		if (object->pager_created && MUST_ASK_PAGER(object, object_offset, compressor_external_state)) {
7634 			if (compressor_external_state == VM_EXTERNAL_STATE_EXISTS) {
7635 				kr = vm_compressor_pager_get(object->pager,
7636 				    vm_object_trunc_page(object_offset + object->paging_offset),
7637 				    kdp_compressor_decompressed_page_ppnum, &my_fault_type,
7638 				    compressor_flags, &compressed_count_delta);
7639 				if (kr == KERN_SUCCESS) {
7640 					return kdp_compressor_decompressed_page_paddr;
7641 				} else {
7642 					return 0;
7643 				}
7644 			}
7645 		}
7646 
7647 		if (object->shadow == VM_OBJECT_NULL) {
7648 			return 0;
7649 		}
7650 
7651 		object_offset += object->vo_shadow_offset;
7652 		object = object->shadow;
7653 	}
7654 }
7655 
7656 /*
7657  * vm_page_validate_cs_fast():
7658  * Performs a few quick checks to determine if the page's code signature
7659  * really needs to be fully validated.  It could:
7660  *	1. have been modified (i.e. automatically tainted),
7661  *	2. have already been validated,
7662  *	3. have already been found to be tainted,
7663  *	4. no longer have a backing store.
7664  * Returns FALSE if the page needs to be fully validated.
7665  */
7666 static boolean_t
vm_page_validate_cs_fast(vm_page_t page,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset)7667 vm_page_validate_cs_fast(
7668 	vm_page_t       page,
7669 	vm_map_size_t   fault_page_size,
7670 	vm_map_offset_t fault_phys_offset)
7671 {
7672 	vm_object_t     object;
7673 
7674 	object = VM_PAGE_OBJECT(page);
7675 	vm_object_lock_assert_held(object);
7676 
7677 	if (page->vmp_wpmapped &&
7678 	    !VMP_CS_TAINTED(page, fault_page_size, fault_phys_offset)) {
7679 		/*
7680 		 * This page was mapped for "write" access sometime in the
7681 		 * past and could still be modifiable in the future.
7682 		 * Consider it tainted.
7683 		 * [ If the page was already found to be "tainted", no
7684 		 * need to re-validate. ]
7685 		 */
7686 		vm_object_lock_assert_exclusive(object);
7687 		VMP_CS_SET_VALIDATED(page, fault_page_size, fault_phys_offset, TRUE);
7688 		VMP_CS_SET_TAINTED(page, fault_page_size, fault_phys_offset, TRUE);
7689 		if (cs_debug) {
7690 			printf("CODESIGNING: %s: "
7691 			    "page %p obj %p off 0x%llx "
7692 			    "was modified\n",
7693 			    __FUNCTION__,
7694 			    page, object, page->vmp_offset);
7695 		}
7696 		vm_cs_validated_dirtied++;
7697 	}
7698 
7699 	if (VMP_CS_VALIDATED(page, fault_page_size, fault_phys_offset) ||
7700 	    VMP_CS_TAINTED(page, fault_page_size, fault_phys_offset)) {
7701 		return TRUE;
7702 	}
7703 	vm_object_lock_assert_exclusive(object);
7704 
7705 #if CHECK_CS_VALIDATION_BITMAP
7706 	kern_return_t kr;
7707 
7708 	kr = vnode_pager_cs_check_validation_bitmap(
7709 		object->pager,
7710 		page->vmp_offset + object->paging_offset,
7711 		CS_BITMAP_CHECK);
7712 	if (kr == KERN_SUCCESS) {
7713 		page->vmp_cs_validated = VMP_CS_ALL_TRUE;
7714 		page->vmp_cs_tainted = VMP_CS_ALL_FALSE;
7715 		vm_cs_bitmap_validated++;
7716 		return TRUE;
7717 	}
7718 #endif /* CHECK_CS_VALIDATION_BITMAP */
7719 
7720 	if (!object->alive || object->terminating || object->pager == NULL) {
7721 		/*
7722 		 * The object is terminating and we don't have its pager
7723 		 * so we can't validate the data...
7724 		 */
7725 		return TRUE;
7726 	}
7727 
7728 	/* we need to really validate this page */
7729 	vm_object_lock_assert_exclusive(object);
7730 	return FALSE;
7731 }
7732 
7733 void
vm_page_validate_cs_mapped_slow(vm_page_t page,const void * kaddr)7734 vm_page_validate_cs_mapped_slow(
7735 	vm_page_t       page,
7736 	const void      *kaddr)
7737 {
7738 	vm_object_t             object;
7739 	memory_object_offset_t  mo_offset;
7740 	memory_object_t         pager;
7741 	struct vnode            *vnode;
7742 	int                     validated, tainted, nx;
7743 
7744 	assert(page->vmp_busy);
7745 	object = VM_PAGE_OBJECT(page);
7746 	vm_object_lock_assert_exclusive(object);
7747 
7748 	vm_cs_validates++;
7749 
7750 	/*
7751 	 * Since we get here to validate a page that was brought in by
7752 	 * the pager, we know that this pager is all setup and ready
7753 	 * by now.
7754 	 */
7755 	assert(object->code_signed);
7756 	assert(!object->internal);
7757 	assert(object->pager != NULL);
7758 	assert(object->pager_ready);
7759 
7760 	pager = object->pager;
7761 	assert(object->paging_in_progress);
7762 	vnode = vnode_pager_lookup_vnode(pager);
7763 	mo_offset = page->vmp_offset + object->paging_offset;
7764 
7765 	/* verify the SHA1 hash for this page */
7766 	validated = 0;
7767 	tainted = 0;
7768 	nx = 0;
7769 	cs_validate_page(vnode,
7770 	    pager,
7771 	    mo_offset,
7772 	    (const void *)((const char *)kaddr),
7773 	    &validated,
7774 	    &tainted,
7775 	    &nx);
7776 
7777 	page->vmp_cs_validated |= validated;
7778 	page->vmp_cs_tainted |= tainted;
7779 	page->vmp_cs_nx |= nx;
7780 
7781 #if CHECK_CS_VALIDATION_BITMAP
7782 	if (page->vmp_cs_validated == VMP_CS_ALL_TRUE &&
7783 	    page->vmp_cs_tainted == VMP_CS_ALL_FALSE) {
7784 		vnode_pager_cs_check_validation_bitmap(object->pager,
7785 		    mo_offset,
7786 		    CS_BITMAP_SET);
7787 	}
7788 #endif /* CHECK_CS_VALIDATION_BITMAP */
7789 }
7790 
7791 void
vm_page_validate_cs_mapped(vm_page_t page,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset,const void * kaddr)7792 vm_page_validate_cs_mapped(
7793 	vm_page_t       page,
7794 	vm_map_size_t   fault_page_size,
7795 	vm_map_offset_t fault_phys_offset,
7796 	const void      *kaddr)
7797 {
7798 	if (!vm_page_validate_cs_fast(page, fault_page_size, fault_phys_offset)) {
7799 		vm_page_validate_cs_mapped_slow(page, kaddr);
7800 	}
7801 }
7802 
7803 static void
vm_page_map_and_validate_cs(vm_object_t object,vm_page_t page)7804 vm_page_map_and_validate_cs(
7805 	vm_object_t     object,
7806 	vm_page_t       page)
7807 {
7808 	vm_object_offset_t      offset;
7809 	vm_map_offset_t         koffset;
7810 	vm_map_size_t           ksize;
7811 	vm_offset_t             kaddr;
7812 	kern_return_t           kr;
7813 	boolean_t               busy_page;
7814 	boolean_t               need_unmap;
7815 
7816 	vm_object_lock_assert_exclusive(object);
7817 
7818 	assert(object->code_signed);
7819 	offset = page->vmp_offset;
7820 
7821 	busy_page = page->vmp_busy;
7822 	if (!busy_page) {
7823 		/* keep page busy while we map (and unlock) the VM object */
7824 		page->vmp_busy = TRUE;
7825 	}
7826 
7827 	/*
7828 	 * Take a paging reference on the VM object
7829 	 * to protect it from collapse or bypass,
7830 	 * and keep it from disappearing too.
7831 	 */
7832 	vm_object_paging_begin(object);
7833 
7834 	/* map the page in the kernel address space */
7835 	ksize = PAGE_SIZE_64;
7836 	koffset = 0;
7837 	need_unmap = FALSE;
7838 	kr = vm_paging_map_object(page,
7839 	    object,
7840 	    offset,
7841 	    VM_PROT_READ,
7842 	    FALSE,                       /* can't unlock object ! */
7843 	    &ksize,
7844 	    &koffset,
7845 	    &need_unmap);
7846 	if (kr != KERN_SUCCESS) {
7847 		panic("%s: could not map page: 0x%x", __FUNCTION__, kr);
7848 	}
7849 	kaddr = CAST_DOWN(vm_offset_t, koffset);
7850 
7851 	/* validate the mapped page */
7852 	vm_page_validate_cs_mapped_slow(page, (const void *) kaddr);
7853 
7854 	assert(page->vmp_busy);
7855 	assert(object == VM_PAGE_OBJECT(page));
7856 	vm_object_lock_assert_exclusive(object);
7857 
7858 	if (!busy_page) {
7859 		PAGE_WAKEUP_DONE(page);
7860 	}
7861 	if (need_unmap) {
7862 		/* unmap the map from the kernel address space */
7863 		vm_paging_unmap_object(object, koffset, koffset + ksize);
7864 		koffset = 0;
7865 		ksize = 0;
7866 		kaddr = 0;
7867 	}
7868 	vm_object_paging_end(object);
7869 }
7870 
7871 void
vm_page_validate_cs(vm_page_t page,vm_map_size_t fault_page_size,vm_map_offset_t fault_phys_offset)7872 vm_page_validate_cs(
7873 	vm_page_t       page,
7874 	vm_map_size_t   fault_page_size,
7875 	vm_map_offset_t fault_phys_offset)
7876 {
7877 	vm_object_t             object;
7878 
7879 	object = VM_PAGE_OBJECT(page);
7880 	vm_object_lock_assert_held(object);
7881 
7882 	if (vm_page_validate_cs_fast(page, fault_page_size, fault_phys_offset)) {
7883 		return;
7884 	}
7885 	vm_page_map_and_validate_cs(object, page);
7886 }
7887 
7888 void
vm_page_validate_cs_mapped_chunk(vm_page_t page,const void * kaddr,vm_offset_t chunk_offset,vm_size_t chunk_size,boolean_t * validated_p,unsigned * tainted_p)7889 vm_page_validate_cs_mapped_chunk(
7890 	vm_page_t       page,
7891 	const void      *kaddr,
7892 	vm_offset_t     chunk_offset,
7893 	vm_size_t       chunk_size,
7894 	boolean_t       *validated_p,
7895 	unsigned        *tainted_p)
7896 {
7897 	vm_object_t             object;
7898 	vm_object_offset_t      offset, offset_in_page;
7899 	memory_object_t         pager;
7900 	struct vnode            *vnode;
7901 	boolean_t               validated;
7902 	unsigned                tainted;
7903 
7904 	*validated_p = FALSE;
7905 	*tainted_p = 0;
7906 
7907 	assert(page->vmp_busy);
7908 	object = VM_PAGE_OBJECT(page);
7909 	vm_object_lock_assert_exclusive(object);
7910 
7911 	assert(object->code_signed);
7912 	offset = page->vmp_offset;
7913 
7914 	if (!object->alive || object->terminating || object->pager == NULL) {
7915 		/*
7916 		 * The object is terminating and we don't have its pager
7917 		 * so we can't validate the data...
7918 		 */
7919 		return;
7920 	}
7921 	/*
7922 	 * Since we get here to validate a page that was brought in by
7923 	 * the pager, we know that this pager is all setup and ready
7924 	 * by now.
7925 	 */
7926 	assert(!object->internal);
7927 	assert(object->pager != NULL);
7928 	assert(object->pager_ready);
7929 
7930 	pager = object->pager;
7931 	assert(object->paging_in_progress);
7932 	vnode = vnode_pager_lookup_vnode(pager);
7933 
7934 	/* verify the signature for this chunk */
7935 	offset_in_page = chunk_offset;
7936 	assert(offset_in_page < PAGE_SIZE);
7937 
7938 	tainted = 0;
7939 	validated = cs_validate_range(vnode,
7940 	    pager,
7941 	    (object->paging_offset +
7942 	    offset +
7943 	    offset_in_page),
7944 	    (const void *)((const char *)kaddr
7945 	    + offset_in_page),
7946 	    chunk_size,
7947 	    &tainted);
7948 	if (validated) {
7949 		*validated_p = TRUE;
7950 	}
7951 	if (tainted) {
7952 		*tainted_p = tainted;
7953 	}
7954 }
7955 
7956 static void
vm_rtfrecord_lock(void)7957 vm_rtfrecord_lock(void)
7958 {
7959 	lck_spin_lock(&vm_rtfr_slock);
7960 }
7961 
7962 static void
vm_rtfrecord_unlock(void)7963 vm_rtfrecord_unlock(void)
7964 {
7965 	lck_spin_unlock(&vm_rtfr_slock);
7966 }
7967 
7968 unsigned int
vmrtfaultinfo_bufsz(void)7969 vmrtfaultinfo_bufsz(void)
7970 {
7971 	return vmrtf_num_records * sizeof(vm_rtfault_record_t);
7972 }
7973 
7974 #include <kern/backtrace.h>
7975 
7976 __attribute__((noinline))
7977 static void
vm_record_rtfault(thread_t cthread,uint64_t fstart,vm_map_offset_t fault_vaddr,int type_of_fault)7978 vm_record_rtfault(thread_t cthread, uint64_t fstart, vm_map_offset_t fault_vaddr, int type_of_fault)
7979 {
7980 	uint64_t fend = mach_continuous_time();
7981 
7982 	uint64_t cfpc = 0;
7983 	uint64_t ctid = cthread->thread_id;
7984 	uint64_t cupid = get_current_unique_pid();
7985 
7986 	uintptr_t bpc = 0;
7987 	errno_t btr = 0;
7988 
7989 	/*
7990 	 * Capture a single-frame backtrace.  This extracts just the program
7991 	 * counter at the point of the fault, and should not use copyin to get
7992 	 * Rosetta save state.
7993 	 */
7994 	struct backtrace_control ctl = {
7995 		.btc_user_thread = cthread,
7996 		.btc_user_copy = backtrace_user_copy_error,
7997 	};
7998 	unsigned int bfrs = backtrace_user(&bpc, 1U, &ctl, NULL);
7999 	if ((btr == 0) && (bfrs > 0)) {
8000 		cfpc = bpc;
8001 	}
8002 
8003 	assert((fstart != 0) && fend >= fstart);
8004 	vm_rtfrecord_lock();
8005 	assert(vmrtfrs.vmrtfr_curi <= vmrtfrs.vmrtfr_maxi);
8006 
8007 	vmrtfrs.vmrtf_total++;
8008 	vm_rtfault_record_t *cvmr = &vmrtfrs.vm_rtf_records[vmrtfrs.vmrtfr_curi++];
8009 
8010 	cvmr->rtfabstime = fstart;
8011 	cvmr->rtfduration = fend - fstart;
8012 	cvmr->rtfaddr = fault_vaddr;
8013 	cvmr->rtfpc = cfpc;
8014 	cvmr->rtftype = type_of_fault;
8015 	cvmr->rtfupid = cupid;
8016 	cvmr->rtftid = ctid;
8017 
8018 	if (vmrtfrs.vmrtfr_curi > vmrtfrs.vmrtfr_maxi) {
8019 		vmrtfrs.vmrtfr_curi = 0;
8020 	}
8021 
8022 	vm_rtfrecord_unlock();
8023 }
8024 
8025 int
vmrtf_extract(uint64_t cupid,__unused boolean_t isroot,unsigned long vrecordsz,void * vrecords,unsigned long * vmrtfrv)8026 vmrtf_extract(uint64_t cupid, __unused boolean_t isroot, unsigned long vrecordsz, void *vrecords, unsigned long *vmrtfrv)
8027 {
8028 	vm_rtfault_record_t *cvmrd = vrecords;
8029 	size_t residue = vrecordsz;
8030 	size_t numextracted = 0;
8031 	boolean_t early_exit = FALSE;
8032 
8033 	vm_rtfrecord_lock();
8034 
8035 	for (int vmfi = 0; vmfi <= vmrtfrs.vmrtfr_maxi; vmfi++) {
8036 		if (residue < sizeof(vm_rtfault_record_t)) {
8037 			early_exit = TRUE;
8038 			break;
8039 		}
8040 
8041 		if (vmrtfrs.vm_rtf_records[vmfi].rtfupid != cupid) {
8042 #if     DEVELOPMENT || DEBUG
8043 			if (isroot == FALSE) {
8044 				continue;
8045 			}
8046 #else
8047 			continue;
8048 #endif /* DEVDEBUG */
8049 		}
8050 
8051 		*cvmrd = vmrtfrs.vm_rtf_records[vmfi];
8052 		cvmrd++;
8053 		residue -= sizeof(vm_rtfault_record_t);
8054 		numextracted++;
8055 	}
8056 
8057 	vm_rtfrecord_unlock();
8058 
8059 	*vmrtfrv = numextracted;
8060 	return early_exit;
8061 }
8062 
8063 /*
8064  * Only allow one diagnosis to be in flight at a time, to avoid
8065  * creating too much additional memory usage.
8066  */
8067 static volatile uint_t vmtc_diagnosing;
8068 unsigned int vmtc_total = 0;
8069 
8070 /*
8071  * Type used to update telemetry for the diagnosis counts.
8072  */
8073 CA_EVENT(vmtc_telemetry,
8074     CA_INT, vmtc_num_byte,            /* number of corrupt bytes found */
8075     CA_BOOL, vmtc_undiagnosed,        /* undiagnosed because more than 1 at a time */
8076     CA_BOOL, vmtc_not_eligible,       /* the page didn't qualify */
8077     CA_BOOL, vmtc_copyin_fail,        /* unable to copy in the page */
8078     CA_BOOL, vmtc_not_found,          /* no corruption found even though CS failed */
8079     CA_BOOL, vmtc_one_bit_flip,       /* single bit flip */
8080     CA_BOOL, vmtc_testing);           /* caused on purpose by testing */
8081 
8082 #if DEVELOPMENT || DEBUG
8083 /*
8084  * Buffers used to compare before/after page contents.
8085  * Stashed to aid when debugging crashes.
8086  */
8087 static size_t vmtc_last_buffer_size = 0;
8088 static uint64_t *vmtc_last_before_buffer = NULL;
8089 static uint64_t *vmtc_last_after_buffer = NULL;
8090 
8091 /*
8092  * Needed to record corruptions due to testing.
8093  */
8094 static uintptr_t corruption_test_va = 0;
8095 #endif /* DEVELOPMENT || DEBUG */
8096 
8097 /*
8098  * Stash a copy of data from a possibly corrupt page.
8099  */
8100 static uint64_t *
vmtc_get_page_data(vm_map_offset_t code_addr,vm_page_t page)8101 vmtc_get_page_data(
8102 	vm_map_offset_t code_addr,
8103 	vm_page_t       page)
8104 {
8105 	uint64_t        *buffer = NULL;
8106 	addr64_t        buffer_paddr;
8107 	addr64_t        page_paddr;
8108 	extern void     bcopy_phys(addr64_t from, addr64_t to, vm_size_t bytes);
8109 	uint_t          size = MIN(vm_map_page_size(current_map()), PAGE_SIZE);
8110 
8111 	/*
8112 	 * Need an aligned buffer to do a physical copy.
8113 	 */
8114 	if (kernel_memory_allocate(kernel_map, (vm_offset_t *)&buffer,
8115 	    size, size - 1, KMA_KOBJECT, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) {
8116 		return NULL;
8117 	}
8118 	buffer_paddr = kvtophys((vm_offset_t)buffer);
8119 	page_paddr = ptoa(VM_PAGE_GET_PHYS_PAGE(page));
8120 
8121 	/* adjust the page start address if we need only 4K of a 16K page */
8122 	if (size < PAGE_SIZE) {
8123 		uint_t subpage_start = ((code_addr & (PAGE_SIZE - 1)) & ~(size - 1));
8124 		page_paddr += subpage_start;
8125 	}
8126 
8127 	bcopy_phys(page_paddr, buffer_paddr, size);
8128 	return buffer;
8129 }
8130 
8131 /*
8132  * Set things up so we can diagnose a potential text page corruption.
8133  */
8134 static uint64_t *
vmtc_text_page_diagnose_setup(vm_map_offset_t code_addr,vm_page_t page,CA_EVENT_TYPE (vmtc_telemetry)* event)8135 vmtc_text_page_diagnose_setup(
8136 	vm_map_offset_t code_addr,
8137 	vm_page_t       page,
8138 	CA_EVENT_TYPE(vmtc_telemetry) *event)
8139 {
8140 	uint64_t        *buffer = NULL;
8141 
8142 	/*
8143 	 * If another is being diagnosed, skip this one.
8144 	 */
8145 	if (!OSCompareAndSwap(0, 1, &vmtc_diagnosing)) {
8146 		event->vmtc_undiagnosed = true;
8147 		return NULL;
8148 	}
8149 
8150 	/*
8151 	 * Get the contents of the corrupt page.
8152 	 */
8153 	buffer = vmtc_get_page_data(code_addr, page);
8154 	if (buffer == NULL) {
8155 		event->vmtc_copyin_fail = true;
8156 		if (!OSCompareAndSwap(1, 0, &vmtc_diagnosing)) {
8157 			panic("Bad compare and swap in setup!");
8158 		}
8159 		return NULL;
8160 	}
8161 	return buffer;
8162 }
8163 
8164 /*
8165  * Diagnose the text page by comparing its contents with
8166  * the one we've previously saved.
8167  */
8168 static void
vmtc_text_page_diagnose(vm_map_offset_t code_addr,uint64_t * old_code_buffer,CA_EVENT_TYPE (vmtc_telemetry)* event)8169 vmtc_text_page_diagnose(
8170 	vm_map_offset_t code_addr,
8171 	uint64_t        *old_code_buffer,
8172 	CA_EVENT_TYPE(vmtc_telemetry) *event)
8173 {
8174 	uint64_t        *new_code_buffer;
8175 	size_t          size = MIN(vm_map_page_size(current_map()), PAGE_SIZE);
8176 	uint_t          count = (uint_t)size / sizeof(uint64_t);
8177 	uint_t          diff_count = 0;
8178 	bool            bit_flip = false;
8179 	uint_t          b;
8180 	uint64_t        *new;
8181 	uint64_t        *old;
8182 
8183 	new_code_buffer = kalloc_data(size, Z_WAITOK);
8184 	assert(new_code_buffer != NULL);
8185 	if (copyin((user_addr_t)vm_map_trunc_page(code_addr, size - 1), new_code_buffer, size) != 0) {
8186 		/* copyin error, so undo things */
8187 		event->vmtc_copyin_fail = true;
8188 		goto done;
8189 	}
8190 
8191 	new = new_code_buffer;
8192 	old = old_code_buffer;
8193 	for (; count-- > 0; ++new, ++old) {
8194 		if (*new == *old) {
8195 			continue;
8196 		}
8197 
8198 		/*
8199 		 * On first diff, check for a single bit flip
8200 		 */
8201 		if (diff_count == 0) {
8202 			uint64_t x = (*new ^ *old);
8203 			assert(x != 0);
8204 			if ((x & (x - 1)) == 0) {
8205 				bit_flip = true;
8206 				++diff_count;
8207 				continue;
8208 			}
8209 		}
8210 
8211 		/*
8212 		 * count up the number of different bytes.
8213 		 */
8214 		for (b = 0; b < sizeof(uint64_t); ++b) {
8215 			char *n = (char *)new;
8216 			char *o = (char *)old;
8217 			if (n[b] != o[b]) {
8218 				++diff_count;
8219 			}
8220 		}
8221 	}
8222 
8223 	if (diff_count > 1) {
8224 		bit_flip = false;
8225 	}
8226 
8227 	if (diff_count == 0) {
8228 		event->vmtc_not_found = true;
8229 	} else {
8230 		event->vmtc_num_byte = diff_count;
8231 	}
8232 	if (bit_flip) {
8233 		event->vmtc_one_bit_flip = true;
8234 	}
8235 
8236 done:
8237 	/*
8238 	 * Free up the code copy buffers, but save the last
8239 	 * set on development / debug kernels in case they
8240 	 * can provide evidence for debugging memory stomps.
8241 	 */
8242 #if DEVELOPMENT || DEBUG
8243 	if (vmtc_last_before_buffer != NULL) {
8244 		kmem_free(kernel_map, (vm_offset_t)vmtc_last_before_buffer, vmtc_last_buffer_size);
8245 	}
8246 	if (vmtc_last_after_buffer != NULL) {
8247 		kfree_data(vmtc_last_after_buffer, vmtc_last_buffer_size);
8248 	}
8249 	vmtc_last_before_buffer = old_code_buffer;
8250 	vmtc_last_after_buffer = new_code_buffer;
8251 	vmtc_last_buffer_size = size;
8252 #else /* DEVELOPMENT || DEBUG */
8253 	kfree_data(new_code_buffer, size);
8254 	kmem_free(kernel_map, (vm_offset_t)old_code_buffer, size);
8255 #endif /* DEVELOPMENT || DEBUG */
8256 
8257 	/*
8258 	 * We're finished, so clear the diagnosing flag.
8259 	 */
8260 	if (!OSCompareAndSwap(1, 0, &vmtc_diagnosing)) {
8261 		panic("Bad compare and swap in diagnose!");
8262 	}
8263 }
8264 
8265 /*
8266  * For the given map, virt address, find the object, offset, and page.
8267  * This has to lookup the map entry, verify protections, walk any shadow chains.
8268  * If found, returns with the object locked.
8269  */
8270 static kern_return_t
vmtc_revalidate_lookup(vm_map_t map,vm_map_offset_t vaddr,vm_object_t * ret_object,vm_object_offset_t * ret_offset,vm_page_t * ret_page,vm_prot_t * ret_prot)8271 vmtc_revalidate_lookup(
8272 	vm_map_t               map,
8273 	vm_map_offset_t        vaddr,
8274 	vm_object_t            *ret_object,
8275 	vm_object_offset_t     *ret_offset,
8276 	vm_page_t              *ret_page,
8277 	vm_prot_t              *ret_prot)
8278 {
8279 	vm_object_t            object;
8280 	vm_object_offset_t     offset;
8281 	vm_page_t              page;
8282 	kern_return_t          kr = KERN_SUCCESS;
8283 	uint8_t                object_lock_type = OBJECT_LOCK_EXCLUSIVE;
8284 	vm_map_version_t       version;
8285 	boolean_t              wired;
8286 	struct vm_object_fault_info fault_info = {};
8287 	vm_map_t               real_map = NULL;
8288 	vm_prot_t              prot;
8289 	vm_object_t            shadow;
8290 
8291 	/*
8292 	 * Find the object/offset for the given location/map.
8293 	 * Note this returns with the object locked.
8294 	 */
8295 restart:
8296 	vm_map_lock_read(map);
8297 	object = VM_OBJECT_NULL;        /* in case we come around the restart path */
8298 	kr = vm_map_lookup_and_lock_object(&map, vaddr, VM_PROT_READ,
8299 	    object_lock_type, &version, &object, &offset, &prot, &wired,
8300 	    &fault_info, &real_map, NULL);
8301 	vm_map_unlock_read(map);
8302 	if (real_map != NULL && real_map != map) {
8303 		vm_map_unlock(real_map);
8304 	}
8305 
8306 	/*
8307 	 * If there's no page here, fail.
8308 	 */
8309 	if (kr != KERN_SUCCESS || object == NULL) {
8310 		kr = KERN_FAILURE;
8311 		goto done;
8312 	}
8313 
8314 	/*
8315 	 * Chase down any shadow chains to find the actual page.
8316 	 */
8317 	for (;;) {
8318 		/*
8319 		 * See if the page is on the current object.
8320 		 */
8321 		page = vm_page_lookup(object, vm_object_trunc_page(offset));
8322 		if (page != NULL) {
8323 			/* restart the lookup */
8324 			if (page->vmp_restart) {
8325 				vm_object_unlock(object);
8326 				goto restart;
8327 			}
8328 
8329 			/*
8330 			 * If this page is busy, we need to wait for it.
8331 			 */
8332 			if (page->vmp_busy) {
8333 				PAGE_SLEEP(object, page, TRUE);
8334 				vm_object_unlock(object);
8335 				goto restart;
8336 			}
8337 			break;
8338 		}
8339 
8340 		/*
8341 		 * If the object doesn't have the page and
8342 		 * has no shadow, then we can quit.
8343 		 */
8344 		shadow = object->shadow;
8345 		if (shadow == NULL) {
8346 			kr = KERN_FAILURE;
8347 			goto done;
8348 		}
8349 
8350 		/*
8351 		 * Move to the next object
8352 		 */
8353 		offset += object->vo_shadow_offset;
8354 		vm_object_lock(shadow);
8355 		vm_object_unlock(object);
8356 		object = shadow;
8357 		shadow = VM_OBJECT_NULL;
8358 	}
8359 	*ret_object = object;
8360 	*ret_offset = vm_object_trunc_page(offset);
8361 	*ret_page = page;
8362 	*ret_prot = prot;
8363 
8364 done:
8365 	if (kr != KERN_SUCCESS && object != NULL) {
8366 		vm_object_unlock(object);
8367 	}
8368 	return kr;
8369 }
8370 
8371 /*
8372  * Check if a page is wired, needs extra locking.
8373  */
8374 static bool
is_page_wired(vm_page_t page)8375 is_page_wired(vm_page_t page)
8376 {
8377 	bool result;
8378 	vm_page_lock_queues();
8379 	result = VM_PAGE_WIRED(page);
8380 	vm_page_unlock_queues();
8381 	return result;
8382 }
8383 
8384 /*
8385  * A fatal process error has occurred in the given task.
8386  * Recheck the code signing of the text page at the given
8387  * address to check for a text page corruption.
8388  *
8389  * Returns KERN_FAILURE if a page was found to be corrupt
8390  * by failing to match its code signature. KERN_SUCCESS
8391  * means the page is either valid or we don't have the
8392  * information to say it's corrupt.
8393  */
8394 kern_return_t
revalidate_text_page(task_t task,vm_map_offset_t code_addr)8395 revalidate_text_page(task_t task, vm_map_offset_t code_addr)
8396 {
8397 	kern_return_t          kr;
8398 	vm_map_t               map;
8399 	vm_object_t            object = NULL;
8400 	vm_object_offset_t     offset;
8401 	vm_page_t              page = NULL;
8402 	struct vnode           *vnode;
8403 	uint64_t               *diagnose_buffer = NULL;
8404 	CA_EVENT_TYPE(vmtc_telemetry) * event = NULL;
8405 	ca_event_t             ca_event = NULL;
8406 	vm_prot_t              prot;
8407 
8408 	map = task->map;
8409 	if (task->map == NULL) {
8410 		return KERN_SUCCESS;
8411 	}
8412 
8413 	kr = vmtc_revalidate_lookup(map, code_addr, &object, &offset, &page, &prot);
8414 	if (kr != KERN_SUCCESS) {
8415 		goto done;
8416 	}
8417 
8418 	/*
8419 	 * The page must be executable.
8420 	 */
8421 	if (!(prot & VM_PROT_EXECUTE)) {
8422 		goto done;
8423 	}
8424 
8425 	/*
8426 	 * The object needs to have a pager.
8427 	 */
8428 	if (object->pager == NULL) {
8429 		goto done;
8430 	}
8431 
8432 	/*
8433 	 * Needs to be a vnode backed page to have a signature.
8434 	 */
8435 	vnode = vnode_pager_lookup_vnode(object->pager);
8436 	if (vnode == NULL) {
8437 		goto done;
8438 	}
8439 
8440 	/*
8441 	 * Object checks to see if we should proceed.
8442 	 */
8443 	if (!object->code_signed ||     /* no code signature to check */
8444 	    object->internal ||         /* internal objects aren't signed */
8445 	    object->terminating ||      /* the object and its pages are already going away */
8446 	    !object->pager_ready) {     /* this should happen, but check shouldn't hurt */
8447 		goto done;
8448 	}
8449 
8450 
8451 	/*
8452 	 * Check the code signature of the page in question.
8453 	 */
8454 	vm_page_map_and_validate_cs(object, page);
8455 
8456 	/*
8457 	 * At this point:
8458 	 * vmp_cs_validated |= validated (set if a code signature exists)
8459 	 * vmp_cs_tainted |= tainted (set if code signature violation)
8460 	 * vmp_cs_nx |= nx;  ??
8461 	 *
8462 	 * if vmp_pmapped then have to pmap_disconnect..
8463 	 * other flags to check on object or page?
8464 	 */
8465 	if (page->vmp_cs_tainted != VMP_CS_ALL_FALSE) {
8466 #if DEBUG || DEVELOPMENT
8467 		/*
8468 		 * On development builds, a boot-arg can be used to cause
8469 		 * a panic, instead of a quiet repair.
8470 		 */
8471 		if (vmtc_panic_instead) {
8472 			panic("Text page corruption detected: vm_page_t 0x%llx", (long long)(uintptr_t)page);
8473 		}
8474 #endif /* DEBUG || DEVELOPMENT */
8475 
8476 		/*
8477 		 * We're going to invalidate this page. Grab a copy of it for comparison.
8478 		 */
8479 		ca_event = CA_EVENT_ALLOCATE(vmtc_telemetry);
8480 		event = ca_event->data;
8481 		diagnose_buffer = vmtc_text_page_diagnose_setup(code_addr, page, event);
8482 
8483 		/*
8484 		 * Invalidate, i.e. toss, the corrupted page.
8485 		 */
8486 		if (!page->vmp_cleaning &&
8487 		    !page->vmp_laundry &&
8488 		    !page->vmp_fictitious &&
8489 		    !page->vmp_precious &&
8490 		    !page->vmp_absent &&
8491 		    !VMP_ERROR_GET(page) &&
8492 		    !page->vmp_dirty &&
8493 		    !is_page_wired(page)) {
8494 			if (page->vmp_pmapped) {
8495 				int refmod = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(page));
8496 				if (refmod & VM_MEM_MODIFIED) {
8497 					SET_PAGE_DIRTY(page, FALSE);
8498 				}
8499 				if (refmod & VM_MEM_REFERENCED) {
8500 					page->vmp_reference = TRUE;
8501 				}
8502 			}
8503 			/* If the page seems intentionally modified, don't trash it. */
8504 			if (!page->vmp_dirty) {
8505 				VM_PAGE_FREE(page);
8506 			} else {
8507 				event->vmtc_not_eligible = true;
8508 			}
8509 		} else {
8510 			event->vmtc_not_eligible = true;
8511 		}
8512 		vm_object_unlock(object);
8513 		object = VM_OBJECT_NULL;
8514 
8515 		/*
8516 		 * Now try to diagnose the type of failure by faulting
8517 		 * in a new copy and diff'ing it with what we saved.
8518 		 */
8519 		if (diagnose_buffer != NULL) {
8520 			vmtc_text_page_diagnose(code_addr, diagnose_buffer, event);
8521 		}
8522 #if DEBUG || DEVELOPMENT
8523 		if (corruption_test_va != 0) {
8524 			corruption_test_va = 0;
8525 			event->vmtc_testing = true;
8526 		}
8527 #endif /* DEBUG || DEVELOPMENT */
8528 		ktriage_record(thread_tid(current_thread()),
8529 		    KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_TEXT_CORRUPTION),
8530 		    0 /* arg */);
8531 		CA_EVENT_SEND(ca_event);
8532 		printf("Text page corruption detected for pid %d\n", proc_selfpid());
8533 		++vmtc_total;
8534 		return KERN_FAILURE; /* failure means we definitely found a corrupt page */
8535 	}
8536 done:
8537 	if (object != NULL) {
8538 		vm_object_unlock(object);
8539 	}
8540 	return KERN_SUCCESS;
8541 }
8542 
8543 #if DEBUG || DEVELOPMENT
8544 /*
8545  * For implementing unit tests - ask the pmap to corrupt a text page.
8546  * We have to find the page, to get the physical address, then invoke
8547  * the pmap.
8548  */
8549 extern kern_return_t vm_corrupt_text_addr(uintptr_t);
8550 
8551 kern_return_t
vm_corrupt_text_addr(uintptr_t va)8552 vm_corrupt_text_addr(uintptr_t va)
8553 {
8554 	task_t                 task = current_task();
8555 	vm_map_t               map;
8556 	kern_return_t          kr = KERN_SUCCESS;
8557 	vm_object_t            object = VM_OBJECT_NULL;
8558 	vm_object_offset_t     offset;
8559 	vm_page_t              page = NULL;
8560 	pmap_paddr_t           pa;
8561 	vm_prot_t              prot;
8562 
8563 	map = task->map;
8564 	if (task->map == NULL) {
8565 		printf("corrupt_text_addr: no map\n");
8566 		return KERN_FAILURE;
8567 	}
8568 
8569 	kr = vmtc_revalidate_lookup(map, (vm_map_offset_t)va, &object, &offset, &page, &prot);
8570 	if (kr != KERN_SUCCESS) {
8571 		printf("corrupt_text_addr: page lookup failed\n");
8572 		return kr;
8573 	}
8574 	if (!(prot & VM_PROT_EXECUTE)) {
8575 		printf("corrupt_text_addr: page not executable\n");
8576 		return KERN_FAILURE;
8577 	}
8578 
8579 	/* get the physical address to use */
8580 	pa = ptoa(VM_PAGE_GET_PHYS_PAGE(page)) + (va - vm_object_trunc_page(va));
8581 
8582 	/*
8583 	 * Check we have something we can work with.
8584 	 * Due to racing with pageout as we enter the sysctl,
8585 	 * it's theoretically possible to have the page disappear, just
8586 	 * before the lookup.
8587 	 *
8588 	 * That's highly likely to happen often. I've filed a radar 72857482
8589 	 * to bubble up the error here to the sysctl result and have the
8590 	 * test not FAIL in that case.
8591 	 */
8592 	if (page->vmp_busy) {
8593 		printf("corrupt_text_addr: vmp_busy\n");
8594 		kr = KERN_FAILURE;
8595 	}
8596 	if (page->vmp_cleaning) {
8597 		printf("corrupt_text_addr: vmp_cleaning\n");
8598 		kr = KERN_FAILURE;
8599 	}
8600 	if (page->vmp_laundry) {
8601 		printf("corrupt_text_addr: vmp_cleaning\n");
8602 		kr = KERN_FAILURE;
8603 	}
8604 	if (page->vmp_fictitious) {
8605 		printf("corrupt_text_addr: vmp_fictitious\n");
8606 		kr = KERN_FAILURE;
8607 	}
8608 	if (page->vmp_precious) {
8609 		printf("corrupt_text_addr: vmp_precious\n");
8610 		kr = KERN_FAILURE;
8611 	}
8612 	if (page->vmp_absent) {
8613 		printf("corrupt_text_addr: vmp_absent\n");
8614 		kr = KERN_FAILURE;
8615 	}
8616 	if (VMP_ERROR_GET(page)) {
8617 		printf("corrupt_text_addr: vmp_error\n");
8618 		kr = KERN_FAILURE;
8619 	}
8620 	if (page->vmp_dirty) {
8621 		printf("corrupt_text_addr: vmp_dirty\n");
8622 		kr = KERN_FAILURE;
8623 	}
8624 	if (is_page_wired(page)) {
8625 		printf("corrupt_text_addr: wired\n");
8626 		kr = KERN_FAILURE;
8627 	}
8628 	if (!page->vmp_pmapped) {
8629 		printf("corrupt_text_addr: !vmp_pmapped\n");
8630 		kr = KERN_FAILURE;
8631 	}
8632 
8633 	if (kr == KERN_SUCCESS) {
8634 		printf("corrupt_text_addr: using physaddr 0x%llx\n", (long long)pa);
8635 		kr = pmap_test_text_corruption(pa);
8636 		if (kr != KERN_SUCCESS) {
8637 			printf("corrupt_text_addr: pmap error %d\n", kr);
8638 		} else {
8639 			corruption_test_va = va;
8640 		}
8641 	} else {
8642 		printf("corrupt_text_addr: object %p\n", object);
8643 		printf("corrupt_text_addr: offset 0x%llx\n", (uint64_t)offset);
8644 		printf("corrupt_text_addr: va 0x%llx\n", (uint64_t)va);
8645 		printf("corrupt_text_addr: vm_object_trunc_page(va) 0x%llx\n", (uint64_t)vm_object_trunc_page(va));
8646 		printf("corrupt_text_addr: vm_page_t %p\n", page);
8647 		printf("corrupt_text_addr: ptoa(PHYS_PAGE) 0x%llx\n", (uint64_t)ptoa(VM_PAGE_GET_PHYS_PAGE(page)));
8648 		printf("corrupt_text_addr: using physaddr 0x%llx\n", (uint64_t)pa);
8649 	}
8650 
8651 	if (object != VM_OBJECT_NULL) {
8652 		vm_object_unlock(object);
8653 	}
8654 	return kr;
8655 }
8656 
8657 #endif /* DEBUG || DEVELOPMENT */
8658