]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_fault.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / vm / vm_fault.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5ba3f43e 5 *
2d21ac55
A
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.
5ba3f43e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
5ba3f43e 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
5ba3f43e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
5ba3f43e 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
5ba3f43e 35 *
1c79356b
A
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.
5ba3f43e 41 *
1c79356b
A
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.
5ba3f43e 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
5ba3f43e 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
5ba3f43e 52 *
1c79356b
A
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 */
1c79356b
A
64
65#include <mach_cluster_stats.h>
66#include <mach_pagemap.h>
2d21ac55 67#include <libkern/OSAtomic.h>
1c79356b 68
91447636 69#include <mach/mach_types.h>
1c79356b 70#include <mach/kern_return.h>
0a7de745 71#include <mach/message.h> /* for error codes */
91447636
A
72#include <mach/vm_param.h>
73#include <mach/vm_behavior.h>
74#include <mach/memory_object.h>
0a7de745 75/* For memory_object_data_{request,unlock} */
2d21ac55 76#include <mach/sdt.h>
91447636
A
77
78#include <kern/kern_types.h>
1c79356b
A
79#include <kern/host_statistics.h>
80#include <kern/counters.h>
81#include <kern/task.h>
82#include <kern/thread.h>
83#include <kern/sched_prim.h>
84#include <kern/host.h>
85#include <kern/xpr.h>
91447636
A
86#include <kern/mach_param.h>
87#include <kern/macro_help.h>
88#include <kern/zalloc.h>
89#include <kern/misc_protos.h>
39037602 90#include <kern/policy_internal.h>
91447636 91
39236c6e
A
92#include <vm/vm_compressor.h>
93#include <vm/vm_compressor_pager.h>
91447636 94#include <vm/vm_fault.h>
1c79356b
A
95#include <vm/vm_map.h>
96#include <vm/vm_object.h>
97#include <vm/vm_page.h>
55e303ae 98#include <vm/vm_kern.h>
1c79356b
A
99#include <vm/pmap.h>
100#include <vm/vm_pageout.h>
91447636 101#include <vm/vm_protos.h>
2d21ac55
A
102#include <vm/vm_external.h>
103#include <vm/memory_object.h>
0a7de745 104#include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
6d2010ae 105#include <vm/vm_shared_region.h>
1c79356b 106
39236c6e 107#include <sys/codesign.h>
39037602
A
108#include <sys/reason.h>
109#include <sys/signalvar.h>
39236c6e 110
5ba3f43e 111#include <san/kasan.h>
15129b1c 112
0a7de745 113#define VM_FAULT_CLASSIFY 0
1c79356b 114
2d21ac55 115#define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
1c79356b 116
0a7de745 117unsigned int vm_object_pagein_throttle = 16;
1c79356b 118
b0d623f7 119/*
5ba3f43e 120 * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
b0d623f7
A
121 * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts
122 * of memory if they're buggy and can run the system completely out of swap space. If this happens, we
123 * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps
5ba3f43e 124 * keep the UI active so that the user has a chance to kill the offending task before the system
b0d623f7
A
125 * completely hangs.
126 *
127 * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
128 * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold
129 * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a
130 * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
131 */
132
99c3a104 133extern void throttle_lowpri_io(int);
b0d623f7 134
39037602
A
135extern struct vnode *vnode_pager_lookup_vnode(memory_object_t);
136
b0d623f7
A
137uint64_t vm_hard_throttle_threshold;
138
b0d623f7 139
b0d623f7 140
0a7de745
A
141#define NEED_TO_HARD_THROTTLE_THIS_TASK() (vm_wants_task_throttled(current_task()) || \
142 ((vm_page_free_count < vm_page_throttle_limit || \
143 HARD_THROTTLE_LIMIT_REACHED()) && \
144 proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO) >= THROTTLE_LEVEL_THROTTLED))
b0d623f7
A
145
146
0a7de745
A
147#define HARD_THROTTLE_DELAY 10000 /* 10000 us == 10 ms */
148#define SOFT_THROTTLE_DELAY 200 /* 200 us == .2 ms */
04b8595b 149
0a7de745
A
150#define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS 6
151#define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC 20000
04b8595b 152
1c79356b 153
b0d623f7 154boolean_t current_thread_aborted(void);
91447636 155
1c79356b 156/* Forward declarations of internal routines. */
3e170ce0 157static kern_return_t vm_fault_wire_fast(
0a7de745
A
158 vm_map_t map,
159 vm_map_offset_t va,
160 vm_prot_t prot,
161 vm_tag_t wire_tag,
162 vm_map_entry_t entry,
163 pmap_t pmap,
164 vm_map_offset_t pmap_addr,
165 ppnum_t *physpage_p);
1c79356b 166
3e170ce0 167static kern_return_t vm_fault_internal(
0a7de745
A
168 vm_map_t map,
169 vm_map_offset_t vaddr,
170 vm_prot_t caller_prot,
171 boolean_t change_wiring,
172 vm_tag_t wire_tag,
173 int interruptible,
174 pmap_t pmap,
175 vm_map_offset_t pmap_addr,
176 ppnum_t *physpage_p);
3e170ce0
A
177
178static void vm_fault_copy_cleanup(
0a7de745
A
179 vm_page_t page,
180 vm_page_t top_page);
1c79356b 181
3e170ce0 182static void vm_fault_copy_dst_cleanup(
0a7de745 183 vm_page_t page);
1c79356b 184
0a7de745
A
185#if VM_FAULT_CLASSIFY
186extern void vm_fault_classify(vm_object_t object,
187 vm_object_offset_t offset,
188 vm_prot_t fault_type);
1c79356b
A
189
190extern void vm_fault_classify_init(void);
191#endif
192
d1ecb069 193unsigned long vm_pmap_enter_blocked = 0;
316670eb 194unsigned long vm_pmap_enter_retried = 0;
4a3eedf9
A
195
196unsigned long vm_cs_validates = 0;
197unsigned long vm_cs_revalidates = 0;
198unsigned long vm_cs_query_modified = 0;
199unsigned long vm_cs_validated_dirtied = 0;
6d2010ae 200unsigned long vm_cs_bitmap_validated = 0;
d9a64523
A
201#if PMAP_CS
202uint64_t vm_cs_defer_to_pmap_cs = 0;
203uint64_t vm_cs_defer_to_pmap_cs_not = 0;
204#endif /* PMAP_CS */
593a1d5f 205
fe8ab488
A
206void vm_pre_fault(vm_map_offset_t);
207
3e170ce0 208extern char *kdp_compressor_decompressed_page;
0a7de745
A
209extern addr64_t kdp_compressor_decompressed_page_paddr;
210extern ppnum_t kdp_compressor_decompressed_page_ppnum;
3e170ce0 211
d9a64523
A
212struct vmrtfr {
213 int vmrtfr_maxi;
214 int vmrtfr_curi;
215 int64_t vmrtf_total;
216 vm_rtfault_record_t *vm_rtf_records;
217} vmrtfrs;
218#define VMRTF_DEFAULT_BUFSIZE (4096)
219#define VMRTF_NUM_RECORDS_DEFAULT (VMRTF_DEFAULT_BUFSIZE / sizeof(vm_rtfault_record_t))
220int vmrtf_num_records = VMRTF_NUM_RECORDS_DEFAULT;
221
222static void vm_rtfrecord_lock(void);
223static void vm_rtfrecord_unlock(void);
224static void vm_record_rtfault(thread_t, uint64_t, vm_map_offset_t, int);
225
226lck_spin_t vm_rtfr_slock;
227extern lck_grp_t vm_page_lck_grp_bucket;
228extern lck_attr_t vm_page_lck_attr;
229
1c79356b
A
230/*
231 * Routine: vm_fault_init
232 * Purpose:
233 * Initialize our private data structures.
234 */
235void
236vm_fault_init(void)
237{
39236c6e
A
238 int i, vm_compressor_temp;
239 boolean_t need_default_val = TRUE;
b0d623f7
A
240 /*
241 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
242 * computed as a percentage of available memory, and the percentage used is scaled inversely with
39236c6e 243 * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems
b0d623f7
A
244 * and reduce the value down to 10% for very large memory configurations. This helps give us a
245 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
246 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
247 */
248
0a7de745 249 vm_hard_throttle_threshold = sane_size * (35 - MIN((int)(sane_size / (1024 * 1024 * 1024)), 25)) / 100;
39236c6e
A
250
251 /*
252 * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry.
253 */
254
0a7de745
A
255 if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp, sizeof(vm_compressor_temp))) {
256 for (i = 0; i < VM_PAGER_MAX_MODES; i++) {
5ba3f43e 257 if (vm_compressor_temp > 0 &&
0a7de745 258 ((vm_compressor_temp & (1 << i)) == vm_compressor_temp)) {
39236c6e
A
259 need_default_val = FALSE;
260 vm_compressor_mode = vm_compressor_temp;
261 break;
262 }
263 }
0a7de745 264 if (need_default_val) {
39236c6e 265 printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp);
0a7de745 266 }
5ba3f43e 267 }
39236c6e
A
268 if (need_default_val) {
269 /* If no boot arg or incorrect boot arg, try device tree. */
270 PE_get_default("kern.vm_compressor", &vm_compressor_mode, sizeof(vm_compressor_mode));
271 }
39236c6e 272 printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode);
1c79356b
A
273}
274
0a7de745
A
275void
276vm_rtfault_record_init(void)
277{
d9a64523
A
278 PE_parse_boot_argn("vm_rtfault_records", &vmrtf_num_records, sizeof(vmrtf_num_records));
279
280 assert(vmrtf_num_records >= 1);
281 vmrtf_num_records = MAX(vmrtf_num_records, 1);
282 size_t kallocsz = vmrtf_num_records * sizeof(vm_rtfault_record_t);
283 vmrtfrs.vm_rtf_records = kalloc(kallocsz);
284 bzero(vmrtfrs.vm_rtf_records, kallocsz);
285 vmrtfrs.vmrtfr_maxi = vmrtf_num_records - 1;
286 lck_spin_init(&vm_rtfr_slock, &vm_page_lck_grp_bucket, &vm_page_lck_attr);
287}
1c79356b
A
288/*
289 * Routine: vm_fault_cleanup
290 * Purpose:
291 * Clean up the result of vm_fault_page.
292 * Results:
293 * The paging reference for "object" is released.
294 * "object" is unlocked.
295 * If "top_page" is not null, "top_page" is
296 * freed and the paging reference for the object
297 * containing it is released.
298 *
299 * In/out conditions:
300 * "object" must be locked.
301 */
302void
303vm_fault_cleanup(
0a7de745
A
304 vm_object_t object,
305 vm_page_t top_page)
1c79356b
A
306{
307 vm_object_paging_end(object);
0a7de745 308 vm_object_unlock(object);
1c79356b
A
309
310 if (top_page != VM_PAGE_NULL) {
0a7de745 311 object = VM_PAGE_OBJECT(top_page);
2d21ac55
A
312
313 vm_object_lock(object);
314 VM_PAGE_FREE(top_page);
315 vm_object_paging_end(object);
316 vm_object_unlock(object);
1c79356b
A
317 }
318}
319
55e303ae
A
320#define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
321
322
0a7de745 323boolean_t vm_page_deactivate_behind = TRUE;
5ba3f43e
A
324/*
325 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
1c79356b 326 */
0a7de745
A
327#define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
328#define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
b0d623f7
A
329 /* we use it to size an array on the stack */
330
331int vm_default_behind = VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW;
55e303ae 332
0a7de745 333#define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
2d21ac55
A
334
335/*
336 * vm_page_is_sequential
337 *
338 * Determine if sequential access is in progress
339 * in accordance with the behavior specified.
340 * Update state to indicate current access pattern.
341 *
342 * object must have at least the shared lock held
343 */
344static
345void
346vm_fault_is_sequential(
0a7de745
A
347 vm_object_t object,
348 vm_object_offset_t offset,
349 vm_behavior_t behavior)
2d21ac55 350{
0a7de745
A
351 vm_object_offset_t last_alloc;
352 int sequential;
353 int orig_sequential;
2d21ac55 354
0a7de745 355 last_alloc = object->last_alloc;
2d21ac55
A
356 sequential = object->sequential;
357 orig_sequential = sequential;
358
359 switch (behavior) {
360 case VM_BEHAVIOR_RANDOM:
0a7de745 361 /*
2d21ac55
A
362 * reset indicator of sequential behavior
363 */
0a7de745
A
364 sequential = 0;
365 break;
2d21ac55
A
366
367 case VM_BEHAVIOR_SEQUENTIAL:
0a7de745
A
368 if (offset && last_alloc == offset - PAGE_SIZE_64) {
369 /*
2d21ac55
A
370 * advance indicator of sequential behavior
371 */
0a7de745
A
372 if (sequential < MAX_SEQUENTIAL_RUN) {
373 sequential += PAGE_SIZE;
374 }
2d21ac55 375 } else {
0a7de745 376 /*
2d21ac55
A
377 * reset indicator of sequential behavior
378 */
0a7de745 379 sequential = 0;
2d21ac55 380 }
0a7de745 381 break;
2d21ac55
A
382
383 case VM_BEHAVIOR_RSEQNTL:
0a7de745
A
384 if (last_alloc && last_alloc == offset + PAGE_SIZE_64) {
385 /*
2d21ac55
A
386 * advance indicator of sequential behavior
387 */
0a7de745
A
388 if (sequential > -MAX_SEQUENTIAL_RUN) {
389 sequential -= PAGE_SIZE;
390 }
2d21ac55 391 } else {
0a7de745 392 /*
2d21ac55
A
393 * reset indicator of sequential behavior
394 */
0a7de745 395 sequential = 0;
2d21ac55 396 }
0a7de745 397 break;
2d21ac55
A
398
399 case VM_BEHAVIOR_DEFAULT:
400 default:
0a7de745
A
401 if (offset && last_alloc == (offset - PAGE_SIZE_64)) {
402 /*
2d21ac55
A
403 * advance indicator of sequential behavior
404 */
0a7de745
A
405 if (sequential < 0) {
406 sequential = 0;
407 }
408 if (sequential < MAX_SEQUENTIAL_RUN) {
409 sequential += PAGE_SIZE;
410 }
2d21ac55 411 } else if (last_alloc && last_alloc == (offset + PAGE_SIZE_64)) {
0a7de745 412 /*
2d21ac55
A
413 * advance indicator of sequential behavior
414 */
0a7de745
A
415 if (sequential > 0) {
416 sequential = 0;
417 }
418 if (sequential > -MAX_SEQUENTIAL_RUN) {
419 sequential -= PAGE_SIZE;
420 }
2d21ac55 421 } else {
0a7de745 422 /*
2d21ac55
A
423 * reset indicator of sequential behavior
424 */
0a7de745 425 sequential = 0;
2d21ac55 426 }
0a7de745 427 break;
2d21ac55
A
428 }
429 if (sequential != orig_sequential) {
0a7de745
A
430 if (!OSCompareAndSwap(orig_sequential, sequential, (UInt32 *)&object->sequential)) {
431 /*
2d21ac55
A
432 * if someone else has already updated object->sequential
433 * don't bother trying to update it or object->last_alloc
434 */
0a7de745 435 return;
2d21ac55
A
436 }
437 }
438 /*
439 * I'd like to do this with a OSCompareAndSwap64, but that
440 * doesn't exist for PPC... however, it shouldn't matter
441 * that much... last_alloc is maintained so that we can determine
442 * if a sequential access pattern is taking place... if only
443 * one thread is banging on this object, no problem with the unprotected
444 * update... if 2 or more threads are banging away, we run the risk of
445 * someone seeing a mangled update... however, in the face of multiple
446 * accesses, no sequential access pattern can develop anyway, so we
447 * haven't lost any real info.
448 */
449 object->last_alloc = offset;
450}
451
452
b0d623f7
A
453int vm_page_deactivate_behind_count = 0;
454
55e303ae 455/*
2d21ac55
A
456 * vm_page_deactivate_behind
457 *
458 * Determine if sequential access is in progress
459 * in accordance with the behavior specified. If
460 * so, compute a potential page to deactivate and
461 * deactivate it.
55e303ae 462 *
2d21ac55 463 * object must be locked.
55e303ae 464 *
2d21ac55 465 * return TRUE if we actually deactivate a page
55e303ae
A
466 */
467static
468boolean_t
469vm_fault_deactivate_behind(
0a7de745
A
470 vm_object_t object,
471 vm_object_offset_t offset,
472 vm_behavior_t behavior)
55e303ae 473{
0a7de745
A
474 int n;
475 int pages_in_run = 0;
476 int max_pages_in_run = 0;
477 int sequential_run;
478 int sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
479 vm_object_offset_t run_offset = 0;
480 vm_object_offset_t pg_offset = 0;
481 vm_page_t m;
482 vm_page_t page_run[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER];
55e303ae 483
b0d623f7 484 pages_in_run = 0;
55e303ae 485#if TRACEFAULTPAGE
0a7de745 486 dbgTrace(0xBEEF0018, (unsigned int) object, (unsigned int) vm_fault_deactivate_behind); /* (TEST/DEBUG) */
55e303ae
A
487#endif
488
2d21ac55 489 if (object == kernel_object || vm_page_deactivate_behind == FALSE) {
91447636
A
490 /*
491 * Do not deactivate pages from the kernel object: they
492 * are not intended to become pageable.
2d21ac55 493 * or we've disabled the deactivate behind mechanism
91447636
A
494 */
495 return FALSE;
496 }
2d21ac55 497 if ((sequential_run = object->sequential)) {
0a7de745
A
498 if (sequential_run < 0) {
499 sequential_behavior = VM_BEHAVIOR_RSEQNTL;
500 sequential_run = 0 - sequential_run;
501 } else {
502 sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
503 }
2d21ac55 504 }
55e303ae
A
505 switch (behavior) {
506 case VM_BEHAVIOR_RANDOM:
55e303ae
A
507 break;
508 case VM_BEHAVIOR_SEQUENTIAL:
0a7de745 509 if (sequential_run >= (int)PAGE_SIZE) {
b0d623f7
A
510 run_offset = 0 - PAGE_SIZE_64;
511 max_pages_in_run = 1;
512 }
55e303ae
A
513 break;
514 case VM_BEHAVIOR_RSEQNTL:
0a7de745 515 if (sequential_run >= (int)PAGE_SIZE) {
b0d623f7
A
516 run_offset = PAGE_SIZE_64;
517 max_pages_in_run = 1;
518 }
55e303ae
A
519 break;
520 case VM_BEHAVIOR_DEFAULT:
521 default:
0a7de745 522 { vm_object_offset_t behind = vm_default_behind * PAGE_SIZE_64;
2d21ac55 523
0a7de745 524 /*
2d21ac55
A
525 * determine if the run of sequential accesss has been
526 * long enough on an object with default access behavior
527 * to consider it for deactivation
528 */
b0d623f7
A
529 if ((uint64_t)sequential_run >= behind && (sequential_run % (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER * PAGE_SIZE)) == 0) {
530 /*
531 * the comparisons between offset and behind are done
532 * in this kind of odd fashion in order to prevent wrap around
533 * at the end points
534 */
0a7de745
A
535 if (sequential_behavior == VM_BEHAVIOR_SEQUENTIAL) {
536 if (offset >= behind) {
b0d623f7
A
537 run_offset = 0 - behind;
538 pg_offset = PAGE_SIZE_64;
539 max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER;
540 }
2d21ac55 541 } else {
0a7de745 542 if (offset < -behind) {
b0d623f7
A
543 run_offset = behind;
544 pg_offset = 0 - PAGE_SIZE_64;
545 max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER;
546 }
2d21ac55 547 }
55e303ae 548 }
0a7de745 549 break;}
2d21ac55 550 }
0a7de745 551 for (n = 0; n < max_pages_in_run; n++) {
b0d623f7
A
552 m = vm_page_lookup(object, offset + run_offset + (n * pg_offset));
553
d9a64523 554 if (m && !m->vmp_laundry && !m->vmp_busy && !m->vmp_no_cache && (m->vmp_q_state != VM_PAGE_ON_THROTTLED_Q) && !m->vmp_fictitious && !m->vmp_absent) {
b0d623f7 555 page_run[pages_in_run++] = m;
39236c6e
A
556
557 /*
558 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
559 *
560 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
561 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
562 * new reference happens. If no futher references happen on the page after that remote TLB flushes
563 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
564 * by pageout_scan, which is just fine since the last reference would have happened quite far
565 * in the past (TLB caches don't hang around for very long), and of course could just as easily
566 * have happened before we did the deactivate_behind.
567 */
39037602 568 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m), VM_MEM_REFERENCED, PMAP_OPTIONS_NOFLUSH, (void *)NULL);
b0d623f7
A
569 }
570 }
571 if (pages_in_run) {
572 vm_page_lockspin_queues();
573
574 for (n = 0; n < pages_in_run; n++) {
b0d623f7
A
575 m = page_run[n];
576
577 vm_page_deactivate_internal(m, FALSE);
578
579 vm_page_deactivate_behind_count++;
55e303ae 580#if TRACEFAULTPAGE
0a7de745 581 dbgTrace(0xBEEF0019, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */
55e303ae
A
582#endif
583 }
b0d623f7
A
584 vm_page_unlock_queues();
585
586 return TRUE;
55e303ae
A
587 }
588 return FALSE;
589}
1c79356b 590
1c79356b 591
04b8595b 592#if (DEVELOPMENT || DEBUG)
0a7de745
A
593uint32_t vm_page_creation_throttled_hard = 0;
594uint32_t vm_page_creation_throttled_soft = 0;
595uint64_t vm_page_creation_throttle_avoided = 0;
04b8595b
A
596#endif /* DEVELOPMENT || DEBUG */
597
6d2010ae 598static int
04b8595b 599vm_page_throttled(boolean_t page_kept)
b0d623f7 600{
0a7de745
A
601 clock_sec_t elapsed_sec;
602 clock_sec_t tv_sec;
603 clock_usec_t tv_usec;
5ba3f43e 604
b0d623f7 605 thread_t thread = current_thread();
5ba3f43e 606
0a7de745
A
607 if (thread->options & TH_OPT_VMPRIV) {
608 return 0;
609 }
b0d623f7 610
04b8595b
A
611 if (thread->t_page_creation_throttled) {
612 thread->t_page_creation_throttled = 0;
5ba3f43e 613
0a7de745 614 if (page_kept == FALSE) {
04b8595b 615 goto no_throttle;
0a7de745 616 }
04b8595b
A
617 }
618 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
619#if (DEVELOPMENT || DEBUG)
620 thread->t_page_creation_throttled_hard++;
621 OSAddAtomic(1, &vm_page_creation_throttled_hard);
622#endif /* DEVELOPMENT || DEBUG */
0a7de745 623 return HARD_THROTTLE_DELAY;
04b8595b 624 }
b0d623f7 625
39037602 626 if ((vm_page_free_count < vm_page_throttle_limit || (VM_CONFIG_COMPRESSOR_IS_PRESENT && SWAPPER_NEEDS_TO_UNTHROTTLE())) &&
04b8595b 627 thread->t_page_creation_count > (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS * VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC)) {
3e170ce0
A
628 if (vm_page_free_wanted == 0 && vm_page_free_wanted_privileged == 0) {
629#if (DEVELOPMENT || DEBUG)
630 OSAddAtomic64(1, &vm_page_creation_throttle_avoided);
631#endif
632 goto no_throttle;
633 }
b0d623f7
A
634 clock_get_system_microtime(&tv_sec, &tv_usec);
635
636 elapsed_sec = tv_sec - thread->t_page_creation_time;
637
04b8595b
A
638 if (elapsed_sec <= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS ||
639 (thread->t_page_creation_count / elapsed_sec) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC) {
04b8595b 640 if (elapsed_sec >= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS)) {
b0d623f7
A
641 /*
642 * we'll reset our stats to give a well behaved app
643 * that was unlucky enough to accumulate a bunch of pages
644 * over a long period of time a chance to get out of
645 * the throttled state... we reset the counter and timestamp
646 * so that if it stays under the rate limit for the next second
5ba3f43e 647 * it will be back in our good graces... if it exceeds it, it
b0d623f7
A
648 * will remain in the throttled state
649 */
650 thread->t_page_creation_time = tv_sec;
04b8595b 651 thread->t_page_creation_count = VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC * (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS - 1);
b0d623f7 652 }
d9a64523 653 VM_PAGEOUT_DEBUG(vm_page_throttle_count, 1);
b0d623f7 654
04b8595b
A
655 thread->t_page_creation_throttled = 1;
656
39037602 657 if (VM_CONFIG_COMPRESSOR_IS_PRESENT && HARD_THROTTLE_LIMIT_REACHED()) {
04b8595b
A
658#if (DEVELOPMENT || DEBUG)
659 thread->t_page_creation_throttled_hard++;
660 OSAddAtomic(1, &vm_page_creation_throttled_hard);
661#endif /* DEVELOPMENT || DEBUG */
0a7de745 662 return HARD_THROTTLE_DELAY;
04b8595b
A
663 } else {
664#if (DEVELOPMENT || DEBUG)
665 thread->t_page_creation_throttled_soft++;
666 OSAddAtomic(1, &vm_page_creation_throttled_soft);
667#endif /* DEVELOPMENT || DEBUG */
0a7de745 668 return SOFT_THROTTLE_DELAY;
04b8595b 669 }
b0d623f7
A
670 }
671 thread->t_page_creation_time = tv_sec;
672 thread->t_page_creation_count = 0;
673 }
04b8595b
A
674no_throttle:
675 thread->t_page_creation_count++;
676
0a7de745 677 return 0;
b0d623f7
A
678}
679
3e170ce0 680
2d21ac55
A
681/*
682 * check for various conditions that would
683 * prevent us from creating a ZF page...
684 * cleanup is based on being called from vm_fault_page
685 *
686 * object must be locked
d9a64523 687 * object == m->vmp_object
2d21ac55
A
688 */
689static vm_fault_return_t
d9a64523 690vm_fault_check(vm_object_t object, vm_page_t m, vm_page_t first_m, wait_interrupt_t interruptible_state, boolean_t page_throttle)
2d21ac55 691{
6d2010ae
A
692 int throttle_delay;
693
0a7de745 694 if (object->shadow_severed ||
b0d623f7 695 VM_OBJECT_PURGEABLE_FAULT_ERROR(object)) {
0a7de745 696 /*
b0d623f7
A
697 * Either:
698 * 1. the shadow chain was severed,
699 * 2. the purgeable object is volatile or empty and is marked
700 * to fault on access while volatile.
701 * Just have to return an error at this point
2d21ac55 702 */
0a7de745
A
703 if (m != VM_PAGE_NULL) {
704 VM_PAGE_FREE(m);
705 }
2d21ac55
A
706 vm_fault_cleanup(object, first_m);
707
708 thread_interrupt_level(interruptible_state);
709
0a7de745 710 return VM_FAULT_MEMORY_ERROR;
2d21ac55 711 }
3e170ce0
A
712 if (page_throttle == TRUE) {
713 if ((throttle_delay = vm_page_throttled(FALSE))) {
714 /*
715 * we're throttling zero-fills...
716 * treat this as if we couldn't grab a page
717 */
0a7de745 718 if (m != VM_PAGE_NULL) {
3e170ce0 719 VM_PAGE_FREE(m);
0a7de745 720 }
3e170ce0 721 vm_fault_cleanup(object, first_m);
2d21ac55 722
3e170ce0 723 VM_DEBUG_EVENT(vmf_check_zfdelay, VMF_CHECK_ZFDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0);
b0d623f7 724
3e170ce0 725 delay(throttle_delay);
b0d623f7 726
3e170ce0
A
727 if (current_thread_aborted()) {
728 thread_interrupt_level(interruptible_state);
729 return VM_FAULT_INTERRUPTED;
730 }
6d2010ae 731 thread_interrupt_level(interruptible_state);
2d21ac55 732
0a7de745 733 return VM_FAULT_MEMORY_SHORTAGE;
3e170ce0 734 }
2d21ac55 735 }
0a7de745 736 return VM_FAULT_SUCCESS;
2d21ac55
A
737}
738
739
740/*
741 * do the work to zero fill a page and
742 * inject it into the correct paging queue
743 *
d9a64523 744 * m->vmp_object must be locked
2d21ac55
A
745 * page queue lock must NOT be held
746 */
747static int
748vm_fault_zero_page(vm_page_t m, boolean_t no_zero_fill)
749{
0a7de745
A
750 int my_fault = DBG_ZERO_FILL_FAULT;
751 vm_object_t object;
39037602
A
752
753 object = VM_PAGE_OBJECT(m);
2d21ac55
A
754
755 /*
756 * This is is a zero-fill page fault...
757 *
758 * Checking the page lock is a waste of
759 * time; this page was absent, so
760 * it can't be page locked by a pager.
761 *
762 * we also consider it undefined
763 * with respect to instruction
764 * execution. i.e. it is the responsibility
765 * of higher layers to call for an instruction
766 * sync after changing the contents and before
5ba3f43e 767 * sending a program into this area. We
2d21ac55
A
768 * choose this approach for performance
769 */
d9a64523 770 m->vmp_pmapped = TRUE;
2d21ac55 771
d9a64523
A
772 m->vmp_cs_validated = FALSE;
773 m->vmp_cs_tainted = FALSE;
774 m->vmp_cs_nx = FALSE;
2d21ac55 775
6d2010ae
A
776 if (no_zero_fill == TRUE) {
777 my_fault = DBG_NZF_PAGE_FAULT;
fe8ab488 778
0a7de745
A
779 if (m->vmp_absent && m->vmp_busy) {
780 return my_fault;
781 }
6d2010ae 782 } else {
2d21ac55
A
783 vm_page_zero_fill(m);
784
785 VM_STAT_INCR(zero_fill_count);
786 DTRACE_VM2(zfod, int, 1, (uint64_t *), NULL);
787 }
d9a64523 788 assert(!m->vmp_laundry);
39037602 789 assert(object != kernel_object);
d9a64523 790 //assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0);
2d21ac55 791
39037602 792 if (!VM_DYNAMIC_PAGING_ENABLED() &&
0a7de745
A
793 (object->purgable == VM_PURGABLE_DENY ||
794 object->purgable == VM_PURGABLE_NONVOLATILE ||
795 object->purgable == VM_PURGABLE_VOLATILE)) {
b0d623f7 796 vm_page_lockspin_queues();
2d21ac55 797
39037602 798 if (!VM_DYNAMIC_PAGING_ENABLED()) {
39236c6e 799 assert(!VM_PAGE_WIRED(m));
6d2010ae 800
39236c6e
A
801 /*
802 * can't be on the pageout queue since we don't
803 * have a pager to try and clean to
804 */
39037602 805 vm_page_queues_remove(m, TRUE);
3e170ce0 806 vm_page_check_pageable_safe(m);
0a7de745 807 vm_page_queue_enter(&vm_page_queue_throttled, m, vmp_pageq);
d9a64523 808 m->vmp_q_state = VM_PAGE_ON_THROTTLED_Q;
39236c6e
A
809 vm_page_throttled_count++;
810 }
2d21ac55 811 vm_page_unlock_queues();
2d21ac55 812 }
0a7de745 813 return my_fault;
2d21ac55
A
814}
815
816
1c79356b
A
817/*
818 * Routine: vm_fault_page
819 * Purpose:
820 * Find the resident page for the virtual memory
821 * specified by the given virtual memory object
822 * and offset.
823 * Additional arguments:
824 * The required permissions for the page is given
825 * in "fault_type". Desired permissions are included
2d21ac55 826 * in "protection".
5ba3f43e 827 * fault_info is passed along to determine pagein cluster
2d21ac55
A
828 * limits... it contains the expected reference pattern,
829 * cluster size if available, etc...
1c79356b
A
830 *
831 * If the desired page is known to be resident (for
832 * example, because it was previously wired down), asserting
833 * the "unwiring" parameter will speed the search.
834 *
835 * If the operation can be interrupted (by thread_abort
836 * or thread_terminate), then the "interruptible"
837 * parameter should be asserted.
838 *
839 * Results:
840 * The page containing the proper data is returned
841 * in "result_page".
842 *
843 * In/out conditions:
844 * The source object must be locked and referenced,
845 * and must donate one paging reference. The reference
846 * is not affected. The paging reference and lock are
847 * consumed.
848 *
849 * If the call succeeds, the object in which "result_page"
850 * resides is left locked and holding a paging reference.
851 * If this is not the original object, a busy page in the
852 * original object is returned in "top_page", to prevent other
853 * callers from pursuing this same data, along with a paging
854 * reference for the original object. The "top_page" should
855 * be destroyed when this guarantee is no longer required.
856 * The "result_page" is also left busy. It is not removed
857 * from the pageout queues.
b0d623f7 858 * Special Case:
5ba3f43e 859 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
b0d623f7 860 * fault succeeded but there's no VM page (i.e. the VM object
0a7de745 861 * does not actually hold VM pages, but device memory or
b0d623f7
A
862 * large pages). The object is still locked and we still hold a
863 * paging_in_progress reference.
1c79356b 864 */
b0d623f7 865unsigned int vm_fault_page_blocked_access = 0;
316670eb 866unsigned int vm_fault_page_forced_retry = 0;
1c79356b
A
867
868vm_fault_return_t
869vm_fault_page(
870 /* Arguments: */
0a7de745
A
871 vm_object_t first_object, /* Object to begin search */
872 vm_object_offset_t first_offset, /* Offset into object */
873 vm_prot_t fault_type, /* What access is requested */
874 boolean_t must_be_resident,/* Must page be resident? */
875 boolean_t caller_lookup, /* caller looked up page */
1c79356b 876 /* Modifies in place: */
0a7de745
A
877 vm_prot_t *protection, /* Protection for mapping */
878 vm_page_t *result_page, /* Page found, if successful */
39236c6e 879 /* Returns: */
0a7de745
A
880 vm_page_t *top_page, /* Page in top object, if
881 * not result_page. */
1c79356b 882 int *type_of_fault, /* if non-null, fill in with type of fault
0a7de745 883 * COW, zero-fill, etc... returned in trace point */
1c79356b 884 /* More arguments: */
0a7de745
A
885 kern_return_t *error_code, /* code if page is in error */
886 boolean_t no_zero_fill, /* don't zero fill absent pages */
887 boolean_t data_supply, /* treat as data_supply if
888 * it is a write fault and a full
889 * page is provided */
2d21ac55 890 vm_object_fault_info_t fault_info)
1c79356b 891{
0a7de745
A
892 vm_page_t m;
893 vm_object_t object;
894 vm_object_offset_t offset;
895 vm_page_t first_m;
896 vm_object_t next_object;
897 vm_object_t copy_object;
898 boolean_t look_for_page;
899 boolean_t force_fault_retry = FALSE;
900 vm_prot_t access_required = fault_type;
901 vm_prot_t wants_copy_flag;
902 kern_return_t wait_result;
903 wait_interrupt_t interruptible_state;
904 boolean_t data_already_requested = FALSE;
905 vm_behavior_t orig_behavior;
906 vm_size_t orig_cluster_size;
907 vm_fault_return_t error;
908 int my_fault;
909 uint32_t try_failed_count;
910 int interruptible; /* how may fault be interrupted? */
911 int external_state = VM_EXTERNAL_STATE_UNKNOWN;
912 memory_object_t pager;
913 vm_fault_return_t retval;
914 int grab_options;
1c79356b 915
1c79356b 916/*
5ba3f43e 917 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
39037602 918 * marked as paged out in the compressor pager or the pager doesn't exist.
5ba3f43e
A
919 * Note also that if the pager for an internal object
920 * has not been created, the pager is not invoked regardless of the value
39037602 921 * of MUST_ASK_PAGER().
1c79356b
A
922 *
923 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
39037602 924 * is marked as paged out in the compressor pager.
1c79356b
A
925 * PAGED_OUT() is used to determine if a page has already been pushed
926 * into a copy object in order to avoid a redundant page out operation.
927 */
0a7de745 928#define MUST_ASK_PAGER(o, f, s) \
39236c6e 929 ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
39037602 930
39236c6e
A
931#define PAGED_OUT(o, f) \
932 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
1c79356b
A
933
934/*
935 * Recovery actions
936 */
0a7de745
A
937#define RELEASE_PAGE(m) \
938 MACRO_BEGIN \
939 PAGE_WAKEUP_DONE(m); \
940 if ( !VM_PAGE_PAGEABLE(m)) { \
941 vm_page_lockspin_queues(); \
942 if ( !VM_PAGE_PAGEABLE(m)) { \
943 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE) \
944 vm_page_deactivate(m); \
945 else \
946 vm_page_activate(m); \
947 } \
948 vm_page_unlock_queues(); \
949 } \
1c79356b
A
950 MACRO_END
951
952#if TRACEFAULTPAGE
0a7de745 953 dbgTrace(0xBEEF0002, (unsigned int) first_object, (unsigned int) first_offset); /* (TEST/DEBUG) */
1c79356b
A
954#endif
955
2d21ac55 956 interruptible = fault_info->interruptible;
9bccf70c 957 interruptible_state = thread_interrupt_level(interruptible);
5ba3f43e 958
1c79356b
A
959 /*
960 * INVARIANTS (through entire routine):
961 *
962 * 1) At all times, we must either have the object
963 * lock or a busy page in some object to prevent
964 * some other thread from trying to bring in
965 * the same page.
966 *
967 * Note that we cannot hold any locks during the
968 * pager access or when waiting for memory, so
969 * we use a busy page then.
970 *
1c79356b
A
971 * 2) To prevent another thread from racing us down the
972 * shadow chain and entering a new page in the top
973 * object before we do, we must keep a busy page in
974 * the top object while following the shadow chain.
975 *
976 * 3) We must increment paging_in_progress on any object
2d21ac55
A
977 * for which we have a busy page before dropping
978 * the object lock
1c79356b
A
979 *
980 * 4) We leave busy pages on the pageout queues.
981 * If the pageout daemon comes across a busy page,
982 * it will remove the page from the pageout queues.
983 */
984
1c79356b
A
985 object = first_object;
986 offset = first_offset;
987 first_m = VM_PAGE_NULL;
988 access_required = fault_type;
989
2d21ac55 990
1c79356b 991 XPR(XPR_VM_FAULT,
0a7de745
A
992 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
993 object, offset, fault_type, *protection, 0);
1c79356b
A
994
995 /*
2d21ac55 996 * default type of fault
1c79356b 997 */
2d21ac55 998 my_fault = DBG_CACHE_HIT_FAULT;
1c79356b
A
999
1000 while (TRUE) {
1001#if TRACEFAULTPAGE
0a7de745 1002 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1c79356b 1003#endif
39037602
A
1004
1005 grab_options = 0;
1006#if CONFIG_SECLUDED_MEMORY
1007 if (object->can_grab_secluded) {
1008 grab_options |= VM_PAGE_GRAB_SECLUDED;
1009 }
1010#endif /* CONFIG_SECLUDED_MEMORY */
1011
1c79356b 1012 if (!object->alive) {
0a7de745 1013 /*
2d21ac55
A
1014 * object is no longer valid
1015 * clean up and return error
1016 */
1c79356b 1017 vm_fault_cleanup(object, first_m);
9bccf70c 1018 thread_interrupt_level(interruptible_state);
2d21ac55 1019
0a7de745 1020 return VM_FAULT_MEMORY_ERROR;
1c79356b 1021 }
2d21ac55 1022
b0d623f7
A
1023 if (!object->pager_created && object->phys_contiguous) {
1024 /*
1025 * A physically-contiguous object without a pager:
1026 * must be a "large page" object. We do not deal
1027 * with VM pages for this object.
1028 */
39236c6e 1029 caller_lookup = FALSE;
b0d623f7
A
1030 m = VM_PAGE_NULL;
1031 goto phys_contig_object;
1032 }
1033
1034 if (object->blocked_access) {
1035 /*
1036 * Access to this VM object has been blocked.
1037 * Replace our "paging_in_progress" reference with
1038 * a "activity_in_progress" reference and wait for
1039 * access to be unblocked.
1040 */
39236c6e 1041 caller_lookup = FALSE; /* no longer valid after sleep */
b0d623f7
A
1042 vm_object_activity_begin(object);
1043 vm_object_paging_end(object);
1044 while (object->blocked_access) {
1045 vm_object_sleep(object,
0a7de745
A
1046 VM_OBJECT_EVENT_UNBLOCKED,
1047 THREAD_UNINT);
b0d623f7
A
1048 }
1049 vm_fault_page_blocked_access++;
1050 vm_object_paging_begin(object);
1051 vm_object_activity_end(object);
1052 }
1053
2d21ac55
A
1054 /*
1055 * See whether the page at 'offset' is resident
1056 */
39236c6e
A
1057 if (caller_lookup == TRUE) {
1058 /*
1059 * The caller has already looked up the page
1060 * and gave us the result in "result_page".
1061 * We can use this for the first lookup but
1062 * it loses its validity as soon as we unlock
1063 * the object.
1064 */
1065 m = *result_page;
1066 caller_lookup = FALSE; /* no longer valid after that */
1067 } else {
1068 m = vm_page_lookup(object, offset);
1069 }
1c79356b 1070#if TRACEFAULTPAGE
0a7de745 1071 dbgTrace(0xBEEF0004, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */
1c79356b
A
1072#endif
1073 if (m != VM_PAGE_NULL) {
d9a64523 1074 if (m->vmp_busy) {
0a7de745 1075 /*
2d21ac55
A
1076 * The page is being brought in,
1077 * wait for it and then retry.
2d21ac55 1078 */
1c79356b 1079#if TRACEFAULTPAGE
0a7de745 1080 dbgTrace(0xBEEF0005, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */
1c79356b 1081#endif
316670eb 1082 wait_result = PAGE_SLEEP(object, m, interruptible);
1c79356b 1083
316670eb
A
1084 XPR(XPR_VM_FAULT,
1085 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
1086 object, offset,
1087 m, 0, 0);
1088 counter(c_vm_fault_page_block_busy_kernel++);
2d21ac55 1089
316670eb
A
1090 if (wait_result != THREAD_AWAKENED) {
1091 vm_fault_cleanup(object, first_m);
1092 thread_interrupt_level(interruptible_state);
6d2010ae 1093
0a7de745
A
1094 if (wait_result == THREAD_RESTART) {
1095 return VM_FAULT_RETRY;
1096 } else {
1097 return VM_FAULT_INTERRUPTED;
1098 }
1c79356b 1099 }
316670eb 1100 continue;
1c79356b 1101 }
d9a64523
A
1102 if (m->vmp_laundry) {
1103 m->vmp_free_when_done = FALSE;
1c79356b 1104
0a7de745 1105 if (!m->vmp_cleaning) {
316670eb 1106 vm_pageout_steal_laundry(m, FALSE);
0a7de745 1107 }
316670eb 1108 }
39037602 1109 if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) {
91447636 1110 /*
2d21ac55 1111 * Guard page: off limits !
91447636 1112 */
2d21ac55
A
1113 if (fault_type == VM_PROT_NONE) {
1114 /*
1115 * The fault is not requesting any
1116 * access to the guard page, so it must
1117 * be just to wire or unwire it.
1118 * Let's pretend it succeeded...
1119 */
d9a64523 1120 m->vmp_busy = TRUE;
2d21ac55
A
1121 *result_page = m;
1122 assert(first_m == VM_PAGE_NULL);
1123 *top_page = first_m;
0a7de745 1124 if (type_of_fault) {
2d21ac55 1125 *type_of_fault = DBG_GUARD_FAULT;
0a7de745 1126 }
99c3a104 1127 thread_interrupt_level(interruptible_state);
2d21ac55
A
1128 return VM_FAULT_SUCCESS;
1129 } else {
1130 /*
1131 * The fault requests access to the
1132 * guard page: let's deny that !
1133 */
1134 vm_fault_cleanup(object, first_m);
1135 thread_interrupt_level(interruptible_state);
1136 return VM_FAULT_MEMORY_ERROR;
1137 }
91447636 1138 }
1c79356b 1139
d9a64523 1140 if (m->vmp_error) {
0a7de745 1141 /*
2d21ac55
A
1142 * The page is in error, give up now.
1143 */
1c79356b 1144#if TRACEFAULTPAGE
0a7de745 1145 dbgTrace(0xBEEF0006, (unsigned int) m, (unsigned int) error_code); /* (TEST/DEBUG) */
1c79356b 1146#endif
0a7de745
A
1147 if (error_code) {
1148 *error_code = KERN_MEMORY_ERROR;
1149 }
1c79356b 1150 VM_PAGE_FREE(m);
2d21ac55 1151
1c79356b 1152 vm_fault_cleanup(object, first_m);
9bccf70c 1153 thread_interrupt_level(interruptible_state);
1c79356b 1154
0a7de745 1155 return VM_FAULT_MEMORY_ERROR;
2d21ac55 1156 }
d9a64523 1157 if (m->vmp_restart) {
0a7de745 1158 /*
2d21ac55
A
1159 * The pager wants us to restart
1160 * at the top of the chain,
1161 * typically because it has moved the
1162 * page to another pager, then do so.
1163 */
1c79356b 1164#if TRACEFAULTPAGE
0a7de745 1165 dbgTrace(0xBEEF0007, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */
1c79356b
A
1166#endif
1167 VM_PAGE_FREE(m);
2d21ac55 1168
1c79356b 1169 vm_fault_cleanup(object, first_m);
9bccf70c 1170 thread_interrupt_level(interruptible_state);
1c79356b 1171
0a7de745 1172 return VM_FAULT_RETRY;
2d21ac55 1173 }
d9a64523 1174 if (m->vmp_absent) {
0a7de745 1175 /*
2d21ac55
A
1176 * The page isn't busy, but is absent,
1177 * therefore it's deemed "unavailable".
1178 *
1c79356b
A
1179 * Remove the non-existent page (unless it's
1180 * in the top object) and move on down to the
1181 * next object (if there is one).
1182 */
1183#if TRACEFAULTPAGE
0a7de745 1184 dbgTrace(0xBEEF0008, (unsigned int) m, (unsigned int) object->shadow); /* (TEST/DEBUG) */
1c79356b 1185#endif
1c79356b 1186 next_object = object->shadow;
1c79356b 1187
2d21ac55 1188 if (next_object == VM_OBJECT_NULL) {
1c79356b
A
1189 /*
1190 * Absent page at bottom of shadow
1191 * chain; zero fill the page we left
2d21ac55
A
1192 * busy in the first object, and free
1193 * the absent page.
1c79356b 1194 */
2d21ac55 1195 assert(!must_be_resident);
55e303ae
A
1196
1197 /*
2d21ac55
A
1198 * check for any conditions that prevent
1199 * us from creating a new zero-fill page
5ba3f43e 1200 * vm_fault_check will do all of the
2d21ac55
A
1201 * fault cleanup in the case of an error condition
1202 * including resetting the thread_interrupt_level
55e303ae 1203 */
04b8595b 1204 error = vm_fault_check(object, m, first_m, interruptible_state, (type_of_fault == NULL) ? TRUE : FALSE);
55e303ae 1205
0a7de745
A
1206 if (error != VM_FAULT_SUCCESS) {
1207 return error;
1208 }
55e303ae 1209
1c79356b 1210 XPR(XPR_VM_FAULT,
2d21ac55 1211 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
0a7de745
A
1212 object, offset,
1213 m,
1214 first_object, 0);
2d21ac55 1215
1c79356b 1216 if (object != first_object) {
0a7de745 1217 /*
2d21ac55
A
1218 * free the absent page we just found
1219 */
1c79356b 1220 VM_PAGE_FREE(m);
2d21ac55
A
1221
1222 /*
1223 * drop reference and lock on current object
1224 */
1c79356b
A
1225 vm_object_paging_end(object);
1226 vm_object_unlock(object);
2d21ac55
A
1227
1228 /*
5ba3f43e 1229 * grab the original page we
2d21ac55
A
1230 * 'soldered' in place and
1231 * retake lock on 'first_object'
1232 */
1c79356b
A
1233 m = first_m;
1234 first_m = VM_PAGE_NULL;
1c79356b 1235
2d21ac55
A
1236 object = first_object;
1237 offset = first_offset;
0b4e3aa0 1238
1c79356b 1239 vm_object_lock(object);
9bccf70c 1240 } else {
0a7de745 1241 /*
2d21ac55
A
1242 * we're going to use the absent page we just found
1243 * so convert it to a 'busy' page
1244 */
0a7de745 1245 m->vmp_absent = FALSE;
d9a64523 1246 m->vmp_busy = TRUE;
0b4e3aa0 1247 }
0a7de745 1248 if (fault_info->mark_zf_absent && no_zero_fill == TRUE) {
d9a64523 1249 m->vmp_absent = TRUE;
0a7de745 1250 }
2d21ac55
A
1251 /*
1252 * zero-fill the page and put it on
1253 * the correct paging queue
1254 */
1255 my_fault = vm_fault_zero_page(m, no_zero_fill);
1256
1c79356b
A
1257 break;
1258 } else {
0a7de745 1259 if (must_be_resident) {
1c79356b 1260 vm_object_paging_end(object);
0a7de745 1261 } else if (object != first_object) {
1c79356b
A
1262 vm_object_paging_end(object);
1263 VM_PAGE_FREE(m);
1264 } else {
1265 first_m = m;
d9a64523
A
1266 m->vmp_absent = FALSE;
1267 m->vmp_busy = TRUE;
1c79356b 1268
2d21ac55 1269 vm_page_lockspin_queues();
39037602 1270 vm_page_queues_remove(m, FALSE);
1c79356b
A
1271 vm_page_unlock_queues();
1272 }
1273 XPR(XPR_VM_FAULT,
1274 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
0a7de745
A
1275 object, offset,
1276 next_object,
1277 offset + object->vo_shadow_offset, 0);
2d21ac55 1278
6d2010ae
A
1279 offset += object->vo_shadow_offset;
1280 fault_info->lo_offset += object->vo_shadow_offset;
1281 fault_info->hi_offset += object->vo_shadow_offset;
1c79356b 1282 access_required = VM_PROT_READ;
2d21ac55 1283
1c79356b
A
1284 vm_object_lock(next_object);
1285 vm_object_unlock(object);
1286 object = next_object;
1287 vm_object_paging_begin(object);
5ba3f43e 1288
2d21ac55
A
1289 /*
1290 * reset to default type of fault
1291 */
1292 my_fault = DBG_CACHE_HIT_FAULT;
1293
1c79356b
A
1294 continue;
1295 }
1296 }
d9a64523 1297 if ((m->vmp_cleaning)
2d21ac55
A
1298 && ((object != first_object) || (object->copy != VM_OBJECT_NULL))
1299 && (fault_type & VM_PROT_WRITE)) {
1c79356b
A
1300 /*
1301 * This is a copy-on-write fault that will
1302 * cause us to revoke access to this page, but
1303 * this page is in the process of being cleaned
1304 * in a clustered pageout. We must wait until
1305 * the cleaning operation completes before
1306 * revoking access to the original page,
1307 * otherwise we might attempt to remove a
1308 * wired mapping.
1309 */
1310#if TRACEFAULTPAGE
0a7de745 1311 dbgTrace(0xBEEF0009, (unsigned int) m, (unsigned int) offset); /* (TEST/DEBUG) */
1c79356b
A
1312#endif
1313 XPR(XPR_VM_FAULT,
1314 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
0a7de745
A
1315 object, offset,
1316 m, 0, 0);
2d21ac55
A
1317 /*
1318 * take an extra ref so that object won't die
1319 */
1320 vm_object_reference_locked(object);
1321
1c79356b 1322 vm_fault_cleanup(object, first_m);
5ba3f43e 1323
1c79356b
A
1324 counter(c_vm_fault_page_block_backoff_kernel++);
1325 vm_object_lock(object);
1326 assert(object->ref_count > 0);
2d21ac55 1327
1c79356b 1328 m = vm_page_lookup(object, offset);
2d21ac55 1329
d9a64523 1330 if (m != VM_PAGE_NULL && m->vmp_cleaning) {
1c79356b 1331 PAGE_ASSERT_WAIT(m, interruptible);
2d21ac55 1332
1c79356b 1333 vm_object_unlock(object);
9bccf70c 1334 wait_result = thread_block(THREAD_CONTINUE_NULL);
1c79356b 1335 vm_object_deallocate(object);
2d21ac55 1336
1c79356b
A
1337 goto backoff;
1338 } else {
1339 vm_object_unlock(object);
2d21ac55 1340
1c79356b 1341 vm_object_deallocate(object);
9bccf70c 1342 thread_interrupt_level(interruptible_state);
2d21ac55 1343
0a7de745 1344 return VM_FAULT_RETRY;
1c79356b
A
1345 }
1346 }
d9a64523 1347 if (type_of_fault == NULL && (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) &&
b0d623f7 1348 !(fault_info != NULL && fault_info->stealth)) {
0a7de745 1349 /*
2d21ac55
A
1350 * If we were passed a non-NULL pointer for
1351 * "type_of_fault", than we came from
1352 * vm_fault... we'll let it deal with
1353 * this condition, since it
d9a64523 1354 * needs to see m->vmp_speculative to correctly
2d21ac55
A
1355 * account the pageins, otherwise...
1356 * take it off the speculative queue, we'll
1357 * let the caller of vm_fault_page deal
1358 * with getting it onto the correct queue
b0d623f7
A
1359 *
1360 * If the caller specified in fault_info that
1361 * it wants a "stealth" fault, we also leave
1362 * the page in the speculative queue.
2d21ac55 1363 */
0a7de745
A
1364 vm_page_lockspin_queues();
1365 if (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) {
39037602 1366 vm_page_queues_remove(m, FALSE);
0a7de745
A
1367 }
1368 vm_page_unlock_queues();
2d21ac55 1369 }
39037602 1370 assert(object == VM_PAGE_OBJECT(m));
1c79356b 1371
39037602 1372 if (object->code_signed) {
2d21ac55
A
1373 /*
1374 * CODE SIGNING:
1375 * We just paged in a page from a signed
1376 * memory object but we don't need to
1377 * validate it now. We'll validate it if
1378 * when it gets mapped into a user address
1379 * space for the first time or when the page
1380 * gets copied to another object as a result
1381 * of a copy-on-write.
1382 */
1c79356b 1383 }
2d21ac55 1384
1c79356b 1385 /*
2d21ac55
A
1386 * We mark the page busy and leave it on
1387 * the pageout queues. If the pageout
1388 * deamon comes across it, then it will
1389 * remove the page from the queue, but not the object
1c79356b 1390 */
1c79356b 1391#if TRACEFAULTPAGE
0a7de745 1392 dbgTrace(0xBEEF000B, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */
1c79356b 1393#endif
1c79356b
A
1394 XPR(XPR_VM_FAULT,
1395 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
0a7de745 1396 object, offset, m, 0, 0);
d9a64523
A
1397 assert(!m->vmp_busy);
1398 assert(!m->vmp_absent);
2d21ac55 1399
d9a64523 1400 m->vmp_busy = TRUE;
1c79356b
A
1401 break;
1402 }
5ba3f43e 1403
1c79356b 1404
2d21ac55
A
1405 /*
1406 * we get here when there is no page present in the object at
1407 * the offset we're interested in... we'll allocate a page
1408 * at this point if the pager associated with
1409 * this object can provide the data or we're the top object...
1410 * object is locked; m == NULL
1411 */
5ba3f43e 1412
39236c6e
A
1413 if (must_be_resident) {
1414 if (fault_type == VM_PROT_NONE &&
1415 object == kernel_object) {
1416 /*
1417 * We've been called from vm_fault_unwire()
1418 * while removing a map entry that was allocated
1419 * with KMA_KOBJECT and KMA_VAONLY. This page
1420 * is not present and there's nothing more to
1421 * do here (nothing to unwire).
1422 */
1423 vm_fault_cleanup(object, first_m);
1424 thread_interrupt_level(interruptible_state);
1425
1426 return VM_FAULT_MEMORY_ERROR;
1427 }
1428
316670eb 1429 goto dont_look_for_page;
39236c6e 1430 }
5ba3f43e
A
1431
1432 /* Don't expect to fault pages into the kernel object. */
1433 assert(object != kernel_object);
1434
39236c6e 1435 data_supply = FALSE;
39236c6e 1436
0a7de745 1437 look_for_page = (object->pager_created && (MUST_ASK_PAGER(object, offset, external_state) == TRUE) && !data_supply);
5ba3f43e 1438
1c79356b 1439#if TRACEFAULTPAGE
0a7de745 1440 dbgTrace(0xBEEF000C, (unsigned int) look_for_page, (unsigned int) object); /* (TEST/DEBUG) */
1c79356b 1441#endif
316670eb 1442 if (!look_for_page && object == first_object && !object->phys_contiguous) {
1c79356b 1443 /*
316670eb 1444 * Allocate a new page for this object/offset pair as a placeholder
1c79356b 1445 */
39037602 1446 m = vm_page_grab_options(grab_options);
1c79356b 1447#if TRACEFAULTPAGE
0a7de745 1448 dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */
1c79356b
A
1449#endif
1450 if (m == VM_PAGE_NULL) {
1451 vm_fault_cleanup(object, first_m);
9bccf70c 1452 thread_interrupt_level(interruptible_state);
2d21ac55 1453
0a7de745 1454 return VM_FAULT_MEMORY_SHORTAGE;
1c79356b 1455 }
316670eb
A
1456
1457 if (fault_info && fault_info->batch_pmap_op == TRUE) {
3e170ce0 1458 vm_page_insert_internal(m, object, offset, VM_KERN_MEMORY_NONE, FALSE, TRUE, TRUE, FALSE, NULL);
316670eb
A
1459 } else {
1460 vm_page_insert(m, object, offset);
1461 }
1c79356b 1462 }
316670eb 1463 if (look_for_page) {
0a7de745
A
1464 kern_return_t rc;
1465 int my_fault_type;
1c79356b
A
1466
1467 /*
1468 * If the memory manager is not ready, we
1469 * cannot make requests.
1470 */
1471 if (!object->pager_ready) {
1472#if TRACEFAULTPAGE
0a7de745 1473 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1c79356b 1474#endif
0a7de745
A
1475 if (m != VM_PAGE_NULL) {
1476 VM_PAGE_FREE(m);
1477 }
2d21ac55 1478
1c79356b 1479 XPR(XPR_VM_FAULT,
0a7de745
A
1480 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1481 object, offset, 0, 0, 0);
2d21ac55
A
1482
1483 /*
1484 * take an extra ref so object won't die
1485 */
1486 vm_object_reference_locked(object);
1c79356b
A
1487 vm_fault_cleanup(object, first_m);
1488 counter(c_vm_fault_page_block_backoff_kernel++);
2d21ac55 1489
1c79356b
A
1490 vm_object_lock(object);
1491 assert(object->ref_count > 0);
2d21ac55 1492
1c79356b 1493 if (!object->pager_ready) {
2d21ac55
A
1494 wait_result = vm_object_assert_wait(object, VM_OBJECT_EVENT_PAGER_READY, interruptible);
1495
1c79356b 1496 vm_object_unlock(object);
0a7de745 1497 if (wait_result == THREAD_WAITING) {
9bccf70c 1498 wait_result = thread_block(THREAD_CONTINUE_NULL);
0a7de745 1499 }
1c79356b 1500 vm_object_deallocate(object);
2d21ac55 1501
1c79356b
A
1502 goto backoff;
1503 } else {
1504 vm_object_unlock(object);
1505 vm_object_deallocate(object);
9bccf70c 1506 thread_interrupt_level(interruptible_state);
1c79356b 1507
0a7de745 1508 return VM_FAULT_RETRY;
0b4e3aa0 1509 }
0b4e3aa0 1510 }
2d21ac55 1511 if (!object->internal && !object->phys_contiguous && object->paging_in_progress > vm_object_pagein_throttle) {
1c79356b 1512 /*
2d21ac55
A
1513 * If there are too many outstanding page
1514 * requests pending on this external object, we
1515 * wait for them to be resolved now.
1c79356b 1516 */
1c79356b 1517#if TRACEFAULTPAGE
0a7de745 1518 dbgTrace(0xBEEF0010, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */
1c79356b 1519#endif
0a7de745 1520 if (m != VM_PAGE_NULL) {
1c79356b 1521 VM_PAGE_FREE(m);
0a7de745 1522 }
1c79356b 1523 /*
2d21ac55 1524 * take an extra ref so object won't die
1c79356b 1525 */
2d21ac55 1526 vm_object_reference_locked(object);
1c79356b 1527
1c79356b 1528 vm_fault_cleanup(object, first_m);
2d21ac55 1529
1c79356b 1530 counter(c_vm_fault_page_block_backoff_kernel++);
2d21ac55 1531
1c79356b
A
1532 vm_object_lock(object);
1533 assert(object->ref_count > 0);
2d21ac55 1534
6d2010ae 1535 if (object->paging_in_progress >= vm_object_pagein_throttle) {
0a7de745 1536 vm_object_assert_wait(object, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS, interruptible);
2d21ac55 1537
1c79356b 1538 vm_object_unlock(object);
9bccf70c 1539 wait_result = thread_block(THREAD_CONTINUE_NULL);
1c79356b 1540 vm_object_deallocate(object);
2d21ac55 1541
1c79356b
A
1542 goto backoff;
1543 } else {
1544 vm_object_unlock(object);
1545 vm_object_deallocate(object);
9bccf70c 1546 thread_interrupt_level(interruptible_state);
2d21ac55 1547
0a7de745 1548 return VM_FAULT_RETRY;
1c79356b
A
1549 }
1550 }
39037602 1551 if (object->internal) {
fe8ab488 1552 int compressed_count_delta;
39236c6e 1553
39037602
A
1554 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);
1555
39236c6e
A
1556 if (m == VM_PAGE_NULL) {
1557 /*
1558 * Allocate a new page for this object/offset pair as a placeholder
1559 */
39037602 1560 m = vm_page_grab_options(grab_options);
39236c6e 1561#if TRACEFAULTPAGE
0a7de745 1562 dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */
39236c6e
A
1563#endif
1564 if (m == VM_PAGE_NULL) {
39236c6e
A
1565 vm_fault_cleanup(object, first_m);
1566 thread_interrupt_level(interruptible_state);
1567
0a7de745 1568 return VM_FAULT_MEMORY_SHORTAGE;
39236c6e
A
1569 }
1570
d9a64523 1571 m->vmp_absent = TRUE;
39236c6e 1572 if (fault_info && fault_info->batch_pmap_op == TRUE) {
3e170ce0 1573 vm_page_insert_internal(m, object, offset, VM_KERN_MEMORY_NONE, FALSE, TRUE, TRUE, FALSE, NULL);
39236c6e
A
1574 } else {
1575 vm_page_insert(m, object, offset);
1576 }
1577 }
d9a64523 1578 assert(m->vmp_busy);
5ba3f43e 1579
d9a64523 1580 m->vmp_absent = TRUE;
39236c6e
A
1581 pager = object->pager;
1582
fe8ab488 1583 assert(object->paging_in_progress > 0);
39236c6e
A
1584 vm_object_unlock(object);
1585
fe8ab488
A
1586 rc = vm_compressor_pager_get(
1587 pager,
1588 offset + object->paging_offset,
39037602 1589 VM_PAGE_GET_PHYS_PAGE(m),
fe8ab488
A
1590 &my_fault_type,
1591 0,
1592 &compressed_count_delta);
39236c6e 1593
04b8595b 1594 if (type_of_fault == NULL) {
0a7de745 1595 int throttle_delay;
04b8595b
A
1596
1597 /*
1598 * we weren't called from vm_fault, so we
1599 * need to apply page creation throttling
1600 * do it before we re-acquire any locks
1601 */
1602 if (my_fault_type == DBG_COMPRESSOR_FAULT) {
1603 if ((throttle_delay = vm_page_throttled(TRUE))) {
1604 VM_DEBUG_EVENT(vmf_compressordelay, VMF_COMPRESSORDELAY, DBG_FUNC_NONE, throttle_delay, 0, 1, 0);
1605 delay(throttle_delay);
1606 }
1607 }
1608 }
39236c6e 1609 vm_object_lock(object);
fe8ab488
A
1610 assert(object->paging_in_progress > 0);
1611
1612 vm_compressor_pager_count(
1613 pager,
1614 compressed_count_delta,
1615 FALSE, /* shared_lock */
1616 object);
39236c6e
A
1617
1618 switch (rc) {
1619 case KERN_SUCCESS:
d9a64523
A
1620 m->vmp_absent = FALSE;
1621 m->vmp_dirty = TRUE;
39037602 1622 if ((object->wimg_bits &
0a7de745 1623 VM_WIMG_MASK) !=
39236c6e
A
1624 VM_WIMG_USE_DEFAULT) {
1625 /*
1626 * If the page is not cacheable,
1627 * we can't let its contents
1628 * linger in the data cache
1629 * after the decompression.
1630 */
1631 pmap_sync_page_attributes_phys(
39037602 1632 VM_PAGE_GET_PHYS_PAGE(m));
fe8ab488 1633 } else {
d9a64523 1634 m->vmp_written_by_kernel = TRUE;
fe8ab488
A
1635 }
1636
1637 /*
1638 * If the object is purgeable, its
1639 * owner's purgeable ledgers have been
1640 * updated in vm_page_insert() but the
1641 * page was also accounted for in a
1642 * "compressed purgeable" ledger, so
1643 * update that now.
1644 */
d9a64523 1645 if (((object->purgable !=
0a7de745
A
1646 VM_PURGABLE_DENY) ||
1647 object->vo_ledger_tag) &&
d9a64523 1648 (object->vo_owner !=
0a7de745 1649 NULL)) {
fe8ab488
A
1650 /*
1651 * One less compressed
d9a64523 1652 * purgeable/tagged page.
fe8ab488 1653 */
d9a64523 1654 vm_object_owner_compressed_update(
fe8ab488
A
1655 object,
1656 -1);
1657 }
1658
39236c6e
A
1659 break;
1660 case KERN_MEMORY_FAILURE:
d9a64523
A
1661 m->vmp_unusual = TRUE;
1662 m->vmp_error = TRUE;
1663 m->vmp_absent = FALSE;
39236c6e
A
1664 break;
1665 case KERN_MEMORY_ERROR:
d9a64523 1666 assert(m->vmp_absent);
39236c6e
A
1667 break;
1668 default:
fe8ab488 1669 panic("vm_fault_page(): unexpected "
0a7de745
A
1670 "error %d from "
1671 "vm_compressor_pager_get()\n",
1672 rc);
39236c6e
A
1673 }
1674 PAGE_WAKEUP_DONE(m);
1675
1676 rc = KERN_SUCCESS;
1677 goto data_requested;
1678 }
1679 my_fault_type = DBG_PAGEIN_FAULT;
5ba3f43e 1680
2d21ac55 1681 if (m != VM_PAGE_NULL) {
316670eb
A
1682 VM_PAGE_FREE(m);
1683 m = VM_PAGE_NULL;
0b4e3aa0 1684 }
1c79356b 1685
1c79356b 1686#if TRACEFAULTPAGE
0a7de745 1687 dbgTrace(0xBEEF0012, (unsigned int) object, (unsigned int) 0); /* (TEST/DEBUG) */
1c79356b 1688#endif
2d21ac55 1689
1c79356b 1690 /*
2d21ac55 1691 * It's possible someone called vm_object_destroy while we weren't
5ba3f43e 1692 * holding the object lock. If that has happened, then bail out
2d21ac55 1693 * here.
1c79356b 1694 */
2d21ac55
A
1695
1696 pager = object->pager;
1697
1698 if (pager == MEMORY_OBJECT_NULL) {
1699 vm_fault_cleanup(object, first_m);
1700 thread_interrupt_level(interruptible_state);
1701 return VM_FAULT_MEMORY_ERROR;
1702 }
1c79356b
A
1703
1704 /*
2d21ac55
A
1705 * We have an absent page in place for the faulting offset,
1706 * so we can release the object lock.
1c79356b
A
1707 */
1708
d9a64523 1709 if (object->object_is_shared_cache) {
5ba3f43e
A
1710 set_thread_rwlock_boost();
1711 }
1712
2d21ac55 1713 vm_object_unlock(object);
1c79356b
A
1714
1715 /*
2d21ac55
A
1716 * If this object uses a copy_call strategy,
1717 * and we are interested in a copy of this object
1718 * (having gotten here only by following a
1719 * shadow chain), then tell the memory manager
1720 * via a flag added to the desired_access
1721 * parameter, so that it can detect a race
1722 * between our walking down the shadow chain
1723 * and its pushing pages up into a copy of
1724 * the object that it manages.
1c79356b 1725 */
0a7de745 1726 if (object->copy_strategy == MEMORY_OBJECT_COPY_CALL && object != first_object) {
1c79356b 1727 wants_copy_flag = VM_PROT_WANTS_COPY;
0a7de745 1728 } else {
1c79356b 1729 wants_copy_flag = VM_PROT_NONE;
0a7de745 1730 }
1c79356b
A
1731
1732 XPR(XPR_VM_FAULT,
1733 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
0a7de745
A
1734 object, offset, m,
1735 access_required | wants_copy_flag, 0);
1c79356b 1736
316670eb
A
1737 if (object->copy == first_object) {
1738 /*
1739 * if we issue the memory_object_data_request in
1740 * this state, we are subject to a deadlock with
1741 * the underlying filesystem if it is trying to
1742 * shrink the file resulting in a push of pages
1743 * into the copy object... that push will stall
1744 * on the placeholder page, and if the pushing thread
1745 * is holding a lock that is required on the pagein
1746 * path (such as a truncate lock), we'll deadlock...
1747 * to avoid this potential deadlock, we throw away
1748 * our placeholder page before calling memory_object_data_request
1749 * and force this thread to retry the vm_fault_page after
1750 * we have issued the I/O. the second time through this path
1751 * we will find the page already in the cache (presumably still
1752 * busy waiting for the I/O to complete) and then complete
1753 * the fault w/o having to go through memory_object_data_request again
1754 */
1755 assert(first_m != VM_PAGE_NULL);
39037602 1756 assert(VM_PAGE_OBJECT(first_m) == first_object);
5ba3f43e 1757
316670eb
A
1758 vm_object_lock(first_object);
1759 VM_PAGE_FREE(first_m);
1760 vm_object_paging_end(first_object);
1761 vm_object_unlock(first_object);
1762
1763 first_m = VM_PAGE_NULL;
1764 force_fault_retry = TRUE;
1765
1766 vm_fault_page_forced_retry++;
1767 }
1768
1769 if (data_already_requested == TRUE) {
1770 orig_behavior = fault_info->behavior;
1771 orig_cluster_size = fault_info->cluster_size;
1772
1773 fault_info->behavior = VM_BEHAVIOR_RANDOM;
1774 fault_info->cluster_size = PAGE_SIZE;
1775 }
2d21ac55
A
1776 /*
1777 * Call the memory manager to retrieve the data.
1778 */
1779 rc = memory_object_data_request(
1780 pager,
1781 offset + object->paging_offset,
1782 PAGE_SIZE,
1783 access_required | wants_copy_flag,
1784 (memory_object_fault_info_t)fault_info);
1c79356b 1785
316670eb
A
1786 if (data_already_requested == TRUE) {
1787 fault_info->behavior = orig_behavior;
1788 fault_info->cluster_size = orig_cluster_size;
0a7de745 1789 } else {
316670eb 1790 data_already_requested = TRUE;
0a7de745 1791 }
316670eb 1792
fe8ab488 1793 DTRACE_VM2(maj_fault, int, 1, (uint64_t *), NULL);
1c79356b 1794#if TRACEFAULTPAGE
0a7de745 1795 dbgTrace(0xBEEF0013, (unsigned int) object, (unsigned int) rc); /* (TEST/DEBUG) */
1c79356b 1796#endif
2d21ac55
A
1797 vm_object_lock(object);
1798
d9a64523 1799 if (object->object_is_shared_cache) {
5ba3f43e
A
1800 clear_thread_rwlock_boost();
1801 }
1802
0a7de745 1803data_requested:
1c79356b 1804 if (rc != KERN_SUCCESS) {
1c79356b 1805 vm_fault_cleanup(object, first_m);
9bccf70c 1806 thread_interrupt_level(interruptible_state);
2d21ac55 1807
0a7de745
A
1808 return (rc == MACH_SEND_INTERRUPTED) ?
1809 VM_FAULT_INTERRUPTED :
1810 VM_FAULT_MEMORY_ERROR;
b0d623f7
A
1811 } else {
1812 clock_sec_t tv_sec;
1813 clock_usec_t tv_usec;
39236c6e
A
1814
1815 if (my_fault_type == DBG_PAGEIN_FAULT) {
1816 clock_get_system_microtime(&tv_sec, &tv_usec);
1817 current_thread()->t_page_creation_time = tv_sec;
1818 current_thread()->t_page_creation_count = 0;
1819 }
1c79356b 1820 }
6d2010ae 1821 if ((interruptible != THREAD_UNINT) && (current_thread()->sched_flags & TH_SFLAG_ABORT)) {
1c79356b 1822 vm_fault_cleanup(object, first_m);
9bccf70c 1823 thread_interrupt_level(interruptible_state);
2d21ac55 1824
0a7de745 1825 return VM_FAULT_INTERRUPTED;
1c79356b 1826 }
316670eb 1827 if (force_fault_retry == TRUE) {
316670eb
A
1828 vm_fault_cleanup(object, first_m);
1829 thread_interrupt_level(interruptible_state);
1830
0a7de745 1831 return VM_FAULT_RETRY;
316670eb 1832 }
2d21ac55 1833 if (m == VM_PAGE_NULL && object->phys_contiguous) {
91447636
A
1834 /*
1835 * No page here means that the object we
5ba3f43e 1836 * initially looked up was "physically
91447636
A
1837 * contiguous" (i.e. device memory). However,
1838 * with Virtual VRAM, the object might not
1839 * be backed by that device memory anymore,
1840 * so we're done here only if the object is
1841 * still "phys_contiguous".
1842 * Otherwise, if the object is no longer
1843 * "phys_contiguous", we need to retry the
1844 * page fault against the object's new backing
1845 * store (different memory object).
1846 */
0a7de745 1847phys_contig_object:
b0d623f7 1848 goto done;
91447636 1849 }
2d21ac55
A
1850 /*
1851 * potentially a pagein fault
1852 * if we make it through the state checks
1853 * above, than we'll count it as such
1854 */
39236c6e 1855 my_fault = my_fault_type;
91447636
A
1856
1857 /*
1858 * Retry with same object/offset, since new data may
1859 * be in a different page (i.e., m is meaningless at
1860 * this point).
1861 */
1c79356b
A
1862 continue;
1863 }
316670eb 1864dont_look_for_page:
1c79356b 1865 /*
5ba3f43e 1866 * We get here if the object has no pager, or an existence map
2d21ac55
A
1867 * exists and indicates the page isn't present on the pager
1868 * or we're unwiring a page. If a pager exists, but there
d9a64523 1869 * is no existence map, then the m->vmp_absent case above handles
2d21ac55 1870 * the ZF case when the pager can't provide the page
1c79356b
A
1871 */
1872#if TRACEFAULTPAGE
0a7de745 1873 dbgTrace(0xBEEF0014, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */
1c79356b 1874#endif
0a7de745 1875 if (object == first_object) {
1c79356b 1876 first_m = m;
0a7de745 1877 } else {
1c79356b 1878 assert(m == VM_PAGE_NULL);
0a7de745 1879 }
1c79356b
A
1880
1881 XPR(XPR_VM_FAULT,
1882 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
0a7de745
A
1883 object, offset, m,
1884 object->shadow, 0);
2d21ac55 1885
1c79356b 1886 next_object = object->shadow;
2d21ac55 1887
1c79356b 1888 if (next_object == VM_OBJECT_NULL) {
1c79356b 1889 /*
2d21ac55
A
1890 * we've hit the bottom of the shadown chain,
1891 * fill the page in the top object with zeros.
1c79356b 1892 */
2d21ac55 1893 assert(!must_be_resident);
1c79356b
A
1894
1895 if (object != first_object) {
1896 vm_object_paging_end(object);
1897 vm_object_unlock(object);
1898
1899 object = first_object;
1900 offset = first_offset;
1901 vm_object_lock(object);
1902 }
1c79356b 1903 m = first_m;
39037602 1904 assert(VM_PAGE_OBJECT(m) == object);
1c79356b
A
1905 first_m = VM_PAGE_NULL;
1906
55e303ae 1907 /*
2d21ac55
A
1908 * check for any conditions that prevent
1909 * us from creating a new zero-fill page
5ba3f43e 1910 * vm_fault_check will do all of the
2d21ac55
A
1911 * fault cleanup in the case of an error condition
1912 * including resetting the thread_interrupt_level
55e303ae 1913 */
04b8595b 1914 error = vm_fault_check(object, m, first_m, interruptible_state, (type_of_fault == NULL) ? TRUE : FALSE);
55e303ae 1915
0a7de745
A
1916 if (error != VM_FAULT_SUCCESS) {
1917 return error;
1918 }
55e303ae 1919
2d21ac55 1920 if (m == VM_PAGE_NULL) {
39037602 1921 m = vm_page_grab_options(grab_options);
1c79356b 1922
2d21ac55
A
1923 if (m == VM_PAGE_NULL) {
1924 vm_fault_cleanup(object, VM_PAGE_NULL);
1925 thread_interrupt_level(interruptible_state);
55e303ae 1926
0a7de745 1927 return VM_FAULT_MEMORY_SHORTAGE;
2d21ac55
A
1928 }
1929 vm_page_insert(m, object, offset);
0b4e3aa0 1930 }
0a7de745 1931 if (fault_info->mark_zf_absent && no_zero_fill == TRUE) {
d9a64523 1932 m->vmp_absent = TRUE;
0a7de745 1933 }
fe8ab488
A
1934
1935 my_fault = vm_fault_zero_page(m, no_zero_fill);
1936
1c79356b 1937 break;
2d21ac55 1938 } else {
0a7de745 1939 /*
2d21ac55
A
1940 * Move on to the next object. Lock the next
1941 * object before unlocking the current one.
1942 */
0a7de745 1943 if ((object != first_object) || must_be_resident) {
1c79356b 1944 vm_object_paging_end(object);
0a7de745 1945 }
2d21ac55 1946
6d2010ae
A
1947 offset += object->vo_shadow_offset;
1948 fault_info->lo_offset += object->vo_shadow_offset;
1949 fault_info->hi_offset += object->vo_shadow_offset;
1c79356b 1950 access_required = VM_PROT_READ;
2d21ac55 1951
1c79356b
A
1952 vm_object_lock(next_object);
1953 vm_object_unlock(object);
2d21ac55 1954
1c79356b
A
1955 object = next_object;
1956 vm_object_paging_begin(object);
1957 }
1958 }
1959
1960 /*
1961 * PAGE HAS BEEN FOUND.
1962 *
1963 * This page (m) is:
1964 * busy, so that we can play with it;
1965 * not absent, so that nobody else will fill it;
1966 * possibly eligible for pageout;
1967 *
1968 * The top-level page (first_m) is:
1969 * VM_PAGE_NULL if the page was found in the
1970 * top-level object;
1971 * busy, not absent, and ineligible for pageout.
1972 *
1973 * The current object (object) is locked. A paging
1974 * reference is held for the current and top-level
1975 * objects.
1976 */
1977
1978#if TRACEFAULTPAGE
0a7de745 1979 dbgTrace(0xBEEF0015, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */
1c79356b 1980#endif
0a7de745 1981#if EXTRA_ASSERTIONS
d9a64523 1982 assert(m->vmp_busy && !m->vmp_absent);
b0d623f7 1983 assert((first_m == VM_PAGE_NULL) ||
0a7de745
A
1984 (first_m->vmp_busy && !first_m->vmp_absent &&
1985 !first_m->vmp_active && !first_m->vmp_inactive && !first_m->vmp_secluded));
1986#endif /* EXTRA_ASSERTIONS */
1c79356b
A
1987
1988 XPR(XPR_VM_FAULT,
2d21ac55 1989 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
0a7de745
A
1990 object, offset, m,
1991 first_object, first_m);
2d21ac55 1992
1c79356b 1993 /*
2d21ac55
A
1994 * If the page is being written, but isn't
1995 * already owned by the top-level object,
1996 * we have to copy it into a new page owned
1997 * by the top-level object.
1c79356b 1998 */
b0d623f7 1999 if (object != first_object) {
1c79356b 2000#if TRACEFAULTPAGE
0a7de745 2001 dbgTrace(0xBEEF0016, (unsigned int) object, (unsigned int) fault_type); /* (TEST/DEBUG) */
1c79356b 2002#endif
0a7de745 2003 if (fault_type & VM_PROT_WRITE) {
1c79356b
A
2004 vm_page_t copy_m;
2005
2d21ac55
A
2006 /*
2007 * We only really need to copy if we
2008 * want to write it.
2009 */
1c79356b
A
2010 assert(!must_be_resident);
2011
2012 /*
2d21ac55
A
2013 * If we try to collapse first_object at this
2014 * point, we may deadlock when we try to get
2015 * the lock on an intermediate object (since we
2016 * have the bottom object locked). We can't
2017 * unlock the bottom object, because the page
2018 * we found may move (by collapse) if we do.
1c79356b 2019 *
2d21ac55
A
2020 * Instead, we first copy the page. Then, when
2021 * we have no more use for the bottom object,
2022 * we unlock it and try to collapse.
1c79356b 2023 *
2d21ac55
A
2024 * Note that we copy the page even if we didn't
2025 * need to... that's the breaks.
1c79356b
A
2026 */
2027
2028 /*
2d21ac55 2029 * Allocate a page for the copy
1c79356b 2030 */
39037602 2031 copy_m = vm_page_grab_options(grab_options);
2d21ac55 2032
1c79356b
A
2033 if (copy_m == VM_PAGE_NULL) {
2034 RELEASE_PAGE(m);
2d21ac55 2035
1c79356b 2036 vm_fault_cleanup(object, first_m);
9bccf70c 2037 thread_interrupt_level(interruptible_state);
1c79356b 2038
0a7de745 2039 return VM_FAULT_MEMORY_SHORTAGE;
2d21ac55 2040 }
1c79356b
A
2041 XPR(XPR_VM_FAULT,
2042 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
0a7de745
A
2043 object, offset,
2044 m, copy_m, 0);
2d21ac55 2045
1c79356b
A
2046 vm_page_copy(m, copy_m);
2047
2048 /*
2d21ac55
A
2049 * If another map is truly sharing this
2050 * page with us, we have to flush all
2051 * uses of the original page, since we
2052 * can't distinguish those which want the
2053 * original from those which need the
2054 * new copy.
1c79356b 2055 *
2d21ac55
A
2056 * XXXO If we know that only one map has
2057 * access to this page, then we could
2058 * avoid the pmap_disconnect() call.
1c79356b 2059 */
0a7de745
A
2060 if (m->vmp_pmapped) {
2061 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
2062 }
1c79356b 2063
d9a64523 2064 if (m->vmp_clustered) {
fe8ab488
A
2065 VM_PAGE_COUNT_AS_PAGEIN(m);
2066 VM_PAGE_CONSUME_CLUSTERED(m);
2067 }
d9a64523 2068 assert(!m->vmp_cleaning);
1c79356b
A
2069
2070 /*
2d21ac55 2071 * We no longer need the old page or object.
1c79356b 2072 */
39236c6e
A
2073 RELEASE_PAGE(m);
2074
39037602
A
2075 /*
2076 * This check helps with marking the object as having a sequential pattern
2077 * Normally we'll miss doing this below because this fault is about COW to
2078 * the first_object i.e. bring page in from disk, push to object above but
2079 * don't update the file object's sequential pattern.
2080 */
0a7de745 2081 if (object->internal == FALSE) {
39037602
A
2082 vm_fault_is_sequential(object, offset, fault_info->behavior);
2083 }
2084
1c79356b
A
2085 vm_object_paging_end(object);
2086 vm_object_unlock(object);
2087
2d21ac55
A
2088 my_fault = DBG_COW_FAULT;
2089 VM_STAT_INCR(cow_faults);
2090 DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL);
1c79356b 2091 current_task()->cow_faults++;
2d21ac55 2092
1c79356b
A
2093 object = first_object;
2094 offset = first_offset;
2095
2096 vm_object_lock(object);
2d21ac55
A
2097 /*
2098 * get rid of the place holder
2099 * page that we soldered in earlier
2100 */
1c79356b
A
2101 VM_PAGE_FREE(first_m);
2102 first_m = VM_PAGE_NULL;
5ba3f43e 2103
2d21ac55
A
2104 /*
2105 * and replace it with the
2106 * page we just copied into
2107 */
d9a64523 2108 assert(copy_m->vmp_busy);
1c79356b 2109 vm_page_insert(copy_m, object, offset);
316670eb 2110 SET_PAGE_DIRTY(copy_m, TRUE);
1c79356b 2111
2d21ac55 2112 m = copy_m;
1c79356b 2113 /*
2d21ac55
A
2114 * Now that we've gotten the copy out of the
2115 * way, let's try to collapse the top object.
2116 * But we have to play ugly games with
2117 * paging_in_progress to do that...
5ba3f43e
A
2118 */
2119 vm_object_paging_end(object);
0c530ab8 2120 vm_object_collapse(object, offset, TRUE);
1c79356b 2121 vm_object_paging_begin(object);
0a7de745
A
2122 } else {
2123 *protection &= (~VM_PROT_WRITE);
2124 }
1c79356b 2125 }
1c79356b 2126 /*
2d21ac55
A
2127 * Now check whether the page needs to be pushed into the
2128 * copy object. The use of asymmetric copy on write for
2129 * shared temporary objects means that we may do two copies to
2130 * satisfy the fault; one above to get the page from a
2131 * shadowed object, and one here to push it into the copy.
1c79356b 2132 */
2d21ac55 2133 try_failed_count = 0;
1c79356b 2134
b0d623f7 2135 while ((copy_object = first_object->copy) != VM_OBJECT_NULL) {
0a7de745
A
2136 vm_object_offset_t copy_offset;
2137 vm_page_t copy_m;
1c79356b
A
2138
2139#if TRACEFAULTPAGE
0a7de745 2140 dbgTrace(0xBEEF0017, (unsigned int) copy_object, (unsigned int) fault_type); /* (TEST/DEBUG) */
1c79356b
A
2141#endif
2142 /*
2d21ac55
A
2143 * If the page is being written, but hasn't been
2144 * copied to the copy-object, we have to copy it there.
1c79356b 2145 */
1c79356b
A
2146 if ((fault_type & VM_PROT_WRITE) == 0) {
2147 *protection &= ~VM_PROT_WRITE;
2148 break;
2149 }
2150
2151 /*
2d21ac55
A
2152 * If the page was guaranteed to be resident,
2153 * we must have already performed the copy.
1c79356b 2154 */
0a7de745 2155 if (must_be_resident) {
1c79356b 2156 break;
0a7de745 2157 }
1c79356b
A
2158
2159 /*
2d21ac55 2160 * Try to get the lock on the copy_object.
1c79356b
A
2161 */
2162 if (!vm_object_lock_try(copy_object)) {
2d21ac55
A
2163 vm_object_unlock(object);
2164 try_failed_count++;
1c79356b 2165
0a7de745 2166 mutex_pause(try_failed_count); /* wait a bit */
1c79356b 2167 vm_object_lock(object);
2d21ac55 2168
1c79356b
A
2169 continue;
2170 }
2d21ac55 2171 try_failed_count = 0;
1c79356b
A
2172
2173 /*
2d21ac55
A
2174 * Make another reference to the copy-object,
2175 * to keep it from disappearing during the
2176 * copy.
1c79356b 2177 */
2d21ac55 2178 vm_object_reference_locked(copy_object);
1c79356b
A
2179
2180 /*
2d21ac55 2181 * Does the page exist in the copy?
1c79356b 2182 */
6d2010ae 2183 copy_offset = first_offset - copy_object->vo_shadow_offset;
2d21ac55 2184
0a7de745 2185 if (copy_object->vo_size <= copy_offset) {
1c79356b
A
2186 /*
2187 * Copy object doesn't cover this page -- do nothing.
2188 */
2189 ;
0a7de745 2190 } else if ((copy_m = vm_page_lookup(copy_object, copy_offset)) != VM_PAGE_NULL) {
2d21ac55
A
2191 /*
2192 * Page currently exists in the copy object
2193 */
d9a64523 2194 if (copy_m->vmp_busy) {
1c79356b 2195 /*
2d21ac55
A
2196 * If the page is being brought
2197 * in, wait for it and then retry.
1c79356b
A
2198 */
2199 RELEASE_PAGE(m);
2d21ac55
A
2200
2201 /*
2202 * take an extra ref so object won't die
2203 */
2204 vm_object_reference_locked(copy_object);
1c79356b
A
2205 vm_object_unlock(copy_object);
2206 vm_fault_cleanup(object, first_m);
2207 counter(c_vm_fault_page_block_backoff_kernel++);
2d21ac55 2208
1c79356b
A
2209 vm_object_lock(copy_object);
2210 assert(copy_object->ref_count > 0);
2211 VM_OBJ_RES_DECR(copy_object);
2d21ac55 2212 vm_object_lock_assert_exclusive(copy_object);
1c79356b
A
2213 copy_object->ref_count--;
2214 assert(copy_object->ref_count > 0);
2215 copy_m = vm_page_lookup(copy_object, copy_offset);
5ba3f43e 2216
d9a64523 2217 if (copy_m != VM_PAGE_NULL && copy_m->vmp_busy) {
1c79356b 2218 PAGE_ASSERT_WAIT(copy_m, interruptible);
2d21ac55 2219
1c79356b 2220 vm_object_unlock(copy_object);
9bccf70c 2221 wait_result = thread_block(THREAD_CONTINUE_NULL);
1c79356b 2222 vm_object_deallocate(copy_object);
2d21ac55 2223
1c79356b
A
2224 goto backoff;
2225 } else {
2226 vm_object_unlock(copy_object);
2227 vm_object_deallocate(copy_object);
9bccf70c 2228 thread_interrupt_level(interruptible_state);
2d21ac55 2229
0a7de745 2230 return VM_FAULT_RETRY;
1c79356b
A
2231 }
2232 }
0a7de745 2233 } else if (!PAGED_OUT(copy_object, copy_offset)) {
1c79356b
A
2234 /*
2235 * If PAGED_OUT is TRUE, then the page used to exist
2236 * in the copy-object, and has already been paged out.
2237 * We don't need to repeat this. If PAGED_OUT is
2238 * FALSE, then either we don't know (!pager_created,
2239 * for example) or it hasn't been paged out.
2240 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2241 * We must copy the page to the copy object.
d9a64523 2242 *
2d21ac55 2243 * Allocate a page for the copy
1c79356b
A
2244 */
2245 copy_m = vm_page_alloc(copy_object, copy_offset);
2d21ac55 2246
1c79356b
A
2247 if (copy_m == VM_PAGE_NULL) {
2248 RELEASE_PAGE(m);
2d21ac55 2249
1c79356b 2250 VM_OBJ_RES_DECR(copy_object);
2d21ac55 2251 vm_object_lock_assert_exclusive(copy_object);
1c79356b
A
2252 copy_object->ref_count--;
2253 assert(copy_object->ref_count > 0);
2d21ac55 2254
1c79356b
A
2255 vm_object_unlock(copy_object);
2256 vm_fault_cleanup(object, first_m);
9bccf70c 2257 thread_interrupt_level(interruptible_state);
1c79356b 2258
0a7de745 2259 return VM_FAULT_MEMORY_SHORTAGE;
2d21ac55 2260 }
1c79356b 2261 /*
2d21ac55 2262 * Must copy page into copy-object.
1c79356b 2263 */
1c79356b 2264 vm_page_copy(m, copy_m);
5ba3f43e 2265
1c79356b 2266 /*
2d21ac55
A
2267 * If the old page was in use by any users
2268 * of the copy-object, it must be removed
2269 * from all pmaps. (We can't know which
2270 * pmaps use it.)
1c79356b 2271 */
0a7de745
A
2272 if (m->vmp_pmapped) {
2273 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
2274 }
1c79356b 2275
d9a64523 2276 if (m->vmp_clustered) {
fe8ab488
A
2277 VM_PAGE_COUNT_AS_PAGEIN(m);
2278 VM_PAGE_CONSUME_CLUSTERED(m);
2279 }
1c79356b 2280 /*
2d21ac55
A
2281 * If there's a pager, then immediately
2282 * page out this page, using the "initialize"
2283 * option. Else, we use the copy.
1c79356b 2284 */
0a7de745 2285 if ((!copy_object->pager_ready)
39236c6e 2286 || VM_COMPRESSOR_PAGER_STATE_GET(copy_object, copy_offset) == VM_EXTERNAL_STATE_ABSENT
0a7de745 2287 ) {
2d21ac55 2288 vm_page_lockspin_queues();
d9a64523 2289 assert(!m->vmp_cleaning);
1c79356b
A
2290 vm_page_activate(copy_m);
2291 vm_page_unlock_queues();
2d21ac55 2292
316670eb 2293 SET_PAGE_DIRTY(copy_m, TRUE);
1c79356b 2294 PAGE_WAKEUP_DONE(copy_m);
316670eb 2295 } else {
d9a64523
A
2296 assert(copy_m->vmp_busy == TRUE);
2297 assert(!m->vmp_cleaning);
1c79356b
A
2298
2299 /*
2d21ac55 2300 * dirty is protected by the object lock
1c79356b 2301 */
316670eb 2302 SET_PAGE_DIRTY(copy_m, TRUE);
1c79356b 2303
2d21ac55
A
2304 /*
2305 * The page is already ready for pageout:
2306 * not on pageout queues and busy.
2307 * Unlock everything except the
2308 * copy_object itself.
2309 */
1c79356b
A
2310 vm_object_unlock(object);
2311
2312 /*
2d21ac55
A
2313 * Write the page to the copy-object,
2314 * flushing it from the kernel.
1c79356b 2315 */
1c79356b
A
2316 vm_pageout_initialize_page(copy_m);
2317
2318 /*
2d21ac55
A
2319 * Since the pageout may have
2320 * temporarily dropped the
2321 * copy_object's lock, we
2322 * check whether we'll have
2323 * to deallocate the hard way.
1c79356b 2324 */
2d21ac55 2325 if ((copy_object->shadow != object) || (copy_object->ref_count == 1)) {
1c79356b
A
2326 vm_object_unlock(copy_object);
2327 vm_object_deallocate(copy_object);
2328 vm_object_lock(object);
2d21ac55 2329
1c79356b
A
2330 continue;
2331 }
1c79356b 2332 /*
2d21ac55
A
2333 * Pick back up the old object's
2334 * lock. [It is safe to do so,
2335 * since it must be deeper in the
2336 * object tree.]
1c79356b 2337 */
1c79356b
A
2338 vm_object_lock(object);
2339 }
316670eb 2340
1c79356b 2341 /*
2d21ac55
A
2342 * Because we're pushing a page upward
2343 * in the object tree, we must restart
2344 * any faults that are waiting here.
2345 * [Note that this is an expansion of
2346 * PAGE_WAKEUP that uses the THREAD_RESTART
2347 * wait result]. Can't turn off the page's
2348 * busy bit because we're not done with it.
1c79356b 2349 */
d9a64523
A
2350 if (m->vmp_wanted) {
2351 m->vmp_wanted = FALSE;
2d21ac55 2352 thread_wakeup_with_result((event_t) m, THREAD_RESTART);
1c79356b
A
2353 }
2354 }
1c79356b 2355 /*
2d21ac55
A
2356 * The reference count on copy_object must be
2357 * at least 2: one for our extra reference,
2358 * and at least one from the outside world
2359 * (we checked that when we last locked
2360 * copy_object).
1c79356b 2361 */
2d21ac55 2362 vm_object_lock_assert_exclusive(copy_object);
1c79356b
A
2363 copy_object->ref_count--;
2364 assert(copy_object->ref_count > 0);
2d21ac55 2365
5ba3f43e 2366 VM_OBJ_RES_DECR(copy_object);
1c79356b
A
2367 vm_object_unlock(copy_object);
2368
2369 break;
2370 }
b0d623f7
A
2371
2372done:
1c79356b
A
2373 *result_page = m;
2374 *top_page = first_m;
2375
2376 XPR(XPR_VM_FAULT,
0a7de745
A
2377 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2378 object, offset, m, first_m, 0);
1c79356b 2379
2d21ac55 2380 if (m != VM_PAGE_NULL) {
39037602
A
2381 assert(VM_PAGE_OBJECT(m) == object);
2382
b0d623f7 2383 retval = VM_FAULT_SUCCESS;
fe8ab488 2384
2d21ac55 2385 if (my_fault == DBG_PAGEIN_FAULT) {
fe8ab488 2386 VM_PAGE_COUNT_AS_PAGEIN(m);
2d21ac55 2387
0a7de745 2388 if (object->internal) {
b0d623f7 2389 my_fault = DBG_PAGEIND_FAULT;
0a7de745 2390 } else {
b0d623f7 2391 my_fault = DBG_PAGEINV_FAULT;
0a7de745 2392 }
2d21ac55 2393
0a7de745 2394 /*
2d21ac55
A
2395 * evaluate access pattern and update state
2396 * vm_fault_deactivate_behind depends on the
2397 * state being up to date
2398 */
0a7de745 2399 vm_fault_is_sequential(object, offset, fault_info->behavior);
d9a64523 2400 vm_fault_deactivate_behind(object, offset, fault_info->behavior);
d9a64523 2401 } else if (type_of_fault == NULL && my_fault == DBG_CACHE_HIT_FAULT) {
0a7de745 2402 /*
d9a64523
A
2403 * we weren't called from vm_fault, so handle the
2404 * accounting here for hits in the cache
2405 */
0a7de745
A
2406 if (m->vmp_clustered) {
2407 VM_PAGE_COUNT_AS_PAGEIN(m);
d9a64523
A
2408 VM_PAGE_CONSUME_CLUSTERED(m);
2409 }
0a7de745 2410 vm_fault_is_sequential(object, offset, fault_info->behavior);
2d21ac55 2411 vm_fault_deactivate_behind(object, offset, fault_info->behavior);
39236c6e 2412 } else if (my_fault == DBG_COMPRESSOR_FAULT || my_fault == DBG_COMPRESSOR_SWAPIN_FAULT) {
39236c6e 2413 VM_STAT_INCR(decompressions);
2d21ac55 2414 }
0a7de745
A
2415 if (type_of_fault) {
2416 *type_of_fault = my_fault;
2417 }
b0d623f7
A
2418 } else {
2419 retval = VM_FAULT_SUCCESS_NO_VM_PAGE;
2420 assert(first_m == VM_PAGE_NULL);
2421 assert(object == first_object);
2422 }
2d21ac55 2423
55e303ae
A
2424 thread_interrupt_level(interruptible_state);
2425
1c79356b 2426#if TRACEFAULTPAGE
0a7de745 2427 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS, 0); /* (TEST/DEBUG) */
1c79356b 2428#endif
b0d623f7 2429 return retval;
1c79356b 2430
2d21ac55 2431backoff:
9bccf70c 2432 thread_interrupt_level(interruptible_state);
2d21ac55 2433
0a7de745
A
2434 if (wait_result == THREAD_INTERRUPTED) {
2435 return VM_FAULT_INTERRUPTED;
2436 }
2437 return VM_FAULT_RETRY;
1c79356b 2438
0a7de745 2439#undef RELEASE_PAGE
1c79356b
A
2440}
2441
2d21ac55
A
2442
2443
593a1d5f
A
2444/*
2445 * CODE SIGNING:
2446 * When soft faulting a page, we have to validate the page if:
2447 * 1. the page is being mapped in user space
2448 * 2. the page hasn't already been found to be "tainted"
2449 * 3. the page belongs to a code-signed object
2450 * 4. the page has not been validated yet or has been mapped for write.
2451 */
0a7de745
A
2452#define VM_FAULT_NEED_CS_VALIDATION(pmap, page, page_obj) \
2453 ((pmap) != kernel_pmap /*1*/ && \
2454 !(page)->vmp_cs_tainted /*2*/ && \
2455 (page_obj)->code_signed /*3*/ && \
2456 (!(page)->vmp_cs_validated || (page)->vmp_wpmapped /*4*/ ))
593a1d5f
A
2457
2458
55e303ae 2459/*
2d21ac55 2460 * page queue lock must NOT be held
d9a64523 2461 * m->vmp_object must be locked
2d21ac55 2462 *
d9a64523 2463 * NOTE: m->vmp_object could be locked "shared" only if we are called
2d21ac55
A
2464 * from vm_fault() as part of a soft fault. If so, we must be
2465 * careful not to modify the VM object in any way that is not
2466 * legal under a shared lock...
55e303ae 2467 */
39037602 2468extern int panic_on_cs_killed;
15129b1c
A
2469extern int proc_selfpid(void);
2470extern char *proc_name_address(void *p);
2d21ac55
A
2471unsigned long cs_enter_tainted_rejected = 0;
2472unsigned long cs_enter_tainted_accepted = 0;
2473kern_return_t
2474vm_fault_enter(vm_page_t m,
0a7de745
A
2475 pmap_t pmap,
2476 vm_map_offset_t vaddr,
2477 vm_prot_t prot,
2478 vm_prot_t caller_prot,
2479 boolean_t wired,
2480 boolean_t change_wiring,
2481 vm_tag_t wire_tag,
2482 vm_object_fault_info_t fault_info,
2483 boolean_t *need_retry,
2484 int *type_of_fault)
55e303ae 2485{
0a7de745
A
2486 kern_return_t kr, pe_result;
2487 boolean_t previously_pmapped = m->vmp_pmapped;
2488 boolean_t must_disconnect = 0;
2489 boolean_t map_is_switched, map_is_switch_protected;
2490 boolean_t cs_violation;
2491 int cs_enforcement_enabled;
3e170ce0 2492 vm_prot_t fault_type;
0a7de745
A
2493 vm_object_t object;
2494 boolean_t no_cache = fault_info->no_cache;
2495 boolean_t cs_bypass = fault_info->cs_bypass;
2496 int pmap_options = fault_info->pmap_options;
5ba3f43e 2497
3e170ce0 2498 fault_type = change_wiring ? VM_PROT_NONE : caller_prot;
39037602 2499 object = VM_PAGE_OBJECT(m);
3e170ce0 2500
39037602 2501 vm_object_lock_assert_held(object);
5ba3f43e
A
2502
2503#if KASAN
2504 if (pmap == kernel_pmap) {
2505 kasan_notify_address(vaddr, PAGE_SIZE);
2506 }
2507#endif
2508
39037602 2509 LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED);
2d21ac55 2510
39037602 2511 if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) {
d9a64523 2512 assert(m->vmp_fictitious);
2d21ac55
A
2513 return KERN_SUCCESS;
2514 }
2515
6d2010ae 2516 if (*type_of_fault == DBG_ZERO_FILL_FAULT) {
39037602 2517 vm_object_lock_assert_exclusive(object);
d9a64523 2518 } else if ((fault_type & VM_PROT_WRITE) == 0 &&
0a7de745 2519 (!m->vmp_wpmapped
d9a64523 2520#if VM_OBJECT_ACCESS_TRACKING
0a7de745 2521 || object->access_tracking
d9a64523 2522#endif /* VM_OBJECT_ACCESS_TRACKING */
0a7de745 2523 )) {
2d21ac55 2524 /*
6d2010ae
A
2525 * This is not a "write" fault, so we
2526 * might not have taken the object lock
2527 * exclusively and we might not be able
2528 * to update the "wpmapped" bit in
2529 * vm_fault_enter().
2530 * Let's just grant read access to
2531 * the page for now and we'll
2532 * soft-fault again if we need write
2533 * access later...
2d21ac55 2534 */
5ba3f43e
A
2535
2536 /* This had better not be a JIT page. */
2537 if (!pmap_has_prot_policy(prot)) {
2538 prot &= ~VM_PROT_WRITE;
2539 } else {
2540 assert(cs_bypass);
2541 }
fe8ab488 2542 }
d9a64523 2543 if (m->vmp_pmapped == FALSE) {
d9a64523 2544 if (m->vmp_clustered) {
fe8ab488
A
2545 if (*type_of_fault == DBG_CACHE_HIT_FAULT) {
2546 /*
2547 * found it in the cache, but this
d9a64523 2548 * is the first fault-in of the page (m->vmp_pmapped == FALSE)
fe8ab488
A
2549 * so it must have come in as part of
2550 * a cluster... account 1 pagein against it
2551 */
0a7de745 2552 if (object->internal) {
fe8ab488 2553 *type_of_fault = DBG_PAGEIND_FAULT;
0a7de745 2554 } else {
fe8ab488 2555 *type_of_fault = DBG_PAGEINV_FAULT;
0a7de745 2556 }
5ba3f43e 2557
fe8ab488 2558 VM_PAGE_COUNT_AS_PAGEIN(m);
55e303ae 2559 }
fe8ab488 2560 VM_PAGE_CONSUME_CLUSTERED(m);
2d21ac55 2561 }
6d2010ae 2562 }
2d21ac55
A
2563
2564 if (*type_of_fault != DBG_COW_FAULT) {
2565 DTRACE_VM2(as_fault, int, 1, (uint64_t *), NULL);
2566
2567 if (pmap == kernel_pmap) {
2568 DTRACE_VM2(kernel_asflt, int, 1, (uint64_t *), NULL);
2569 }
2570 }
2571
b0d623f7 2572 /* Validate code signature if necessary. */
d9a64523
A
2573 if (!cs_bypass &&
2574 VM_FAULT_NEED_CS_VALIDATION(pmap, m, object)) {
39037602 2575 vm_object_lock_assert_exclusive(object);
593a1d5f 2576
d9a64523 2577 if (m->vmp_cs_validated) {
593a1d5f
A
2578 vm_cs_revalidates++;
2579 }
2580
5ba3f43e 2581 /* VM map is locked, so 1 ref will remain on VM object -
b0d623f7 2582 * so no harm if vm_page_validate_cs drops the object lock */
d9a64523
A
2583
2584#if PMAP_CS
2585 if (fault_info->pmap_cs_associated &&
2586 pmap_cs_enforced(pmap) &&
2587 !m->vmp_cs_validated &&
2588 !m->vmp_cs_tainted &&
2589 !m->vmp_cs_nx &&
2590 (prot & VM_PROT_EXECUTE) &&
2591 (caller_prot & VM_PROT_EXECUTE)) {
2592 /*
2593 * With pmap_cs, the pmap layer will validate the
2594 * code signature for any executable pmap mapping.
2595 * No need for us to validate this page too:
2596 * in pmap_cs we trust...
2597 */
2598 vm_cs_defer_to_pmap_cs++;
2599 } else {
2600 vm_cs_defer_to_pmap_cs_not++;
2601 vm_page_validate_cs(m);
2602 }
2603#else /* PMAP_CS */
593a1d5f 2604 vm_page_validate_cs(m);
d9a64523 2605#endif /* PMAP_CS */
593a1d5f
A
2606 }
2607
0a7de745 2608#define page_immutable(m, prot) ((m)->vmp_cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/ )
d9a64523 2609#define page_nx(m) ((m)->vmp_cs_nx)
b0d623f7
A
2610
2611 map_is_switched = ((pmap != vm_map_pmap(current_task()->map)) &&
0a7de745 2612 (pmap == vm_map_pmap(current_thread()->map)));
b0d623f7 2613 map_is_switch_protected = current_thread()->map->switch_protect;
5ba3f43e 2614
b0d623f7 2615 /* If the map is switched, and is switch-protected, we must protect
5ba3f43e 2616 * some pages from being write-faulted: immutable pages because by
b0d623f7
A
2617 * definition they may not be written, and executable pages because that
2618 * would provide a way to inject unsigned code.
2619 * If the page is immutable, we can simply return. However, we can't
2620 * immediately determine whether a page is executable anywhere. But,
2621 * we can disconnect it everywhere and remove the executable protection
5ba3f43e 2622 * from the current map. We do that below right before we do the
b0d623f7
A
2623 * PMAP_ENTER.
2624 */
d9a64523 2625 cs_enforcement_enabled = cs_process_enforcement(NULL);
39236c6e 2626
0a7de745
A
2627 if (cs_enforcement_enabled && map_is_switched &&
2628 map_is_switch_protected && page_immutable(m, prot) &&
2629 (prot & VM_PROT_WRITE)) {
b0d623f7
A
2630 return KERN_CODESIGN_ERROR;
2631 }
2632
c18c124e 2633 if (cs_enforcement_enabled && page_nx(m) && (prot & VM_PROT_EXECUTE)) {
0a7de745 2634 if (cs_debug) {
c18c124e 2635 printf("page marked to be NX, not letting it be mapped EXEC\n");
0a7de745 2636 }
c18c124e
A
2637 return KERN_CODESIGN_ERROR;
2638 }
2639
b0d623f7
A
2640 /* A page could be tainted, or pose a risk of being tainted later.
2641 * Check whether the receiving process wants it, and make it feel
2642 * the consequences (that hapens in cs_invalid_page()).
5ba3f43e
A
2643 * For CS Enforcement, two other conditions will
2644 * cause that page to be tainted as well:
b0d623f7
A
2645 * - pmapping an unsigned page executable - this means unsigned code;
2646 * - writeable mapping of a validated page - the content of that page
2647 * can be changed without the kernel noticing, therefore unsigned
2648 * code can be created
2649 */
d9a64523
A
2650 if (cs_bypass) {
2651 /* code-signing is bypassed */
2652 cs_violation = FALSE;
2653 } else if (m->vmp_cs_tainted) {
2654 /* tainted page */
2655 cs_violation = TRUE;
2656 } else if (!cs_enforcement_enabled) {
2657 /* no further code-signing enforcement */
2658 cs_violation = FALSE;
2659 } else if (page_immutable(m, prot) &&
0a7de745
A
2660 ((prot & VM_PROT_WRITE) ||
2661 m->vmp_wpmapped)) {
d9a64523
A
2662 /*
2663 * The page should be immutable, but is in danger of being
2664 * modified.
2665 * This is the case where we want policy from the code
2666 * directory - is the page immutable or not? For now we have
2667 * to assume that code pages will be immutable, data pages not.
2668 * We'll assume a page is a code page if it has a code directory
2669 * and we fault for execution.
2670 * That is good enough since if we faulted the code page for
2671 * writing in another map before, it is wpmapped; if we fault
2672 * it for writing in this map later it will also be faulted for
2673 * executing at the same time; and if we fault for writing in
2674 * another map later, we will disconnect it from this pmap so
2675 * we'll notice the change.
2676 */
2677 cs_violation = TRUE;
2678 } else if (!m->vmp_cs_validated &&
0a7de745 2679 (prot & VM_PROT_EXECUTE)
d9a64523 2680#if PMAP_CS
0a7de745
A
2681 /*
2682 * Executable pages will be validated by pmap_cs;
2683 * in pmap_cs we trust...
2684 * If pmap_cs is turned off, this is a code-signing
2685 * violation.
2686 */
2687 && !(pmap_cs_enforced(pmap))
d9a64523 2688#endif /* PMAP_CS */
0a7de745 2689 ) {
d9a64523
A
2690 cs_violation = TRUE;
2691 } else {
2692 cs_violation = FALSE;
2693 }
2694
2695 if (cs_violation) {
b0d623f7
A
2696 /* We will have a tainted page. Have to handle the special case
2697 * of a switched map now. If the map is not switched, standard
2698 * procedure applies - call cs_invalid_page().
2699 * If the map is switched, the real owner is invalid already.
2700 * There is no point in invalidating the switching process since
2701 * it will not be executing from the map. So we don't call
2702 * cs_invalid_page() in that case. */
39037602 2703 boolean_t reject_page, cs_killed;
0a7de745
A
2704 if (map_is_switched) {
2705 assert(pmap == vm_map_pmap(current_thread()->map));
b0d623f7
A
2706 assert(!(prot & VM_PROT_WRITE) || (map_is_switch_protected == FALSE));
2707 reject_page = FALSE;
2708 } else {
0a7de745 2709 if (cs_debug > 5) {
d9a64523 2710 printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s prot: 0x%x\n",
0a7de745
A
2711 object->code_signed ? "yes" : "no",
2712 m->vmp_cs_validated ? "yes" : "no",
2713 m->vmp_cs_tainted ? "yes" : "no",
2714 m->vmp_wpmapped ? "yes" : "no",
2715 (int)prot);
2716 }
39037602 2717 reject_page = cs_invalid_page((addr64_t) vaddr, &cs_killed);
b0d623f7 2718 }
5ba3f43e 2719
b0d623f7 2720 if (reject_page) {
fe8ab488 2721 /* reject the invalid page: abort the page fault */
0a7de745
A
2722 int pid;
2723 const char *procname;
2724 task_t task;
2725 vm_object_t file_object, shadow;
2726 vm_object_offset_t file_offset;
2727 char *pathname, *filename;
2728 vm_size_t pathname_len, filename_len;
2729 boolean_t truncated_path;
15129b1c 2730#define __PATH_MAX 1024
0a7de745
A
2731 struct timespec mtime, cs_mtime;
2732 int shadow_depth;
2733 os_reason_t codesigning_exit_reason = OS_REASON_NULL;
15129b1c 2734
b0d623f7
A
2735 kr = KERN_CODESIGN_ERROR;
2736 cs_enter_tainted_rejected++;
15129b1c
A
2737
2738 /* get process name and pid */
2739 procname = "?";
2740 task = current_task();
2741 pid = proc_selfpid();
0a7de745 2742 if (task->bsd_info != NULL) {
15129b1c 2743 procname = proc_name_address(task->bsd_info);
0a7de745 2744 }
15129b1c
A
2745
2746 /* get file's VM object */
39037602 2747 file_object = object;
d9a64523 2748 file_offset = m->vmp_offset;
39037602 2749 for (shadow = file_object->shadow,
0a7de745
A
2750 shadow_depth = 0;
2751 shadow != VM_OBJECT_NULL;
2752 shadow = file_object->shadow,
2753 shadow_depth++) {
15129b1c 2754 vm_object_lock_shared(shadow);
39037602 2755 if (file_object != object) {
15129b1c
A
2756 vm_object_unlock(file_object);
2757 }
2758 file_offset += file_object->vo_shadow_offset;
2759 file_object = shadow;
2760 }
2761
2762 mtime.tv_sec = 0;
2763 mtime.tv_nsec = 0;
2764 cs_mtime.tv_sec = 0;
2765 cs_mtime.tv_nsec = 0;
2766
2767 /* get file's pathname and/or filename */
2768 pathname = NULL;
2769 filename = NULL;
2770 pathname_len = 0;
2771 filename_len = 0;
2772 truncated_path = FALSE;
3e170ce0
A
2773 /* no pager -> no file -> no pathname, use "<nil>" in that case */
2774 if (file_object->pager != NULL) {
15129b1c
A
2775 pathname = (char *)kalloc(__PATH_MAX * 2);
2776 if (pathname) {
fe8ab488 2777 pathname[0] = '\0';
15129b1c
A
2778 pathname_len = __PATH_MAX;
2779 filename = pathname + pathname_len;
2780 filename_len = __PATH_MAX;
2781 }
2782 vnode_pager_get_object_name(file_object->pager,
0a7de745
A
2783 pathname,
2784 pathname_len,
2785 filename,
2786 filename_len,
2787 &truncated_path);
3e170ce0
A
2788 if (pathname) {
2789 /* safety first... */
0a7de745
A
2790 pathname[__PATH_MAX - 1] = '\0';
2791 filename[__PATH_MAX - 1] = '\0';
3e170ce0 2792 }
15129b1c 2793 vnode_pager_get_object_mtime(file_object->pager,
0a7de745
A
2794 &mtime,
2795 &cs_mtime);
15129b1c
A
2796 }
2797 printf("CODE SIGNING: process %d[%s]: "
0a7de745
A
2798 "rejecting invalid page at address 0x%llx "
2799 "from offset 0x%llx in file \"%s%s%s\" "
2800 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2801 "(signed:%d validated:%d tainted:%d nx:%d "
2802 "wpmapped:%d dirty:%d depth:%d)\n",
2803 pid, procname, (addr64_t) vaddr,
2804 file_offset,
2805 (pathname ? pathname : "<nil>"),
2806 (truncated_path ? "/.../" : ""),
2807 (truncated_path ? filename : ""),
2808 cs_mtime.tv_sec, cs_mtime.tv_nsec,
2809 ((cs_mtime.tv_sec == mtime.tv_sec &&
2810 cs_mtime.tv_nsec == mtime.tv_nsec)
2811 ? "=="
2812 : "!="),
2813 mtime.tv_sec, mtime.tv_nsec,
2814 object->code_signed,
2815 m->vmp_cs_validated,
2816 m->vmp_cs_tainted,
2817 m->vmp_cs_nx,
2818 m->vmp_wpmapped,
2819 m->vmp_dirty,
2820 shadow_depth);
39037602
A
2821
2822 /*
2823 * We currently only generate an exit reason if cs_invalid_page directly killed a process. If cs_invalid_page
2824 * did not kill the process (more the case on desktop), vm_fault_enter will not satisfy the fault and whether the
2825 * process dies is dependent on whether there is a signal handler registered for SIGSEGV and how that handler
2826 * will deal with the segmentation fault.
2827 */
2828 if (cs_killed) {
2829 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
0a7de745 2830 pid, OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_INVALID_PAGE, 0, 0);
39037602
A
2831
2832 codesigning_exit_reason = os_reason_create(OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_INVALID_PAGE);
2833 if (codesigning_exit_reason == NULL) {
2834 printf("vm_fault_enter: failed to allocate codesigning exit reason\n");
2835 } else {
2836 mach_vm_address_t data_addr = 0;
2837 struct codesigning_exit_reason_info *ceri = NULL;
2838 uint32_t reason_buffer_size_estimate = kcdata_estimate_required_buffer_size(1, sizeof(*ceri));
2839
d190cdc3 2840 if (os_reason_alloc_buffer_noblock(codesigning_exit_reason, reason_buffer_size_estimate)) {
39037602
A
2841 printf("vm_fault_enter: failed to allocate buffer for codesigning exit reason\n");
2842 } else {
2843 if (KERN_SUCCESS == kcdata_get_memory_addr(&codesigning_exit_reason->osr_kcd_descriptor,
0a7de745 2844 EXIT_REASON_CODESIGNING_INFO, sizeof(*ceri), &data_addr)) {
39037602
A
2845 ceri = (struct codesigning_exit_reason_info *)data_addr;
2846 static_assert(__PATH_MAX == sizeof(ceri->ceri_pathname));
2847
2848 ceri->ceri_virt_addr = vaddr;
2849 ceri->ceri_file_offset = file_offset;
0a7de745 2850 if (pathname) {
39037602 2851 strncpy((char *)&ceri->ceri_pathname, pathname, sizeof(ceri->ceri_pathname));
0a7de745 2852 } else {
39037602 2853 ceri->ceri_pathname[0] = '\0';
0a7de745
A
2854 }
2855 if (filename) {
39037602 2856 strncpy((char *)&ceri->ceri_filename, filename, sizeof(ceri->ceri_filename));
0a7de745 2857 } else {
39037602 2858 ceri->ceri_filename[0] = '\0';
0a7de745 2859 }
39037602
A
2860 ceri->ceri_path_truncated = (truncated_path);
2861 ceri->ceri_codesig_modtime_secs = cs_mtime.tv_sec;
2862 ceri->ceri_codesig_modtime_nsecs = cs_mtime.tv_nsec;
2863 ceri->ceri_page_modtime_secs = mtime.tv_sec;
2864 ceri->ceri_page_modtime_nsecs = mtime.tv_nsec;
2865 ceri->ceri_object_codesigned = (object->code_signed);
d9a64523
A
2866 ceri->ceri_page_codesig_validated = (m->vmp_cs_validated);
2867 ceri->ceri_page_codesig_tainted = (m->vmp_cs_tainted);
2868 ceri->ceri_page_codesig_nx = (m->vmp_cs_nx);
2869 ceri->ceri_page_wpmapped = (m->vmp_wpmapped);
2870 ceri->ceri_page_slid = 0;
2871 ceri->ceri_page_dirty = (m->vmp_dirty);
39037602
A
2872 ceri->ceri_page_shadow_depth = shadow_depth;
2873 } else {
2874#if DEBUG || DEVELOPMENT
2875 panic("vm_fault_enter: failed to allocate kcdata for codesigning exit reason");
2876#else
2877 printf("vm_fault_enter: failed to allocate kcdata for codesigning exit reason\n");
2878#endif /* DEBUG || DEVELOPMENT */
2879 /* Free the buffer */
d190cdc3 2880 os_reason_alloc_buffer_noblock(codesigning_exit_reason, 0);
39037602
A
2881 }
2882 }
2883 }
2884
2885 set_thread_exit_reason(current_thread(), codesigning_exit_reason, FALSE);
2886 }
2887 if (panic_on_cs_killed &&
d9a64523 2888 object->object_is_shared_cache) {
39037602 2889 panic("CODE SIGNING: process %d[%s]: "
0a7de745
A
2890 "rejecting invalid page at address 0x%llx "
2891 "from offset 0x%llx in file \"%s%s%s\" "
2892 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2893 "(signed:%d validated:%d tainted:%d nx:%d"
2894 "wpmapped:%d dirty:%d depth:%d)\n",
2895 pid, procname, (addr64_t) vaddr,
2896 file_offset,
2897 (pathname ? pathname : "<nil>"),
2898 (truncated_path ? "/.../" : ""),
2899 (truncated_path ? filename : ""),
2900 cs_mtime.tv_sec, cs_mtime.tv_nsec,
2901 ((cs_mtime.tv_sec == mtime.tv_sec &&
2902 cs_mtime.tv_nsec == mtime.tv_nsec)
2903 ? "=="
2904 : "!="),
2905 mtime.tv_sec, mtime.tv_nsec,
2906 object->code_signed,
2907 m->vmp_cs_validated,
2908 m->vmp_cs_tainted,
2909 m->vmp_cs_nx,
2910 m->vmp_wpmapped,
2911 m->vmp_dirty,
2912 shadow_depth);
39037602
A
2913 }
2914
2915 if (file_object != object) {
15129b1c
A
2916 vm_object_unlock(file_object);
2917 }
2918 if (pathname_len != 0) {
2919 kfree(pathname, __PATH_MAX * 2);
2920 pathname = NULL;
2921 filename = NULL;
2922 }
b0d623f7 2923 } else {
fe8ab488 2924 /* proceed with the invalid page */
b0d623f7 2925 kr = KERN_SUCCESS;
d9a64523 2926 if (!m->vmp_cs_validated &&
39037602 2927 !object->code_signed) {
fe8ab488 2928 /*
490019cf
A
2929 * This page has not been (fully) validated but
2930 * does not belong to a code-signed object
2931 * so it should not be forcefully considered
fe8ab488
A
2932 * as tainted.
2933 * We're just concerned about it here because
2934 * we've been asked to "execute" it but that
2935 * does not mean that it should cause other
2936 * accesses to fail.
2937 * This happens when a debugger sets a
2938 * breakpoint and we then execute code in
2939 * that page. Marking the page as "tainted"
2940 * would cause any inspection tool ("leaks",
2941 * "vmmap", "CrashReporter", ...) to get killed
2942 * due to code-signing violation on that page,
2943 * even though they're just reading it and not
2944 * executing from it.
2945 */
fe8ab488
A
2946 } else {
2947 /*
2948 * Page might have been tainted before or not;
2949 * now it definitively is. If the page wasn't
2950 * tainted, we must disconnect it from all
2951 * pmaps later, to force existing mappings
2952 * through that code path for re-consideration
2953 * of the validity of that page.
2954 */
d9a64523
A
2955 must_disconnect = !m->vmp_cs_tainted;
2956 m->vmp_cs_tainted = TRUE;
fe8ab488 2957 }
b0d623f7 2958 cs_enter_tainted_accepted++;
2d21ac55 2959 }
15129b1c
A
2960 if (kr != KERN_SUCCESS) {
2961 if (cs_debug) {
2962 printf("CODESIGNING: vm_fault_enter(0x%llx): "
0a7de745
A
2963 "*** INVALID PAGE ***\n",
2964 (long long)vaddr);
15129b1c 2965 }
39236c6e 2966#if !SECURE_KERNEL
15129b1c 2967 if (cs_enforcement_panic) {
39236c6e
A
2968 panic("CODESIGNING: panicking on invalid page\n");
2969 }
2970#endif
2d21ac55
A
2971 }
2972 } else {
2973 /* proceed with the valid page */
2974 kr = KERN_SUCCESS;
2975 }
2976
0a7de745
A
2977 boolean_t page_queues_locked = FALSE;
2978#define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \
2979MACRO_BEGIN \
2980 if (! page_queues_locked) { \
2981 page_queues_locked = TRUE; \
2982 vm_page_lockspin_queues(); \
2983 } \
39236c6e 2984MACRO_END
0a7de745
A
2985#define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \
2986MACRO_BEGIN \
2987 if (page_queues_locked) { \
2988 page_queues_locked = FALSE; \
2989 vm_page_unlock_queues(); \
2990 } \
39236c6e
A
2991MACRO_END
2992
2993 /*
2994 * Hold queues lock to manipulate
2995 * the page queues. Change wiring
2996 * case is obvious.
2997 */
d9a64523 2998 assert((m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) || object != compressor_object);
39037602
A
2999
3000#if CONFIG_BACKGROUND_QUEUE
3001 vm_page_update_background_state(m);
3002#endif
d9a64523 3003 if (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) {
39236c6e
A
3004 /*
3005 * Compressor pages are neither wired
3006 * nor pageable and should never change.
3007 */
39037602 3008 assert(object == compressor_object);
39236c6e 3009 } else if (change_wiring) {
0a7de745 3010 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
39236c6e
A
3011
3012 if (wired) {
3013 if (kr == KERN_SUCCESS) {
5ba3f43e 3014 vm_page_wire(m, wire_tag, TRUE);
39236c6e
A
3015 }
3016 } else {
0a7de745 3017 vm_page_unwire(m, TRUE);
39236c6e
A
3018 }
3019 /* we keep the page queues lock, if we need it later */
39236c6e 3020 } else {
39037602
A
3021 if (object->internal == TRUE) {
3022 /*
3023 * don't allow anonymous pages on
3024 * the speculative queues
3025 */
3026 no_cache = FALSE;
3027 }
0a7de745
A
3028 if (kr != KERN_SUCCESS) {
3029 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3030 vm_page_deactivate(m);
39236c6e 3031 /* we keep the page queues lock, if we need it later */
d9a64523 3032 } else if (((m->vmp_q_state == VM_PAGE_NOT_ON_Q) ||
0a7de745
A
3033 (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) ||
3034 (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) ||
3035 ((m->vmp_q_state != VM_PAGE_ON_THROTTLED_Q) && no_cache)) &&
3036 !VM_PAGE_WIRED(m)) {
5ba3f43e 3037 if (vm_page_local_q &&
39236c6e 3038 (*type_of_fault == DBG_COW_FAULT ||
0a7de745
A
3039 *type_of_fault == DBG_ZERO_FILL_FAULT)) {
3040 struct vpl *lq;
3041 uint32_t lid;
39236c6e 3042
d9a64523 3043 assert(m->vmp_q_state == VM_PAGE_NOT_ON_Q);
39037602 3044
39236c6e 3045 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
39037602 3046 vm_object_lock_assert_exclusive(object);
39236c6e
A
3047
3048 /*
3049 * we got a local queue to stuff this
3050 * new page on...
3051 * its safe to manipulate local and
3052 * local_id at this point since we're
3053 * behind an exclusive object lock and
3054 * the page is not on any global queue.
3055 *
3056 * we'll use the current cpu number to
3057 * select the queue note that we don't
3058 * need to disable preemption... we're
39037602 3059 * going to be behind the local queue's
39236c6e
A
3060 * lock to do the real work
3061 */
3062 lid = cpu_number();
3063
3064 lq = &vm_page_local_q[lid].vpl_un.vpl;
3065
3066 VPL_LOCK(&lq->vpl_lock);
3067
3e170ce0 3068 vm_page_check_pageable_safe(m);
0a7de745 3069 vm_page_queue_enter(&lq->vpl_queue, m, vmp_pageq);
d9a64523
A
3070 m->vmp_q_state = VM_PAGE_ON_ACTIVE_LOCAL_Q;
3071 m->vmp_local_id = lid;
39236c6e 3072 lq->vpl_count++;
5ba3f43e 3073
0a7de745 3074 if (object->internal) {
39236c6e 3075 lq->vpl_internal_count++;
0a7de745 3076 } else {
39236c6e 3077 lq->vpl_external_count++;
0a7de745 3078 }
39236c6e
A
3079
3080 VPL_UNLOCK(&lq->vpl_lock);
3081
0a7de745 3082 if (lq->vpl_count > vm_page_local_q_soft_limit) {
39236c6e
A
3083 /*
3084 * we're beyond the soft limit
3085 * for the local queue
3086 * vm_page_reactivate_local will
3087 * 'try' to take the global page
3088 * queue lock... if it can't
3089 * that's ok... we'll let the
3090 * queue continue to grow up
3091 * to the hard limit... at that
3092 * point we'll wait for the
3093 * lock... once we've got the
3094 * lock, we'll transfer all of
3095 * the pages from the local
3096 * queue to the global active
3097 * queue
3098 */
3099 vm_page_reactivate_local(lid, FALSE, FALSE);
3100 }
3101 } else {
39236c6e
A
3102 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3103
3104 /*
3105 * test again now that we hold the
3106 * page queue lock
3107 */
3108 if (!VM_PAGE_WIRED(m)) {
d9a64523 3109 if (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) {
39037602 3110 vm_page_queues_remove(m, FALSE);
39236c6e 3111
d9a64523
A
3112 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated, 1);
3113 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_fault_reactivated, 1);
39236c6e
A
3114 }
3115
0a7de745
A
3116 if (!VM_PAGE_ACTIVE_OR_INACTIVE(m) ||
3117 no_cache) {
39236c6e
A
3118 /*
3119 * If this is a no_cache mapping
3120 * and the page has never been
3121 * mapped before or was
3122 * previously a no_cache page,
3123 * then we want to leave pages
3124 * in the speculative state so
3125 * that they can be readily
3126 * recycled if free memory runs
3127 * low. Otherwise the page is
5ba3f43e 3128 * activated as normal.
39236c6e
A
3129 */
3130
3131 if (no_cache &&
3132 (!previously_pmapped ||
0a7de745 3133 m->vmp_no_cache)) {
d9a64523 3134 m->vmp_no_cache = TRUE;
39236c6e 3135
0a7de745 3136 if (m->vmp_q_state != VM_PAGE_ON_SPECULATIVE_Q) {
39236c6e 3137 vm_page_speculate(m, FALSE);
0a7de745
A
3138 }
3139 } else if (!VM_PAGE_ACTIVE_OR_INACTIVE(m)) {
39236c6e
A
3140 vm_page_activate(m);
3141 }
3142 }
3143 }
3144 /* we keep the page queues lock, if we need it later */
3145 }
3146 }
3147 }
39236c6e
A
3148 /* we're done with the page queues lock, if we ever took it */
3149 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3150
3151
b0d623f7
A
3152 /* If we have a KERN_SUCCESS from the previous checks, we either have
3153 * a good page, or a tainted page that has been accepted by the process.
3154 * In both cases the page will be entered into the pmap.
3155 * If the page is writeable, we need to disconnect it from other pmaps
3156 * now so those processes can take note.
3157 */
2d21ac55 3158 if (kr == KERN_SUCCESS) {
0a7de745 3159 /*
2d21ac55 3160 * NOTE: we may only hold the vm_object lock SHARED
5ba3f43e 3161 * at this point, so we need the phys_page lock to
fe8ab488
A
3162 * properly serialize updating the pmapped and
3163 * xpmapped bits
2d21ac55 3164 */
d9a64523 3165 if ((prot & VM_PROT_EXECUTE) && !m->vmp_xpmapped) {
0a7de745 3166 ppnum_t phys_page = VM_PAGE_GET_PHYS_PAGE(m);
fe8ab488 3167
39037602 3168 pmap_lock_phys_page(phys_page);
fe8ab488
A
3169 /*
3170 * go ahead and take the opportunity
3171 * to set 'pmapped' here so that we don't
3172 * need to grab this lock a 2nd time
3173 * just below
3174 */
d9a64523 3175 m->vmp_pmapped = TRUE;
5ba3f43e 3176
d9a64523 3177 if (!m->vmp_xpmapped) {
d9a64523 3178 m->vmp_xpmapped = TRUE;
fe8ab488 3179
39037602 3180 pmap_unlock_phys_page(phys_page);
fe8ab488 3181
0a7de745 3182 if (!object->internal) {
fe8ab488 3183 OSAddAtomic(1, &vm_page_xpmapped_external_count);
0a7de745 3184 }
fe8ab488 3185
5ba3f43e
A
3186#if defined(__arm__) || defined(__arm64__)
3187 pmap_sync_page_data_phys(phys_page);
3188#else
39037602
A
3189 if (object->internal &&
3190 object->pager != NULL) {
fe8ab488
A
3191 /*
3192 * This page could have been
3193 * uncompressed by the
3194 * compressor pager and its
3195 * contents might be only in
3196 * the data cache.
3197 * Since it's being mapped for
3198 * "execute" for the fist time,
3199 * make sure the icache is in
3200 * sync.
3201 */
39037602
A
3202 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);
3203 pmap_sync_page_data_phys(phys_page);
fe8ab488 3204 }
5ba3f43e 3205#endif
0a7de745 3206 } else {
39037602 3207 pmap_unlock_phys_page(phys_page);
0a7de745 3208 }
fe8ab488 3209 } else {
d9a64523 3210 if (m->vmp_pmapped == FALSE) {
0a7de745 3211 ppnum_t phys_page = VM_PAGE_GET_PHYS_PAGE(m);
39037602
A
3212
3213 pmap_lock_phys_page(phys_page);
d9a64523 3214 m->vmp_pmapped = TRUE;
39037602 3215 pmap_unlock_phys_page(phys_page);
fe8ab488
A
3216 }
3217 }
6d2010ae
A
3218
3219 if (fault_type & VM_PROT_WRITE) {
d9a64523 3220 if (m->vmp_wpmapped == FALSE) {
39037602
A
3221 vm_object_lock_assert_exclusive(object);
3222 if (!object->internal && object->pager) {
3223 task_update_logical_writes(current_task(), PAGE_SIZE, TASK_WRITE_DEFERRED, vnode_pager_lookup_vnode(object->pager));
3224 }
d9a64523 3225 m->vmp_wpmapped = TRUE;
6d2010ae
A
3226 }
3227 if (must_disconnect) {
3228 /*
5ba3f43e 3229 * We can only get here
6d2010ae
A
3230 * because of the CSE logic
3231 */
39236c6e 3232 assert(cs_enforcement_enabled);
39037602 3233 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
5ba3f43e 3234 /*
6d2010ae 3235 * If we are faulting for a write, we can clear
b0d623f7
A
3236 * the execute bit - that will ensure the page is
3237 * checked again before being executable, which
3238 * protects against a map switch.
3239 * This only happens the first time the page
5ba3f43e 3240 * gets tainted, so we won't get stuck here
6d2010ae
A
3241 * to make an already writeable page executable.
3242 */
0a7de745 3243 if (!cs_bypass) {
5ba3f43e 3244 assert(!pmap_has_prot_policy(prot));
6d2010ae
A
3245 prot &= ~VM_PROT_EXECUTE;
3246 }
b0d623f7 3247 }
4a3eedf9 3248 }
39037602 3249 assert(VM_PAGE_OBJECT(m) == object);
d1ecb069 3250
d9a64523
A
3251#if VM_OBJECT_ACCESS_TRACKING
3252 if (object->access_tracking) {
3253 DTRACE_VM2(access_tracking, vm_map_offset_t, vaddr, int, fault_type);
3254 if (fault_type & VM_PROT_WRITE) {
3255 object->access_tracking_writes++;
3256 vm_object_access_tracking_writes++;
3257 } else {
3258 object->access_tracking_reads++;
3259 vm_object_access_tracking_reads++;
3260 }
3261 }
3262#endif /* VM_OBJECT_ACCESS_TRACKING */
3263
3264#if PMAP_CS
3265 /*
3266 * If CS enforcement is on, we don't ask for an executable page if the
3267 * fault does not call for execution, because that can fail in
3268 * situations where the caller only actually wanted read access.
3269 * However, it may be better to instead retry without execute on
3270 * failure, or pass a flag into pmap_enter to do the right thing.
3271 */
3272 // TODO: <rdar://problem/30997388> maybe do something better than masking out VM_PROT_EXECUTE on non-execute faults
3273 if (pmap_cs_enforced(pmap) && !(caller_prot & VM_PROT_EXECUTE)) {
3274 prot &= ~VM_PROT_EXECUTE;
3275 }
3276#endif
3277
d1ecb069
A
3278 /* Prevent a deadlock by not
3279 * holding the object lock if we need to wait for a page in
3280 * pmap_enter() - <rdar://problem/7138958> */
316670eb 3281 PMAP_ENTER_OPTIONS(pmap, vaddr, m, prot, fault_type, 0,
0a7de745
A
3282 wired,
3283 pmap_options | PMAP_OPTIONS_NOWAIT,
3284 pe_result);
5ba3f43e
A
3285#if __x86_64__
3286 if (pe_result == KERN_INVALID_ARGUMENT &&
3287 pmap == PMAP_NULL &&
3288 wired) {
3289 /*
3290 * Wiring a page in a pmap-less VM map:
3291 * VMware's "vmmon" kernel extension does this
3292 * to grab pages.
3293 * Let it proceed even though the PMAP_ENTER() failed.
3294 */
3295 pe_result = KERN_SUCCESS;
3296 }
3297#endif /* __x86_64__ */
d1ecb069 3298
0a7de745 3299 if (pe_result == KERN_RESOURCE_SHORTAGE) {
316670eb
A
3300 if (need_retry) {
3301 /*
3302 * this will be non-null in the case where we hold the lock
3303 * on the top-object in this chain... we can't just drop
3304 * the lock on the object we're inserting the page into
3305 * and recall the PMAP_ENTER since we can still cause
5ba3f43e 3306 * a deadlock if one of the critical paths tries to
316670eb
A
3307 * acquire the lock on the top-object and we're blocked
3308 * in PMAP_ENTER waiting for memory... our only recourse
5ba3f43e 3309 * is to deal with it at a higher level where we can
316670eb
A
3310 * drop both locks.
3311 */
3312 *need_retry = TRUE;
3313 vm_pmap_enter_retried++;
3314 goto after_the_pmap_enter;
3315 }
d1ecb069 3316 /* The nonblocking version of pmap_enter did not succeed.
316670eb 3317 * and we don't need to drop other locks and retry
5ba3f43e 3318 * at the level above us, so
316670eb 3319 * use the blocking version instead. Requires marking
d1ecb069 3320 * the page busy and unlocking the object */
d9a64523 3321 boolean_t was_busy = m->vmp_busy;
39236c6e 3322
39037602 3323 vm_object_lock_assert_exclusive(object);
39236c6e 3324
d9a64523 3325 m->vmp_busy = TRUE;
39037602 3326 vm_object_unlock(object);
5ba3f43e 3327
fe8ab488 3328 PMAP_ENTER_OPTIONS(pmap, vaddr, m, prot, fault_type,
0a7de745
A
3329 0, wired,
3330 pmap_options, pe_result);
5ba3f43e 3331
39037602
A
3332 assert(VM_PAGE_OBJECT(m) == object);
3333
d1ecb069 3334 /* Take the object lock again. */
39037602 3335 vm_object_lock(object);
5ba3f43e 3336
d1ecb069
A
3337 /* If the page was busy, someone else will wake it up.
3338 * Otherwise, we have to do it now. */
d9a64523 3339 assert(m->vmp_busy);
0a7de745 3340 if (!was_busy) {
d1ecb069
A
3341 PAGE_WAKEUP_DONE(m);
3342 }
3343 vm_pmap_enter_blocked++;
3344 }
5ba3f43e
A
3345
3346 kr = pe_result;
2d21ac55
A
3347 }
3348
6d2010ae 3349after_the_pmap_enter:
2d21ac55 3350 return kr;
55e303ae
A
3351}
3352
fe8ab488
A
3353void
3354vm_pre_fault(vm_map_offset_t vaddr)
3355{
3356 if (pmap_find_phys(current_map()->pmap, vaddr) == 0) {
5ba3f43e 3357 vm_fault(current_map(), /* map */
0a7de745
A
3358 vaddr, /* vaddr */
3359 VM_PROT_READ, /* fault_type */
3360 FALSE, /* change_wiring */
3361 VM_KERN_MEMORY_NONE, /* tag - not wiring */
3362 THREAD_UNINT, /* interruptible */
3363 NULL, /* caller_pmap */
3364 0 /* caller_pmap_addr */);
fe8ab488
A
3365 }
3366}
3367
2d21ac55 3368
1c79356b
A
3369/*
3370 * Routine: vm_fault
3371 * Purpose:
3372 * Handle page faults, including pseudo-faults
3373 * used to change the wiring status of pages.
3374 * Returns:
3375 * Explicit continuations have been removed.
3376 * Implementation:
3377 * vm_fault and vm_fault_page save mucho state
3378 * in the moral equivalent of a closure. The state
3379 * structure is allocated when first entering vm_fault
3380 * and deallocated when leaving vm_fault.
3381 */
3382
91447636 3383extern int _map_enter_debug;
0a7de745 3384extern uint64_t get_current_unique_pid(void);
91447636 3385
2d21ac55
A
3386unsigned long vm_fault_collapse_total = 0;
3387unsigned long vm_fault_collapse_skipped = 0;
3388
39236c6e 3389
1c79356b 3390kern_return_t
5ba3f43e 3391vm_fault_external(
0a7de745
A
3392 vm_map_t map,
3393 vm_map_offset_t vaddr,
3394 vm_prot_t fault_type,
3395 boolean_t change_wiring,
3396 int interruptible,
3397 pmap_t caller_pmap,
3398 vm_map_offset_t caller_pmap_addr)
fe8ab488 3399{
5ba3f43e 3400 return vm_fault_internal(map, vaddr, fault_type, change_wiring, vm_tag_bt(),
0a7de745
A
3401 interruptible, caller_pmap, caller_pmap_addr,
3402 NULL);
fe8ab488
A
3403}
3404
5ba3f43e
A
3405kern_return_t
3406vm_fault(
0a7de745
A
3407 vm_map_t map,
3408 vm_map_offset_t vaddr,
3409 vm_prot_t fault_type,
3410 boolean_t change_wiring,
3411 vm_tag_t wire_tag, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3412 int interruptible,
3413 pmap_t caller_pmap,
3414 vm_map_offset_t caller_pmap_addr)
5ba3f43e
A
3415{
3416 return vm_fault_internal(map, vaddr, fault_type, change_wiring, wire_tag,
0a7de745
A
3417 interruptible, caller_pmap, caller_pmap_addr,
3418 NULL);
5ba3f43e 3419}
3e170ce0 3420
fe8ab488
A
3421kern_return_t
3422vm_fault_internal(
0a7de745
A
3423 vm_map_t map,
3424 vm_map_offset_t vaddr,
3425 vm_prot_t caller_prot,
3426 boolean_t change_wiring,
3427 vm_tag_t wire_tag, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */
3428 int interruptible,
3429 pmap_t caller_pmap,
3430 vm_map_offset_t caller_pmap_addr,
3431 ppnum_t *physpage_p)
1c79356b 3432{
0a7de745
A
3433 vm_map_version_t version; /* Map version for verificiation */
3434 boolean_t wired; /* Should mapping be wired down? */
3435 vm_object_t object; /* Top-level object */
3436 vm_object_offset_t offset; /* Top-level offset */
3437 vm_prot_t prot; /* Protection for mapping */
3438 vm_object_t old_copy_object; /* Saved copy object */
3439 vm_page_t result_page; /* Result of vm_fault_page */
3440 vm_page_t top_page; /* Placeholder page */
3441 kern_return_t kr;
3442
3443 vm_page_t m; /* Fast access to result_page */
3444 kern_return_t error_code;
3445 vm_object_t cur_object;
3446 vm_object_t m_object = NULL;
3447 vm_object_offset_t cur_offset;
3448 vm_page_t cur_m;
3449 vm_object_t new_object;
1c79356b 3450 int type_of_fault;
0a7de745
A
3451 pmap_t pmap;
3452 wait_interrupt_t interruptible_state;
3453 vm_map_t real_map = map;
3454 vm_map_t original_map = map;
3455 boolean_t object_locks_dropped = FALSE;
3456 vm_prot_t fault_type;
3457 vm_prot_t original_fault_type;
d9a64523 3458 struct vm_object_fault_info fault_info = {};
0a7de745
A
3459 boolean_t need_collapse = FALSE;
3460 boolean_t need_retry = FALSE;
3461 boolean_t *need_retry_ptr = NULL;
3462 int object_lock_type = 0;
3463 int cur_object_lock_type;
3464 vm_object_t top_object = VM_OBJECT_NULL;
3465 vm_object_t written_on_object = VM_OBJECT_NULL;
3466 memory_object_t written_on_pager = NULL;
3467 vm_object_offset_t written_on_offset = 0;
3468 int throttle_delay;
3469 int compressed_count_delta;
3470 int grab_options;
3471 vm_map_offset_t trace_vaddr;
3472 vm_map_offset_t trace_real_vaddr;
d190cdc3 3473#if DEVELOPMENT || DEBUG
0a7de745 3474 vm_map_offset_t real_vaddr;
1c79356b 3475
39037602 3476 real_vaddr = vaddr;
d190cdc3
A
3477#endif /* DEVELOPMENT || DEBUG */
3478 trace_real_vaddr = vaddr;
39037602 3479 vaddr = vm_map_trunc_page(vaddr, PAGE_MASK);
de355530 3480
d190cdc3 3481 if (map == kernel_map) {
5ba3f43e
A
3482 trace_vaddr = VM_KERNEL_ADDRHIDE(vaddr);
3483 trace_real_vaddr = VM_KERNEL_ADDRHIDE(trace_real_vaddr);
d190cdc3
A
3484 } else {
3485 trace_vaddr = vaddr;
3486 }
3487
5ba3f43e 3488 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
3489 (MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_START,
3490 ((uint64_t)trace_vaddr >> 32),
3491 trace_vaddr,
3492 (map == kernel_map),
3493 0,
3494 0);
1c79356b 3495
0c530ab8 3496 if (get_preemption_level() != 0) {
0a7de745
A
3497 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3498 (MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END,
3499 ((uint64_t)trace_vaddr >> 32),
3500 trace_vaddr,
3501 KERN_FAILURE,
3502 0,
3503 0);
3504
3505 return KERN_FAILURE;
9bccf70c 3506 }
5ba3f43e 3507
d9a64523
A
3508 thread_t cthread = current_thread();
3509 boolean_t rtfault = (cthread->sched_mode == TH_MODE_REALTIME);
3510 uint64_t fstart = 0;
3511
3512 if (rtfault) {
3513 fstart = mach_continuous_time();
3514 }
3515
9bccf70c 3516 interruptible_state = thread_interrupt_level(interruptible);
1c79356b 3517
3e170ce0
A
3518 fault_type = (change_wiring ? VM_PROT_NONE : caller_prot);
3519
2d21ac55
A
3520 VM_STAT_INCR(faults);
3521 current_task()->faults++;
3522 original_fault_type = fault_type;
3523
0a7de745
A
3524 if (fault_type & VM_PROT_WRITE) {
3525 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
3526 } else {
3527 object_lock_type = OBJECT_LOCK_SHARED;
3528 }
2d21ac55
A
3529
3530 cur_object_lock_type = OBJECT_LOCK_SHARED;
3531
5ba3f43e
A
3532 if ((map == kernel_map) && (caller_prot & VM_PROT_WRITE)) {
3533 if (compressor_map) {
3534 if ((vaddr >= vm_map_min(compressor_map)) && (vaddr < vm_map_max(compressor_map))) {
3535 panic("Write fault on compressor map, va: %p type: %u bounds: %p->%p", (void *) vaddr, caller_prot, (void *) vm_map_min(compressor_map), (void *) vm_map_max(compressor_map));
5ba3f43e
A
3536 }
3537 }
3538 }
2d21ac55 3539RetryFault:
d9a64523
A
3540 assert(written_on_object == VM_OBJECT_NULL);
3541
1c79356b
A
3542 /*
3543 * assume we will hit a page in the cache
3544 * otherwise, explicitly override with
3545 * the real fault type once we determine it
3546 */
3547 type_of_fault = DBG_CACHE_HIT_FAULT;
3548
1c79356b
A
3549 /*
3550 * Find the backing store object and offset into
3551 * it to begin the search.
3552 */
0c530ab8 3553 fault_type = original_fault_type;
1c79356b
A
3554 map = original_map;
3555 vm_map_lock_read(map);
1c79356b 3556
2d21ac55 3557 kr = vm_map_lookup_locked(&map, vaddr, fault_type,
0a7de745
A
3558 object_lock_type, &version,
3559 &object, &offset, &prot, &wired,
3560 &fault_info,
3561 &real_map);
1c79356b
A
3562
3563 if (kr != KERN_SUCCESS) {
3564 vm_map_unlock_read(map);
3565 goto done;
3566 }
2d21ac55
A
3567 pmap = real_map->pmap;
3568 fault_info.interruptible = interruptible;
b0d623f7 3569 fault_info.stealth = FALSE;
6d2010ae 3570 fault_info.io_sync = FALSE;
0b4c1975 3571 fault_info.mark_zf_absent = FALSE;
316670eb 3572 fault_info.batch_pmap_op = FALSE;
1c79356b
A
3573
3574 /*
2d21ac55
A
3575 * If the page is wired, we must fault for the current protection
3576 * value, to avoid further faults.
1c79356b 3577 */
2d21ac55 3578 if (wired) {
1c79356b 3579 fault_type = prot | VM_PROT_WRITE;
2d21ac55
A
3580 /*
3581 * since we're treating this fault as a 'write'
3582 * we must hold the top object lock exclusively
3583 */
3584 if (object_lock_type == OBJECT_LOCK_SHARED) {
0a7de745 3585 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2d21ac55
A
3586
3587 if (vm_object_lock_upgrade(object) == FALSE) {
0a7de745 3588 /*
2d21ac55
A
3589 * couldn't upgrade, so explictly
3590 * take the lock exclusively
3591 */
0a7de745 3592 vm_object_lock(object);
2d21ac55
A
3593 }
3594 }
3595 }
1c79356b 3596
0a7de745 3597#if VM_FAULT_CLASSIFY
1c79356b
A
3598 /*
3599 * Temporary data gathering code
3600 */
3601 vm_fault_classify(object, offset, fault_type);
3602#endif
3603 /*
3604 * Fast fault code. The basic idea is to do as much as
3605 * possible while holding the map lock and object locks.
3606 * Busy pages are not used until the object lock has to
3607 * be dropped to do something (copy, zero fill, pmap enter).
3608 * Similarly, paging references aren't acquired until that
3609 * point, and object references aren't used.
3610 *
3611 * If we can figure out what to do
3612 * (zero fill, copy on write, pmap enter) while holding
3613 * the locks, then it gets done. Otherwise, we give up,
3614 * and use the original fault path (which doesn't hold
3615 * the map lock, and relies on busy pages).
3616 * The give up cases include:
0a7de745 3617 * - Have to talk to pager.
1c79356b
A
3618 * - Page is busy, absent or in error.
3619 * - Pager has locked out desired access.
3620 * - Fault needs to be restarted.
3621 * - Have to push page into copy object.
3622 *
3623 * The code is an infinite loop that moves one level down
3624 * the shadow chain each time. cur_object and cur_offset
0a7de745 3625 * refer to the current object being examined. object and offset
1c79356b
A
3626 * are the original object from the map. The loop is at the
3627 * top level if and only if object and cur_object are the same.
3628 *
3629 * Invariants: Map lock is held throughout. Lock is held on
3630 * original object and cur_object (if different) when
3631 * continuing or exiting loop.
3632 *
3633 */
3634
5ba3f43e
A
3635#if defined(__arm64__)
3636 /*
3637 * Fail if reading an execute-only page in a
3638 * pmap that enforces execute-only protection.
3639 */
3640 if (fault_type == VM_PROT_READ &&
0a7de745
A
3641 (prot & VM_PROT_EXECUTE) &&
3642 !(prot & VM_PROT_READ) &&
3643 pmap_enforces_execute_only(pmap)) {
3644 vm_object_unlock(object);
3645 vm_map_unlock_read(map);
3646 if (real_map != map) {
3647 vm_map_unlock(real_map);
3648 }
3649 kr = KERN_PROTECTION_FAILURE;
3650 goto done;
5ba3f43e
A
3651 }
3652#endif
1c79356b
A
3653
3654 /*
2d21ac55
A
3655 * If this page is to be inserted in a copy delay object
3656 * for writing, and if the object has a copy, then the
3657 * copy delay strategy is implemented in the slow fault page.
1c79356b 3658 */
2d21ac55 3659 if (object->copy_strategy == MEMORY_OBJECT_COPY_DELAY &&
0a7de745
A
3660 object->copy != VM_OBJECT_NULL && (fault_type & VM_PROT_WRITE)) {
3661 goto handle_copy_delay;
3662 }
2d21ac55 3663
1c79356b
A
3664 cur_object = object;
3665 cur_offset = offset;
3666
39037602
A
3667 grab_options = 0;
3668#if CONFIG_SECLUDED_MEMORY
3669 if (object->can_grab_secluded) {
3670 grab_options |= VM_PAGE_GRAB_SECLUDED;
3671 }
3672#endif /* CONFIG_SECLUDED_MEMORY */
3673
1c79356b 3674 while (TRUE) {
b0d623f7 3675 if (!cur_object->pager_created &&
0a7de745 3676 cur_object->phys_contiguous) { /* superpage */
b0d623f7 3677 break;
0a7de745 3678 }
b0d623f7
A
3679
3680 if (cur_object->blocked_access) {
3681 /*
3682 * Access to this VM object has been blocked.
3683 * Let the slow path handle it.
3684 */
3685 break;
3686 }
3687
1c79356b 3688 m = vm_page_lookup(cur_object, cur_offset);
39037602 3689 m_object = NULL;
2d21ac55 3690
1c79356b 3691 if (m != VM_PAGE_NULL) {
39037602
A
3692 m_object = cur_object;
3693
d9a64523 3694 if (m->vmp_busy) {
0a7de745 3695 wait_result_t result;
143cc14e 3696
2d21ac55
A
3697 /*
3698 * in order to do the PAGE_ASSERT_WAIT, we must
3699 * have object that 'm' belongs to locked exclusively
3700 */
3701 if (object != cur_object) {
2d21ac55 3702 if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
0a7de745 3703 cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2d21ac55
A
3704
3705 if (vm_object_lock_upgrade(cur_object) == FALSE) {
0a7de745 3706 /*
2d21ac55 3707 * couldn't upgrade so go do a full retry
39236c6e
A
3708 * immediately since we can no longer be
3709 * certain about cur_object (since we
3710 * don't hold a reference on it)...
3711 * first drop the top object lock
2d21ac55 3712 */
39236c6e
A
3713 vm_object_unlock(object);
3714
0a7de745
A
3715 vm_map_unlock_read(map);
3716 if (real_map != map) {
3717 vm_map_unlock(real_map);
3718 }
2d21ac55
A
3719
3720 goto RetryFault;
3721 }
3722 }
3723 } else if (object_lock_type == OBJECT_LOCK_SHARED) {
0a7de745 3724 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2d21ac55
A
3725
3726 if (vm_object_lock_upgrade(object) == FALSE) {
0a7de745 3727 /*
2d21ac55
A
3728 * couldn't upgrade, so explictly take the lock
3729 * exclusively and go relookup the page since we
3730 * will have dropped the object lock and
3731 * a different thread could have inserted
3732 * a page at this offset
3733 * no need for a full retry since we're
3734 * at the top level of the object chain
3735 */
0a7de745 3736 vm_object_lock(object);
2d21ac55
A
3737
3738 continue;
3739 }
3740 }
d9a64523 3741 if ((m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) && m_object->internal) {
39236c6e 3742 /*
d9a64523 3743 * m->vmp_busy == TRUE and the object is locked exclusively
39236c6e
A
3744 * if m->pageout_queue == TRUE after we acquire the
3745 * queues lock, we are guaranteed that it is stable on
3746 * the pageout queue and therefore reclaimable
3747 *
3748 * NOTE: this is only true for the internal pageout queue
3749 * in the compressor world
3750 */
39037602
A
3751 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);
3752
39236c6e
A
3753 vm_page_lock_queues();
3754
d9a64523 3755 if (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) {
39236c6e
A
3756 vm_pageout_throttle_up(m);
3757 vm_page_unlock_queues();
3758
3759 PAGE_WAKEUP_DONE(m);
3760 goto reclaimed_from_pageout;
3761 }
3762 vm_page_unlock_queues();
3763 }
0a7de745 3764 if (object != cur_object) {
39236c6e 3765 vm_object_unlock(object);
0a7de745 3766 }
39236c6e 3767
143cc14e 3768 vm_map_unlock_read(map);
0a7de745
A
3769 if (real_map != map) {
3770 vm_map_unlock(real_map);
3771 }
143cc14e 3772
143cc14e 3773 result = PAGE_ASSERT_WAIT(m, interruptible);
1c79356b 3774
143cc14e
A
3775 vm_object_unlock(cur_object);
3776
3777 if (result == THREAD_WAITING) {
0a7de745 3778 result = thread_block(THREAD_CONTINUE_NULL);
143cc14e
A
3779
3780 counter(c_vm_fault_page_block_busy_kernel++);
3781 }
0a7de745
A
3782 if (result == THREAD_AWAKENED || result == THREAD_RESTART) {
3783 goto RetryFault;
3784 }
143cc14e
A
3785
3786 kr = KERN_ABORTED;
3787 goto done;
3788 }
39236c6e 3789reclaimed_from_pageout:
d9a64523 3790 if (m->vmp_laundry) {
316670eb
A
3791 if (object != cur_object) {
3792 if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
3793 cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
3794
3795 vm_object_unlock(object);
3796 vm_object_unlock(cur_object);
3797
3798 vm_map_unlock_read(map);
0a7de745 3799 if (real_map != map) {
316670eb 3800 vm_map_unlock(real_map);
0a7de745 3801 }
316670eb
A
3802
3803 goto RetryFault;
3804 }
316670eb 3805 } else if (object_lock_type == OBJECT_LOCK_SHARED) {
316670eb
A
3806 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
3807
3808 if (vm_object_lock_upgrade(object) == FALSE) {
3809 /*
3810 * couldn't upgrade, so explictly take the lock
3811 * exclusively and go relookup the page since we
3812 * will have dropped the object lock and
3813 * a different thread could have inserted
3814 * a page at this offset
3815 * no need for a full retry since we're
3816 * at the top level of the object chain
3817 */
3818 vm_object_lock(object);
3819
3820 continue;
3821 }
3822 }
316670eb
A
3823 vm_pageout_steal_laundry(m, FALSE);
3824 }
3825
39037602 3826 if (VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) {
2d21ac55
A
3827 /*
3828 * Guard page: let the slow path deal with it
3829 */
3830 break;
3831 }
d9a64523 3832 if (m->vmp_unusual && (m->vmp_error || m->vmp_restart || m->vmp_private || m->vmp_absent)) {
0a7de745 3833 /*
2d21ac55 3834 * Unusual case... let the slow path deal with it
1c79356b
A
3835 */
3836 break;
3837 }
39037602 3838 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object)) {
0a7de745 3839 if (object != cur_object) {
b0d623f7 3840 vm_object_unlock(object);
0a7de745 3841 }
b0d623f7 3842 vm_map_unlock_read(map);
0a7de745
A
3843 if (real_map != map) {
3844 vm_map_unlock(real_map);
3845 }
b0d623f7
A
3846 vm_object_unlock(cur_object);
3847 kr = KERN_MEMORY_ERROR;
3848 goto done;
3849 }
39037602 3850 assert(m_object == VM_PAGE_OBJECT(m));
6d2010ae 3851
39037602 3852 if (VM_FAULT_NEED_CS_VALIDATION(map->pmap, m, m_object) ||
fe8ab488 3853 (physpage_p != NULL && (prot & VM_PROT_WRITE))) {
6d2010ae 3854upgrade_for_validation:
2d21ac55 3855 /*
4a3eedf9 3856 * We might need to validate this page
2d21ac55
A
3857 * against its code signature, so we
3858 * want to hold the VM object exclusively.
3859 */
0a7de745 3860 if (object != cur_object) {
2d21ac55
A
3861 if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
3862 vm_object_unlock(object);
3863 vm_object_unlock(cur_object);
3864
0a7de745 3865 cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2d21ac55
A
3866
3867 vm_map_unlock_read(map);
0a7de745 3868 if (real_map != map) {
2d21ac55 3869 vm_map_unlock(real_map);
0a7de745 3870 }
2d21ac55
A
3871
3872 goto RetryFault;
3873 }
2d21ac55 3874 } else if (object_lock_type == OBJECT_LOCK_SHARED) {
0a7de745 3875 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2d21ac55
A
3876
3877 if (vm_object_lock_upgrade(object) == FALSE) {
0a7de745 3878 /*
2d21ac55
A
3879 * couldn't upgrade, so explictly take the lock
3880 * exclusively and go relookup the page since we
3881 * will have dropped the object lock and
3882 * a different thread could have inserted
3883 * a page at this offset
3884 * no need for a full retry since we're
3885 * at the top level of the object chain
3886 */
0a7de745 3887 vm_object_lock(object);
2d21ac55
A
3888
3889 continue;
3890 }
3891 }
3892 }
1c79356b
A
3893 /*
3894 * Two cases of map in faults:
3895 * - At top level w/o copy object.
3896 * - Read fault anywhere.
3897 * --> must disallow write.
3898 */
3899
4a3eedf9 3900 if (object == cur_object && object->copy == VM_OBJECT_NULL) {
2d21ac55 3901 goto FastPmapEnter;
4a3eedf9 3902 }
1c79356b
A
3903
3904 if ((fault_type & VM_PROT_WRITE) == 0) {
5ba3f43e
A
3905 if (!pmap_has_prot_policy(prot)) {
3906 prot &= ~VM_PROT_WRITE;
3907 } else {
3908 /*
3909 * For a protection that the pmap cares
3910 * about, we must hand over the full
3911 * set of protections (so that the pmap
3912 * layer can apply any desired policy).
3913 * This means that cs_bypass must be
3914 * set, as this can force us to pass
3915 * RWX.
3916 */
3917 assert(fault_info.cs_bypass);
3918 }
39037602 3919
0a7de745
A
3920 if (object != cur_object) {
3921 /*
c910b4d9
A
3922 * We still need to hold the top object
3923 * lock here to prevent a race between
3924 * a read fault (taking only "shared"
3925 * locks) and a write fault (taking
3926 * an "exclusive" lock on the top
3927 * object.
3928 * Otherwise, as soon as we release the
3929 * top lock, the write fault could
3930 * proceed and actually complete before
3931 * the read fault, and the copied page's
3932 * translation could then be overwritten
3933 * by the read fault's translation for
3934 * the original page.
3935 *
3936 * Let's just record what the top object
3937 * is and we'll release it later.
2d21ac55 3938 */
c910b4d9 3939 top_object = object;
2d21ac55
A
3940
3941 /*
3942 * switch to the object that has the new page
3943 */
1c79356b 3944 object = cur_object;
2d21ac55 3945 object_lock_type = cur_object_lock_type;
1c79356b 3946 }
1c79356b 3947FastPmapEnter:
39037602
A
3948 assert(m_object == VM_PAGE_OBJECT(m));
3949
1c79356b 3950 /*
2d21ac55
A
3951 * prepare for the pmap_enter...
3952 * object and map are both locked
3953 * m contains valid data
d9a64523 3954 * object == m->vmp_object
2d21ac55
A
3955 * cur_object == NULL or it's been unlocked
3956 * no paging references on either object or cur_object
1c79356b 3957 */
0a7de745 3958 if (top_object != VM_OBJECT_NULL || object_lock_type != OBJECT_LOCK_EXCLUSIVE) {
39236c6e 3959 need_retry_ptr = &need_retry;
0a7de745 3960 } else {
39236c6e 3961 need_retry_ptr = NULL;
0a7de745 3962 }
39236c6e 3963
2d21ac55 3964 if (caller_pmap) {
0a7de745
A
3965 kr = vm_fault_enter(m,
3966 caller_pmap,
3967 caller_pmap_addr,
3968 prot,
3969 caller_prot,
3970 wired,
3971 change_wiring,
3972 wire_tag,
3973 &fault_info,
3974 need_retry_ptr,
3975 &type_of_fault);
9bccf70c 3976 } else {
0a7de745
A
3977 kr = vm_fault_enter(m,
3978 pmap,
3979 vaddr,
3980 prot,
3981 caller_prot,
3982 wired,
3983 change_wiring,
3984 wire_tag,
3985 &fault_info,
3986 need_retry_ptr,
3987 &type_of_fault);
9bccf70c 3988 }
39037602
A
3989#if DEVELOPMENT || DEBUG
3990 {
0a7de745 3991 int event_code = 0;
39037602 3992
0a7de745
A
3993 if (m_object->internal) {
3994 event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_INTERNAL));
3995 } else if (m_object->object_is_shared_cache) {
3996 event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_SHAREDCACHE));
3997 } else {
3998 event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_EXTERNAL));
3999 }
0b4e3aa0 4000
0a7de745 4001 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, event_code, trace_real_vaddr, (fault_info.user_tag << 16) | (caller_prot << 8) | type_of_fault, m->vmp_offset, get_current_unique_pid(), 0);
39037602 4002
0a7de745 4003 DTRACE_VM6(real_fault, vm_map_offset_t, real_vaddr, vm_map_offset_t, m->vmp_offset, int, event_code, int, caller_prot, int, type_of_fault, int, fault_info.user_tag);
39037602
A
4004 }
4005#endif
fe8ab488
A
4006 if (kr == KERN_SUCCESS &&
4007 physpage_p != NULL) {
4008 /* for vm_map_wire_and_extract() */
39037602 4009 *physpage_p = VM_PAGE_GET_PHYS_PAGE(m);
fe8ab488 4010 if (prot & VM_PROT_WRITE) {
39037602 4011 vm_object_lock_assert_exclusive(m_object);
d9a64523 4012 m->vmp_dirty = TRUE;
fe8ab488
A
4013 }
4014 }
4015
c910b4d9
A
4016 if (top_object != VM_OBJECT_NULL) {
4017 /*
4018 * It's safe to drop the top object
4019 * now that we've done our
4020 * vm_fault_enter(). Any other fault
4021 * in progress for that virtual
4022 * address will either find our page
4023 * and translation or put in a new page
4024 * and translation.
4025 */
4026 vm_object_unlock(top_object);
4027 top_object = VM_OBJECT_NULL;
4028 }
4029
0a7de745
A
4030 if (need_collapse == TRUE) {
4031 vm_object_collapse(object, offset, TRUE);
4032 }
5ba3f43e 4033
316670eb
A
4034 if (need_retry == FALSE &&
4035 (type_of_fault == DBG_PAGEIND_FAULT || type_of_fault == DBG_PAGEINV_FAULT || type_of_fault == DBG_CACHE_HIT_FAULT)) {
0a7de745 4036 /*
2d21ac55
A
4037 * evaluate access pattern and update state
4038 * vm_fault_deactivate_behind depends on the
4039 * state being up to date
4040 */
0a7de745 4041 vm_fault_is_sequential(m_object, cur_offset, fault_info.behavior);
0c530ab8 4042
d9a64523 4043 vm_fault_deactivate_behind(m_object, cur_offset, fault_info.behavior);
1c79356b 4044 }
1c79356b 4045 /*
2d21ac55 4046 * That's it, clean up and return.
1c79356b 4047 */
0a7de745
A
4048 if (m->vmp_busy) {
4049 PAGE_WAKEUP_DONE(m);
4050 }
6601e61a 4051
d9a64523 4052 if (need_retry == FALSE && !m_object->internal && (fault_type & VM_PROT_WRITE)) {
d9a64523
A
4053 vm_object_paging_begin(m_object);
4054
4055 assert(written_on_object == VM_OBJECT_NULL);
4056 written_on_object = m_object;
4057 written_on_pager = m_object->pager;
4058 written_on_offset = m_object->paging_offset + m->vmp_offset;
4059 }
1c79356b 4060 vm_object_unlock(object);
143cc14e 4061
1c79356b 4062 vm_map_unlock_read(map);
0a7de745 4063 if (real_map != map) {
91447636 4064 vm_map_unlock(real_map);
0a7de745 4065 }
1c79356b 4066
316670eb
A
4067 if (need_retry == TRUE) {
4068 /*
4069 * vm_fault_enter couldn't complete the PMAP_ENTER...
4070 * at this point we don't hold any locks so it's safe
4071 * to ask the pmap layer to expand the page table to
4072 * accommodate this mapping... once expanded, we'll
4073 * re-drive the fault which should result in vm_fault_enter
4074 * being able to successfully enter the mapping this time around
4075 */
fe8ab488
A
4076 (void)pmap_enter_options(
4077 pmap, vaddr, 0, 0, 0, 0, 0,
4078 PMAP_OPTIONS_NOENTER, NULL);
5ba3f43e 4079
316670eb
A
4080 need_retry = FALSE;
4081 goto RetryFault;
4082 }
2d21ac55 4083 goto done;
1c79356b 4084 }
1c79356b 4085 /*
2d21ac55 4086 * COPY ON WRITE FAULT
b0d623f7
A
4087 */
4088 assert(object_lock_type == OBJECT_LOCK_EXCLUSIVE);
4089
0a7de745 4090 /*
2d21ac55
A
4091 * If objects match, then
4092 * object->copy must not be NULL (else control
4093 * would be in previous code block), and we
4094 * have a potential push into the copy object
4095 * with which we can't cope with here.
1c79356b 4096 */
2d21ac55 4097 if (cur_object == object) {
0a7de745 4098 /*
2d21ac55
A
4099 * must take the slow path to
4100 * deal with the copy push
4101 */
1c79356b 4102 break;
2d21ac55 4103 }
5ba3f43e 4104
1c79356b 4105 /*
2d21ac55
A
4106 * This is now a shadow based copy on write
4107 * fault -- it requires a copy up the shadow
4108 * chain.
6d2010ae 4109 */
39037602 4110 assert(m_object == VM_PAGE_OBJECT(m));
5ba3f43e 4111
6d2010ae 4112 if ((cur_object_lock_type == OBJECT_LOCK_SHARED) &&
39037602 4113 VM_FAULT_NEED_CS_VALIDATION(NULL, m, m_object)) {
6d2010ae
A
4114 goto upgrade_for_validation;
4115 }
4116
4117 /*
2d21ac55
A
4118 * Allocate a page in the original top level
4119 * object. Give up if allocate fails. Also
4120 * need to remember current page, as it's the
4121 * source of the copy.
1c79356b 4122 *
5ba3f43e 4123 * at this point we hold locks on both
2d21ac55
A
4124 * object and cur_object... no need to take
4125 * paging refs or mark pages BUSY since
4126 * we don't drop either object lock until
4127 * the page has been copied and inserted
1c79356b
A
4128 */
4129 cur_m = m;
39037602
A
4130 m = vm_page_grab_options(grab_options);
4131 m_object = NULL;
2d21ac55 4132
1c79356b 4133 if (m == VM_PAGE_NULL) {
0a7de745 4134 /*
2d21ac55
A
4135 * no free page currently available...
4136 * must take the slow path
4137 */
1c79356b
A
4138 break;
4139 }
1c79356b 4140 /*
2d21ac55 4141 * Now do the copy. Mark the source page busy...
1c79356b
A
4142 *
4143 * NOTE: This code holds the map lock across
4144 * the page copy.
4145 */
1c79356b
A
4146 vm_page_copy(cur_m, m);
4147 vm_page_insert(m, object, offset);
39037602 4148 m_object = object;
316670eb 4149 SET_PAGE_DIRTY(m, FALSE);
1c79356b
A
4150
4151 /*
2d21ac55 4152 * Now cope with the source page and object
1c79356b 4153 */
0a7de745
A
4154 if (object->ref_count > 1 && cur_m->vmp_pmapped) {
4155 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m));
4156 }
5ba3f43e 4157
d9a64523 4158 if (cur_m->vmp_clustered) {
fe8ab488
A
4159 VM_PAGE_COUNT_AS_PAGEIN(cur_m);
4160 VM_PAGE_CONSUME_CLUSTERED(cur_m);
39037602 4161 vm_fault_is_sequential(cur_object, cur_offset, fault_info.behavior);
fe8ab488 4162 }
2d21ac55 4163 need_collapse = TRUE;
1c79356b 4164
2d21ac55
A
4165 if (!cur_object->internal &&
4166 cur_object->copy_strategy == MEMORY_OBJECT_COPY_DELAY) {
0a7de745 4167 /*
2d21ac55
A
4168 * The object from which we've just
4169 * copied a page is most probably backed
4170 * by a vnode. We don't want to waste too
4171 * much time trying to collapse the VM objects
4172 * and create a bottleneck when several tasks
4173 * map the same file.
4174 */
0a7de745
A
4175 if (cur_object->copy == object) {
4176 /*
2d21ac55
A
4177 * Shared mapping or no COW yet.
4178 * We can never collapse a copy
4179 * object into its backing object.
4180 */
0a7de745 4181 need_collapse = FALSE;
2d21ac55 4182 } else if (cur_object->copy == object->shadow &&
0a7de745
A
4183 object->shadow->resident_page_count == 0) {
4184 /*
2d21ac55
A
4185 * Shared mapping after a COW occurred.
4186 */
0a7de745 4187 need_collapse = FALSE;
2d21ac55
A
4188 }
4189 }
1c79356b
A
4190 vm_object_unlock(cur_object);
4191
0a7de745
A
4192 if (need_collapse == FALSE) {
4193 vm_fault_collapse_skipped++;
4194 }
2d21ac55
A
4195 vm_fault_collapse_total++;
4196
4197 type_of_fault = DBG_COW_FAULT;
4198 VM_STAT_INCR(cow_faults);
4199 DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL);
4200 current_task()->cow_faults++;
1c79356b
A
4201
4202 goto FastPmapEnter;
2d21ac55 4203 } else {
1c79356b 4204 /*
2d21ac55 4205 * No page at cur_object, cur_offset... m == NULL
1c79356b 4206 */
1c79356b 4207 if (cur_object->pager_created) {
0a7de745 4208 int compressor_external_state = VM_EXTERNAL_STATE_UNKNOWN;
39236c6e 4209
0a7de745
A
4210 if (MUST_ASK_PAGER(cur_object, cur_offset, compressor_external_state) == TRUE) {
4211 int my_fault_type;
4212 int c_flags = C_DONT_BLOCK;
4213 boolean_t insert_cur_object = FALSE;
39236c6e 4214
0a7de745 4215 /*
2d21ac55 4216 * May have to talk to a pager...
39236c6e
A
4217 * if so, take the slow path by
4218 * doing a 'break' from the while (TRUE) loop
4219 *
4220 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
4221 * if the compressor is active and the page exists there
2d21ac55 4222 */
0a7de745 4223 if (compressor_external_state != VM_EXTERNAL_STATE_EXISTS) {
39236c6e 4224 break;
0a7de745 4225 }
39236c6e
A
4226
4227 if (map == kernel_map || real_map == kernel_map) {
4228 /*
4229 * can't call into the compressor with the kernel_map
4230 * lock held, since the compressor may try to operate
4231 * on the kernel map in order to return an empty c_segment
4232 */
4233 break;
4234 }
4235 if (object != cur_object) {
0a7de745 4236 if (fault_type & VM_PROT_WRITE) {
39236c6e 4237 c_flags |= C_KEEP;
0a7de745 4238 } else {
39236c6e 4239 insert_cur_object = TRUE;
0a7de745 4240 }
39236c6e
A
4241 }
4242 if (insert_cur_object == TRUE) {
39236c6e 4243 if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
39236c6e
A
4244 cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4245
4246 if (vm_object_lock_upgrade(cur_object) == FALSE) {
4247 /*
4248 * couldn't upgrade so go do a full retry
4249 * immediately since we can no longer be
4250 * certain about cur_object (since we
4251 * don't hold a reference on it)...
4252 * first drop the top object lock
4253 */
4254 vm_object_unlock(object);
4255
4256 vm_map_unlock_read(map);
0a7de745 4257 if (real_map != map) {
39236c6e 4258 vm_map_unlock(real_map);
0a7de745 4259 }
39236c6e
A
4260
4261 goto RetryFault;
4262 }
4263 }
4264 } else if (object_lock_type == OBJECT_LOCK_SHARED) {
39236c6e
A
4265 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4266
4267 if (object != cur_object) {
4268 /*
4269 * we can't go for the upgrade on the top
4270 * lock since the upgrade may block waiting
4271 * for readers to drain... since we hold
4272 * cur_object locked at this point, waiting
4273 * for the readers to drain would represent
4274 * a lock order inversion since the lock order
4275 * for objects is the reference order in the
4276 * shadown chain
4277 */
4278 vm_object_unlock(object);
4279 vm_object_unlock(cur_object);
4280
4281 vm_map_unlock_read(map);
0a7de745 4282 if (real_map != map) {
39236c6e 4283 vm_map_unlock(real_map);
0a7de745 4284 }
39236c6e
A
4285
4286 goto RetryFault;
4287 }
4288 if (vm_object_lock_upgrade(object) == FALSE) {
4289 /*
4290 * couldn't upgrade, so explictly take the lock
4291 * exclusively and go relookup the page since we
4292 * will have dropped the object lock and
4293 * a different thread could have inserted
4294 * a page at this offset
4295 * no need for a full retry since we're
4296 * at the top level of the object chain
4297 */
4298 vm_object_lock(object);
5ba3f43e 4299
39236c6e
A
4300 continue;
4301 }
4302 }
39037602
A
4303 m = vm_page_grab_options(grab_options);
4304 m_object = NULL;
39236c6e
A
4305
4306 if (m == VM_PAGE_NULL) {
4307 /*
4308 * no free page currently available...
4309 * must take the slow path
4310 */
4311 break;
4312 }
fe8ab488
A
4313
4314 /*
4315 * The object is and remains locked
4316 * so no need to take a
4317 * "paging_in_progress" reference.
4318 */
4319 boolean_t shared_lock;
4320 if ((object == cur_object &&
0a7de745 4321 object_lock_type == OBJECT_LOCK_EXCLUSIVE) ||
fe8ab488 4322 (object != cur_object &&
0a7de745 4323 cur_object_lock_type == OBJECT_LOCK_EXCLUSIVE)) {
fe8ab488
A
4324 shared_lock = FALSE;
4325 } else {
4326 shared_lock = TRUE;
4327 }
4328
4329 kr = vm_compressor_pager_get(
4330 cur_object->pager,
4331 (cur_offset +
0a7de745 4332 cur_object->paging_offset),
39037602 4333 VM_PAGE_GET_PHYS_PAGE(m),
fe8ab488
A
4334 &my_fault_type,
4335 c_flags,
4336 &compressed_count_delta);
4337
4338 vm_compressor_pager_count(
4339 cur_object->pager,
4340 compressed_count_delta,
4341 shared_lock,
4342 cur_object);
4343
4344 if (kr != KERN_SUCCESS) {
39037602
A
4345 vm_page_release(m, FALSE);
4346 m = VM_PAGE_NULL;
39236c6e
A
4347 break;
4348 }
d9a64523 4349 m->vmp_dirty = TRUE;
39236c6e 4350
fe8ab488
A
4351 /*
4352 * If the object is purgeable, its
4353 * owner's purgeable ledgers will be
4354 * updated in vm_page_insert() but the
4355 * page was also accounted for in a
4356 * "compressed purgeable" ledger, so
4357 * update that now.
4358 */
4359 if (object != cur_object &&
4360 !insert_cur_object) {
4361 /*
4362 * We're not going to insert
4363 * the decompressed page into
4364 * the object it came from.
4365 *
4366 * We're dealing with a
4367 * copy-on-write fault on
4368 * "object".
4369 * We're going to decompress
4370 * the page directly into the
4371 * target "object" while
4372 * keepin the compressed
4373 * page for "cur_object", so
4374 * no ledger update in that
4375 * case.
4376 */
d9a64523 4377 } else if (((cur_object->purgable ==
0a7de745
A
4378 VM_PURGABLE_DENY) &&
4379 (!cur_object->vo_ledger_tag)) ||
4380 (cur_object->vo_owner ==
4381 NULL)) {
fe8ab488
A
4382 /*
4383 * "cur_object" is not purgeable
d9a64523
A
4384 * and is not ledger-taged, or
4385 * there's no owner for it,
4386 * so no owner's ledgers to
4387 * update.
fe8ab488
A
4388 */
4389 } else {
4390 /*
4391 * One less compressed
d9a64523 4392 * purgeable/tagged page for
fe8ab488
A
4393 * cur_object's owner.
4394 */
d9a64523 4395 vm_object_owner_compressed_update(
fe8ab488
A
4396 cur_object,
4397 -1);
4398 }
4399
4400 if (insert_cur_object) {
39236c6e 4401 vm_page_insert(m, cur_object, cur_offset);
39037602 4402 m_object = cur_object;
fe8ab488 4403 } else {
39236c6e 4404 vm_page_insert(m, object, offset);
39037602 4405 m_object = object;
fe8ab488 4406 }
39236c6e 4407
39037602 4408 if ((m_object->wimg_bits & VM_WIMG_MASK) != VM_WIMG_USE_DEFAULT) {
0a7de745 4409 /*
39236c6e
A
4410 * If the page is not cacheable,
4411 * we can't let its contents
4412 * linger in the data cache
4413 * after the decompression.
4414 */
39037602 4415 pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m));
39236c6e 4416 }
fe8ab488 4417
39236c6e
A
4418 type_of_fault = my_fault_type;
4419
4420 VM_STAT_INCR(decompressions);
4421
4422 if (cur_object != object) {
4423 if (insert_cur_object) {
4424 top_object = object;
4425 /*
4426 * switch to the object that has the new page
4427 */
4428 object = cur_object;
4429 object_lock_type = cur_object_lock_type;
4430 } else {
4431 vm_object_unlock(cur_object);
4432 cur_object = object;
4433 }
4434 }
4435 goto FastPmapEnter;
2d21ac55 4436 }
1c79356b 4437 /*
2d21ac55
A
4438 * existence map present and indicates
4439 * that the pager doesn't have this page
1c79356b 4440 */
1c79356b 4441 }
1c79356b 4442 if (cur_object->shadow == VM_OBJECT_NULL) {
2d21ac55
A
4443 /*
4444 * Zero fill fault. Page gets
4445 * inserted into the original object.
4446 */
b0d623f7 4447 if (cur_object->shadow_severed ||
39037602
A
4448 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object) ||
4449 cur_object == compressor_object ||
4450 cur_object == kernel_object ||
4451 cur_object == vm_submap_object) {
0a7de745
A
4452 if (object != cur_object) {
4453 vm_object_unlock(cur_object);
4454 }
1c79356b 4455 vm_object_unlock(object);
2d21ac55 4456
1c79356b 4457 vm_map_unlock_read(map);
0a7de745 4458 if (real_map != map) {
91447636 4459 vm_map_unlock(real_map);
0a7de745 4460 }
1c79356b 4461
2d21ac55
A
4462 kr = KERN_MEMORY_ERROR;
4463 goto done;
4464 }
0a7de745 4465 if (cur_object != object) {
2d21ac55 4466 vm_object_unlock(cur_object);
1c79356b 4467
2d21ac55 4468 cur_object = object;
55e303ae 4469 }
2d21ac55 4470 if (object_lock_type == OBJECT_LOCK_SHARED) {
0a7de745 4471 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2d21ac55
A
4472
4473 if (vm_object_lock_upgrade(object) == FALSE) {
0a7de745 4474 /*
2d21ac55
A
4475 * couldn't upgrade so do a full retry on the fault
4476 * since we dropped the object lock which
4477 * could allow another thread to insert
4478 * a page at this offset
4479 */
0a7de745
A
4480 vm_map_unlock_read(map);
4481 if (real_map != map) {
4482 vm_map_unlock(real_map);
4483 }
2d21ac55
A
4484
4485 goto RetryFault;
4486 }
1c79356b
A
4487 }
4488 m = vm_page_alloc(object, offset);
39037602 4489 m_object = NULL;
2d21ac55 4490
1c79356b 4491 if (m == VM_PAGE_NULL) {
0a7de745 4492 /*
2d21ac55
A
4493 * no free page currently available...
4494 * must take the slow path
4495 */
1c79356b
A
4496 break;
4497 }
39037602 4498 m_object = object;
1c79356b 4499
1c79356b 4500 /*
2d21ac55 4501 * Now zero fill page...
5ba3f43e 4502 * the page is probably going to
2d21ac55
A
4503 * be written soon, so don't bother
4504 * to clear the modified bit
1c79356b 4505 *
2d21ac55
A
4506 * NOTE: This code holds the map
4507 * lock across the zero fill.
1c79356b 4508 */
2d21ac55 4509 type_of_fault = vm_fault_zero_page(m, map->no_zero_fill);
143cc14e 4510
1c79356b 4511 goto FastPmapEnter;
0a7de745 4512 }
1c79356b 4513 /*
2d21ac55 4514 * On to the next level in the shadow chain
1c79356b 4515 */
6d2010ae 4516 cur_offset += cur_object->vo_shadow_offset;
1c79356b 4517 new_object = cur_object->shadow;
2d21ac55
A
4518
4519 /*
4520 * take the new_object's lock with the indicated state
4521 */
0a7de745
A
4522 if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
4523 vm_object_lock_shared(new_object);
4524 } else {
4525 vm_object_lock(new_object);
4526 }
2d21ac55 4527
0a7de745 4528 if (cur_object != object) {
1c79356b 4529 vm_object_unlock(cur_object);
0a7de745 4530 }
2d21ac55 4531
1c79356b
A
4532 cur_object = new_object;
4533
4534 continue;
4535 }
4536 }
1c79356b 4537 /*
2d21ac55
A
4538 * Cleanup from fast fault failure. Drop any object
4539 * lock other than original and drop map lock.
1c79356b 4540 */
0a7de745 4541 if (object != cur_object) {
1c79356b 4542 vm_object_unlock(cur_object);
0a7de745 4543 }
2d21ac55
A
4544
4545 /*
4546 * must own the object lock exclusively at this point
4547 */
4548 if (object_lock_type == OBJECT_LOCK_SHARED) {
0a7de745 4549 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2d21ac55
A
4550
4551 if (vm_object_lock_upgrade(object) == FALSE) {
0a7de745 4552 /*
2d21ac55
A
4553 * couldn't upgrade, so explictly
4554 * take the lock exclusively
4555 * no need to retry the fault at this
4556 * point since "vm_fault_page" will
4557 * completely re-evaluate the state
4558 */
0a7de745 4559 vm_object_lock(object);
2d21ac55 4560 }
1c79356b 4561 }
143cc14e 4562
2d21ac55
A
4563handle_copy_delay:
4564 vm_map_unlock_read(map);
0a7de745 4565 if (real_map != map) {
91447636 4566 vm_map_unlock(real_map);
0a7de745 4567 }
1c79356b 4568
813fb2f6 4569 if (__improbable(object == compressor_object ||
0a7de745
A
4570 object == kernel_object ||
4571 object == vm_submap_object)) {
813fb2f6
A
4572 /*
4573 * These objects are explicitly managed and populated by the
4574 * kernel. The virtual ranges backed by these objects should
4575 * either have wired pages or "holes" that are not supposed to
4576 * be accessed at all until they get explicitly populated.
4577 * We should never have to resolve a fault on a mapping backed
4578 * by one of these VM objects and providing a zero-filled page
4579 * would be wrong here, so let's fail the fault and let the
4580 * caller crash or recover.
4581 */
4582 vm_object_unlock(object);
4583 kr = KERN_MEMORY_ERROR;
4584 goto done;
4585 }
4586
39037602
A
4587 assert(object != compressor_object);
4588 assert(object != kernel_object);
4589 assert(object != vm_submap_object);
4590
0a7de745 4591 /*
2d21ac55
A
4592 * Make a reference to this object to
4593 * prevent its disposal while we are messing with
4594 * it. Once we have the reference, the map is free
4595 * to be diddled. Since objects reference their
4596 * shadows (and copies), they will stay around as well.
1c79356b 4597 */
2d21ac55 4598 vm_object_reference_locked(object);
1c79356b
A
4599 vm_object_paging_begin(object);
4600
0a7de745 4601 XPR(XPR_VM_FAULT, "vm_fault -> vm_fault_page\n", 0, 0, 0, 0, 0);
55e303ae 4602
2d21ac55 4603 error_code = 0;
55e303ae 4604
39236c6e 4605 result_page = VM_PAGE_NULL;
1c79356b 4606 kr = vm_fault_page(object, offset, fault_type,
0a7de745
A
4607 (change_wiring && !wired),
4608 FALSE, /* page not looked up */
4609 &prot, &result_page, &top_page,
4610 &type_of_fault,
4611 &error_code, map->no_zero_fill,
4612 FALSE, &fault_info);
1c79356b
A
4613
4614 /*
2d21ac55
A
4615 * if kr != VM_FAULT_SUCCESS, then the paging reference
4616 * has been dropped and the object unlocked... the ref_count
4617 * is still held
4618 *
4619 * if kr == VM_FAULT_SUCCESS, then the paging reference
4620 * is still held along with the ref_count on the original object
4621 *
b0d623f7 4622 * the object is returned locked with a paging reference
2d21ac55 4623 *
5ba3f43e 4624 * if top_page != NULL, then it's BUSY and the
2d21ac55
A
4625 * object it belongs to has a paging reference
4626 * but is returned unlocked
1c79356b 4627 */
b0d623f7
A
4628 if (kr != VM_FAULT_SUCCESS &&
4629 kr != VM_FAULT_SUCCESS_NO_VM_PAGE) {
0a7de745 4630 /*
2d21ac55
A
4631 * we didn't succeed, lose the object reference immediately.
4632 */
1c79356b
A
4633 vm_object_deallocate(object);
4634
2d21ac55
A
4635 /*
4636 * See why we failed, and take corrective action.
4637 */
4638 switch (kr) {
1c79356b 4639 case VM_FAULT_MEMORY_SHORTAGE:
5ba3f43e 4640 if (vm_page_wait((change_wiring) ?
0a7de745
A
4641 THREAD_UNINT :
4642 THREAD_ABORTSAFE)) {
1c79356b 4643 goto RetryFault;
0a7de745
A
4644 }
4645 /*
4646 * fall thru
4647 */
1c79356b
A
4648 case VM_FAULT_INTERRUPTED:
4649 kr = KERN_ABORTED;
4650 goto done;
4651 case VM_FAULT_RETRY:
4652 goto RetryFault;
1c79356b 4653 case VM_FAULT_MEMORY_ERROR:
0a7de745 4654 if (error_code) {
1c79356b 4655 kr = error_code;
0a7de745 4656 } else {
1c79356b 4657 kr = KERN_MEMORY_ERROR;
0a7de745 4658 }
1c79356b 4659 goto done;
b0d623f7
A
4660 default:
4661 panic("vm_fault: unexpected error 0x%x from "
0a7de745 4662 "vm_fault_page()\n", kr);
2d21ac55 4663 }
1c79356b 4664 }
1c79356b 4665 m = result_page;
39037602 4666 m_object = NULL;
1c79356b 4667
2d21ac55 4668 if (m != VM_PAGE_NULL) {
39037602 4669 m_object = VM_PAGE_OBJECT(m);
0b4e3aa0 4670 assert((change_wiring && !wired) ?
0a7de745
A
4671 (top_page == VM_PAGE_NULL) :
4672 ((top_page == VM_PAGE_NULL) == (m_object == object)));
0b4e3aa0 4673 }
1c79356b
A
4674
4675 /*
2d21ac55
A
4676 * What to do with the resulting page from vm_fault_page
4677 * if it doesn't get entered into the physical map:
1c79356b 4678 */
0a7de745
A
4679#define RELEASE_PAGE(m) \
4680 MACRO_BEGIN \
4681 PAGE_WAKEUP_DONE(m); \
4682 if ( !VM_PAGE_PAGEABLE(m)) { \
4683 vm_page_lockspin_queues(); \
4684 if ( !VM_PAGE_PAGEABLE(m)) \
4685 vm_page_activate(m); \
4686 vm_page_unlock_queues(); \
4687 } \
1c79356b
A
4688 MACRO_END
4689
5ba3f43e
A
4690
4691 object_locks_dropped = FALSE;
1c79356b 4692 /*
2d21ac55 4693 * We must verify that the maps have not changed
5ba3f43e
A
4694 * since our last lookup. vm_map_verify() needs the
4695 * map lock (shared) but we are holding object locks.
4696 * So we do a try_lock() first and, if that fails, we
4697 * drop the object locks and go in for the map lock again.
1c79356b 4698 */
5ba3f43e 4699 if (!vm_map_try_lock_read(original_map)) {
5ba3f43e
A
4700 if (m != VM_PAGE_NULL) {
4701 old_copy_object = m_object->copy;
4702 vm_object_unlock(m_object);
4703 } else {
4704 old_copy_object = VM_OBJECT_NULL;
4705 vm_object_unlock(object);
4706 }
4707
4708 object_locks_dropped = TRUE;
4709
4710 vm_map_lock_read(original_map);
b0d623f7 4711 }
2d21ac55 4712
1c79356b 4713 if ((map != original_map) || !vm_map_verify(map, &version)) {
5ba3f43e
A
4714 if (object_locks_dropped == FALSE) {
4715 if (m != VM_PAGE_NULL) {
4716 old_copy_object = m_object->copy;
4717 vm_object_unlock(m_object);
4718 } else {
4719 old_copy_object = VM_OBJECT_NULL;
4720 vm_object_unlock(object);
4721 }
0a7de745 4722
5ba3f43e
A
4723 object_locks_dropped = TRUE;
4724 }
4725
4726 /*
4727 * no object locks are held at this point
4728 */
0a7de745
A
4729 vm_object_t retry_object;
4730 vm_object_offset_t retry_offset;
4731 vm_prot_t retry_prot;
1c79356b
A
4732
4733 /*
2d21ac55
A
4734 * To avoid trying to write_lock the map while another
4735 * thread has it read_locked (in vm_map_pageable), we
4736 * do not try for write permission. If the page is
4737 * still writable, we will get write permission. If it
4738 * is not, or has been marked needs_copy, we enter the
4739 * mapping without write permission, and will merely
4740 * take another fault.
1c79356b
A
4741 */
4742 map = original_map;
2d21ac55 4743
1c79356b 4744 kr = vm_map_lookup_locked(&map, vaddr,
0a7de745
A
4745 fault_type & ~VM_PROT_WRITE,
4746 OBJECT_LOCK_EXCLUSIVE, &version,
4747 &retry_object, &retry_offset, &retry_prot,
4748 &wired,
4749 &fault_info,
4750 &real_map);
91447636 4751 pmap = real_map->pmap;
1c79356b
A
4752
4753 if (kr != KERN_SUCCESS) {
4754 vm_map_unlock_read(map);
2d21ac55
A
4755
4756 if (m != VM_PAGE_NULL) {
39037602
A
4757 assert(VM_PAGE_OBJECT(m) == m_object);
4758
0a7de745 4759 /*
2d21ac55
A
4760 * retake the lock so that
4761 * we can drop the paging reference
4762 * in vm_fault_cleanup and do the
4763 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4764 */
39037602 4765 vm_object_lock(m_object);
2d21ac55 4766
0b4e3aa0 4767 RELEASE_PAGE(m);
2d21ac55 4768
39037602 4769 vm_fault_cleanup(m_object, top_page);
0b4e3aa0 4770 } else {
0a7de745 4771 /*
2d21ac55
A
4772 * retake the lock so that
4773 * we can drop the paging reference
4774 * in vm_fault_cleanup
4775 */
0a7de745 4776 vm_object_lock(object);
2d21ac55 4777
0a7de745 4778 vm_fault_cleanup(object, top_page);
0b4e3aa0 4779 }
2d21ac55
A
4780 vm_object_deallocate(object);
4781
1c79356b
A
4782 goto done;
4783 }
1c79356b 4784 vm_object_unlock(retry_object);
1c79356b 4785
2d21ac55 4786 if ((retry_object != object) || (retry_offset != offset)) {
1c79356b 4787 vm_map_unlock_read(map);
0a7de745 4788 if (real_map != map) {
91447636 4789 vm_map_unlock(real_map);
0a7de745 4790 }
2d21ac55
A
4791
4792 if (m != VM_PAGE_NULL) {
39037602
A
4793 assert(VM_PAGE_OBJECT(m) == m_object);
4794
0a7de745 4795 /*
2d21ac55
A
4796 * retake the lock so that
4797 * we can drop the paging reference
4798 * in vm_fault_cleanup and do the
4799 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4800 */
0a7de745 4801 vm_object_lock(m_object);
2d21ac55 4802
0b4e3aa0 4803 RELEASE_PAGE(m);
2d21ac55 4804
39037602 4805 vm_fault_cleanup(m_object, top_page);
0b4e3aa0 4806 } else {
0a7de745 4807 /*
2d21ac55
A
4808 * retake the lock so that
4809 * we can drop the paging reference
4810 * in vm_fault_cleanup
4811 */
0a7de745 4812 vm_object_lock(object);
2d21ac55 4813
0a7de745 4814 vm_fault_cleanup(object, top_page);
0b4e3aa0 4815 }
2d21ac55
A
4816 vm_object_deallocate(object);
4817
1c79356b
A
4818 goto RetryFault;
4819 }
1c79356b 4820 /*
2d21ac55
A
4821 * Check whether the protection has changed or the object
4822 * has been copied while we left the map unlocked.
1c79356b 4823 */
5ba3f43e
A
4824 if (pmap_has_prot_policy(retry_prot)) {
4825 /* If the pmap layer cares, pass the full set. */
4826 prot = retry_prot;
4827 } else {
4828 prot &= retry_prot;
4829 }
0b4e3aa0 4830 }
1c79356b 4831
5ba3f43e
A
4832 if (object_locks_dropped == TRUE) {
4833 if (m != VM_PAGE_NULL) {
4834 vm_object_lock(m_object);
4835
4836 if (m_object->copy != old_copy_object) {
4837 /*
4838 * The copy object changed while the top-level object
4839 * was unlocked, so take away write permission.
4840 */
4841 assert(!pmap_has_prot_policy(prot));
4842 prot &= ~VM_PROT_WRITE;
4843 }
0a7de745 4844 } else {
5ba3f43e 4845 vm_object_lock(object);
0a7de745 4846 }
5ba3f43e
A
4847
4848 object_locks_dropped = FALSE;
4849 }
1c79356b
A
4850
4851 /*
2d21ac55
A
4852 * If we want to wire down this page, but no longer have
4853 * adequate permissions, we must start all over.
1c79356b 4854 */
2d21ac55 4855 if (wired && (fault_type != (prot | VM_PROT_WRITE))) {
5ba3f43e 4856 vm_map_unlock_read(map);
0a7de745 4857 if (real_map != map) {
91447636 4858 vm_map_unlock(real_map);
0a7de745 4859 }
1c79356b 4860
2d21ac55 4861 if (m != VM_PAGE_NULL) {
39037602
A
4862 assert(VM_PAGE_OBJECT(m) == m_object);
4863
2d21ac55 4864 RELEASE_PAGE(m);
91447636 4865
39037602 4866 vm_fault_cleanup(m_object, top_page);
0a7de745
A
4867 } else {
4868 vm_fault_cleanup(object, top_page);
4869 }
0b4e3aa0 4870
2d21ac55 4871 vm_object_deallocate(object);
55e303ae 4872
2d21ac55
A
4873 goto RetryFault;
4874 }
4875 if (m != VM_PAGE_NULL) {
55e303ae 4876 /*
2d21ac55
A
4877 * Put this page into the physical map.
4878 * We had to do the unlock above because pmap_enter
4879 * may cause other faults. The page may be on
4880 * the pageout queues. If the pageout daemon comes
4881 * across the page, it will remove it from the queues.
55e303ae 4882 */
2d21ac55
A
4883 if (caller_pmap) {
4884 kr = vm_fault_enter(m,
0a7de745
A
4885 caller_pmap,
4886 caller_pmap_addr,
4887 prot,
4888 caller_prot,
4889 wired,
4890 change_wiring,
4891 wire_tag,
4892 &fault_info,
4893 NULL,
4894 &type_of_fault);
2d21ac55
A
4895 } else {
4896 kr = vm_fault_enter(m,
0a7de745
A
4897 pmap,
4898 vaddr,
4899 prot,
4900 caller_prot,
4901 wired,
4902 change_wiring,
4903 wire_tag,
4904 &fault_info,
4905 NULL,
4906 &type_of_fault);
2d21ac55 4907 }
39037602
A
4908 assert(VM_PAGE_OBJECT(m) == m_object);
4909
4910#if DEVELOPMENT || DEBUG
0a7de745
A
4911 {
4912 int event_code = 0;
39037602 4913
0a7de745
A
4914 if (m_object->internal) {
4915 event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_INTERNAL));
4916 } else if (m_object->object_is_shared_cache) {
4917 event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_SHAREDCACHE));
4918 } else {
4919 event_code = (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_REAL_FAULT_ADDR_EXTERNAL));
4920 }
39037602 4921
0a7de745 4922 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, event_code, trace_real_vaddr, (fault_info.user_tag << 16) | (caller_prot << 8) | type_of_fault, m->vmp_offset, get_current_unique_pid(), 0);
39037602 4923
0a7de745
A
4924 DTRACE_VM6(real_fault, vm_map_offset_t, real_vaddr, vm_map_offset_t, m->vmp_offset, int, event_code, int, caller_prot, int, type_of_fault, int, fault_info.user_tag);
4925 }
39037602 4926#endif
2d21ac55
A
4927 if (kr != KERN_SUCCESS) {
4928 /* abort this page fault */
5ba3f43e 4929 vm_map_unlock_read(map);
0a7de745 4930 if (real_map != map) {
2d21ac55 4931 vm_map_unlock(real_map);
0a7de745 4932 }
2d21ac55 4933 PAGE_WAKEUP_DONE(m);
39037602 4934 vm_fault_cleanup(m_object, top_page);
2d21ac55
A
4935 vm_object_deallocate(object);
4936 goto done;
0b4e3aa0 4937 }
fe8ab488
A
4938 if (physpage_p != NULL) {
4939 /* for vm_map_wire_and_extract() */
39037602 4940 *physpage_p = VM_PAGE_GET_PHYS_PAGE(m);
fe8ab488 4941 if (prot & VM_PROT_WRITE) {
39037602 4942 vm_object_lock_assert_exclusive(m_object);
d9a64523 4943 m->vmp_dirty = TRUE;
fe8ab488
A
4944 }
4945 }
0b4e3aa0 4946 } else {
0a7de745
A
4947 vm_map_entry_t entry;
4948 vm_map_offset_t laddr;
4949 vm_map_offset_t ldelta, hdelta;
143cc14e 4950
5ba3f43e 4951 /*
0b4e3aa0 4952 * do a pmap block mapping from the physical address
5ba3f43e 4953 * in the object
0b4e3aa0 4954 */
9bccf70c 4955
0a7de745 4956 if (real_map != map) {
91447636 4957 vm_map_unlock(real_map);
0a7de745 4958 }
2d21ac55 4959
9bccf70c
A
4960 if (original_map != map) {
4961 vm_map_unlock_read(map);
4962 vm_map_lock_read(original_map);
4963 map = original_map;
4964 }
91447636 4965 real_map = map;
9bccf70c
A
4966
4967 laddr = vaddr;
4968 hdelta = 0xFFFFF000;
4969 ldelta = 0xFFFFF000;
4970
2d21ac55 4971 while (vm_map_lookup_entry(map, laddr, &entry)) {
0a7de745 4972 if (ldelta > (laddr - entry->vme_start)) {
9bccf70c 4973 ldelta = laddr - entry->vme_start;
0a7de745
A
4974 }
4975 if (hdelta > (entry->vme_end - laddr)) {
9bccf70c 4976 hdelta = entry->vme_end - laddr;
0a7de745 4977 }
2d21ac55 4978 if (entry->is_sub_map) {
5ba3f43e 4979 laddr = ((laddr - entry->vme_start)
0a7de745 4980 + VME_OFFSET(entry));
3e170ce0 4981 vm_map_lock_read(VME_SUBMAP(entry));
2d21ac55 4982
0a7de745 4983 if (map != real_map) {
9bccf70c 4984 vm_map_unlock_read(map);
0a7de745 4985 }
2d21ac55 4986 if (entry->use_pmap) {
91447636 4987 vm_map_unlock_read(real_map);
3e170ce0 4988 real_map = VME_SUBMAP(entry);
9bccf70c 4989 }
3e170ce0 4990 map = VME_SUBMAP(entry);
9bccf70c
A
4991 } else {
4992 break;
4993 }
4994 }
4995
5ba3f43e 4996 if (vm_map_lookup_entry(map, laddr, &entry) &&
3e170ce0
A
4997 (VME_OBJECT(entry) != NULL) &&
4998 (VME_OBJECT(entry) == object)) {
4999 int superpage;
2d21ac55 5000
3e170ce0 5001 if (!object->pager_created &&
4bd07ac2
A
5002 object->phys_contiguous &&
5003 VME_OFFSET(entry) == 0 &&
5004 (entry->vme_end - entry->vme_start == object->vo_size) &&
0a7de745 5005 VM_MAP_PAGE_ALIGNED(entry->vme_start, (object->vo_size - 1))) {
3e170ce0
A
5006 superpage = VM_MEM_SUPERPAGE;
5007 } else {
5008 superpage = 0;
5009 }
fe8ab488
A
5010
5011 if (superpage && physpage_p) {
5012 /* for vm_map_wire_and_extract() */
3e170ce0 5013 *physpage_p = (ppnum_t)
0a7de745
A
5014 ((((vm_map_offset_t)
5015 object->vo_shadow_offset)
5016 + VME_OFFSET(entry)
5017 + (laddr - entry->vme_start))
5018 >> PAGE_SHIFT);
fe8ab488
A
5019 }
5020
2d21ac55
A
5021 if (caller_pmap) {
5022 /*
5023 * Set up a block mapped area
5024 */
fe8ab488 5025 assert((uint32_t)((ldelta + hdelta) >> PAGE_SHIFT) == ((ldelta + hdelta) >> PAGE_SHIFT));
5ba3f43e 5026 kr = pmap_map_block(caller_pmap,
0a7de745
A
5027 (addr64_t)(caller_pmap_addr - ldelta),
5028 (ppnum_t)((((vm_map_offset_t) (VME_OBJECT(entry)->vo_shadow_offset)) +
5029 VME_OFFSET(entry) + (laddr - entry->vme_start) - ldelta) >> PAGE_SHIFT),
5030 (uint32_t)((ldelta + hdelta) >> PAGE_SHIFT), prot,
5031 (VM_WIMG_MASK & (int)object->wimg_bits) | superpage, 0);
5ba3f43e
A
5032
5033 if (kr != KERN_SUCCESS) {
5034 goto cleanup;
5035 }
5036 } else {
2d21ac55
A
5037 /*
5038 * Set up a block mapped area
5039 */
fe8ab488 5040 assert((uint32_t)((ldelta + hdelta) >> PAGE_SHIFT) == ((ldelta + hdelta) >> PAGE_SHIFT));
5ba3f43e 5041 kr = pmap_map_block(real_map->pmap,
0a7de745
A
5042 (addr64_t)(vaddr - ldelta),
5043 (ppnum_t)((((vm_map_offset_t)(VME_OBJECT(entry)->vo_shadow_offset)) +
5044 VME_OFFSET(entry) + (laddr - entry->vme_start) - ldelta) >> PAGE_SHIFT),
5045 (uint32_t)((ldelta + hdelta) >> PAGE_SHIFT), prot,
5046 (VM_WIMG_MASK & (int)object->wimg_bits) | superpage, 0);
5ba3f43e
A
5047
5048 if (kr != KERN_SUCCESS) {
5049 goto cleanup;
5050 }
9bccf70c
A
5051 }
5052 }
0b4e3aa0 5053 }
1c79356b 5054
5ba3f43e
A
5055 /*
5056 * Success
5057 */
5058 kr = KERN_SUCCESS;
5059
5060 /*
5061 * TODO: could most of the done cases just use cleanup?
5062 */
5063cleanup:
1c79356b 5064 /*
2d21ac55 5065 * Unlock everything, and return
1c79356b 5066 */
5ba3f43e 5067 vm_map_unlock_read(map);
0a7de745 5068 if (real_map != map) {
91447636 5069 vm_map_unlock(real_map);
0a7de745 5070 }
2d21ac55
A
5071
5072 if (m != VM_PAGE_NULL) {
39037602
A
5073 assert(VM_PAGE_OBJECT(m) == m_object);
5074
d9a64523 5075 if (!m_object->internal && (fault_type & VM_PROT_WRITE)) {
d9a64523
A
5076 vm_object_paging_begin(m_object);
5077
5078 assert(written_on_object == VM_OBJECT_NULL);
5079 written_on_object = m_object;
5080 written_on_pager = m_object->pager;
5081 written_on_offset = m_object->paging_offset + m->vmp_offset;
5082 }
0b4e3aa0 5083 PAGE_WAKEUP_DONE(m);
1c79356b 5084
39037602 5085 vm_fault_cleanup(m_object, top_page);
0a7de745
A
5086 } else {
5087 vm_fault_cleanup(object, top_page);
5088 }
1c79356b 5089
2d21ac55
A
5090 vm_object_deallocate(object);
5091
0a7de745 5092#undef RELEASE_PAGE
91447636 5093
2d21ac55 5094done:
9bccf70c 5095 thread_interrupt_level(interruptible_state);
1c79356b 5096
39236c6e 5097 /*
04b8595b 5098 * Only I/O throttle on faults which cause a pagein/swapin.
39236c6e
A
5099 */
5100 if ((type_of_fault == DBG_PAGEIND_FAULT) || (type_of_fault == DBG_PAGEINV_FAULT) || (type_of_fault == DBG_COMPRESSOR_SWAPIN_FAULT)) {
5101 throttle_lowpri_io(1);
04b8595b
A
5102 } else {
5103 if (kr == KERN_SUCCESS && type_of_fault != DBG_CACHE_HIT_FAULT && type_of_fault != DBG_GUARD_FAULT) {
04b8595b 5104 if ((throttle_delay = vm_page_throttled(TRUE))) {
04b8595b 5105 if (vm_debug_events) {
0a7de745 5106 if (type_of_fault == DBG_COMPRESSOR_FAULT) {
04b8595b 5107 VM_DEBUG_EVENT(vmf_compressordelay, VMF_COMPRESSORDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0);
0a7de745 5108 } else if (type_of_fault == DBG_COW_FAULT) {
04b8595b 5109 VM_DEBUG_EVENT(vmf_cowdelay, VMF_COWDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0);
0a7de745 5110 } else {
04b8595b 5111 VM_DEBUG_EVENT(vmf_zfdelay, VMF_ZFDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0);
0a7de745 5112 }
04b8595b
A
5113 }
5114 delay(throttle_delay);
5115 }
5116 }
5117 }
d9a64523
A
5118
5119 if (written_on_object) {
d9a64523
A
5120 vnode_pager_dirtied(written_on_pager, written_on_offset, written_on_offset + PAGE_SIZE_64);
5121
5122 vm_object_lock(written_on_object);
5123 vm_object_paging_end(written_on_object);
5124 vm_object_unlock(written_on_object);
5125
5126 written_on_object = VM_OBJECT_NULL;
5127 }
5128
5129 if (rtfault) {
5130 vm_record_rtfault(cthread, fstart, trace_vaddr, type_of_fault);
5131 }
5132
5ba3f43e 5133 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
5134 (MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END,
5135 ((uint64_t)trace_vaddr >> 32),
5136 trace_vaddr,
5137 kr,
5138 type_of_fault,
5139 0);
5140
5141 return kr;
1c79356b
A
5142}
5143
5144/*
5145 * vm_fault_wire:
5146 *
5147 * Wire down a range of virtual addresses in a map.
5148 */
5149kern_return_t
5150vm_fault_wire(
0a7de745
A
5151 vm_map_t map,
5152 vm_map_entry_t entry,
3e170ce0 5153 vm_prot_t prot,
5ba3f43e 5154 vm_tag_t wire_tag,
0a7de745
A
5155 pmap_t pmap,
5156 vm_map_offset_t pmap_addr,
5157 ppnum_t *physpage_p)
1c79356b 5158{
0a7de745
A
5159 vm_map_offset_t va;
5160 vm_map_offset_t end_addr = entry->vme_end;
5161 kern_return_t rc;
1c79356b
A
5162
5163 assert(entry->in_transition);
5164
5ba3f43e
A
5165 if ((VME_OBJECT(entry) != NULL) &&
5166 !entry->is_sub_map &&
3e170ce0 5167 VME_OBJECT(entry)->phys_contiguous) {
9bccf70c
A
5168 return KERN_SUCCESS;
5169 }
5170
1c79356b
A
5171 /*
5172 * Inform the physical mapping system that the
5173 * range of addresses may not fault, so that
5174 * page tables and such can be locked down as well.
5175 */
5176
5ba3f43e 5177 pmap_pageable(pmap, pmap_addr,
0a7de745 5178 pmap_addr + (end_addr - entry->vme_start), FALSE);
1c79356b
A
5179
5180 /*
5181 * We simulate a fault to get the page and enter it
5182 * in the physical map.
5183 */
5184
5185 for (va = entry->vme_start; va < end_addr; va += PAGE_SIZE) {
5ba3f43e 5186 rc = vm_fault_wire_fast(map, va, prot, wire_tag, entry, pmap,
0a7de745
A
5187 pmap_addr + (va - entry->vme_start),
5188 physpage_p);
fe8ab488 5189 if (rc != KERN_SUCCESS) {
5ba3f43e 5190 rc = vm_fault_internal(map, va, prot, TRUE, wire_tag,
0a7de745
A
5191 ((pmap == kernel_pmap)
5192 ? THREAD_UNINT
5193 : THREAD_ABORTSAFE),
5194 pmap,
5195 (pmap_addr +
5196 (va - entry->vme_start)),
5197 physpage_p);
2d21ac55 5198 DTRACE_VM2(softlock, int, 1, (uint64_t *), NULL);
1c79356b
A
5199 }
5200
5201 if (rc != KERN_SUCCESS) {
0a7de745 5202 struct vm_map_entry tmp_entry = *entry;
1c79356b
A
5203
5204 /* unwire wired pages */
5205 tmp_entry.vme_end = va;
5ba3f43e 5206 vm_fault_unwire(map,
0a7de745 5207 &tmp_entry, FALSE, pmap, pmap_addr);
1c79356b
A
5208
5209 return rc;
5210 }
5211 }
5212 return KERN_SUCCESS;
5213}
5214
5215/*
5216 * vm_fault_unwire:
5217 *
5218 * Unwire a range of virtual addresses in a map.
5219 */
5220void
5221vm_fault_unwire(
0a7de745
A
5222 vm_map_t map,
5223 vm_map_entry_t entry,
5224 boolean_t deallocate,
5225 pmap_t pmap,
5226 vm_map_offset_t pmap_addr)
1c79356b 5227{
0a7de745
A
5228 vm_map_offset_t va;
5229 vm_map_offset_t end_addr = entry->vme_end;
5230 vm_object_t object;
d9a64523 5231 struct vm_object_fault_info fault_info = {};
5ba3f43e 5232 unsigned int unwired_pages;
1c79356b 5233
3e170ce0 5234 object = (entry->is_sub_map) ? VM_OBJECT_NULL : VME_OBJECT(entry);
1c79356b 5235
2d21ac55
A
5236 /*
5237 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
5238 * do anything since such memory is wired by default. So we don't have
5239 * anything to undo here.
5240 */
5241
0a7de745 5242 if (object != VM_OBJECT_NULL && object->phys_contiguous) {
2d21ac55 5243 return;
0a7de745 5244 }
2d21ac55
A
5245
5246 fault_info.interruptible = THREAD_UNINT;
5247 fault_info.behavior = entry->behavior;
3e170ce0 5248 fault_info.user_tag = VME_ALIAS(entry);
fe8ab488
A
5249 if (entry->iokit_acct ||
5250 (!entry->is_sub_map && !entry->use_pmap)) {
5251 fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT;
5252 }
3e170ce0
A
5253 fault_info.lo_offset = VME_OFFSET(entry);
5254 fault_info.hi_offset = (entry->vme_end - entry->vme_start) + VME_OFFSET(entry);
2d21ac55 5255 fault_info.no_cache = entry->no_cache;
b0d623f7 5256 fault_info.stealth = TRUE;
2d21ac55 5257
5ba3f43e
A
5258 unwired_pages = 0;
5259
1c79356b
A
5260 /*
5261 * Since the pages are wired down, we must be able to
5262 * get their mappings from the physical map system.
5263 */
5264
5265 for (va = entry->vme_start; va < end_addr; va += PAGE_SIZE) {
1c79356b 5266 if (object == VM_OBJECT_NULL) {
593a1d5f 5267 if (pmap) {
5ba3f43e 5268 pmap_change_wiring(pmap,
0a7de745 5269 pmap_addr + (va - entry->vme_start), FALSE);
593a1d5f 5270 }
5ba3f43e 5271 (void) vm_fault(map, va, VM_PROT_NONE,
0a7de745 5272 TRUE, VM_KERN_MEMORY_NONE, THREAD_UNINT, pmap, pmap_addr);
1c79356b 5273 } else {
0a7de745
A
5274 vm_prot_t prot;
5275 vm_page_t result_page;
5276 vm_page_t top_page;
5277 vm_object_t result_object;
1c79356b
A
5278 vm_fault_return_t result;
5279
d9a64523
A
5280 /* cap cluster size at maximum UPL size */
5281 upl_size_t cluster_size;
5282 if (os_sub_overflow(end_addr, va, &cluster_size)) {
5283 cluster_size = 0 - (upl_size_t)PAGE_SIZE;
b0d623f7 5284 }
d9a64523 5285 fault_info.cluster_size = cluster_size;
2d21ac55 5286
1c79356b
A
5287 do {
5288 prot = VM_PROT_NONE;
5289
5290 vm_object_lock(object);
5291 vm_object_paging_begin(object);
5292 XPR(XPR_VM_FAULT,
0a7de745
A
5293 "vm_fault_unwire -> vm_fault_page\n",
5294 0, 0, 0, 0, 0);
39236c6e 5295 result_page = VM_PAGE_NULL;
0a7de745 5296 result = vm_fault_page(
2d21ac55 5297 object,
3e170ce0 5298 (VME_OFFSET(entry) +
0a7de745 5299 (va - entry->vme_start)),
2d21ac55 5300 VM_PROT_NONE, TRUE,
39236c6e 5301 FALSE, /* page not looked up */
2d21ac55
A
5302 &prot, &result_page, &top_page,
5303 (int *)0,
5ba3f43e 5304 NULL, map->no_zero_fill,
2d21ac55 5305 FALSE, &fault_info);
1c79356b
A
5306 } while (result == VM_FAULT_RETRY);
5307
2d21ac55
A
5308 /*
5309 * If this was a mapping to a file on a device that has been forcibly
5310 * unmounted, then we won't get a page back from vm_fault_page(). Just
5311 * move on to the next one in case the remaining pages are mapped from
5312 * different objects. During a forced unmount, the object is terminated
5313 * so the alive flag will be false if this happens. A forced unmount will
5ba3f43e 5314 * will occur when an external disk is unplugged before the user does an
2d21ac55
A
5315 * eject, so we don't want to panic in that situation.
5316 */
5317
0a7de745 5318 if (result == VM_FAULT_MEMORY_ERROR && !object->alive) {
2d21ac55 5319 continue;
0a7de745 5320 }
2d21ac55 5321
39236c6e
A
5322 if (result == VM_FAULT_MEMORY_ERROR &&
5323 object == kernel_object) {
5324 /*
5325 * This must have been allocated with
5326 * KMA_KOBJECT and KMA_VAONLY and there's
5327 * no physical page at this offset.
5328 * We're done (no page to free).
5329 */
5330 assert(deallocate);
5331 continue;
5332 }
5333
0a7de745 5334 if (result != VM_FAULT_SUCCESS) {
1c79356b 5335 panic("vm_fault_unwire: failure");
0a7de745 5336 }
1c79356b 5337
39037602 5338 result_object = VM_PAGE_OBJECT(result_page);
2d21ac55 5339
1c79356b 5340 if (deallocate) {
39037602 5341 assert(VM_PAGE_GET_PHYS_PAGE(result_page) !=
0a7de745 5342 vm_page_fictitious_addr);
39037602 5343 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page));
5ba3f43e
A
5344 if (VM_PAGE_WIRED(result_page)) {
5345 unwired_pages++;
5346 }
1c79356b
A
5347 VM_PAGE_FREE(result_page);
5348 } else {
0a7de745 5349 if ((pmap) && (VM_PAGE_GET_PHYS_PAGE(result_page) != vm_page_guard_addr)) {
5ba3f43e 5350 pmap_change_wiring(pmap,
6d2010ae 5351 pmap_addr + (va - entry->vme_start), FALSE);
0a7de745 5352 }
6d2010ae
A
5353
5354
b0d623f7
A
5355 if (VM_PAGE_WIRED(result_page)) {
5356 vm_page_lockspin_queues();
0b4c1975 5357 vm_page_unwire(result_page, TRUE);
b0d623f7 5358 vm_page_unlock_queues();
5ba3f43e 5359 unwired_pages++;
b0d623f7 5360 }
0a7de745 5361 if (entry->zero_wired_pages) {
39037602 5362 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page));
b0d623f7
A
5363 entry->zero_wired_pages = FALSE;
5364 }
5365
1c79356b
A
5366 PAGE_WAKEUP_DONE(result_page);
5367 }
1c79356b
A
5368 vm_fault_cleanup(result_object, top_page);
5369 }
5370 }
5371
5372 /*
5373 * Inform the physical mapping system that the range
5374 * of addresses may fault, so that page tables and
5375 * such may be unwired themselves.
5376 */
5377
5ba3f43e 5378 pmap_pageable(pmap, pmap_addr,
0a7de745 5379 pmap_addr + (end_addr - entry->vme_start), TRUE);
1c79356b 5380
5ba3f43e 5381 if (kernel_object == object) {
0a7de745 5382 vm_tag_update_size(fault_info.user_tag, -ptoa_64(unwired_pages));
5ba3f43e 5383 }
1c79356b
A
5384}
5385
5386/*
5387 * vm_fault_wire_fast:
5388 *
5389 * Handle common case of a wire down page fault at the given address.
5390 * If successful, the page is inserted into the associated physical map.
5391 * The map entry is passed in to avoid the overhead of a map lookup.
5392 *
5393 * NOTE: the given address should be truncated to the
5394 * proper page address.
5395 *
5396 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
5397 * a standard error specifying why the fault is fatal is returned.
5398 *
5399 * The map in question must be referenced, and remains so.
5400 * Caller has a read lock on the map.
5401 *
5402 * This is a stripped version of vm_fault() for wiring pages. Anything
5403 * other than the common case will return KERN_FAILURE, and the caller
5404 * is expected to call vm_fault().
5405 */
3e170ce0 5406static kern_return_t
1c79356b 5407vm_fault_wire_fast(
0a7de745
A
5408 __unused vm_map_t map,
5409 vm_map_offset_t va,
5ba3f43e 5410 __unused vm_prot_t caller_prot,
0a7de745
A
5411 vm_tag_t wire_tag,
5412 vm_map_entry_t entry,
5413 pmap_t pmap,
5414 vm_map_offset_t pmap_addr,
5415 ppnum_t *physpage_p)
1c79356b 5416{
0a7de745
A
5417 vm_object_t object;
5418 vm_object_offset_t offset;
5419 vm_page_t m;
5420 vm_prot_t prot;
5421 thread_t thread = current_thread();
5422 int type_of_fault;
5423 kern_return_t kr;
d9a64523 5424 struct vm_object_fault_info fault_info = {};
1c79356b 5425
2d21ac55 5426 VM_STAT_INCR(faults);
1c79356b 5427
0a7de745
A
5428 if (thread != THREAD_NULL && thread->task != TASK_NULL) {
5429 thread->task->faults++;
5430 }
1c79356b
A
5431
5432/*
5433 * Recovery actions
5434 */
5435
0a7de745
A
5436#undef RELEASE_PAGE
5437#define RELEASE_PAGE(m) { \
5438 PAGE_WAKEUP_DONE(m); \
5439 vm_page_lockspin_queues(); \
5440 vm_page_unwire(m, TRUE); \
5441 vm_page_unlock_queues(); \
1c79356b
A
5442}
5443
5444
0a7de745
A
5445#undef UNLOCK_THINGS
5446#define UNLOCK_THINGS { \
5447 vm_object_paging_end(object); \
5448 vm_object_unlock(object); \
1c79356b
A
5449}
5450
0a7de745
A
5451#undef UNLOCK_AND_DEALLOCATE
5452#define UNLOCK_AND_DEALLOCATE { \
5453 UNLOCK_THINGS; \
5454 vm_object_deallocate(object); \
1c79356b
A
5455}
5456/*
5457 * Give up and have caller do things the hard way.
5458 */
5459
0a7de745
A
5460#define GIVE_UP { \
5461 UNLOCK_AND_DEALLOCATE; \
5462 return(KERN_FAILURE); \
1c79356b
A
5463}
5464
5465
5466 /*
5467 * If this entry is not directly to a vm_object, bail out.
5468 */
fe8ab488
A
5469 if (entry->is_sub_map) {
5470 assert(physpage_p == NULL);
0a7de745 5471 return KERN_FAILURE;
fe8ab488 5472 }
1c79356b
A
5473
5474 /*
5475 * Find the backing store object and offset into it.
5476 */
5477
3e170ce0
A
5478 object = VME_OBJECT(entry);
5479 offset = (va - entry->vme_start) + VME_OFFSET(entry);
1c79356b
A
5480 prot = entry->protection;
5481
0a7de745 5482 /*
1c79356b
A
5483 * Make a reference to this object to prevent its
5484 * disposal while we are messing with it.
5485 */
5486
5487 vm_object_lock(object);
2d21ac55 5488 vm_object_reference_locked(object);
ff6e181a 5489 vm_object_paging_begin(object);
1c79356b
A
5490
5491 /*
5492 * INVARIANTS (through entire routine):
5493 *
5494 * 1) At all times, we must either have the object
5495 * lock or a busy page in some object to prevent
5496 * some other thread from trying to bring in
5497 * the same page.
5498 *
5499 * 2) Once we have a busy page, we must remove it from
5500 * the pageout queues, so that the pageout daemon
5501 * will not grab it away.
5502 *
5503 */
5504
5505 /*
5506 * Look for page in top-level object. If it's not there or
5507 * there's something going on, give up.
5508 */
5509 m = vm_page_lookup(object, offset);
d9a64523 5510 if ((m == VM_PAGE_NULL) || (m->vmp_busy) ||
0a7de745 5511 (m->vmp_unusual && (m->vmp_error || m->vmp_restart || m->vmp_absent))) {
1c79356b
A
5512 GIVE_UP;
5513 }
d9a64523 5514 if (m->vmp_fictitious &&
39037602 5515 VM_PAGE_GET_PHYS_PAGE(m) == vm_page_guard_addr) {
2d21ac55
A
5516 /*
5517 * Guard pages are fictitious pages and are never
5518 * entered into a pmap, so let's say it's been wired...
5519 */
5520 kr = KERN_SUCCESS;
5521 goto done;
5522 }
5523
1c79356b
A
5524 /*
5525 * Wire the page down now. All bail outs beyond this
5ba3f43e 5526 * point must unwire the page.
1c79356b
A
5527 */
5528
2d21ac55 5529 vm_page_lockspin_queues();
5ba3f43e 5530 vm_page_wire(m, wire_tag, TRUE);
1c79356b
A
5531 vm_page_unlock_queues();
5532
5533 /*
5534 * Mark page busy for other threads.
5535 */
d9a64523
A
5536 assert(!m->vmp_busy);
5537 m->vmp_busy = TRUE;
5538 assert(!m->vmp_absent);
1c79356b
A
5539
5540 /*
5541 * Give up if the page is being written and there's a copy object
5542 */
5543 if ((object->copy != VM_OBJECT_NULL) && (prot & VM_PROT_WRITE)) {
5544 RELEASE_PAGE(m);
5545 GIVE_UP;
5546 }
5547
d9a64523
A
5548 fault_info.user_tag = VME_ALIAS(entry);
5549 fault_info.pmap_options = 0;
5550 if (entry->iokit_acct ||
5551 (!entry->is_sub_map && !entry->use_pmap)) {
5552 fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT;
5553 }
5554
1c79356b
A
5555 /*
5556 * Put this page into the physical map.
1c79356b 5557 */
2d21ac55
A
5558 type_of_fault = DBG_CACHE_HIT_FAULT;
5559 kr = vm_fault_enter(m,
0a7de745
A
5560 pmap,
5561 pmap_addr,
5562 prot,
5563 prot,
5564 TRUE, /* wired */
5565 FALSE, /* change_wiring */
5566 wire_tag,
5567 &fault_info,
5568 NULL,
5569 &type_of_fault);
39037602
A
5570 if (kr != KERN_SUCCESS) {
5571 RELEASE_PAGE(m);
5572 GIVE_UP;
5573 }
2d21ac55
A
5574
5575done:
1c79356b
A
5576 /*
5577 * Unlock everything, and return
5578 */
5579
fe8ab488
A
5580 if (physpage_p) {
5581 /* for vm_map_wire_and_extract() */
5582 if (kr == KERN_SUCCESS) {
39037602
A
5583 assert(object == VM_PAGE_OBJECT(m));
5584 *physpage_p = VM_PAGE_GET_PHYS_PAGE(m);
fe8ab488 5585 if (prot & VM_PROT_WRITE) {
39037602 5586 vm_object_lock_assert_exclusive(object);
d9a64523 5587 m->vmp_dirty = TRUE;
fe8ab488
A
5588 }
5589 } else {
5590 *physpage_p = 0;
5591 }
5592 }
5593
1c79356b
A
5594 PAGE_WAKEUP_DONE(m);
5595 UNLOCK_AND_DEALLOCATE;
5596
2d21ac55 5597 return kr;
1c79356b
A
5598}
5599
5600/*
5601 * Routine: vm_fault_copy_cleanup
5602 * Purpose:
5603 * Release a page used by vm_fault_copy.
5604 */
5605
3e170ce0 5606static void
1c79356b 5607vm_fault_copy_cleanup(
0a7de745
A
5608 vm_page_t page,
5609 vm_page_t top_page)
1c79356b 5610{
0a7de745 5611 vm_object_t object = VM_PAGE_OBJECT(page);
1c79356b
A
5612
5613 vm_object_lock(object);
5614 PAGE_WAKEUP_DONE(page);
0a7de745 5615 if (!VM_PAGE_PAGEABLE(page)) {
b0d623f7 5616 vm_page_lockspin_queues();
0a7de745 5617 if (!VM_PAGE_PAGEABLE(page)) {
b0d623f7 5618 vm_page_activate(page);
39037602 5619 }
b0d623f7
A
5620 vm_page_unlock_queues();
5621 }
1c79356b
A
5622 vm_fault_cleanup(object, top_page);
5623}
5624
3e170ce0 5625static void
1c79356b 5626vm_fault_copy_dst_cleanup(
0a7de745 5627 vm_page_t page)
1c79356b 5628{
0a7de745 5629 vm_object_t object;
1c79356b
A
5630
5631 if (page != VM_PAGE_NULL) {
39037602 5632 object = VM_PAGE_OBJECT(page);
1c79356b 5633 vm_object_lock(object);
2d21ac55 5634 vm_page_lockspin_queues();
0b4c1975 5635 vm_page_unwire(page, TRUE);
1c79356b 5636 vm_page_unlock_queues();
5ba3f43e 5637 vm_object_paging_end(object);
1c79356b
A
5638 vm_object_unlock(object);
5639 }
5640}
5641
5642/*
5643 * Routine: vm_fault_copy
5644 *
5645 * Purpose:
5646 * Copy pages from one virtual memory object to another --
5647 * neither the source nor destination pages need be resident.
5648 *
5649 * Before actually copying a page, the version associated with
5650 * the destination address map wil be verified.
5651 *
5652 * In/out conditions:
5653 * The caller must hold a reference, but not a lock, to
5654 * each of the source and destination objects and to the
5655 * destination map.
5656 *
5657 * Results:
5658 * Returns KERN_SUCCESS if no errors were encountered in
5659 * reading or writing the data. Returns KERN_INTERRUPTED if
5660 * the operation was interrupted (only possible if the
5661 * "interruptible" argument is asserted). Other return values
5662 * indicate a permanent error in copying the data.
5663 *
5664 * The actual amount of data copied will be returned in the
5665 * "copy_size" argument. In the event that the destination map
5666 * verification failed, this amount may be less than the amount
5667 * requested.
5668 */
5669kern_return_t
5670vm_fault_copy(
0a7de745
A
5671 vm_object_t src_object,
5672 vm_object_offset_t src_offset,
5673 vm_map_size_t *copy_size, /* INOUT */
5674 vm_object_t dst_object,
5675 vm_object_offset_t dst_offset,
5676 vm_map_t dst_map,
5677 vm_map_version_t *dst_version,
5678 int interruptible)
1c79356b 5679{
0a7de745 5680 vm_page_t result_page;
5ba3f43e 5681
0a7de745
A
5682 vm_page_t src_page;
5683 vm_page_t src_top_page;
5684 vm_prot_t src_prot;
1c79356b 5685
0a7de745
A
5686 vm_page_t dst_page;
5687 vm_page_t dst_top_page;
5688 vm_prot_t dst_prot;
1c79356b 5689
0a7de745
A
5690 vm_map_size_t amount_left;
5691 vm_object_t old_copy_object;
5692 vm_object_t result_page_object = NULL;
5693 kern_return_t error = 0;
5694 vm_fault_return_t result;
1c79356b 5695
0a7de745 5696 vm_map_size_t part_size;
d9a64523
A
5697 struct vm_object_fault_info fault_info_src = {};
5698 struct vm_object_fault_info fault_info_dst = {};
1c79356b
A
5699
5700 /*
5701 * In order not to confuse the clustered pageins, align
5702 * the different offsets on a page boundary.
5703 */
1c79356b 5704
0a7de745
A
5705#define RETURN(x) \
5706 MACRO_BEGIN \
5707 *copy_size -= amount_left; \
5708 MACRO_RETURN(x); \
1c79356b
A
5709 MACRO_END
5710
91447636 5711 amount_left = *copy_size;
2d21ac55
A
5712
5713 fault_info_src.interruptible = interruptible;
5714 fault_info_src.behavior = VM_BEHAVIOR_SEQUENTIAL;
2d21ac55
A
5715 fault_info_src.lo_offset = vm_object_trunc_page(src_offset);
5716 fault_info_src.hi_offset = fault_info_src.lo_offset + amount_left;
b0d623f7 5717 fault_info_src.stealth = TRUE;
2d21ac55
A
5718
5719 fault_info_dst.interruptible = interruptible;
5720 fault_info_dst.behavior = VM_BEHAVIOR_SEQUENTIAL;
2d21ac55
A
5721 fault_info_dst.lo_offset = vm_object_trunc_page(dst_offset);
5722 fault_info_dst.hi_offset = fault_info_dst.lo_offset + amount_left;
b0d623f7 5723 fault_info_dst.stealth = TRUE;
2d21ac55 5724
1c79356b
A
5725 do { /* while (amount_left > 0) */
5726 /*
5727 * There may be a deadlock if both source and destination
5728 * pages are the same. To avoid this deadlock, the copy must
5729 * start by getting the destination page in order to apply
5730 * COW semantics if any.
5731 */
5732
0a7de745 5733RetryDestinationFault:;
1c79356b 5734
0a7de745 5735 dst_prot = VM_PROT_WRITE | VM_PROT_READ;
1c79356b
A
5736
5737 vm_object_lock(dst_object);
5738 vm_object_paging_begin(dst_object);
5739
d9a64523
A
5740 /* cap cluster size at maximum UPL size */
5741 upl_size_t cluster_size;
5742 if (os_convert_overflow(amount_left, &cluster_size)) {
5743 cluster_size = 0 - (upl_size_t)PAGE_SIZE;
b0d623f7 5744 }
d9a64523 5745 fault_info_dst.cluster_size = cluster_size;
2d21ac55 5746
0a7de745 5747 XPR(XPR_VM_FAULT, "vm_fault_copy -> vm_fault_page\n", 0, 0, 0, 0, 0);
39236c6e 5748 dst_page = VM_PAGE_NULL;
b0d623f7 5749 result = vm_fault_page(dst_object,
0a7de745
A
5750 vm_object_trunc_page(dst_offset),
5751 VM_PROT_WRITE | VM_PROT_READ,
5752 FALSE,
5753 FALSE, /* page not looked up */
5754 &dst_prot, &dst_page, &dst_top_page,
5755 (int *)0,
5756 &error,
5757 dst_map->no_zero_fill,
5758 FALSE, &fault_info_dst);
b0d623f7 5759 switch (result) {
1c79356b
A
5760 case VM_FAULT_SUCCESS:
5761 break;
5762 case VM_FAULT_RETRY:
5763 goto RetryDestinationFault;
5764 case VM_FAULT_MEMORY_SHORTAGE:
0a7de745 5765 if (vm_page_wait(interruptible)) {
1c79356b 5766 goto RetryDestinationFault;
0a7de745
A
5767 }
5768 /* fall thru */
1c79356b
A
5769 case VM_FAULT_INTERRUPTED:
5770 RETURN(MACH_SEND_INTERRUPTED);
b0d623f7
A
5771 case VM_FAULT_SUCCESS_NO_VM_PAGE:
5772 /* success but no VM page: fail the copy */
5773 vm_object_paging_end(dst_object);
5774 vm_object_unlock(dst_object);
0a7de745 5775 /*FALLTHROUGH*/
1c79356b 5776 case VM_FAULT_MEMORY_ERROR:
0a7de745
A
5777 if (error) {
5778 return error;
5779 } else {
5780 return KERN_MEMORY_ERROR;
5781 }
b0d623f7
A
5782 default:
5783 panic("vm_fault_copy: unexpected error 0x%x from "
0a7de745 5784 "vm_fault_page()\n", result);
1c79356b 5785 }
0a7de745 5786 assert((dst_prot & VM_PROT_WRITE) != VM_PROT_NONE);
1c79356b 5787
39037602
A
5788 assert(dst_object == VM_PAGE_OBJECT(dst_page));
5789 old_copy_object = dst_object->copy;
1c79356b
A
5790
5791 /*
5792 * There exists the possiblity that the source and
5793 * destination page are the same. But we can't
5794 * easily determine that now. If they are the
5795 * same, the call to vm_fault_page() for the
5796 * destination page will deadlock. To prevent this we
5797 * wire the page so we can drop busy without having
5ba3f43e 5798 * the page daemon steal the page. We clean up the
1c79356b
A
5799 * top page but keep the paging reference on the object
5800 * holding the dest page so it doesn't go away.
5801 */
5802
2d21ac55 5803 vm_page_lockspin_queues();
3e170ce0 5804 vm_page_wire(dst_page, VM_KERN_MEMORY_OSFMK, TRUE);
1c79356b
A
5805 vm_page_unlock_queues();
5806 PAGE_WAKEUP_DONE(dst_page);
39037602 5807 vm_object_unlock(dst_object);
1c79356b
A
5808
5809 if (dst_top_page != VM_PAGE_NULL) {
5810 vm_object_lock(dst_object);
5811 VM_PAGE_FREE(dst_top_page);
5812 vm_object_paging_end(dst_object);
5813 vm_object_unlock(dst_object);
5814 }
5815
0a7de745 5816RetrySourceFault:;
1c79356b
A
5817
5818 if (src_object == VM_OBJECT_NULL) {
5819 /*
5820 * No source object. We will just
5821 * zero-fill the page in dst_object.
5822 */
5823 src_page = VM_PAGE_NULL;
e3027f41 5824 result_page = VM_PAGE_NULL;
1c79356b
A
5825 } else {
5826 vm_object_lock(src_object);
5827 src_page = vm_page_lookup(src_object,
0a7de745 5828 vm_object_trunc_page(src_offset));
e3027f41 5829 if (src_page == dst_page) {
1c79356b 5830 src_prot = dst_prot;
e3027f41
A
5831 result_page = VM_PAGE_NULL;
5832 } else {
1c79356b
A
5833 src_prot = VM_PROT_READ;
5834 vm_object_paging_begin(src_object);
5835
d9a64523
A
5836 /* cap cluster size at maximum UPL size */
5837 if (os_convert_overflow(amount_left, &cluster_size)) {
5838 cluster_size = 0 - (upl_size_t)PAGE_SIZE;
b0d623f7 5839 }
d9a64523 5840 fault_info_src.cluster_size = cluster_size;
2d21ac55 5841
1c79356b 5842 XPR(XPR_VM_FAULT,
0a7de745
A
5843 "vm_fault_copy(2) -> vm_fault_page\n",
5844 0, 0, 0, 0, 0);
39236c6e 5845 result_page = VM_PAGE_NULL;
b0d623f7 5846 result = vm_fault_page(
5ba3f43e 5847 src_object,
b0d623f7
A
5848 vm_object_trunc_page(src_offset),
5849 VM_PROT_READ, FALSE,
39236c6e 5850 FALSE, /* page not looked up */
5ba3f43e 5851 &src_prot,
b0d623f7
A
5852 &result_page, &src_top_page,
5853 (int *)0, &error, FALSE,
5854 FALSE, &fault_info_src);
5855
5856 switch (result) {
1c79356b
A
5857 case VM_FAULT_SUCCESS:
5858 break;
5859 case VM_FAULT_RETRY:
5860 goto RetrySourceFault;
5861 case VM_FAULT_MEMORY_SHORTAGE:
0a7de745 5862 if (vm_page_wait(interruptible)) {
1c79356b 5863 goto RetrySourceFault;
0a7de745
A
5864 }
5865 /* fall thru */
1c79356b
A
5866 case VM_FAULT_INTERRUPTED:
5867 vm_fault_copy_dst_cleanup(dst_page);
5868 RETURN(MACH_SEND_INTERRUPTED);
b0d623f7
A
5869 case VM_FAULT_SUCCESS_NO_VM_PAGE:
5870 /* success but no VM page: fail */
5871 vm_object_paging_end(src_object);
5872 vm_object_unlock(src_object);
0a7de745 5873 /*FALLTHROUGH*/
1c79356b
A
5874 case VM_FAULT_MEMORY_ERROR:
5875 vm_fault_copy_dst_cleanup(dst_page);
0a7de745
A
5876 if (error) {
5877 return error;
5878 } else {
5879 return KERN_MEMORY_ERROR;
5880 }
b0d623f7
A
5881 default:
5882 panic("vm_fault_copy(2): unexpected "
0a7de745
A
5883 "error 0x%x from "
5884 "vm_fault_page()\n", result);
1c79356b
A
5885 }
5886
39037602 5887 result_page_object = VM_PAGE_OBJECT(result_page);
1c79356b 5888 assert((src_top_page == VM_PAGE_NULL) ==
0a7de745 5889 (result_page_object == src_object));
1c79356b 5890 }
0a7de745 5891 assert((src_prot & VM_PROT_READ) != VM_PROT_NONE);
39037602 5892 vm_object_unlock(result_page_object);
1c79356b
A
5893 }
5894
5ba3f43e
A
5895 vm_map_lock_read(dst_map);
5896
1c79356b 5897 if (!vm_map_verify(dst_map, dst_version)) {
5ba3f43e 5898 vm_map_unlock_read(dst_map);
0a7de745 5899 if (result_page != VM_PAGE_NULL && src_page != dst_page) {
e3027f41 5900 vm_fault_copy_cleanup(result_page, src_top_page);
0a7de745 5901 }
1c79356b
A
5902 vm_fault_copy_dst_cleanup(dst_page);
5903 break;
5904 }
39037602 5905 assert(dst_object == VM_PAGE_OBJECT(dst_page));
1c79356b 5906
39037602 5907 vm_object_lock(dst_object);
1c79356b 5908
39037602
A
5909 if (dst_object->copy != old_copy_object) {
5910 vm_object_unlock(dst_object);
5ba3f43e 5911 vm_map_unlock_read(dst_map);
0a7de745 5912 if (result_page != VM_PAGE_NULL && src_page != dst_page) {
e3027f41 5913 vm_fault_copy_cleanup(result_page, src_top_page);
0a7de745 5914 }
1c79356b
A
5915 vm_fault_copy_dst_cleanup(dst_page);
5916 break;
5917 }
39037602 5918 vm_object_unlock(dst_object);
1c79356b
A
5919
5920 /*
5921 * Copy the page, and note that it is dirty
5922 * immediately.
5923 */
5924
5925 if (!page_aligned(src_offset) ||
0a7de745
A
5926 !page_aligned(dst_offset) ||
5927 !page_aligned(amount_left)) {
5928 vm_object_offset_t src_po,
5929 dst_po;
1c79356b 5930
91447636
A
5931 src_po = src_offset - vm_object_trunc_page(src_offset);
5932 dst_po = dst_offset - vm_object_trunc_page(dst_offset);
1c79356b
A
5933
5934 if (dst_po > src_po) {
5935 part_size = PAGE_SIZE - dst_po;
5936 } else {
5937 part_size = PAGE_SIZE - src_po;
5938 }
0a7de745 5939 if (part_size > (amount_left)) {
1c79356b
A
5940 part_size = amount_left;
5941 }
5942
e3027f41 5943 if (result_page == VM_PAGE_NULL) {
b0d623f7
A
5944 assert((vm_offset_t) dst_po == dst_po);
5945 assert((vm_size_t) part_size == part_size);
1c79356b 5946 vm_page_part_zero_fill(dst_page,
0a7de745
A
5947 (vm_offset_t) dst_po,
5948 (vm_size_t) part_size);
1c79356b 5949 } else {
b0d623f7
A
5950 assert((vm_offset_t) src_po == src_po);
5951 assert((vm_offset_t) dst_po == dst_po);
5952 assert((vm_size_t) part_size == part_size);
5953 vm_page_part_copy(result_page,
0a7de745
A
5954 (vm_offset_t) src_po,
5955 dst_page,
5956 (vm_offset_t) dst_po,
5957 (vm_size_t)part_size);
5958 if (!dst_page->vmp_dirty) {
1c79356b 5959 vm_object_lock(dst_object);
316670eb 5960 SET_PAGE_DIRTY(dst_page, TRUE);
39037602 5961 vm_object_unlock(dst_object);
1c79356b 5962 }
1c79356b
A
5963 }
5964 } else {
5965 part_size = PAGE_SIZE;
5966
0a7de745 5967 if (result_page == VM_PAGE_NULL) {
1c79356b 5968 vm_page_zero_fill(dst_page);
0a7de745 5969 } else {
39037602 5970 vm_object_lock(result_page_object);
e3027f41 5971 vm_page_copy(result_page, dst_page);
39037602 5972 vm_object_unlock(result_page_object);
316670eb 5973
0a7de745 5974 if (!dst_page->vmp_dirty) {
1c79356b 5975 vm_object_lock(dst_object);
316670eb 5976 SET_PAGE_DIRTY(dst_page, TRUE);
39037602 5977 vm_object_unlock(dst_object);
1c79356b
A
5978 }
5979 }
1c79356b
A
5980 }
5981
5982 /*
5983 * Unlock everything, and return
5984 */
5985
5ba3f43e 5986 vm_map_unlock_read(dst_map);
1c79356b 5987
0a7de745 5988 if (result_page != VM_PAGE_NULL && src_page != dst_page) {
e3027f41 5989 vm_fault_copy_cleanup(result_page, src_top_page);
0a7de745 5990 }
1c79356b
A
5991 vm_fault_copy_dst_cleanup(dst_page);
5992
5993 amount_left -= part_size;
5994 src_offset += part_size;
5995 dst_offset += part_size;
5996 } while (amount_left > 0);
5997
5998 RETURN(KERN_SUCCESS);
0a7de745 5999#undef RETURN
1c79356b 6000
5ba3f43e 6001 /*NOTREACHED*/
1c79356b
A
6002}
6003
0a7de745 6004#if VM_FAULT_CLASSIFY
1c79356b
A
6005/*
6006 * Temporary statistics gathering support.
6007 */
6008
6009/*
6010 * Statistics arrays:
6011 */
0a7de745
A
6012#define VM_FAULT_TYPES_MAX 5
6013#define VM_FAULT_LEVEL_MAX 8
1c79356b 6014
0a7de745 6015int vm_fault_stats[VM_FAULT_TYPES_MAX][VM_FAULT_LEVEL_MAX];
1c79356b 6016
0a7de745
A
6017#define VM_FAULT_TYPE_ZERO_FILL 0
6018#define VM_FAULT_TYPE_MAP_IN 1
6019#define VM_FAULT_TYPE_PAGER 2
6020#define VM_FAULT_TYPE_COPY 3
6021#define VM_FAULT_TYPE_OTHER 4
1c79356b
A
6022
6023
6024void
0a7de745
A
6025vm_fault_classify(vm_object_t object,
6026 vm_object_offset_t offset,
6027 vm_prot_t fault_type)
1c79356b 6028{
0a7de745
A
6029 int type, level = 0;
6030 vm_page_t m;
1c79356b
A
6031
6032 while (TRUE) {
6033 m = vm_page_lookup(object, offset);
5ba3f43e 6034 if (m != VM_PAGE_NULL) {
0a7de745 6035 if (m->vmp_busy || m->vmp_error || m->vmp_restart || m->vmp_absent) {
1c79356b
A
6036 type = VM_FAULT_TYPE_OTHER;
6037 break;
6038 }
6039 if (((fault_type & VM_PROT_WRITE) == 0) ||
6040 ((level == 0) && object->copy == VM_OBJECT_NULL)) {
6041 type = VM_FAULT_TYPE_MAP_IN;
5ba3f43e 6042 break;
1c79356b
A
6043 }
6044 type = VM_FAULT_TYPE_COPY;
6045 break;
0a7de745 6046 } else {
1c79356b
A
6047 if (object->pager_created) {
6048 type = VM_FAULT_TYPE_PAGER;
6049 break;
6050 }
6051 if (object->shadow == VM_OBJECT_NULL) {
6052 type = VM_FAULT_TYPE_ZERO_FILL;
6053 break;
0a7de745 6054 }
1c79356b 6055
6d2010ae 6056 offset += object->vo_shadow_offset;
1c79356b
A
6057 object = object->shadow;
6058 level++;
6059 continue;
6060 }
6061 }
6062
0a7de745 6063 if (level > VM_FAULT_LEVEL_MAX) {
1c79356b 6064 level = VM_FAULT_LEVEL_MAX;
0a7de745 6065 }
1c79356b
A
6066
6067 vm_fault_stats[type][level] += 1;
6068
6069 return;
6070}
6071
6072/* cleanup routine to call from debugger */
6073
6074void
6075vm_fault_classify_init(void)
6076{
6077 int type, level;
6078
6079 for (type = 0; type < VM_FAULT_TYPES_MAX; type++) {
6080 for (level = 0; level < VM_FAULT_LEVEL_MAX; level++) {
6081 vm_fault_stats[type][level] = 0;
6082 }
6083 }
6084
6085 return;
6086}
0a7de745 6087#endif /* VM_FAULT_CLASSIFY */
2d21ac55 6088
3e170ce0 6089vm_offset_t
39037602 6090kdp_lightweight_fault(vm_map_t map, vm_offset_t cur_target_addr)
3e170ce0 6091{
0a7de745
A
6092 vm_map_entry_t entry;
6093 vm_object_t object;
6094 vm_offset_t object_offset;
6095 vm_page_t m;
6096 int compressor_external_state, compressed_count_delta;
6097 int compressor_flags = (C_DONT_BLOCK | C_KEEP | C_KDP);
6098 int my_fault_type = VM_PROT_READ;
6099 kern_return_t kr;
3e170ce0 6100
3e170ce0
A
6101 if (not_in_kdp) {
6102 panic("kdp_lightweight_fault called from outside of debugger context");
6103 }
6104
6105 assert(map != VM_MAP_NULL);
6106
6107 assert((cur_target_addr & PAGE_MASK) == 0);
6108 if ((cur_target_addr & PAGE_MASK) != 0) {
6109 return 0;
6110 }
6111
6112 if (kdp_lck_rw_lock_is_acquired_exclusive(&map->lock)) {
6113 return 0;
6114 }
6115
6116 if (!vm_map_lookup_entry(map, cur_target_addr, &entry)) {
6117 return 0;
6118 }
6119
6120 if (entry->is_sub_map) {
6121 return 0;
6122 }
6123
6124 object = VME_OBJECT(entry);
6125 if (object == VM_OBJECT_NULL) {
6126 return 0;
6127 }
6128
6129 object_offset = cur_target_addr - entry->vme_start + VME_OFFSET(entry);
6130
6131 while (TRUE) {
6132 if (kdp_lck_rw_lock_is_acquired_exclusive(&object->Lock)) {
6133 return 0;
6134 }
6135
6136 if (object->pager_created && (object->paging_in_progress ||
0a7de745 6137 object->activity_in_progress)) {
3e170ce0
A
6138 return 0;
6139 }
6140
6141 m = kdp_vm_page_lookup(object, object_offset);
6142
6143 if (m != VM_PAGE_NULL) {
3e170ce0
A
6144 if ((object->wimg_bits & VM_WIMG_MASK) != VM_WIMG_DEFAULT) {
6145 return 0;
6146 }
6147
d9a64523 6148 if (m->vmp_laundry || m->vmp_busy || m->vmp_free_when_done || m->vmp_absent || m->vmp_error || m->vmp_cleaning ||
0a7de745 6149 m->vmp_overwriting || m->vmp_restart || m->vmp_unusual) {
3e170ce0
A
6150 return 0;
6151 }
6152
d9a64523
A
6153 assert(!m->vmp_private);
6154 if (m->vmp_private) {
3e170ce0
A
6155 return 0;
6156 }
6157
d9a64523
A
6158 assert(!m->vmp_fictitious);
6159 if (m->vmp_fictitious) {
3e170ce0
A
6160 return 0;
6161 }
6162
d9a64523
A
6163 assert(m->vmp_q_state != VM_PAGE_USED_BY_COMPRESSOR);
6164 if (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) {
3e170ce0
A
6165 return 0;
6166 }
2d21ac55 6167
39037602 6168 return ptoa(VM_PAGE_GET_PHYS_PAGE(m));
3e170ce0
A
6169 }
6170
6171 compressor_external_state = VM_EXTERNAL_STATE_UNKNOWN;
6172
6173 if (object->pager_created && MUST_ASK_PAGER(object, object_offset, compressor_external_state)) {
6174 if (compressor_external_state == VM_EXTERNAL_STATE_EXISTS) {
6175 kr = vm_compressor_pager_get(object->pager, (object_offset + object->paging_offset),
0a7de745
A
6176 kdp_compressor_decompressed_page_ppnum, &my_fault_type,
6177 compressor_flags, &compressed_count_delta);
3e170ce0 6178 if (kr == KERN_SUCCESS) {
3e170ce0
A
6179 return kdp_compressor_decompressed_page_paddr;
6180 } else {
6181 return 0;
6182 }
6183 }
6184 }
6185
6186 if (object->shadow == VM_OBJECT_NULL) {
6187 return 0;
6188 }
6189
6190 object_offset += object->vo_shadow_offset;
6191 object = object->shadow;
6192 }
39037602 6193}
3e170ce0 6194
d9a64523
A
6195/*
6196 * vm_page_validate_cs_fast():
6197 * Performs a few quick checks to determine if the page's code signature
6198 * really needs to be fully validated. It could:
6199 * 1. have been modified (i.e. automatically tainted),
6200 * 2. have already been validated,
6201 * 3. have already been found to be tainted,
6202 * 4. no longer have a backing store.
6203 * Returns FALSE if the page needs to be fully validated.
6204 */
6205static boolean_t
6206vm_page_validate_cs_fast(
0a7de745 6207 vm_page_t page)
593a1d5f 6208{
0a7de745 6209 vm_object_t object;
593a1d5f 6210
39037602 6211 object = VM_PAGE_OBJECT(page);
d9a64523 6212 vm_object_lock_assert_held(object);
593a1d5f 6213
d9a64523 6214 if (page->vmp_wpmapped && !page->vmp_cs_tainted) {
593a1d5f
A
6215 /*
6216 * This page was mapped for "write" access sometime in the
6217 * past and could still be modifiable in the future.
6218 * Consider it tainted.
6219 * [ If the page was already found to be "tainted", no
6220 * need to re-validate. ]
6221 */
d9a64523
A
6222 vm_object_lock_assert_exclusive(object);
6223 page->vmp_cs_validated = TRUE;
6224 page->vmp_cs_tainted = TRUE;
593a1d5f 6225 if (cs_debug) {
d9a64523 6226 printf("CODESIGNING: %s: "
0a7de745
A
6227 "page %p obj %p off 0x%llx "
6228 "was modified\n",
6229 __FUNCTION__,
6230 page, object, page->vmp_offset);
593a1d5f
A
6231 }
6232 vm_cs_validated_dirtied++;
6233 }
6234
d9a64523
A
6235 if (page->vmp_cs_validated || page->vmp_cs_tainted) {
6236 return TRUE;
593a1d5f 6237 }
d9a64523 6238 vm_object_lock_assert_exclusive(object);
593a1d5f 6239
d9a64523
A
6240#if CHECK_CS_VALIDATION_BITMAP
6241 kern_return_t kr;
593a1d5f 6242
d9a64523
A
6243 kr = vnode_pager_cs_check_validation_bitmap(
6244 object->pager,
6245 page->vmp_offset + object->paging_offset,
6246 CS_BITMAP_CHECK);
6247 if (kr == KERN_SUCCESS) {
6248 page->vmp_cs_validated = TRUE;
6249 page->vmp_cs_tainted = FALSE;
6250 vm_cs_bitmap_validated++;
6251 return TRUE;
6252 }
6253#endif /* CHECK_CS_VALIDATION_BITMAP */
593a1d5f
A
6254
6255 if (!object->alive || object->terminating || object->pager == NULL) {
6256 /*
6257 * The object is terminating and we don't have its pager
6258 * so we can't validate the data...
6259 */
d9a64523 6260 return TRUE;
593a1d5f 6261 }
d9a64523
A
6262
6263 /* we need to really validate this page */
6264 vm_object_lock_assert_exclusive(object);
6265 return FALSE;
6266}
6267
6268void
6269vm_page_validate_cs_mapped_slow(
0a7de745
A
6270 vm_page_t page,
6271 const void *kaddr)
d9a64523 6272{
0a7de745
A
6273 vm_object_t object;
6274 memory_object_offset_t mo_offset;
6275 memory_object_t pager;
6276 struct vnode *vnode;
6277 boolean_t validated;
6278 unsigned tainted;
d9a64523
A
6279
6280 assert(page->vmp_busy);
6281 object = VM_PAGE_OBJECT(page);
6282 vm_object_lock_assert_exclusive(object);
6283
6284 vm_cs_validates++;
6285
593a1d5f
A
6286 /*
6287 * Since we get here to validate a page that was brought in by
6288 * the pager, we know that this pager is all setup and ready
6289 * by now.
6290 */
d9a64523 6291 assert(object->code_signed);
593a1d5f
A
6292 assert(!object->internal);
6293 assert(object->pager != NULL);
6294 assert(object->pager_ready);
6295
6296 pager = object->pager;
b0d623f7 6297 assert(object->paging_in_progress);
39037602 6298 vnode = vnode_pager_lookup_vnode(pager);
d9a64523 6299 mo_offset = page->vmp_offset + object->paging_offset;
593a1d5f
A
6300
6301 /* verify the SHA1 hash for this page */
39037602
A
6302 tainted = 0;
6303 validated = cs_validate_range(vnode,
0a7de745
A
6304 pager,
6305 mo_offset,
6306 (const void *)((const char *)kaddr),
6307 PAGE_SIZE_64,
6308 &tainted);
39037602
A
6309
6310 if (tainted & CS_VALIDATE_TAINTED) {
d9a64523 6311 page->vmp_cs_tainted = TRUE;
39037602
A
6312 }
6313 if (tainted & CS_VALIDATE_NX) {
d9a64523 6314 page->vmp_cs_nx = TRUE;
3e170ce0 6315 }
39037602 6316 if (validated) {
d9a64523
A
6317 page->vmp_cs_validated = TRUE;
6318 }
6319
6320#if CHECK_CS_VALIDATION_BITMAP
6321 if (page->vmp_cs_validated && !page->vmp_cs_tainted) {
6322 vnode_pager_cs_check_validation_bitmap(object->pager,
0a7de745
A
6323 mo_offset,
6324 CS_BITMAP_SET);
d9a64523
A
6325 }
6326#endif /* CHECK_CS_VALIDATION_BITMAP */
6327}
6328
6329void
6330vm_page_validate_cs_mapped(
0a7de745
A
6331 vm_page_t page,
6332 const void *kaddr)
d9a64523
A
6333{
6334 if (!vm_page_validate_cs_fast(page)) {
6335 vm_page_validate_cs_mapped_slow(page, kaddr);
593a1d5f
A
6336 }
6337}
6338
2d21ac55
A
6339void
6340vm_page_validate_cs(
0a7de745 6341 vm_page_t page)
2d21ac55 6342{
0a7de745
A
6343 vm_object_t object;
6344 vm_object_offset_t offset;
6345 vm_map_offset_t koffset;
6346 vm_map_size_t ksize;
6347 vm_offset_t kaddr;
6348 kern_return_t kr;
6349 boolean_t busy_page;
6350 boolean_t need_unmap;
2d21ac55 6351
39037602
A
6352 object = VM_PAGE_OBJECT(page);
6353 vm_object_lock_assert_held(object);
2d21ac55 6354
d9a64523 6355 if (vm_page_validate_cs_fast(page)) {
6d2010ae
A
6356 return;
6357 }
39037602 6358 vm_object_lock_assert_exclusive(object);
4a3eedf9 6359
2d21ac55 6360 assert(object->code_signed);
d9a64523 6361 offset = page->vmp_offset;
2d21ac55 6362
d9a64523 6363 busy_page = page->vmp_busy;
2d21ac55
A
6364 if (!busy_page) {
6365 /* keep page busy while we map (and unlock) the VM object */
d9a64523 6366 page->vmp_busy = TRUE;
2d21ac55 6367 }
5ba3f43e 6368
2d21ac55
A
6369 /*
6370 * Take a paging reference on the VM object
6371 * to protect it from collapse or bypass,
6372 * and keep it from disappearing too.
6373 */
6374 vm_object_paging_begin(object);
6375
6376 /* map the page in the kernel address space */
2d21ac55 6377 ksize = PAGE_SIZE_64;
39236c6e
A
6378 koffset = 0;
6379 need_unmap = FALSE;
6380 kr = vm_paging_map_object(page,
0a7de745
A
6381 object,
6382 offset,
6383 VM_PROT_READ,
6384 FALSE, /* can't unlock object ! */
6385 &ksize,
6386 &koffset,
6387 &need_unmap);
2d21ac55 6388 if (kr != KERN_SUCCESS) {
d9a64523 6389 panic("%s: could not map page: 0x%x\n", __FUNCTION__, kr);
2d21ac55
A
6390 }
6391 kaddr = CAST_DOWN(vm_offset_t, koffset);
6392
593a1d5f 6393 /* validate the mapped page */
d9a64523 6394 vm_page_validate_cs_mapped_slow(page, (const void *) kaddr);
2d21ac55 6395
d9a64523 6396 assert(page->vmp_busy);
39037602 6397 assert(object == VM_PAGE_OBJECT(page));
2d21ac55
A
6398 vm_object_lock_assert_exclusive(object);
6399
2d21ac55
A
6400 if (!busy_page) {
6401 PAGE_WAKEUP_DONE(page);
6402 }
39236c6e 6403 if (need_unmap) {
2d21ac55
A
6404 /* unmap the map from the kernel address space */
6405 vm_paging_unmap_object(object, koffset, koffset + ksize);
6406 koffset = 0;
6407 ksize = 0;
6408 kaddr = 0;
6409 }
6410 vm_object_paging_end(object);
6411}
3e170ce0
A
6412
6413void
6414vm_page_validate_cs_mapped_chunk(
0a7de745
A
6415 vm_page_t page,
6416 const void *kaddr,
6417 vm_offset_t chunk_offset,
6418 vm_size_t chunk_size,
6419 boolean_t *validated_p,
6420 unsigned *tainted_p)
3e170ce0 6421{
0a7de745
A
6422 vm_object_t object;
6423 vm_object_offset_t offset, offset_in_page;
6424 memory_object_t pager;
6425 struct vnode *vnode;
6426 boolean_t validated;
6427 unsigned tainted;
3e170ce0
A
6428
6429 *validated_p = FALSE;
6430 *tainted_p = 0;
6431
d9a64523 6432 assert(page->vmp_busy);
39037602
A
6433 object = VM_PAGE_OBJECT(page);
6434 vm_object_lock_assert_exclusive(object);
3e170ce0 6435
3e170ce0 6436 assert(object->code_signed);
d9a64523 6437 offset = page->vmp_offset;
3e170ce0
A
6438
6439 if (!object->alive || object->terminating || object->pager == NULL) {
6440 /*
6441 * The object is terminating and we don't have its pager
6442 * so we can't validate the data...
6443 */
6444 return;
6445 }
6446 /*
6447 * Since we get here to validate a page that was brought in by
6448 * the pager, we know that this pager is all setup and ready
6449 * by now.
6450 */
6451 assert(!object->internal);
6452 assert(object->pager != NULL);
6453 assert(object->pager_ready);
6454
6455 pager = object->pager;
6456 assert(object->paging_in_progress);
39037602 6457 vnode = vnode_pager_lookup_vnode(pager);
3e170ce0
A
6458
6459 /* verify the signature for this chunk */
6460 offset_in_page = chunk_offset;
6461 assert(offset_in_page < PAGE_SIZE);
3e170ce0
A
6462
6463 tainted = 0;
39037602 6464 validated = cs_validate_range(vnode,
0a7de745
A
6465 pager,
6466 (object->paging_offset +
6467 offset +
6468 offset_in_page),
6469 (const void *)((const char *)kaddr
6470 + offset_in_page),
6471 chunk_size,
6472 &tainted);
3e170ce0
A
6473 if (validated) {
6474 *validated_p = TRUE;
6475 }
6476 if (tainted) {
6477 *tainted_p = tainted;
6478 }
6479}
d9a64523 6480
0a7de745
A
6481static void
6482vm_rtfrecord_lock(void)
6483{
d9a64523
A
6484 lck_spin_lock(&vm_rtfr_slock);
6485}
6486
0a7de745
A
6487static void
6488vm_rtfrecord_unlock(void)
6489{
d9a64523
A
6490 lck_spin_unlock(&vm_rtfr_slock);
6491}
6492
0a7de745
A
6493unsigned int
6494vmrtfaultinfo_bufsz(void)
6495{
6496 return vmrtf_num_records * sizeof(vm_rtfault_record_t);
d9a64523
A
6497}
6498
6499#include <kern/backtrace.h>
6500
0a7de745
A
6501static void
6502vm_record_rtfault(thread_t cthread, uint64_t fstart, vm_map_offset_t fault_vaddr, int type_of_fault)
6503{
d9a64523
A
6504 uint64_t fend = mach_continuous_time();
6505
6506 uint64_t cfpc = 0;
6507 uint64_t ctid = cthread->thread_id;
6508 uint64_t cupid = get_current_unique_pid();
6509
6510 uintptr_t bpc = 0;
6511 uint32_t bfrs = 0;
6512 bool u64 = false;
6513
6514 /* Capture a single-frame backtrace; this extracts just the program
6515 * counter at the point of the fault into "bpc", and should perform no
6516 * further user stack traversals, thus avoiding copyin()s and further
6517 * faults.
6518 */
6519 int btr = backtrace_thread_user(cthread, &bpc, 1U, &bfrs, &u64);
6520
6521 if ((btr == 0) && (bfrs > 0)) {
6522 cfpc = bpc;
6523 }
6524
6525 assert((fstart != 0) && fend >= fstart);
6526 vm_rtfrecord_lock();
6527 assert(vmrtfrs.vmrtfr_curi <= vmrtfrs.vmrtfr_maxi);
6528
6529 vmrtfrs.vmrtf_total++;
6530 vm_rtfault_record_t *cvmr = &vmrtfrs.vm_rtf_records[vmrtfrs.vmrtfr_curi++];
6531
6532 cvmr->rtfabstime = fstart;
6533 cvmr->rtfduration = fend - fstart;
6534 cvmr->rtfaddr = fault_vaddr;
6535 cvmr->rtfpc = cfpc;
6536 cvmr->rtftype = type_of_fault;
6537 cvmr->rtfupid = cupid;
6538 cvmr->rtftid = ctid;
6539
6540 if (vmrtfrs.vmrtfr_curi > vmrtfrs.vmrtfr_maxi) {
6541 vmrtfrs.vmrtfr_curi = 0;
6542 }
6543
6544 vm_rtfrecord_unlock();
6545}
6546
0a7de745
A
6547int
6548vmrtf_extract(uint64_t cupid, __unused boolean_t isroot, int vrecordsz, void *vrecords, int *vmrtfrv)
6549{
d9a64523
A
6550 vm_rtfault_record_t *cvmrd = vrecords;
6551 size_t residue = vrecordsz;
6552 int numextracted = 0;
6553 boolean_t early_exit = FALSE;
6554
6555 vm_rtfrecord_lock();
6556
6557 for (int vmfi = 0; vmfi <= vmrtfrs.vmrtfr_maxi; vmfi++) {
d9a64523
A
6558 if (residue < sizeof(vm_rtfault_record_t)) {
6559 early_exit = TRUE;
6560 break;
6561 }
6562
6563 if (vmrtfrs.vm_rtf_records[vmfi].rtfupid != cupid) {
0a7de745 6564#if DEVELOPMENT || DEBUG
d9a64523
A
6565 if (isroot == FALSE) {
6566 continue;
6567 }
6568#else
6569 continue;
6570#endif /* DEVDEBUG */
6571 }
6572
6573 *cvmrd = vmrtfrs.vm_rtf_records[vmfi];
6574 cvmrd++;
6575 residue -= sizeof(vm_rtfault_record_t);
6576 numextracted++;
6577 }
6578
6579 vm_rtfrecord_unlock();
6580
6581 *vmrtfrv = numextracted;
0a7de745 6582 return early_exit;
d9a64523 6583}