]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_fault.c
b339dbd7d515bdeac804da4b0068e2cc44981242
[apple/xnu.git] / osfmk / vm / vm_fault.c
1 /*
2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: vm_fault.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 *
62 * Page fault handling module.
63 */
64
65 #include <mach_cluster_stats.h>
66 #include <mach_pagemap.h>
67 #include <mach_kdb.h>
68 #include <libkern/OSAtomic.h>
69
70 #include <mach/mach_types.h>
71 #include <mach/kern_return.h>
72 #include <mach/message.h> /* for error codes */
73 #include <mach/vm_param.h>
74 #include <mach/vm_behavior.h>
75 #include <mach/memory_object.h>
76 /* For memory_object_data_{request,unlock} */
77 #include <mach/sdt.h>
78
79 #include <kern/kern_types.h>
80 #include <kern/host_statistics.h>
81 #include <kern/counters.h>
82 #include <kern/task.h>
83 #include <kern/thread.h>
84 #include <kern/sched_prim.h>
85 #include <kern/host.h>
86 #include <kern/xpr.h>
87 #include <kern/mach_param.h>
88 #include <kern/macro_help.h>
89 #include <kern/zalloc.h>
90 #include <kern/misc_protos.h>
91
92 #include <ppc/proc_reg.h>
93
94 #include <vm/vm_fault.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_kern.h>
99 #include <vm/pmap.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_protos.h>
102 #include <vm/vm_external.h>
103 #include <vm/memory_object.h>
104 #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
105
106 #include <sys/kdebug.h>
107
108 #define VM_FAULT_CLASSIFY 0
109
110 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
111
112 int vm_object_pagein_throttle = 16;
113
114 /*
115 * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
116 * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts
117 * of memory if they're buggy and can run the system completely out of swap space. If this happens, we
118 * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps
119 * keep the UI active so that the user has a chance to kill the offending task before the system
120 * completely hangs.
121 *
122 * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
123 * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold
124 * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a
125 * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
126 */
127
128 boolean_t thread_is_io_throttled(void);
129
130 uint64_t vm_hard_throttle_threshold;
131
132 extern unsigned int dp_pages_free, dp_pages_reserve;
133
134 #define NEED_TO_HARD_THROTTLE_THIS_TASK() (((dp_pages_free + dp_pages_reserve < 2000) && \
135 (get_task_resident_size(current_task()) > vm_hard_throttle_threshold) && \
136 (current_task() != kernel_task) && IP_VALID(memory_manager_default)) || \
137 (vm_page_free_count < vm_page_throttle_limit && thread_is_io_throttled() && \
138 (get_task_resident_size(current_task()) > vm_hard_throttle_threshold)))
139
140
141 #define HARD_THROTTLE_DELAY 10000 /* 10000 us == 10 ms */
142
143
144 extern int cs_debug;
145
146 #if MACH_KDB
147 extern struct db_watchpoint *db_watchpoint_list;
148 #endif /* MACH_KDB */
149
150 boolean_t current_thread_aborted(void);
151
152 /* Forward declarations of internal routines. */
153 extern kern_return_t vm_fault_wire_fast(
154 vm_map_t map,
155 vm_map_offset_t va,
156 vm_map_entry_t entry,
157 pmap_t pmap,
158 vm_map_offset_t pmap_addr);
159
160 extern void vm_fault_continue(void);
161
162 extern void vm_fault_copy_cleanup(
163 vm_page_t page,
164 vm_page_t top_page);
165
166 extern void vm_fault_copy_dst_cleanup(
167 vm_page_t page);
168
169 #if VM_FAULT_CLASSIFY
170 extern void vm_fault_classify(vm_object_t object,
171 vm_object_offset_t offset,
172 vm_prot_t fault_type);
173
174 extern void vm_fault_classify_init(void);
175 #endif
176
177
178 unsigned long vm_cs_validates = 0;
179 unsigned long vm_cs_revalidates = 0;
180 unsigned long vm_cs_query_modified = 0;
181 unsigned long vm_cs_validated_dirtied = 0;
182
183 #if CONFIG_ENFORCE_SIGNED_CODE
184 int cs_enforcement_disable=0;
185 #else
186 static const int cs_enforcement_disable=1;
187 #endif
188
189 /*
190 * Routine: vm_fault_init
191 * Purpose:
192 * Initialize our private data structures.
193 */
194 void
195 vm_fault_init(void)
196 {
197 #if !SECURE_KERNEL
198 #if CONFIG_ENFORCE_SIGNED_CODE
199 PE_parse_boot_argn("cs_enforcement_disable", &cs_enforcement_disable,
200 sizeof (cs_enforcement_disable));
201 #endif
202 PE_parse_boot_argn("cs_debug", &cs_debug, sizeof (cs_debug));
203 #endif
204
205 /*
206 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
207 * computed as a percentage of available memory, and the percentage used is scaled inversely with
208 * the amount of memory. The pertange runs between 10% and 35%. We use 35% for small memory systems
209 * and reduce the value down to 10% for very large memory configurations. This helps give us a
210 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
211 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
212 */
213
214 vm_hard_throttle_threshold = sane_size * (35 - MIN((int)(sane_size / (1024*1024*1024)), 25)) / 100;
215 }
216
217 /*
218 * Routine: vm_fault_cleanup
219 * Purpose:
220 * Clean up the result of vm_fault_page.
221 * Results:
222 * The paging reference for "object" is released.
223 * "object" is unlocked.
224 * If "top_page" is not null, "top_page" is
225 * freed and the paging reference for the object
226 * containing it is released.
227 *
228 * In/out conditions:
229 * "object" must be locked.
230 */
231 void
232 vm_fault_cleanup(
233 register vm_object_t object,
234 register vm_page_t top_page)
235 {
236 vm_object_paging_end(object);
237 vm_object_unlock(object);
238
239 if (top_page != VM_PAGE_NULL) {
240 object = top_page->object;
241
242 vm_object_lock(object);
243 VM_PAGE_FREE(top_page);
244 vm_object_paging_end(object);
245 vm_object_unlock(object);
246 }
247 }
248
249 #if MACH_CLUSTER_STATS
250 #define MAXCLUSTERPAGES 16
251 struct {
252 unsigned long pages_in_cluster;
253 unsigned long pages_at_higher_offsets;
254 unsigned long pages_at_lower_offsets;
255 } cluster_stats_in[MAXCLUSTERPAGES];
256 #define CLUSTER_STAT(clause) clause
257 #define CLUSTER_STAT_HIGHER(x) \
258 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
259 #define CLUSTER_STAT_LOWER(x) \
260 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
261 #define CLUSTER_STAT_CLUSTER(x) \
262 ((cluster_stats_in[(x)].pages_in_cluster)++)
263 #else /* MACH_CLUSTER_STATS */
264 #define CLUSTER_STAT(clause)
265 #endif /* MACH_CLUSTER_STATS */
266
267 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
268
269
270 boolean_t vm_page_deactivate_behind = TRUE;
271 /*
272 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
273 */
274 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
275 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
276 /* we use it to size an array on the stack */
277
278 int vm_default_behind = VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW;
279
280 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
281
282 /*
283 * vm_page_is_sequential
284 *
285 * Determine if sequential access is in progress
286 * in accordance with the behavior specified.
287 * Update state to indicate current access pattern.
288 *
289 * object must have at least the shared lock held
290 */
291 static
292 void
293 vm_fault_is_sequential(
294 vm_object_t object,
295 vm_object_offset_t offset,
296 vm_behavior_t behavior)
297 {
298 vm_object_offset_t last_alloc;
299 int sequential;
300 int orig_sequential;
301
302 last_alloc = object->last_alloc;
303 sequential = object->sequential;
304 orig_sequential = sequential;
305
306 switch (behavior) {
307 case VM_BEHAVIOR_RANDOM:
308 /*
309 * reset indicator of sequential behavior
310 */
311 sequential = 0;
312 break;
313
314 case VM_BEHAVIOR_SEQUENTIAL:
315 if (offset && last_alloc == offset - PAGE_SIZE_64) {
316 /*
317 * advance indicator of sequential behavior
318 */
319 if (sequential < MAX_SEQUENTIAL_RUN)
320 sequential += PAGE_SIZE;
321 } else {
322 /*
323 * reset indicator of sequential behavior
324 */
325 sequential = 0;
326 }
327 break;
328
329 case VM_BEHAVIOR_RSEQNTL:
330 if (last_alloc && last_alloc == offset + PAGE_SIZE_64) {
331 /*
332 * advance indicator of sequential behavior
333 */
334 if (sequential > -MAX_SEQUENTIAL_RUN)
335 sequential -= PAGE_SIZE;
336 } else {
337 /*
338 * reset indicator of sequential behavior
339 */
340 sequential = 0;
341 }
342 break;
343
344 case VM_BEHAVIOR_DEFAULT:
345 default:
346 if (offset && last_alloc == (offset - PAGE_SIZE_64)) {
347 /*
348 * advance indicator of sequential behavior
349 */
350 if (sequential < 0)
351 sequential = 0;
352 if (sequential < MAX_SEQUENTIAL_RUN)
353 sequential += PAGE_SIZE;
354
355 } else if (last_alloc && last_alloc == (offset + PAGE_SIZE_64)) {
356 /*
357 * advance indicator of sequential behavior
358 */
359 if (sequential > 0)
360 sequential = 0;
361 if (sequential > -MAX_SEQUENTIAL_RUN)
362 sequential -= PAGE_SIZE;
363 } else {
364 /*
365 * reset indicator of sequential behavior
366 */
367 sequential = 0;
368 }
369 break;
370 }
371 if (sequential != orig_sequential) {
372 if (!OSCompareAndSwap(orig_sequential, sequential, (UInt32 *)&object->sequential)) {
373 /*
374 * if someone else has already updated object->sequential
375 * don't bother trying to update it or object->last_alloc
376 */
377 return;
378 }
379 }
380 /*
381 * I'd like to do this with a OSCompareAndSwap64, but that
382 * doesn't exist for PPC... however, it shouldn't matter
383 * that much... last_alloc is maintained so that we can determine
384 * if a sequential access pattern is taking place... if only
385 * one thread is banging on this object, no problem with the unprotected
386 * update... if 2 or more threads are banging away, we run the risk of
387 * someone seeing a mangled update... however, in the face of multiple
388 * accesses, no sequential access pattern can develop anyway, so we
389 * haven't lost any real info.
390 */
391 object->last_alloc = offset;
392 }
393
394
395 int vm_page_deactivate_behind_count = 0;
396
397 /*
398 * vm_page_deactivate_behind
399 *
400 * Determine if sequential access is in progress
401 * in accordance with the behavior specified. If
402 * so, compute a potential page to deactivate and
403 * deactivate it.
404 *
405 * object must be locked.
406 *
407 * return TRUE if we actually deactivate a page
408 */
409 static
410 boolean_t
411 vm_fault_deactivate_behind(
412 vm_object_t object,
413 vm_object_offset_t offset,
414 vm_behavior_t behavior)
415 {
416 int n;
417 int pages_in_run = 0;
418 int max_pages_in_run = 0;
419 int sequential_run;
420 int sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
421 vm_object_offset_t run_offset = 0;
422 vm_object_offset_t pg_offset = 0;
423 vm_page_t m;
424 vm_page_t page_run[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER];
425
426 pages_in_run = 0;
427 #if TRACEFAULTPAGE
428 dbgTrace(0xBEEF0018, (unsigned int) object, (unsigned int) vm_fault_deactivate_behind); /* (TEST/DEBUG) */
429 #endif
430
431 if (object == kernel_object || vm_page_deactivate_behind == FALSE) {
432 /*
433 * Do not deactivate pages from the kernel object: they
434 * are not intended to become pageable.
435 * or we've disabled the deactivate behind mechanism
436 */
437 return FALSE;
438 }
439 if ((sequential_run = object->sequential)) {
440 if (sequential_run < 0) {
441 sequential_behavior = VM_BEHAVIOR_RSEQNTL;
442 sequential_run = 0 - sequential_run;
443 } else {
444 sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
445 }
446 }
447 switch (behavior) {
448 case VM_BEHAVIOR_RANDOM:
449 break;
450 case VM_BEHAVIOR_SEQUENTIAL:
451 if (sequential_run >= (int)PAGE_SIZE) {
452 run_offset = 0 - PAGE_SIZE_64;
453 max_pages_in_run = 1;
454 }
455 break;
456 case VM_BEHAVIOR_RSEQNTL:
457 if (sequential_run >= (int)PAGE_SIZE) {
458 run_offset = PAGE_SIZE_64;
459 max_pages_in_run = 1;
460 }
461 break;
462 case VM_BEHAVIOR_DEFAULT:
463 default:
464 { vm_object_offset_t behind = vm_default_behind * PAGE_SIZE_64;
465
466 /*
467 * determine if the run of sequential accesss has been
468 * long enough on an object with default access behavior
469 * to consider it for deactivation
470 */
471 if ((uint64_t)sequential_run >= behind && (sequential_run % (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER * PAGE_SIZE)) == 0) {
472 /*
473 * the comparisons between offset and behind are done
474 * in this kind of odd fashion in order to prevent wrap around
475 * at the end points
476 */
477 if (sequential_behavior == VM_BEHAVIOR_SEQUENTIAL) {
478 if (offset >= behind) {
479 run_offset = 0 - behind;
480 pg_offset = PAGE_SIZE_64;
481 max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER;
482 }
483 } else {
484 if (offset < -behind) {
485 run_offset = behind;
486 pg_offset = 0 - PAGE_SIZE_64;
487 max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER;
488 }
489 }
490 }
491 break;
492 }
493 }
494 for (n = 0; n < max_pages_in_run; n++) {
495 m = vm_page_lookup(object, offset + run_offset + (n * pg_offset));
496
497 if (m && !m->busy && !m->no_cache && !m->throttled && !m->fictitious && !m->absent) {
498 page_run[pages_in_run++] = m;
499 pmap_clear_reference(m->phys_page);
500 }
501 }
502 if (pages_in_run) {
503 vm_page_lockspin_queues();
504
505 for (n = 0; n < pages_in_run; n++) {
506
507 m = page_run[n];
508
509 vm_page_deactivate_internal(m, FALSE);
510
511 vm_page_deactivate_behind_count++;
512 #if TRACEFAULTPAGE
513 dbgTrace(0xBEEF0019, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */
514 #endif
515 }
516 vm_page_unlock_queues();
517
518 return TRUE;
519 }
520 return FALSE;
521 }
522
523
524 static boolean_t
525 vm_page_throttled(void)
526 {
527 clock_sec_t elapsed_sec;
528 clock_sec_t tv_sec;
529 clock_usec_t tv_usec;
530
531 thread_t thread = current_thread();
532
533 if (thread->options & TH_OPT_VMPRIV)
534 return (FALSE);
535
536 thread->t_page_creation_count++;
537
538 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
539 return (TRUE);
540
541 if (vm_page_free_count < vm_page_throttle_limit &&
542 thread->t_page_creation_count > vm_page_creation_throttle) {
543
544 clock_get_system_microtime(&tv_sec, &tv_usec);
545
546 elapsed_sec = tv_sec - thread->t_page_creation_time;
547
548 if (elapsed_sec <= 6 || (thread->t_page_creation_count / elapsed_sec) >= (vm_page_creation_throttle / 6)) {
549
550 if (elapsed_sec >= 60) {
551 /*
552 * we'll reset our stats to give a well behaved app
553 * that was unlucky enough to accumulate a bunch of pages
554 * over a long period of time a chance to get out of
555 * the throttled state... we reset the counter and timestamp
556 * so that if it stays under the rate limit for the next second
557 * it will be back in our good graces... if it exceeds it, it
558 * will remain in the throttled state
559 */
560 thread->t_page_creation_time = tv_sec;
561 thread->t_page_creation_count = (vm_page_creation_throttle / 6) * 5;
562 }
563 ++vm_page_throttle_count;
564
565 return (TRUE);
566 }
567 thread->t_page_creation_time = tv_sec;
568 thread->t_page_creation_count = 0;
569 }
570 return (FALSE);
571 }
572
573
574 /*
575 * check for various conditions that would
576 * prevent us from creating a ZF page...
577 * cleanup is based on being called from vm_fault_page
578 *
579 * object must be locked
580 * object == m->object
581 */
582 static vm_fault_return_t
583 vm_fault_check(vm_object_t object, vm_page_t m, vm_page_t first_m, boolean_t interruptible_state)
584 {
585 if (object->shadow_severed ||
586 VM_OBJECT_PURGEABLE_FAULT_ERROR(object)) {
587 /*
588 * Either:
589 * 1. the shadow chain was severed,
590 * 2. the purgeable object is volatile or empty and is marked
591 * to fault on access while volatile.
592 * Just have to return an error at this point
593 */
594 if (m != VM_PAGE_NULL)
595 VM_PAGE_FREE(m);
596 vm_fault_cleanup(object, first_m);
597
598 thread_interrupt_level(interruptible_state);
599
600 return (VM_FAULT_MEMORY_ERROR);
601 }
602 if (vm_backing_store_low) {
603 /*
604 * are we protecting the system from
605 * backing store exhaustion. If so
606 * sleep unless we are privileged.
607 */
608 if (!(current_task()->priv_flags & VM_BACKING_STORE_PRIV)) {
609
610 if (m != VM_PAGE_NULL)
611 VM_PAGE_FREE(m);
612 vm_fault_cleanup(object, first_m);
613
614 assert_wait((event_t)&vm_backing_store_low, THREAD_UNINT);
615
616 thread_block(THREAD_CONTINUE_NULL);
617 thread_interrupt_level(interruptible_state);
618
619 return (VM_FAULT_RETRY);
620 }
621 }
622 if (vm_page_throttled()) {
623 /*
624 * we're throttling zero-fills...
625 * treat this as if we couldn't grab a page
626 */
627 if (m != VM_PAGE_NULL)
628 VM_PAGE_FREE(m);
629 vm_fault_cleanup(object, first_m);
630
631 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
632 delay(HARD_THROTTLE_DELAY);
633
634 if (current_thread_aborted()) {
635 thread_interrupt_level(interruptible_state);
636 return VM_FAULT_INTERRUPTED;
637 }
638 }
639
640 thread_interrupt_level(interruptible_state);
641
642 return (VM_FAULT_MEMORY_SHORTAGE);
643 }
644 return (VM_FAULT_SUCCESS);
645 }
646
647
648 /*
649 * do the work to zero fill a page and
650 * inject it into the correct paging queue
651 *
652 * m->object must be locked
653 * page queue lock must NOT be held
654 */
655 static int
656 vm_fault_zero_page(vm_page_t m, boolean_t no_zero_fill)
657 {
658 int my_fault = DBG_ZERO_FILL_FAULT;
659
660 /*
661 * This is is a zero-fill page fault...
662 *
663 * Checking the page lock is a waste of
664 * time; this page was absent, so
665 * it can't be page locked by a pager.
666 *
667 * we also consider it undefined
668 * with respect to instruction
669 * execution. i.e. it is the responsibility
670 * of higher layers to call for an instruction
671 * sync after changing the contents and before
672 * sending a program into this area. We
673 * choose this approach for performance
674 */
675 m->pmapped = TRUE;
676
677 m->cs_validated = FALSE;
678 m->cs_tainted = FALSE;
679
680 if (no_zero_fill == TRUE)
681 my_fault = DBG_NZF_PAGE_FAULT;
682 else {
683 vm_page_zero_fill(m);
684
685 VM_STAT_INCR(zero_fill_count);
686 DTRACE_VM2(zfod, int, 1, (uint64_t *), NULL);
687 }
688 assert(!m->laundry);
689 assert(m->object != kernel_object);
690 //assert(m->pageq.next == NULL && m->pageq.prev == NULL);
691
692 if (!IP_VALID(memory_manager_default) &&
693 (m->object->purgable == VM_PURGABLE_DENY ||
694 m->object->purgable == VM_PURGABLE_NONVOLATILE ||
695 m->object->purgable == VM_PURGABLE_VOLATILE )) {
696 vm_page_lockspin_queues();
697
698 queue_enter(&vm_page_queue_throttled, m, vm_page_t, pageq);
699 m->throttled = TRUE;
700 vm_page_throttled_count++;
701
702 vm_page_unlock_queues();
703 } else {
704 if (current_thread()->t_page_creation_count > vm_page_creation_throttle) {
705 m->zero_fill = TRUE;
706 VM_ZF_COUNT_INCR();
707 }
708 }
709 return (my_fault);
710 }
711
712
713 /*
714 * Routine: vm_fault_page
715 * Purpose:
716 * Find the resident page for the virtual memory
717 * specified by the given virtual memory object
718 * and offset.
719 * Additional arguments:
720 * The required permissions for the page is given
721 * in "fault_type". Desired permissions are included
722 * in "protection".
723 * fault_info is passed along to determine pagein cluster
724 * limits... it contains the expected reference pattern,
725 * cluster size if available, etc...
726 *
727 * If the desired page is known to be resident (for
728 * example, because it was previously wired down), asserting
729 * the "unwiring" parameter will speed the search.
730 *
731 * If the operation can be interrupted (by thread_abort
732 * or thread_terminate), then the "interruptible"
733 * parameter should be asserted.
734 *
735 * Results:
736 * The page containing the proper data is returned
737 * in "result_page".
738 *
739 * In/out conditions:
740 * The source object must be locked and referenced,
741 * and must donate one paging reference. The reference
742 * is not affected. The paging reference and lock are
743 * consumed.
744 *
745 * If the call succeeds, the object in which "result_page"
746 * resides is left locked and holding a paging reference.
747 * If this is not the original object, a busy page in the
748 * original object is returned in "top_page", to prevent other
749 * callers from pursuing this same data, along with a paging
750 * reference for the original object. The "top_page" should
751 * be destroyed when this guarantee is no longer required.
752 * The "result_page" is also left busy. It is not removed
753 * from the pageout queues.
754 * Special Case:
755 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
756 * fault succeeded but there's no VM page (i.e. the VM object
757 * does not actually hold VM pages, but device memory or
758 * large pages). The object is still locked and we still hold a
759 * paging_in_progress reference.
760 */
761 unsigned int vm_fault_page_blocked_access = 0;
762
763 vm_fault_return_t
764 vm_fault_page(
765 /* Arguments: */
766 vm_object_t first_object, /* Object to begin search */
767 vm_object_offset_t first_offset, /* Offset into object */
768 vm_prot_t fault_type, /* What access is requested */
769 boolean_t must_be_resident,/* Must page be resident? */
770 /* Modifies in place: */
771 vm_prot_t *protection, /* Protection for mapping */
772 /* Returns: */
773 vm_page_t *result_page, /* Page found, if successful */
774 vm_page_t *top_page, /* Page in top object, if
775 * not result_page. */
776 int *type_of_fault, /* if non-null, fill in with type of fault
777 * COW, zero-fill, etc... returned in trace point */
778 /* More arguments: */
779 kern_return_t *error_code, /* code if page is in error */
780 boolean_t no_zero_fill, /* don't zero fill absent pages */
781 #if MACH_PAGEMAP
782 boolean_t data_supply, /* treat as data_supply if
783 * it is a write fault and a full
784 * page is provided */
785 #else
786 __unused boolean_t data_supply,
787 #endif
788 vm_object_fault_info_t fault_info)
789 {
790 vm_page_t m;
791 vm_object_t object;
792 vm_object_offset_t offset;
793 vm_page_t first_m;
794 vm_object_t next_object;
795 vm_object_t copy_object;
796 boolean_t look_for_page;
797 vm_prot_t access_required = fault_type;
798 vm_prot_t wants_copy_flag;
799 CLUSTER_STAT(int pages_at_higher_offsets;)
800 CLUSTER_STAT(int pages_at_lower_offsets;)
801 kern_return_t wait_result;
802 boolean_t interruptible_state;
803 vm_fault_return_t error;
804 int my_fault;
805 uint32_t try_failed_count;
806 int interruptible; /* how may fault be interrupted? */
807 memory_object_t pager;
808 vm_fault_return_t retval;
809
810 /*
811 * MACH page map - an optional optimization where a bit map is maintained
812 * by the VM subsystem for internal objects to indicate which pages of
813 * the object currently reside on backing store. This existence map
814 * duplicates information maintained by the vnode pager. It is
815 * created at the time of the first pageout against the object, i.e.
816 * at the same time pager for the object is created. The optimization
817 * is designed to eliminate pager interaction overhead, if it is
818 * 'known' that the page does not exist on backing store.
819 *
820 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
821 * either marked as paged out in the existence map for the object or no
822 * existence map exists for the object. MUST_ASK_PAGER() is one of the
823 * criteria in the decision to invoke the pager. It is also used as one
824 * of the criteria to terminate the scan for adjacent pages in a clustered
825 * pagein operation. Note that MUST_ASK_PAGER() always evaluates to TRUE for
826 * permanent objects. Note also that if the pager for an internal object
827 * has not been created, the pager is not invoked regardless of the value
828 * of MUST_ASK_PAGER() and that clustered pagein scans are only done on an object
829 * for which a pager has been created.
830 *
831 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
832 * is marked as paged out in the existence map for the object. PAGED_OUT()
833 * PAGED_OUT() is used to determine if a page has already been pushed
834 * into a copy object in order to avoid a redundant page out operation.
835 */
836 #if MACH_PAGEMAP
837 #define MUST_ASK_PAGER(o, f) (vm_external_state_get((o)->existence_map, (f)) \
838 != VM_EXTERNAL_STATE_ABSENT)
839 #define PAGED_OUT(o, f) (vm_external_state_get((o)->existence_map, (f)) \
840 == VM_EXTERNAL_STATE_EXISTS)
841 #else
842 #define MUST_ASK_PAGER(o, f) (TRUE)
843 #define PAGED_OUT(o, f) (FALSE)
844 #endif
845
846 /*
847 * Recovery actions
848 */
849 #define RELEASE_PAGE(m) \
850 MACRO_BEGIN \
851 PAGE_WAKEUP_DONE(m); \
852 if (!m->active && !m->inactive && !m->throttled) { \
853 vm_page_lockspin_queues(); \
854 if (!m->active && !m->inactive && !m->throttled) \
855 vm_page_activate(m); \
856 vm_page_unlock_queues(); \
857 } \
858 MACRO_END
859
860 #if TRACEFAULTPAGE
861 dbgTrace(0xBEEF0002, (unsigned int) first_object, (unsigned int) first_offset); /* (TEST/DEBUG) */
862 #endif
863
864
865 #if MACH_KDB
866 /*
867 * If there are watchpoints set, then
868 * we don't want to give away write permission
869 * on a read fault. Make the task write fault,
870 * so that the watchpoint code notices the access.
871 */
872 if (db_watchpoint_list) {
873 /*
874 * If we aren't asking for write permission,
875 * then don't give it away. We're using write
876 * faults to set the dirty bit.
877 */
878 if (!(fault_type & VM_PROT_WRITE))
879 *protection &= ~VM_PROT_WRITE;
880 }
881 #endif /* MACH_KDB */
882
883 interruptible = fault_info->interruptible;
884 interruptible_state = thread_interrupt_level(interruptible);
885
886 /*
887 * INVARIANTS (through entire routine):
888 *
889 * 1) At all times, we must either have the object
890 * lock or a busy page in some object to prevent
891 * some other thread from trying to bring in
892 * the same page.
893 *
894 * Note that we cannot hold any locks during the
895 * pager access or when waiting for memory, so
896 * we use a busy page then.
897 *
898 * 2) To prevent another thread from racing us down the
899 * shadow chain and entering a new page in the top
900 * object before we do, we must keep a busy page in
901 * the top object while following the shadow chain.
902 *
903 * 3) We must increment paging_in_progress on any object
904 * for which we have a busy page before dropping
905 * the object lock
906 *
907 * 4) We leave busy pages on the pageout queues.
908 * If the pageout daemon comes across a busy page,
909 * it will remove the page from the pageout queues.
910 */
911
912 object = first_object;
913 offset = first_offset;
914 first_m = VM_PAGE_NULL;
915 access_required = fault_type;
916
917
918 XPR(XPR_VM_FAULT,
919 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
920 object, offset, fault_type, *protection, 0);
921
922 /*
923 * default type of fault
924 */
925 my_fault = DBG_CACHE_HIT_FAULT;
926
927 while (TRUE) {
928 #if TRACEFAULTPAGE
929 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
930 #endif
931 if (!object->alive) {
932 /*
933 * object is no longer valid
934 * clean up and return error
935 */
936 vm_fault_cleanup(object, first_m);
937 thread_interrupt_level(interruptible_state);
938
939 return (VM_FAULT_MEMORY_ERROR);
940 }
941
942 if (!object->pager_created && object->phys_contiguous) {
943 /*
944 * A physically-contiguous object without a pager:
945 * must be a "large page" object. We do not deal
946 * with VM pages for this object.
947 */
948 m = VM_PAGE_NULL;
949 goto phys_contig_object;
950 }
951
952 if (object->blocked_access) {
953 /*
954 * Access to this VM object has been blocked.
955 * Replace our "paging_in_progress" reference with
956 * a "activity_in_progress" reference and wait for
957 * access to be unblocked.
958 */
959 vm_object_activity_begin(object);
960 vm_object_paging_end(object);
961 while (object->blocked_access) {
962 vm_object_sleep(object,
963 VM_OBJECT_EVENT_UNBLOCKED,
964 THREAD_UNINT);
965 }
966 vm_fault_page_blocked_access++;
967 vm_object_paging_begin(object);
968 vm_object_activity_end(object);
969 }
970
971 /*
972 * See whether the page at 'offset' is resident
973 */
974 m = vm_page_lookup(object, offset);
975 #if TRACEFAULTPAGE
976 dbgTrace(0xBEEF0004, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */
977 #endif
978 if (m != VM_PAGE_NULL) {
979
980 if (m->busy) {
981 /*
982 * The page is being brought in,
983 * wait for it and then retry.
984 *
985 * A possible optimization: if the page
986 * is known to be resident, we can ignore
987 * pages that are absent (regardless of
988 * whether they're busy).
989 */
990 #if TRACEFAULTPAGE
991 dbgTrace(0xBEEF0005, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */
992 #endif
993 wait_result = PAGE_SLEEP(object, m, interruptible);
994 XPR(XPR_VM_FAULT,
995 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
996 object, offset,
997 m, 0, 0);
998 counter(c_vm_fault_page_block_busy_kernel++);
999
1000 if (wait_result != THREAD_AWAKENED) {
1001 vm_fault_cleanup(object, first_m);
1002 thread_interrupt_level(interruptible_state);
1003
1004 if (wait_result == THREAD_RESTART)
1005 return (VM_FAULT_RETRY);
1006 else
1007 return (VM_FAULT_INTERRUPTED);
1008 }
1009 continue;
1010 }
1011
1012 if (m->phys_page == vm_page_guard_addr) {
1013 /*
1014 * Guard page: off limits !
1015 */
1016 if (fault_type == VM_PROT_NONE) {
1017 /*
1018 * The fault is not requesting any
1019 * access to the guard page, so it must
1020 * be just to wire or unwire it.
1021 * Let's pretend it succeeded...
1022 */
1023 m->busy = TRUE;
1024 *result_page = m;
1025 assert(first_m == VM_PAGE_NULL);
1026 *top_page = first_m;
1027 if (type_of_fault)
1028 *type_of_fault = DBG_GUARD_FAULT;
1029 return VM_FAULT_SUCCESS;
1030 } else {
1031 /*
1032 * The fault requests access to the
1033 * guard page: let's deny that !
1034 */
1035 vm_fault_cleanup(object, first_m);
1036 thread_interrupt_level(interruptible_state);
1037 return VM_FAULT_MEMORY_ERROR;
1038 }
1039 }
1040
1041 if (m->error) {
1042 /*
1043 * The page is in error, give up now.
1044 */
1045 #if TRACEFAULTPAGE
1046 dbgTrace(0xBEEF0006, (unsigned int) m, (unsigned int) error_code); /* (TEST/DEBUG) */
1047 #endif
1048 if (error_code)
1049 *error_code = KERN_MEMORY_ERROR;
1050 VM_PAGE_FREE(m);
1051
1052 vm_fault_cleanup(object, first_m);
1053 thread_interrupt_level(interruptible_state);
1054
1055 return (VM_FAULT_MEMORY_ERROR);
1056 }
1057 if (m->restart) {
1058 /*
1059 * The pager wants us to restart
1060 * at the top of the chain,
1061 * typically because it has moved the
1062 * page to another pager, then do so.
1063 */
1064 #if TRACEFAULTPAGE
1065 dbgTrace(0xBEEF0007, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */
1066 #endif
1067 VM_PAGE_FREE(m);
1068
1069 vm_fault_cleanup(object, first_m);
1070 thread_interrupt_level(interruptible_state);
1071
1072 return (VM_FAULT_RETRY);
1073 }
1074 if (m->absent) {
1075 /*
1076 * The page isn't busy, but is absent,
1077 * therefore it's deemed "unavailable".
1078 *
1079 * Remove the non-existent page (unless it's
1080 * in the top object) and move on down to the
1081 * next object (if there is one).
1082 */
1083 #if TRACEFAULTPAGE
1084 dbgTrace(0xBEEF0008, (unsigned int) m, (unsigned int) object->shadow); /* (TEST/DEBUG) */
1085 #endif
1086 next_object = object->shadow;
1087
1088 if (next_object == VM_OBJECT_NULL) {
1089 /*
1090 * Absent page at bottom of shadow
1091 * chain; zero fill the page we left
1092 * busy in the first object, and free
1093 * the absent page.
1094 */
1095 assert(!must_be_resident);
1096
1097 /*
1098 * check for any conditions that prevent
1099 * us from creating a new zero-fill page
1100 * vm_fault_check will do all of the
1101 * fault cleanup in the case of an error condition
1102 * including resetting the thread_interrupt_level
1103 */
1104 error = vm_fault_check(object, m, first_m, interruptible_state);
1105
1106 if (error != VM_FAULT_SUCCESS)
1107 return (error);
1108
1109 XPR(XPR_VM_FAULT,
1110 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
1111 object, offset,
1112 m,
1113 first_object, 0);
1114
1115 if (object != first_object) {
1116 /*
1117 * free the absent page we just found
1118 */
1119 VM_PAGE_FREE(m);
1120
1121 /*
1122 * drop reference and lock on current object
1123 */
1124 vm_object_paging_end(object);
1125 vm_object_unlock(object);
1126
1127 /*
1128 * grab the original page we
1129 * 'soldered' in place and
1130 * retake lock on 'first_object'
1131 */
1132 m = first_m;
1133 first_m = VM_PAGE_NULL;
1134
1135 object = first_object;
1136 offset = first_offset;
1137
1138 vm_object_lock(object);
1139 } else {
1140 /*
1141 * we're going to use the absent page we just found
1142 * so convert it to a 'busy' page
1143 */
1144 m->absent = FALSE;
1145 m->busy = TRUE;
1146 }
1147 /*
1148 * zero-fill the page and put it on
1149 * the correct paging queue
1150 */
1151 my_fault = vm_fault_zero_page(m, no_zero_fill);
1152
1153 break;
1154 } else {
1155 if (must_be_resident)
1156 vm_object_paging_end(object);
1157 else if (object != first_object) {
1158 vm_object_paging_end(object);
1159 VM_PAGE_FREE(m);
1160 } else {
1161 first_m = m;
1162 m->absent = FALSE;
1163 m->busy = TRUE;
1164
1165 vm_page_lockspin_queues();
1166 VM_PAGE_QUEUES_REMOVE(m);
1167 vm_page_unlock_queues();
1168 }
1169 XPR(XPR_VM_FAULT,
1170 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1171 object, offset,
1172 next_object,
1173 offset+object->shadow_offset,0);
1174
1175 offset += object->shadow_offset;
1176 fault_info->lo_offset += object->shadow_offset;
1177 fault_info->hi_offset += object->shadow_offset;
1178 access_required = VM_PROT_READ;
1179
1180 vm_object_lock(next_object);
1181 vm_object_unlock(object);
1182 object = next_object;
1183 vm_object_paging_begin(object);
1184
1185 /*
1186 * reset to default type of fault
1187 */
1188 my_fault = DBG_CACHE_HIT_FAULT;
1189
1190 continue;
1191 }
1192 }
1193 if ((m->cleaning)
1194 && ((object != first_object) || (object->copy != VM_OBJECT_NULL))
1195 && (fault_type & VM_PROT_WRITE)) {
1196 /*
1197 * This is a copy-on-write fault that will
1198 * cause us to revoke access to this page, but
1199 * this page is in the process of being cleaned
1200 * in a clustered pageout. We must wait until
1201 * the cleaning operation completes before
1202 * revoking access to the original page,
1203 * otherwise we might attempt to remove a
1204 * wired mapping.
1205 */
1206 #if TRACEFAULTPAGE
1207 dbgTrace(0xBEEF0009, (unsigned int) m, (unsigned int) offset); /* (TEST/DEBUG) */
1208 #endif
1209 XPR(XPR_VM_FAULT,
1210 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1211 object, offset,
1212 m, 0, 0);
1213 /*
1214 * take an extra ref so that object won't die
1215 */
1216 vm_object_reference_locked(object);
1217
1218 vm_fault_cleanup(object, first_m);
1219
1220 counter(c_vm_fault_page_block_backoff_kernel++);
1221 vm_object_lock(object);
1222 assert(object->ref_count > 0);
1223
1224 m = vm_page_lookup(object, offset);
1225
1226 if (m != VM_PAGE_NULL && m->cleaning) {
1227 PAGE_ASSERT_WAIT(m, interruptible);
1228
1229 vm_object_unlock(object);
1230 wait_result = thread_block(THREAD_CONTINUE_NULL);
1231 vm_object_deallocate(object);
1232
1233 goto backoff;
1234 } else {
1235 vm_object_unlock(object);
1236
1237 vm_object_deallocate(object);
1238 thread_interrupt_level(interruptible_state);
1239
1240 return (VM_FAULT_RETRY);
1241 }
1242 }
1243 if (type_of_fault == NULL && m->speculative &&
1244 !(fault_info != NULL && fault_info->stealth)) {
1245 /*
1246 * If we were passed a non-NULL pointer for
1247 * "type_of_fault", than we came from
1248 * vm_fault... we'll let it deal with
1249 * this condition, since it
1250 * needs to see m->speculative to correctly
1251 * account the pageins, otherwise...
1252 * take it off the speculative queue, we'll
1253 * let the caller of vm_fault_page deal
1254 * with getting it onto the correct queue
1255 *
1256 * If the caller specified in fault_info that
1257 * it wants a "stealth" fault, we also leave
1258 * the page in the speculative queue.
1259 */
1260 vm_page_lockspin_queues();
1261 VM_PAGE_QUEUES_REMOVE(m);
1262 vm_page_unlock_queues();
1263 }
1264
1265 if (m->encrypted) {
1266 /*
1267 * ENCRYPTED SWAP:
1268 * the user needs access to a page that we
1269 * encrypted before paging it out.
1270 * Decrypt the page now.
1271 * Keep it busy to prevent anyone from
1272 * accessing it during the decryption.
1273 */
1274 m->busy = TRUE;
1275 vm_page_decrypt(m, 0);
1276 assert(object == m->object);
1277 assert(m->busy);
1278 PAGE_WAKEUP_DONE(m);
1279
1280 /*
1281 * Retry from the top, in case
1282 * something changed while we were
1283 * decrypting.
1284 */
1285 continue;
1286 }
1287 ASSERT_PAGE_DECRYPTED(m);
1288
1289 if (m->object->code_signed) {
1290 /*
1291 * CODE SIGNING:
1292 * We just paged in a page from a signed
1293 * memory object but we don't need to
1294 * validate it now. We'll validate it if
1295 * when it gets mapped into a user address
1296 * space for the first time or when the page
1297 * gets copied to another object as a result
1298 * of a copy-on-write.
1299 */
1300 }
1301
1302 /*
1303 * We mark the page busy and leave it on
1304 * the pageout queues. If the pageout
1305 * deamon comes across it, then it will
1306 * remove the page from the queue, but not the object
1307 */
1308 #if TRACEFAULTPAGE
1309 dbgTrace(0xBEEF000B, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */
1310 #endif
1311 XPR(XPR_VM_FAULT,
1312 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1313 object, offset, m, 0, 0);
1314 assert(!m->busy);
1315 assert(!m->absent);
1316
1317 m->busy = TRUE;
1318 break;
1319 }
1320
1321
1322 /*
1323 * we get here when there is no page present in the object at
1324 * the offset we're interested in... we'll allocate a page
1325 * at this point if the pager associated with
1326 * this object can provide the data or we're the top object...
1327 * object is locked; m == NULL
1328 */
1329 look_for_page = (object->pager_created && (MUST_ASK_PAGER(object, offset) == TRUE) && !data_supply);
1330
1331 #if TRACEFAULTPAGE
1332 dbgTrace(0xBEEF000C, (unsigned int) look_for_page, (unsigned int) object); /* (TEST/DEBUG) */
1333 #endif
1334 if ((look_for_page || (object == first_object)) && !must_be_resident && !object->phys_contiguous) {
1335 /*
1336 * Allocate a new page for this object/offset pair
1337 */
1338 m = vm_page_grab();
1339 #if TRACEFAULTPAGE
1340 dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */
1341 #endif
1342 if (m == VM_PAGE_NULL) {
1343
1344 vm_fault_cleanup(object, first_m);
1345 thread_interrupt_level(interruptible_state);
1346
1347 return (VM_FAULT_MEMORY_SHORTAGE);
1348 }
1349 vm_page_insert(m, object, offset);
1350 }
1351 if (look_for_page && !must_be_resident) {
1352 kern_return_t rc;
1353
1354 /*
1355 * If the memory manager is not ready, we
1356 * cannot make requests.
1357 */
1358 if (!object->pager_ready) {
1359 #if TRACEFAULTPAGE
1360 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1361 #endif
1362 if (m != VM_PAGE_NULL)
1363 VM_PAGE_FREE(m);
1364
1365 XPR(XPR_VM_FAULT,
1366 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1367 object, offset, 0, 0, 0);
1368
1369 /*
1370 * take an extra ref so object won't die
1371 */
1372 vm_object_reference_locked(object);
1373 vm_fault_cleanup(object, first_m);
1374 counter(c_vm_fault_page_block_backoff_kernel++);
1375
1376 vm_object_lock(object);
1377 assert(object->ref_count > 0);
1378
1379 if (!object->pager_ready) {
1380 wait_result = vm_object_assert_wait(object, VM_OBJECT_EVENT_PAGER_READY, interruptible);
1381
1382 vm_object_unlock(object);
1383 if (wait_result == THREAD_WAITING)
1384 wait_result = thread_block(THREAD_CONTINUE_NULL);
1385 vm_object_deallocate(object);
1386
1387 goto backoff;
1388 } else {
1389 vm_object_unlock(object);
1390 vm_object_deallocate(object);
1391 thread_interrupt_level(interruptible_state);
1392
1393 return (VM_FAULT_RETRY);
1394 }
1395 }
1396 if (!object->internal && !object->phys_contiguous && object->paging_in_progress > vm_object_pagein_throttle) {
1397 /*
1398 * If there are too many outstanding page
1399 * requests pending on this external object, we
1400 * wait for them to be resolved now.
1401 */
1402 #if TRACEFAULTPAGE
1403 dbgTrace(0xBEEF0010, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */
1404 #endif
1405 if (m != VM_PAGE_NULL)
1406 VM_PAGE_FREE(m);
1407 /*
1408 * take an extra ref so object won't die
1409 */
1410 vm_object_reference_locked(object);
1411
1412 vm_fault_cleanup(object, first_m);
1413
1414 counter(c_vm_fault_page_block_backoff_kernel++);
1415
1416 vm_object_lock(object);
1417 assert(object->ref_count > 0);
1418
1419 if (object->paging_in_progress > vm_object_pagein_throttle) {
1420 vm_object_assert_wait(object, VM_OBJECT_EVENT_PAGING_IN_PROGRESS, interruptible);
1421
1422 vm_object_unlock(object);
1423 wait_result = thread_block(THREAD_CONTINUE_NULL);
1424 vm_object_deallocate(object);
1425
1426 goto backoff;
1427 } else {
1428 vm_object_unlock(object);
1429 vm_object_deallocate(object);
1430 thread_interrupt_level(interruptible_state);
1431
1432 return (VM_FAULT_RETRY);
1433 }
1434 }
1435 if (m != VM_PAGE_NULL) {
1436 /*
1437 * Indicate that the page is waiting for data
1438 * from the memory manager.
1439 */
1440 m->list_req_pending = TRUE;
1441 m->absent = TRUE;
1442 }
1443
1444 #if TRACEFAULTPAGE
1445 dbgTrace(0xBEEF0012, (unsigned int) object, (unsigned int) 0); /* (TEST/DEBUG) */
1446 #endif
1447
1448 /*
1449 * It's possible someone called vm_object_destroy while we weren't
1450 * holding the object lock. If that has happened, then bail out
1451 * here.
1452 */
1453
1454 pager = object->pager;
1455
1456 if (pager == MEMORY_OBJECT_NULL) {
1457 vm_fault_cleanup(object, first_m);
1458 thread_interrupt_level(interruptible_state);
1459 return VM_FAULT_MEMORY_ERROR;
1460 }
1461
1462 /*
1463 * We have an absent page in place for the faulting offset,
1464 * so we can release the object lock.
1465 */
1466
1467 vm_object_unlock(object);
1468
1469 /*
1470 * If this object uses a copy_call strategy,
1471 * and we are interested in a copy of this object
1472 * (having gotten here only by following a
1473 * shadow chain), then tell the memory manager
1474 * via a flag added to the desired_access
1475 * parameter, so that it can detect a race
1476 * between our walking down the shadow chain
1477 * and its pushing pages up into a copy of
1478 * the object that it manages.
1479 */
1480 if (object->copy_strategy == MEMORY_OBJECT_COPY_CALL && object != first_object)
1481 wants_copy_flag = VM_PROT_WANTS_COPY;
1482 else
1483 wants_copy_flag = VM_PROT_NONE;
1484
1485 XPR(XPR_VM_FAULT,
1486 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1487 object, offset, m,
1488 access_required | wants_copy_flag, 0);
1489
1490 /*
1491 * Call the memory manager to retrieve the data.
1492 */
1493 rc = memory_object_data_request(
1494 pager,
1495 offset + object->paging_offset,
1496 PAGE_SIZE,
1497 access_required | wants_copy_flag,
1498 (memory_object_fault_info_t)fault_info);
1499
1500 #if TRACEFAULTPAGE
1501 dbgTrace(0xBEEF0013, (unsigned int) object, (unsigned int) rc); /* (TEST/DEBUG) */
1502 #endif
1503 vm_object_lock(object);
1504
1505 if (rc != KERN_SUCCESS) {
1506
1507 vm_fault_cleanup(object, first_m);
1508 thread_interrupt_level(interruptible_state);
1509
1510 return ((rc == MACH_SEND_INTERRUPTED) ?
1511 VM_FAULT_INTERRUPTED :
1512 VM_FAULT_MEMORY_ERROR);
1513 } else {
1514 clock_sec_t tv_sec;
1515 clock_usec_t tv_usec;
1516
1517 clock_get_system_microtime(&tv_sec, &tv_usec);
1518 current_thread()->t_page_creation_time = tv_sec;
1519 current_thread()->t_page_creation_count = 0;
1520 }
1521 if ((interruptible != THREAD_UNINT) && (current_thread()->sched_mode & TH_MODE_ABORT)) {
1522
1523 vm_fault_cleanup(object, first_m);
1524 thread_interrupt_level(interruptible_state);
1525
1526 return (VM_FAULT_INTERRUPTED);
1527 }
1528 if (m == VM_PAGE_NULL && object->phys_contiguous) {
1529 /*
1530 * No page here means that the object we
1531 * initially looked up was "physically
1532 * contiguous" (i.e. device memory). However,
1533 * with Virtual VRAM, the object might not
1534 * be backed by that device memory anymore,
1535 * so we're done here only if the object is
1536 * still "phys_contiguous".
1537 * Otherwise, if the object is no longer
1538 * "phys_contiguous", we need to retry the
1539 * page fault against the object's new backing
1540 * store (different memory object).
1541 */
1542 phys_contig_object:
1543 goto done;
1544 }
1545 /*
1546 * potentially a pagein fault
1547 * if we make it through the state checks
1548 * above, than we'll count it as such
1549 */
1550 my_fault = DBG_PAGEIN_FAULT;
1551
1552 /*
1553 * Retry with same object/offset, since new data may
1554 * be in a different page (i.e., m is meaningless at
1555 * this point).
1556 */
1557 continue;
1558 }
1559
1560 /*
1561 * We get here if the object has no pager, or an existence map
1562 * exists and indicates the page isn't present on the pager
1563 * or we're unwiring a page. If a pager exists, but there
1564 * is no existence map, then the m->absent case above handles
1565 * the ZF case when the pager can't provide the page
1566 */
1567 #if TRACEFAULTPAGE
1568 dbgTrace(0xBEEF0014, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */
1569 #endif
1570 if (object == first_object)
1571 first_m = m;
1572 else
1573 assert(m == VM_PAGE_NULL);
1574
1575 XPR(XPR_VM_FAULT,
1576 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1577 object, offset, m,
1578 object->shadow, 0);
1579
1580 next_object = object->shadow;
1581
1582 if (next_object == VM_OBJECT_NULL) {
1583 /*
1584 * we've hit the bottom of the shadown chain,
1585 * fill the page in the top object with zeros.
1586 */
1587 assert(!must_be_resident);
1588
1589 if (object != first_object) {
1590 vm_object_paging_end(object);
1591 vm_object_unlock(object);
1592
1593 object = first_object;
1594 offset = first_offset;
1595 vm_object_lock(object);
1596 }
1597 m = first_m;
1598 assert(m->object == object);
1599 first_m = VM_PAGE_NULL;
1600
1601 /*
1602 * check for any conditions that prevent
1603 * us from creating a new zero-fill page
1604 * vm_fault_check will do all of the
1605 * fault cleanup in the case of an error condition
1606 * including resetting the thread_interrupt_level
1607 */
1608 error = vm_fault_check(object, m, first_m, interruptible_state);
1609
1610 if (error != VM_FAULT_SUCCESS)
1611 return (error);
1612
1613 if (m == VM_PAGE_NULL) {
1614 m = vm_page_grab();
1615
1616 if (m == VM_PAGE_NULL) {
1617 vm_fault_cleanup(object, VM_PAGE_NULL);
1618 thread_interrupt_level(interruptible_state);
1619
1620 return (VM_FAULT_MEMORY_SHORTAGE);
1621 }
1622 vm_page_insert(m, object, offset);
1623 }
1624 my_fault = vm_fault_zero_page(m, no_zero_fill);
1625
1626 break;
1627
1628 } else {
1629 /*
1630 * Move on to the next object. Lock the next
1631 * object before unlocking the current one.
1632 */
1633 if ((object != first_object) || must_be_resident)
1634 vm_object_paging_end(object);
1635
1636 offset += object->shadow_offset;
1637 fault_info->lo_offset += object->shadow_offset;
1638 fault_info->hi_offset += object->shadow_offset;
1639 access_required = VM_PROT_READ;
1640
1641 vm_object_lock(next_object);
1642 vm_object_unlock(object);
1643
1644 object = next_object;
1645 vm_object_paging_begin(object);
1646 }
1647 }
1648
1649 /*
1650 * PAGE HAS BEEN FOUND.
1651 *
1652 * This page (m) is:
1653 * busy, so that we can play with it;
1654 * not absent, so that nobody else will fill it;
1655 * possibly eligible for pageout;
1656 *
1657 * The top-level page (first_m) is:
1658 * VM_PAGE_NULL if the page was found in the
1659 * top-level object;
1660 * busy, not absent, and ineligible for pageout.
1661 *
1662 * The current object (object) is locked. A paging
1663 * reference is held for the current and top-level
1664 * objects.
1665 */
1666
1667 #if TRACEFAULTPAGE
1668 dbgTrace(0xBEEF0015, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */
1669 #endif
1670 #if EXTRA_ASSERTIONS
1671 assert(m->busy && !m->absent);
1672 assert((first_m == VM_PAGE_NULL) ||
1673 (first_m->busy && !first_m->absent &&
1674 !first_m->active && !first_m->inactive));
1675 #endif /* EXTRA_ASSERTIONS */
1676
1677 /*
1678 * ENCRYPTED SWAP:
1679 * If we found a page, we must have decrypted it before we
1680 * get here...
1681 */
1682 ASSERT_PAGE_DECRYPTED(m);
1683
1684 XPR(XPR_VM_FAULT,
1685 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1686 object, offset, m,
1687 first_object, first_m);
1688
1689 /*
1690 * If the page is being written, but isn't
1691 * already owned by the top-level object,
1692 * we have to copy it into a new page owned
1693 * by the top-level object.
1694 */
1695 if (object != first_object) {
1696
1697 #if TRACEFAULTPAGE
1698 dbgTrace(0xBEEF0016, (unsigned int) object, (unsigned int) fault_type); /* (TEST/DEBUG) */
1699 #endif
1700 if (fault_type & VM_PROT_WRITE) {
1701 vm_page_t copy_m;
1702
1703 /*
1704 * We only really need to copy if we
1705 * want to write it.
1706 */
1707 assert(!must_be_resident);
1708
1709 /*
1710 * are we protecting the system from
1711 * backing store exhaustion. If so
1712 * sleep unless we are privileged.
1713 */
1714 if (vm_backing_store_low) {
1715 if (!(current_task()->priv_flags & VM_BACKING_STORE_PRIV)) {
1716
1717 RELEASE_PAGE(m);
1718 vm_fault_cleanup(object, first_m);
1719
1720 assert_wait((event_t)&vm_backing_store_low, THREAD_UNINT);
1721
1722 thread_block(THREAD_CONTINUE_NULL);
1723 thread_interrupt_level(interruptible_state);
1724
1725 return (VM_FAULT_RETRY);
1726 }
1727 }
1728 /*
1729 * If we try to collapse first_object at this
1730 * point, we may deadlock when we try to get
1731 * the lock on an intermediate object (since we
1732 * have the bottom object locked). We can't
1733 * unlock the bottom object, because the page
1734 * we found may move (by collapse) if we do.
1735 *
1736 * Instead, we first copy the page. Then, when
1737 * we have no more use for the bottom object,
1738 * we unlock it and try to collapse.
1739 *
1740 * Note that we copy the page even if we didn't
1741 * need to... that's the breaks.
1742 */
1743
1744 /*
1745 * Allocate a page for the copy
1746 */
1747 copy_m = vm_page_grab();
1748
1749 if (copy_m == VM_PAGE_NULL) {
1750 RELEASE_PAGE(m);
1751
1752 vm_fault_cleanup(object, first_m);
1753 thread_interrupt_level(interruptible_state);
1754
1755 return (VM_FAULT_MEMORY_SHORTAGE);
1756 }
1757 XPR(XPR_VM_FAULT,
1758 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
1759 object, offset,
1760 m, copy_m, 0);
1761
1762 vm_page_copy(m, copy_m);
1763
1764 /*
1765 * If another map is truly sharing this
1766 * page with us, we have to flush all
1767 * uses of the original page, since we
1768 * can't distinguish those which want the
1769 * original from those which need the
1770 * new copy.
1771 *
1772 * XXXO If we know that only one map has
1773 * access to this page, then we could
1774 * avoid the pmap_disconnect() call.
1775 */
1776 if (m->pmapped)
1777 pmap_disconnect(m->phys_page);
1778
1779 assert(!m->cleaning);
1780
1781 /*
1782 * We no longer need the old page or object.
1783 */
1784 PAGE_WAKEUP_DONE(m);
1785 vm_object_paging_end(object);
1786 vm_object_unlock(object);
1787
1788 my_fault = DBG_COW_FAULT;
1789 VM_STAT_INCR(cow_faults);
1790 DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL);
1791 current_task()->cow_faults++;
1792
1793 object = first_object;
1794 offset = first_offset;
1795
1796 vm_object_lock(object);
1797 /*
1798 * get rid of the place holder
1799 * page that we soldered in earlier
1800 */
1801 VM_PAGE_FREE(first_m);
1802 first_m = VM_PAGE_NULL;
1803
1804 /*
1805 * and replace it with the
1806 * page we just copied into
1807 */
1808 assert(copy_m->busy);
1809 vm_page_insert(copy_m, object, offset);
1810 copy_m->dirty = TRUE;
1811
1812 m = copy_m;
1813 /*
1814 * Now that we've gotten the copy out of the
1815 * way, let's try to collapse the top object.
1816 * But we have to play ugly games with
1817 * paging_in_progress to do that...
1818 */
1819 vm_object_paging_end(object);
1820 vm_object_collapse(object, offset, TRUE);
1821 vm_object_paging_begin(object);
1822
1823 } else
1824 *protection &= (~VM_PROT_WRITE);
1825 }
1826 /*
1827 * Now check whether the page needs to be pushed into the
1828 * copy object. The use of asymmetric copy on write for
1829 * shared temporary objects means that we may do two copies to
1830 * satisfy the fault; one above to get the page from a
1831 * shadowed object, and one here to push it into the copy.
1832 */
1833 try_failed_count = 0;
1834
1835 while ((copy_object = first_object->copy) != VM_OBJECT_NULL) {
1836 vm_object_offset_t copy_offset;
1837 vm_page_t copy_m;
1838
1839 #if TRACEFAULTPAGE
1840 dbgTrace(0xBEEF0017, (unsigned int) copy_object, (unsigned int) fault_type); /* (TEST/DEBUG) */
1841 #endif
1842 /*
1843 * If the page is being written, but hasn't been
1844 * copied to the copy-object, we have to copy it there.
1845 */
1846 if ((fault_type & VM_PROT_WRITE) == 0) {
1847 *protection &= ~VM_PROT_WRITE;
1848 break;
1849 }
1850
1851 /*
1852 * If the page was guaranteed to be resident,
1853 * we must have already performed the copy.
1854 */
1855 if (must_be_resident)
1856 break;
1857
1858 /*
1859 * Try to get the lock on the copy_object.
1860 */
1861 if (!vm_object_lock_try(copy_object)) {
1862
1863 vm_object_unlock(object);
1864 try_failed_count++;
1865
1866 mutex_pause(try_failed_count); /* wait a bit */
1867 vm_object_lock(object);
1868
1869 continue;
1870 }
1871 try_failed_count = 0;
1872
1873 /*
1874 * Make another reference to the copy-object,
1875 * to keep it from disappearing during the
1876 * copy.
1877 */
1878 vm_object_reference_locked(copy_object);
1879
1880 /*
1881 * Does the page exist in the copy?
1882 */
1883 copy_offset = first_offset - copy_object->shadow_offset;
1884
1885 if (copy_object->size <= copy_offset)
1886 /*
1887 * Copy object doesn't cover this page -- do nothing.
1888 */
1889 ;
1890 else if ((copy_m = vm_page_lookup(copy_object, copy_offset)) != VM_PAGE_NULL) {
1891 /*
1892 * Page currently exists in the copy object
1893 */
1894 if (copy_m->busy) {
1895 /*
1896 * If the page is being brought
1897 * in, wait for it and then retry.
1898 */
1899 RELEASE_PAGE(m);
1900
1901 /*
1902 * take an extra ref so object won't die
1903 */
1904 vm_object_reference_locked(copy_object);
1905 vm_object_unlock(copy_object);
1906 vm_fault_cleanup(object, first_m);
1907 counter(c_vm_fault_page_block_backoff_kernel++);
1908
1909 vm_object_lock(copy_object);
1910 assert(copy_object->ref_count > 0);
1911 VM_OBJ_RES_DECR(copy_object);
1912 vm_object_lock_assert_exclusive(copy_object);
1913 copy_object->ref_count--;
1914 assert(copy_object->ref_count > 0);
1915 copy_m = vm_page_lookup(copy_object, copy_offset);
1916 /*
1917 * ENCRYPTED SWAP:
1918 * it's OK if the "copy_m" page is encrypted,
1919 * because we're not moving it nor handling its
1920 * contents.
1921 */
1922 if (copy_m != VM_PAGE_NULL && copy_m->busy) {
1923 PAGE_ASSERT_WAIT(copy_m, interruptible);
1924
1925 vm_object_unlock(copy_object);
1926 wait_result = thread_block(THREAD_CONTINUE_NULL);
1927 vm_object_deallocate(copy_object);
1928
1929 goto backoff;
1930 } else {
1931 vm_object_unlock(copy_object);
1932 vm_object_deallocate(copy_object);
1933 thread_interrupt_level(interruptible_state);
1934
1935 return (VM_FAULT_RETRY);
1936 }
1937 }
1938 }
1939 else if (!PAGED_OUT(copy_object, copy_offset)) {
1940 /*
1941 * If PAGED_OUT is TRUE, then the page used to exist
1942 * in the copy-object, and has already been paged out.
1943 * We don't need to repeat this. If PAGED_OUT is
1944 * FALSE, then either we don't know (!pager_created,
1945 * for example) or it hasn't been paged out.
1946 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
1947 * We must copy the page to the copy object.
1948 */
1949
1950 if (vm_backing_store_low) {
1951 /*
1952 * we are protecting the system from
1953 * backing store exhaustion. If so
1954 * sleep unless we are privileged.
1955 */
1956 if (!(current_task()->priv_flags & VM_BACKING_STORE_PRIV)) {
1957 assert_wait((event_t)&vm_backing_store_low, THREAD_UNINT);
1958
1959 RELEASE_PAGE(m);
1960 VM_OBJ_RES_DECR(copy_object);
1961 vm_object_lock_assert_exclusive(copy_object);
1962 copy_object->ref_count--;
1963 assert(copy_object->ref_count > 0);
1964
1965 vm_object_unlock(copy_object);
1966 vm_fault_cleanup(object, first_m);
1967 thread_block(THREAD_CONTINUE_NULL);
1968 thread_interrupt_level(interruptible_state);
1969
1970 return (VM_FAULT_RETRY);
1971 }
1972 }
1973 /*
1974 * Allocate a page for the copy
1975 */
1976 copy_m = vm_page_alloc(copy_object, copy_offset);
1977
1978 if (copy_m == VM_PAGE_NULL) {
1979 RELEASE_PAGE(m);
1980
1981 VM_OBJ_RES_DECR(copy_object);
1982 vm_object_lock_assert_exclusive(copy_object);
1983 copy_object->ref_count--;
1984 assert(copy_object->ref_count > 0);
1985
1986 vm_object_unlock(copy_object);
1987 vm_fault_cleanup(object, first_m);
1988 thread_interrupt_level(interruptible_state);
1989
1990 return (VM_FAULT_MEMORY_SHORTAGE);
1991 }
1992 /*
1993 * Must copy page into copy-object.
1994 */
1995 vm_page_copy(m, copy_m);
1996
1997 /*
1998 * If the old page was in use by any users
1999 * of the copy-object, it must be removed
2000 * from all pmaps. (We can't know which
2001 * pmaps use it.)
2002 */
2003 if (m->pmapped)
2004 pmap_disconnect(m->phys_page);
2005
2006 /*
2007 * If there's a pager, then immediately
2008 * page out this page, using the "initialize"
2009 * option. Else, we use the copy.
2010 */
2011 if ((!copy_object->pager_created)
2012 #if MACH_PAGEMAP
2013 || vm_external_state_get(copy_object->existence_map, copy_offset) == VM_EXTERNAL_STATE_ABSENT
2014 #endif
2015 ) {
2016
2017 vm_page_lockspin_queues();
2018 assert(!m->cleaning);
2019 vm_page_activate(copy_m);
2020 vm_page_unlock_queues();
2021
2022 copy_m->dirty = TRUE;
2023 PAGE_WAKEUP_DONE(copy_m);
2024 }
2025 else {
2026 assert(copy_m->busy == TRUE);
2027 assert(!m->cleaning);
2028
2029 /*
2030 * dirty is protected by the object lock
2031 */
2032 copy_m->dirty = TRUE;
2033
2034 /*
2035 * The page is already ready for pageout:
2036 * not on pageout queues and busy.
2037 * Unlock everything except the
2038 * copy_object itself.
2039 */
2040 vm_object_unlock(object);
2041
2042 /*
2043 * Write the page to the copy-object,
2044 * flushing it from the kernel.
2045 */
2046 vm_pageout_initialize_page(copy_m);
2047
2048 /*
2049 * Since the pageout may have
2050 * temporarily dropped the
2051 * copy_object's lock, we
2052 * check whether we'll have
2053 * to deallocate the hard way.
2054 */
2055 if ((copy_object->shadow != object) || (copy_object->ref_count == 1)) {
2056 vm_object_unlock(copy_object);
2057 vm_object_deallocate(copy_object);
2058 vm_object_lock(object);
2059
2060 continue;
2061 }
2062 /*
2063 * Pick back up the old object's
2064 * lock. [It is safe to do so,
2065 * since it must be deeper in the
2066 * object tree.]
2067 */
2068 vm_object_lock(object);
2069 }
2070 /*
2071 * Because we're pushing a page upward
2072 * in the object tree, we must restart
2073 * any faults that are waiting here.
2074 * [Note that this is an expansion of
2075 * PAGE_WAKEUP that uses the THREAD_RESTART
2076 * wait result]. Can't turn off the page's
2077 * busy bit because we're not done with it.
2078 */
2079 if (m->wanted) {
2080 m->wanted = FALSE;
2081 thread_wakeup_with_result((event_t) m, THREAD_RESTART);
2082 }
2083 }
2084 /*
2085 * The reference count on copy_object must be
2086 * at least 2: one for our extra reference,
2087 * and at least one from the outside world
2088 * (we checked that when we last locked
2089 * copy_object).
2090 */
2091 vm_object_lock_assert_exclusive(copy_object);
2092 copy_object->ref_count--;
2093 assert(copy_object->ref_count > 0);
2094
2095 VM_OBJ_RES_DECR(copy_object);
2096 vm_object_unlock(copy_object);
2097
2098 break;
2099 }
2100
2101 done:
2102 *result_page = m;
2103 *top_page = first_m;
2104
2105 XPR(XPR_VM_FAULT,
2106 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2107 object, offset, m, first_m, 0);
2108
2109 if (m != VM_PAGE_NULL) {
2110 retval = VM_FAULT_SUCCESS;
2111 if (my_fault == DBG_PAGEIN_FAULT) {
2112
2113 VM_STAT_INCR(pageins);
2114 DTRACE_VM2(pgin, int, 1, (uint64_t *), NULL);
2115 DTRACE_VM2(maj_fault, int, 1, (uint64_t *), NULL);
2116 current_task()->pageins++;
2117
2118 if (m->object->internal) {
2119 DTRACE_VM2(anonpgin, int, 1, (uint64_t *), NULL);
2120 my_fault = DBG_PAGEIND_FAULT;
2121 } else {
2122 DTRACE_VM2(fspgin, int, 1, (uint64_t *), NULL);
2123 my_fault = DBG_PAGEINV_FAULT;
2124 }
2125
2126 /*
2127 * evaluate access pattern and update state
2128 * vm_fault_deactivate_behind depends on the
2129 * state being up to date
2130 */
2131 vm_fault_is_sequential(object, offset, fault_info->behavior);
2132
2133 vm_fault_deactivate_behind(object, offset, fault_info->behavior);
2134 }
2135 if (type_of_fault)
2136 *type_of_fault = my_fault;
2137 } else {
2138 retval = VM_FAULT_SUCCESS_NO_VM_PAGE;
2139 assert(first_m == VM_PAGE_NULL);
2140 assert(object == first_object);
2141 }
2142
2143 thread_interrupt_level(interruptible_state);
2144
2145 #if TRACEFAULTPAGE
2146 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS, 0); /* (TEST/DEBUG) */
2147 #endif
2148 return retval;
2149
2150 backoff:
2151 thread_interrupt_level(interruptible_state);
2152
2153 if (wait_result == THREAD_INTERRUPTED)
2154 return (VM_FAULT_INTERRUPTED);
2155 return (VM_FAULT_RETRY);
2156
2157 #undef RELEASE_PAGE
2158 }
2159
2160
2161
2162 /*
2163 * CODE SIGNING:
2164 * When soft faulting a page, we have to validate the page if:
2165 * 1. the page is being mapped in user space
2166 * 2. the page hasn't already been found to be "tainted"
2167 * 3. the page belongs to a code-signed object
2168 * 4. the page has not been validated yet or has been mapped for write.
2169 */
2170 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page) \
2171 ((pmap) != kernel_pmap /*1*/ && \
2172 !(page)->cs_tainted /*2*/ && \
2173 (page)->object->code_signed /*3*/ && \
2174 (!(page)->cs_validated || (page)->wpmapped /*4*/))
2175
2176
2177 /*
2178 * page queue lock must NOT be held
2179 * m->object must be locked
2180 *
2181 * NOTE: m->object could be locked "shared" only if we are called
2182 * from vm_fault() as part of a soft fault. If so, we must be
2183 * careful not to modify the VM object in any way that is not
2184 * legal under a shared lock...
2185 */
2186 unsigned long cs_enter_tainted_rejected = 0;
2187 unsigned long cs_enter_tainted_accepted = 0;
2188 kern_return_t
2189 vm_fault_enter(vm_page_t m,
2190 pmap_t pmap,
2191 vm_map_offset_t vaddr,
2192 vm_prot_t prot,
2193 boolean_t wired,
2194 boolean_t change_wiring,
2195 boolean_t no_cache,
2196 int *type_of_fault)
2197 {
2198 unsigned int cache_attr;
2199 kern_return_t kr;
2200 boolean_t previously_pmapped = m->pmapped;
2201 boolean_t must_disconnect = 0;
2202 boolean_t map_is_switched, map_is_switch_protected;
2203
2204 vm_object_lock_assert_held(m->object);
2205 #if DEBUG
2206 lck_mtx_assert(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED);
2207 #endif /* DEBUG */
2208
2209 if (m->phys_page == vm_page_guard_addr) {
2210 assert(m->fictitious);
2211 return KERN_SUCCESS;
2212 }
2213
2214 cache_attr = ((unsigned int)m->object->wimg_bits) & VM_WIMG_MASK;
2215
2216 if (m->pmapped == FALSE) {
2217 /*
2218 * This is the first time this page is being
2219 * mapped in an address space (pmapped == FALSE).
2220 *
2221 * Part of that page may still be in the data cache
2222 * and not flushed to memory. In case we end up
2223 * accessing that page via the instruction cache,
2224 * we need to ensure that the 2 caches are in sync.
2225 */
2226 pmap_sync_page_data_phys(m->phys_page);
2227
2228 if ((*type_of_fault == DBG_CACHE_HIT_FAULT) && m->clustered) {
2229 /*
2230 * found it in the cache, but this
2231 * is the first fault-in of the page (m->pmapped == FALSE)
2232 * so it must have come in as part of
2233 * a cluster... account 1 pagein against it
2234 */
2235 VM_STAT_INCR(pageins);
2236 DTRACE_VM2(pgin, int, 1, (uint64_t *), NULL);
2237
2238 if (m->object->internal) {
2239 DTRACE_VM2(anonpgin, int, 1, (uint64_t *), NULL);
2240 *type_of_fault = DBG_PAGEIND_FAULT;
2241 } else {
2242 DTRACE_VM2(fspgin, int, 1, (uint64_t *), NULL);
2243 *type_of_fault = DBG_PAGEINV_FAULT;
2244 }
2245
2246 current_task()->pageins++;
2247 }
2248 VM_PAGE_CONSUME_CLUSTERED(m);
2249
2250 } else if (cache_attr != VM_WIMG_DEFAULT)
2251 pmap_sync_page_attributes_phys(m->phys_page);
2252
2253 if (*type_of_fault != DBG_COW_FAULT) {
2254 DTRACE_VM2(as_fault, int, 1, (uint64_t *), NULL);
2255
2256 if (pmap == kernel_pmap) {
2257 DTRACE_VM2(kernel_asflt, int, 1, (uint64_t *), NULL);
2258 }
2259 }
2260
2261 /* Validate code signature if necessary. */
2262 if (VM_FAULT_NEED_CS_VALIDATION(pmap, m)) {
2263 vm_object_lock_assert_exclusive(m->object);
2264
2265 if (m->cs_validated) {
2266 vm_cs_revalidates++;
2267 }
2268
2269 /* VM map is locked, so 1 ref will remain on VM object -
2270 * so no harm if vm_page_validate_cs drops the object lock */
2271 vm_page_validate_cs(m);
2272 }
2273
2274 #define page_immutable(m,prot) ((m)->cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/)
2275
2276 map_is_switched = ((pmap != vm_map_pmap(current_task()->map)) &&
2277 (pmap == vm_map_pmap(current_thread()->map)));
2278 map_is_switch_protected = current_thread()->map->switch_protect;
2279
2280 /* If the map is switched, and is switch-protected, we must protect
2281 * some pages from being write-faulted: immutable pages because by
2282 * definition they may not be written, and executable pages because that
2283 * would provide a way to inject unsigned code.
2284 * If the page is immutable, we can simply return. However, we can't
2285 * immediately determine whether a page is executable anywhere. But,
2286 * we can disconnect it everywhere and remove the executable protection
2287 * from the current map. We do that below right before we do the
2288 * PMAP_ENTER.
2289 */
2290 if(!cs_enforcement_disable && map_is_switched &&
2291 map_is_switch_protected && page_immutable(m, prot) &&
2292 (prot & VM_PROT_WRITE))
2293 {
2294 return KERN_CODESIGN_ERROR;
2295 }
2296
2297 /* A page could be tainted, or pose a risk of being tainted later.
2298 * Check whether the receiving process wants it, and make it feel
2299 * the consequences (that hapens in cs_invalid_page()).
2300 * For CS Enforcement, two other conditions will
2301 * cause that page to be tainted as well:
2302 * - pmapping an unsigned page executable - this means unsigned code;
2303 * - writeable mapping of a validated page - the content of that page
2304 * can be changed without the kernel noticing, therefore unsigned
2305 * code can be created
2306 */
2307 if (m->cs_tainted ||
2308 ( !cs_enforcement_disable &&
2309 (/* The page is unsigned and wants to be executable */
2310 (!m->cs_validated && (prot & VM_PROT_EXECUTE)) ||
2311 /* The page should be immutable, but is in danger of being modified
2312 * This is the case where we want policy from the code directory -
2313 * is the page immutable or not? For now we have to assume that
2314 * code pages will be immutable, data pages not.
2315 * We'll assume a page is a code page if it has a code directory
2316 * and we fault for execution.
2317 * That is good enough since if we faulted the code page for
2318 * writing in another map before, it is wpmapped; if we fault
2319 * it for writing in this map later it will also be faulted for executing
2320 * at the same time; and if we fault for writing in another map
2321 * later, we will disconnect it from this pmap so we'll notice
2322 * the change.
2323 */
2324 (page_immutable(m, prot) && ((prot & VM_PROT_WRITE) || m->wpmapped))
2325 ))
2326 )
2327 {
2328 /* We will have a tainted page. Have to handle the special case
2329 * of a switched map now. If the map is not switched, standard
2330 * procedure applies - call cs_invalid_page().
2331 * If the map is switched, the real owner is invalid already.
2332 * There is no point in invalidating the switching process since
2333 * it will not be executing from the map. So we don't call
2334 * cs_invalid_page() in that case. */
2335 boolean_t reject_page;
2336 if(map_is_switched) {
2337 assert(pmap==vm_map_pmap(current_thread()->map));
2338 assert(!(prot & VM_PROT_WRITE) || (map_is_switch_protected == FALSE));
2339 reject_page = FALSE;
2340 } else {
2341 reject_page = cs_invalid_page((addr64_t) vaddr);
2342 }
2343
2344 if (reject_page) {
2345 /* reject the tainted page: abort the page fault */
2346 kr = KERN_CODESIGN_ERROR;
2347 cs_enter_tainted_rejected++;
2348 } else {
2349 /* proceed with the tainted page */
2350 kr = KERN_SUCCESS;
2351 /* Page might have been tainted before or not; now it
2352 * definitively is. If the page wasn't tainted, we must
2353 * disconnect it from all pmaps later. */
2354 must_disconnect = !m->cs_tainted;
2355 m->cs_tainted = TRUE;
2356 cs_enter_tainted_accepted++;
2357 }
2358 if (cs_debug || kr != KERN_SUCCESS) {
2359 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2360 "page %p obj %p off 0x%llx *** INVALID PAGE ***\n",
2361 (long long)vaddr, m, m->object, m->offset);
2362 }
2363
2364 } else {
2365 /* proceed with the valid page */
2366 kr = KERN_SUCCESS;
2367 }
2368
2369 /* If we have a KERN_SUCCESS from the previous checks, we either have
2370 * a good page, or a tainted page that has been accepted by the process.
2371 * In both cases the page will be entered into the pmap.
2372 * If the page is writeable, we need to disconnect it from other pmaps
2373 * now so those processes can take note.
2374 */
2375 if (kr == KERN_SUCCESS) {
2376 /*
2377 * NOTE: we may only hold the vm_object lock SHARED
2378 * at this point, but the update of pmapped is ok
2379 * since this is the ONLY bit updated behind the SHARED
2380 * lock... however, we need to figure out how to do an atomic
2381 * update on a bit field to make this less fragile... right
2382 * now I don't know how to coerce 'C' to give me the offset info
2383 * that's needed for an AtomicCompareAndSwap
2384 */
2385 m->pmapped = TRUE;
2386 if (prot & VM_PROT_WRITE) {
2387 vm_object_lock_assert_exclusive(m->object);
2388 m->wpmapped = TRUE;
2389 if(must_disconnect) {
2390 /* We can only get here
2391 * because of the CSE logic */
2392 assert(cs_enforcement_disable == FALSE);
2393 pmap_disconnect(m->phys_page);
2394 /* If we are faulting for a write, we can clear
2395 * the execute bit - that will ensure the page is
2396 * checked again before being executable, which
2397 * protects against a map switch.
2398 * This only happens the first time the page
2399 * gets tainted, so we won't get stuck here
2400 * to make an already writeable page executable. */
2401 prot &= ~VM_PROT_EXECUTE;
2402 }
2403 }
2404 PMAP_ENTER(pmap, vaddr, m, prot, cache_attr, wired);
2405 }
2406
2407 /*
2408 * Hold queues lock to manipulate
2409 * the page queues. Change wiring
2410 * case is obvious.
2411 */
2412 if (change_wiring) {
2413 vm_page_lockspin_queues();
2414
2415 if (wired) {
2416 if (kr == KERN_SUCCESS) {
2417 vm_page_wire(m);
2418 }
2419 } else {
2420 vm_page_unwire(m);
2421 }
2422 vm_page_unlock_queues();
2423
2424 } else {
2425 if (kr != KERN_SUCCESS) {
2426 vm_page_lockspin_queues();
2427 vm_page_deactivate(m);
2428 vm_page_unlock_queues();
2429 } else {
2430 if (((!m->active && !m->inactive) || no_cache) && !VM_PAGE_WIRED(m) && !m->throttled) {
2431
2432 if ( vm_page_local_q && !no_cache && (*type_of_fault == DBG_COW_FAULT || *type_of_fault == DBG_ZERO_FILL_FAULT) ) {
2433 struct vpl *lq;
2434 uint32_t lid;
2435
2436 /*
2437 * we got a local queue to stuff this new page on...
2438 * its safe to manipulate local and local_id at this point
2439 * since we're behind an exclusive object lock and the
2440 * page is not on any global queue.
2441 *
2442 * we'll use the current cpu number to select the queue
2443 * note that we don't need to disable preemption... we're
2444 * going to behind the local queue's lock to do the real
2445 * work
2446 */
2447 lid = cpu_number();
2448
2449 lq = &vm_page_local_q[lid].vpl_un.vpl;
2450
2451 VPL_LOCK(&lq->vpl_lock);
2452
2453 queue_enter(&lq->vpl_queue, m, vm_page_t, pageq);
2454 m->local = TRUE;
2455 m->local_id = lid;
2456 lq->vpl_count++;
2457
2458 VPL_UNLOCK(&lq->vpl_lock);
2459
2460 if (lq->vpl_count > vm_page_local_q_soft_limit) {
2461 /*
2462 * we're beyond the soft limit for the local queue
2463 * vm_page_reactivate_local will 'try' to take
2464 * the global page queue lock... if it can't that's
2465 * ok... we'll let the queue continue to grow up
2466 * to the hard limit... at that point we'll wait
2467 * for the lock... once we've got the lock, we'll
2468 * transfer all of the pages from the local queue
2469 * to the global active queue
2470 */
2471 vm_page_reactivate_local(lid, FALSE, FALSE);
2472 }
2473 return kr;
2474 }
2475
2476 vm_page_lockspin_queues();
2477 /*
2478 * test again now that we hold the page queue lock
2479 */
2480 if (((!m->active && !m->inactive) || no_cache) && !VM_PAGE_WIRED(m)) {
2481
2482 /*
2483 * If this is a no_cache mapping and the page has never been
2484 * mapped before or was previously a no_cache page, then we
2485 * want to leave pages in the speculative state so that they
2486 * can be readily recycled if free memory runs low. Otherwise
2487 * the page is activated as normal.
2488 */
2489
2490 if (no_cache && (!previously_pmapped || m->no_cache)) {
2491 m->no_cache = TRUE;
2492
2493 if (m->active || m->inactive)
2494 VM_PAGE_QUEUES_REMOVE(m);
2495
2496 if (!m->speculative)
2497 vm_page_speculate(m, TRUE);
2498
2499 } else if (!m->active && !m->inactive)
2500 vm_page_activate(m);
2501
2502 }
2503
2504 vm_page_unlock_queues();
2505 }
2506 }
2507 }
2508 return kr;
2509 }
2510
2511
2512 /*
2513 * Routine: vm_fault
2514 * Purpose:
2515 * Handle page faults, including pseudo-faults
2516 * used to change the wiring status of pages.
2517 * Returns:
2518 * Explicit continuations have been removed.
2519 * Implementation:
2520 * vm_fault and vm_fault_page save mucho state
2521 * in the moral equivalent of a closure. The state
2522 * structure is allocated when first entering vm_fault
2523 * and deallocated when leaving vm_fault.
2524 */
2525
2526 extern int _map_enter_debug;
2527
2528 unsigned long vm_fault_collapse_total = 0;
2529 unsigned long vm_fault_collapse_skipped = 0;
2530
2531 kern_return_t
2532 vm_fault(
2533 vm_map_t map,
2534 vm_map_offset_t vaddr,
2535 vm_prot_t fault_type,
2536 boolean_t change_wiring,
2537 int interruptible,
2538 pmap_t caller_pmap,
2539 vm_map_offset_t caller_pmap_addr)
2540 {
2541 vm_map_version_t version; /* Map version for verificiation */
2542 boolean_t wired; /* Should mapping be wired down? */
2543 vm_object_t object; /* Top-level object */
2544 vm_object_offset_t offset; /* Top-level offset */
2545 vm_prot_t prot; /* Protection for mapping */
2546 vm_object_t old_copy_object; /* Saved copy object */
2547 vm_page_t result_page; /* Result of vm_fault_page */
2548 vm_page_t top_page; /* Placeholder page */
2549 kern_return_t kr;
2550
2551 vm_page_t m; /* Fast access to result_page */
2552 kern_return_t error_code;
2553 vm_object_t cur_object;
2554 vm_object_offset_t cur_offset;
2555 vm_page_t cur_m;
2556 vm_object_t new_object;
2557 int type_of_fault;
2558 pmap_t pmap;
2559 boolean_t interruptible_state;
2560 vm_map_t real_map = map;
2561 vm_map_t original_map = map;
2562 vm_prot_t original_fault_type;
2563 struct vm_object_fault_info fault_info;
2564 boolean_t need_collapse = FALSE;
2565 int object_lock_type = 0;
2566 int cur_object_lock_type;
2567 vm_object_t top_object = VM_OBJECT_NULL;
2568
2569
2570 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_START,
2571 (int)((uint64_t)vaddr >> 32),
2572 (int)vaddr,
2573 0,
2574 0,
2575 0);
2576
2577 if (get_preemption_level() != 0) {
2578 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END,
2579 (int)((uint64_t)vaddr >> 32),
2580 (int)vaddr,
2581 KERN_FAILURE,
2582 0,
2583 0);
2584
2585 return (KERN_FAILURE);
2586 }
2587
2588 interruptible_state = thread_interrupt_level(interruptible);
2589
2590 VM_STAT_INCR(faults);
2591 current_task()->faults++;
2592 original_fault_type = fault_type;
2593
2594 if (fault_type & VM_PROT_WRITE)
2595 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2596 else
2597 object_lock_type = OBJECT_LOCK_SHARED;
2598
2599 cur_object_lock_type = OBJECT_LOCK_SHARED;
2600
2601 RetryFault:
2602 /*
2603 * assume we will hit a page in the cache
2604 * otherwise, explicitly override with
2605 * the real fault type once we determine it
2606 */
2607 type_of_fault = DBG_CACHE_HIT_FAULT;
2608
2609 /*
2610 * Find the backing store object and offset into
2611 * it to begin the search.
2612 */
2613 fault_type = original_fault_type;
2614 map = original_map;
2615 vm_map_lock_read(map);
2616
2617 kr = vm_map_lookup_locked(&map, vaddr, fault_type,
2618 object_lock_type, &version,
2619 &object, &offset, &prot, &wired,
2620 &fault_info,
2621 &real_map);
2622
2623 if (kr != KERN_SUCCESS) {
2624 vm_map_unlock_read(map);
2625 goto done;
2626 }
2627 pmap = real_map->pmap;
2628 fault_info.interruptible = interruptible;
2629 fault_info.stealth = FALSE;
2630
2631 /*
2632 * If the page is wired, we must fault for the current protection
2633 * value, to avoid further faults.
2634 */
2635 if (wired) {
2636 fault_type = prot | VM_PROT_WRITE;
2637 /*
2638 * since we're treating this fault as a 'write'
2639 * we must hold the top object lock exclusively
2640 */
2641 if (object_lock_type == OBJECT_LOCK_SHARED) {
2642
2643 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2644
2645 if (vm_object_lock_upgrade(object) == FALSE) {
2646 /*
2647 * couldn't upgrade, so explictly
2648 * take the lock exclusively
2649 */
2650 vm_object_lock(object);
2651 }
2652 }
2653 }
2654
2655 #if VM_FAULT_CLASSIFY
2656 /*
2657 * Temporary data gathering code
2658 */
2659 vm_fault_classify(object, offset, fault_type);
2660 #endif
2661 /*
2662 * Fast fault code. The basic idea is to do as much as
2663 * possible while holding the map lock and object locks.
2664 * Busy pages are not used until the object lock has to
2665 * be dropped to do something (copy, zero fill, pmap enter).
2666 * Similarly, paging references aren't acquired until that
2667 * point, and object references aren't used.
2668 *
2669 * If we can figure out what to do
2670 * (zero fill, copy on write, pmap enter) while holding
2671 * the locks, then it gets done. Otherwise, we give up,
2672 * and use the original fault path (which doesn't hold
2673 * the map lock, and relies on busy pages).
2674 * The give up cases include:
2675 * - Have to talk to pager.
2676 * - Page is busy, absent or in error.
2677 * - Pager has locked out desired access.
2678 * - Fault needs to be restarted.
2679 * - Have to push page into copy object.
2680 *
2681 * The code is an infinite loop that moves one level down
2682 * the shadow chain each time. cur_object and cur_offset
2683 * refer to the current object being examined. object and offset
2684 * are the original object from the map. The loop is at the
2685 * top level if and only if object and cur_object are the same.
2686 *
2687 * Invariants: Map lock is held throughout. Lock is held on
2688 * original object and cur_object (if different) when
2689 * continuing or exiting loop.
2690 *
2691 */
2692
2693
2694 /*
2695 * If this page is to be inserted in a copy delay object
2696 * for writing, and if the object has a copy, then the
2697 * copy delay strategy is implemented in the slow fault page.
2698 */
2699 if (object->copy_strategy == MEMORY_OBJECT_COPY_DELAY &&
2700 object->copy != VM_OBJECT_NULL && (fault_type & VM_PROT_WRITE))
2701 goto handle_copy_delay;
2702
2703 cur_object = object;
2704 cur_offset = offset;
2705
2706 while (TRUE) {
2707 if (!cur_object->pager_created &&
2708 cur_object->phys_contiguous) /* superpage */
2709 break;
2710
2711 if (cur_object->blocked_access) {
2712 /*
2713 * Access to this VM object has been blocked.
2714 * Let the slow path handle it.
2715 */
2716 break;
2717 }
2718
2719 m = vm_page_lookup(cur_object, cur_offset);
2720
2721 if (m != VM_PAGE_NULL) {
2722 if (m->busy) {
2723 wait_result_t result;
2724
2725 /*
2726 * in order to do the PAGE_ASSERT_WAIT, we must
2727 * have object that 'm' belongs to locked exclusively
2728 */
2729 if (object != cur_object) {
2730 vm_object_unlock(object);
2731
2732 if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
2733
2734 cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2735
2736 if (vm_object_lock_upgrade(cur_object) == FALSE) {
2737 /*
2738 * couldn't upgrade so go do a full retry
2739 * immediately since we've already dropped
2740 * the top object lock associated with this page
2741 * and the current one got dropped due to the
2742 * failed upgrade... the state is no longer valid
2743 */
2744 vm_map_unlock_read(map);
2745 if (real_map != map)
2746 vm_map_unlock(real_map);
2747
2748 goto RetryFault;
2749 }
2750 }
2751 } else if (object_lock_type == OBJECT_LOCK_SHARED) {
2752
2753 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2754
2755 if (vm_object_lock_upgrade(object) == FALSE) {
2756 /*
2757 * couldn't upgrade, so explictly take the lock
2758 * exclusively and go relookup the page since we
2759 * will have dropped the object lock and
2760 * a different thread could have inserted
2761 * a page at this offset
2762 * no need for a full retry since we're
2763 * at the top level of the object chain
2764 */
2765 vm_object_lock(object);
2766
2767 continue;
2768 }
2769 }
2770 vm_map_unlock_read(map);
2771 if (real_map != map)
2772 vm_map_unlock(real_map);
2773
2774 result = PAGE_ASSERT_WAIT(m, interruptible);
2775
2776 vm_object_unlock(cur_object);
2777
2778 if (result == THREAD_WAITING) {
2779 result = thread_block(THREAD_CONTINUE_NULL);
2780
2781 counter(c_vm_fault_page_block_busy_kernel++);
2782 }
2783 if (result == THREAD_AWAKENED || result == THREAD_RESTART)
2784 goto RetryFault;
2785
2786 kr = KERN_ABORTED;
2787 goto done;
2788 }
2789 if (m->phys_page == vm_page_guard_addr) {
2790 /*
2791 * Guard page: let the slow path deal with it
2792 */
2793 break;
2794 }
2795 if (m->unusual && (m->error || m->restart || m->private || m->absent)) {
2796 /*
2797 * Unusual case... let the slow path deal with it
2798 */
2799 break;
2800 }
2801 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m->object)) {
2802 if (object != cur_object)
2803 vm_object_unlock(object);
2804 vm_map_unlock_read(map);
2805 if (real_map != map)
2806 vm_map_unlock(real_map);
2807 vm_object_unlock(cur_object);
2808 kr = KERN_MEMORY_ERROR;
2809 goto done;
2810 }
2811
2812 if (m->encrypted) {
2813 /*
2814 * ENCRYPTED SWAP:
2815 * We've soft-faulted (because it's not in the page
2816 * table) on an encrypted page.
2817 * Keep the page "busy" so that no one messes with
2818 * it during the decryption.
2819 * Release the extra locks we're holding, keep only
2820 * the page's VM object lock.
2821 *
2822 * in order to set 'busy' on 'm', we must
2823 * have object that 'm' belongs to locked exclusively
2824 */
2825 if (object != cur_object) {
2826 vm_object_unlock(object);
2827
2828 if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
2829
2830 cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2831
2832 if (vm_object_lock_upgrade(cur_object) == FALSE) {
2833 /*
2834 * couldn't upgrade so go do a full retry
2835 * immediately since we've already dropped
2836 * the top object lock associated with this page
2837 * and the current one got dropped due to the
2838 * failed upgrade... the state is no longer valid
2839 */
2840 vm_map_unlock_read(map);
2841 if (real_map != map)
2842 vm_map_unlock(real_map);
2843
2844 goto RetryFault;
2845 }
2846 }
2847 } else if (object_lock_type == OBJECT_LOCK_SHARED) {
2848
2849 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2850
2851 if (vm_object_lock_upgrade(object) == FALSE) {
2852 /*
2853 * couldn't upgrade, so explictly take the lock
2854 * exclusively and go relookup the page since we
2855 * will have dropped the object lock and
2856 * a different thread could have inserted
2857 * a page at this offset
2858 * no need for a full retry since we're
2859 * at the top level of the object chain
2860 */
2861 vm_object_lock(object);
2862
2863 continue;
2864 }
2865 }
2866 m->busy = TRUE;
2867
2868 vm_map_unlock_read(map);
2869 if (real_map != map)
2870 vm_map_unlock(real_map);
2871
2872 vm_page_decrypt(m, 0);
2873
2874 assert(m->busy);
2875 PAGE_WAKEUP_DONE(m);
2876
2877 vm_object_unlock(cur_object);
2878 /*
2879 * Retry from the top, in case anything
2880 * changed while we were decrypting...
2881 */
2882 goto RetryFault;
2883 }
2884 ASSERT_PAGE_DECRYPTED(m);
2885
2886 if (VM_FAULT_NEED_CS_VALIDATION(map->pmap, m)) {
2887 /*
2888 * We might need to validate this page
2889 * against its code signature, so we
2890 * want to hold the VM object exclusively.
2891 */
2892 if (object != cur_object) {
2893 if (cur_object_lock_type == OBJECT_LOCK_SHARED) {
2894 vm_object_unlock(object);
2895 vm_object_unlock(cur_object);
2896
2897 cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2898
2899 vm_map_unlock_read(map);
2900 if (real_map != map)
2901 vm_map_unlock(real_map);
2902
2903 goto RetryFault;
2904 }
2905
2906 } else if (object_lock_type == OBJECT_LOCK_SHARED) {
2907
2908 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
2909
2910 if (vm_object_lock_upgrade(object) == FALSE) {
2911 /*
2912 * couldn't upgrade, so explictly take the lock
2913 * exclusively and go relookup the page since we
2914 * will have dropped the object lock and
2915 * a different thread could have inserted
2916 * a page at this offset
2917 * no need for a full retry since we're
2918 * at the top level of the object chain
2919 */
2920 vm_object_lock(object);
2921
2922 continue;
2923 }
2924 }
2925 }
2926 /*
2927 * Two cases of map in faults:
2928 * - At top level w/o copy object.
2929 * - Read fault anywhere.
2930 * --> must disallow write.
2931 */
2932
2933 if (object == cur_object && object->copy == VM_OBJECT_NULL) {
2934 if ((fault_type & VM_PROT_WRITE) == 0) {
2935 /*
2936 * This is not a "write" fault, so we
2937 * might not have taken the object lock
2938 * exclusively and we might not be able
2939 * to update the "wpmapped" bit in
2940 * vm_fault_enter().
2941 * Let's just grant read access to
2942 * the page for now and we'll
2943 * soft-fault again if we need write
2944 * access later...
2945 */
2946 prot &= ~VM_PROT_WRITE;
2947 }
2948 goto FastPmapEnter;
2949 }
2950
2951 if ((fault_type & VM_PROT_WRITE) == 0) {
2952
2953 prot &= ~VM_PROT_WRITE;
2954
2955 if (object != cur_object) {
2956 /*
2957 * We still need to hold the top object
2958 * lock here to prevent a race between
2959 * a read fault (taking only "shared"
2960 * locks) and a write fault (taking
2961 * an "exclusive" lock on the top
2962 * object.
2963 * Otherwise, as soon as we release the
2964 * top lock, the write fault could
2965 * proceed and actually complete before
2966 * the read fault, and the copied page's
2967 * translation could then be overwritten
2968 * by the read fault's translation for
2969 * the original page.
2970 *
2971 * Let's just record what the top object
2972 * is and we'll release it later.
2973 */
2974 top_object = object;
2975
2976 /*
2977 * switch to the object that has the new page
2978 */
2979 object = cur_object;
2980 object_lock_type = cur_object_lock_type;
2981 }
2982 FastPmapEnter:
2983 /*
2984 * prepare for the pmap_enter...
2985 * object and map are both locked
2986 * m contains valid data
2987 * object == m->object
2988 * cur_object == NULL or it's been unlocked
2989 * no paging references on either object or cur_object
2990 */
2991 #if MACH_KDB
2992 if (db_watchpoint_list && (fault_type & VM_PROT_WRITE) == 0)
2993 prot &= ~VM_PROT_WRITE;
2994 #endif
2995 if (caller_pmap) {
2996 kr = vm_fault_enter(m,
2997 caller_pmap,
2998 caller_pmap_addr,
2999 prot,
3000 wired,
3001 change_wiring,
3002 fault_info.no_cache,
3003 &type_of_fault);
3004 } else {
3005 kr = vm_fault_enter(m,
3006 pmap,
3007 vaddr,
3008 prot,
3009 wired,
3010 change_wiring,
3011 fault_info.no_cache,
3012 &type_of_fault);
3013 }
3014
3015 if (top_object != VM_OBJECT_NULL) {
3016 /*
3017 * It's safe to drop the top object
3018 * now that we've done our
3019 * vm_fault_enter(). Any other fault
3020 * in progress for that virtual
3021 * address will either find our page
3022 * and translation or put in a new page
3023 * and translation.
3024 */
3025 vm_object_unlock(top_object);
3026 top_object = VM_OBJECT_NULL;
3027 }
3028
3029 if (need_collapse == TRUE)
3030 vm_object_collapse(object, offset, TRUE);
3031
3032 if (type_of_fault == DBG_PAGEIND_FAULT || type_of_fault == DBG_PAGEINV_FAULT || type_of_fault == DBG_CACHE_HIT_FAULT) {
3033 /*
3034 * evaluate access pattern and update state
3035 * vm_fault_deactivate_behind depends on the
3036 * state being up to date
3037 */
3038 vm_fault_is_sequential(object, cur_offset, fault_info.behavior);
3039
3040 vm_fault_deactivate_behind(object, cur_offset, fault_info.behavior);
3041 }
3042 /*
3043 * That's it, clean up and return.
3044 */
3045 if (m->busy)
3046 PAGE_WAKEUP_DONE(m);
3047
3048 vm_object_unlock(object);
3049
3050 vm_map_unlock_read(map);
3051 if (real_map != map)
3052 vm_map_unlock(real_map);
3053
3054 goto done;
3055 }
3056 /*
3057 * COPY ON WRITE FAULT
3058 */
3059 assert(object_lock_type == OBJECT_LOCK_EXCLUSIVE);
3060
3061 if (vm_page_throttled()) {
3062 /*
3063 * drop all of our locks...
3064 * wait until the free queue is
3065 * pumped back up and then
3066 * redrive the fault
3067 */
3068 if (object != cur_object)
3069 vm_object_unlock(cur_object);
3070 vm_object_unlock(object);
3071 vm_map_unlock_read(map);
3072 if (real_map != map)
3073 vm_map_unlock(real_map);
3074
3075 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
3076 delay(HARD_THROTTLE_DELAY);
3077
3078 if (!current_thread_aborted() && vm_page_wait((change_wiring) ?
3079 THREAD_UNINT :
3080 THREAD_ABORTSAFE))
3081 goto RetryFault;
3082 kr = KERN_ABORTED;
3083 goto done;
3084 }
3085 /*
3086 * If objects match, then
3087 * object->copy must not be NULL (else control
3088 * would be in previous code block), and we
3089 * have a potential push into the copy object
3090 * with which we can't cope with here.
3091 */
3092 if (cur_object == object) {
3093 /*
3094 * must take the slow path to
3095 * deal with the copy push
3096 */
3097 break;
3098 }
3099 /*
3100 * This is now a shadow based copy on write
3101 * fault -- it requires a copy up the shadow
3102 * chain.
3103 *
3104 * Allocate a page in the original top level
3105 * object. Give up if allocate fails. Also
3106 * need to remember current page, as it's the
3107 * source of the copy.
3108 *
3109 * at this point we hold locks on both
3110 * object and cur_object... no need to take
3111 * paging refs or mark pages BUSY since
3112 * we don't drop either object lock until
3113 * the page has been copied and inserted
3114 */
3115 cur_m = m;
3116 m = vm_page_grab();
3117
3118 if (m == VM_PAGE_NULL) {
3119 /*
3120 * no free page currently available...
3121 * must take the slow path
3122 */
3123 break;
3124 }
3125 /*
3126 * Now do the copy. Mark the source page busy...
3127 *
3128 * NOTE: This code holds the map lock across
3129 * the page copy.
3130 */
3131 vm_page_copy(cur_m, m);
3132 vm_page_insert(m, object, offset);
3133 m->dirty = TRUE;
3134
3135 /*
3136 * Now cope with the source page and object
3137 */
3138 if (object->ref_count > 1 && cur_m->pmapped)
3139 pmap_disconnect(cur_m->phys_page);
3140
3141 need_collapse = TRUE;
3142
3143 if (!cur_object->internal &&
3144 cur_object->copy_strategy == MEMORY_OBJECT_COPY_DELAY) {
3145 /*
3146 * The object from which we've just
3147 * copied a page is most probably backed
3148 * by a vnode. We don't want to waste too
3149 * much time trying to collapse the VM objects
3150 * and create a bottleneck when several tasks
3151 * map the same file.
3152 */
3153 if (cur_object->copy == object) {
3154 /*
3155 * Shared mapping or no COW yet.
3156 * We can never collapse a copy
3157 * object into its backing object.
3158 */
3159 need_collapse = FALSE;
3160 } else if (cur_object->copy == object->shadow &&
3161 object->shadow->resident_page_count == 0) {
3162 /*
3163 * Shared mapping after a COW occurred.
3164 */
3165 need_collapse = FALSE;
3166 }
3167 }
3168 vm_object_unlock(cur_object);
3169
3170 if (need_collapse == FALSE)
3171 vm_fault_collapse_skipped++;
3172 vm_fault_collapse_total++;
3173
3174 type_of_fault = DBG_COW_FAULT;
3175 VM_STAT_INCR(cow_faults);
3176 DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL);
3177 current_task()->cow_faults++;
3178
3179 goto FastPmapEnter;
3180
3181 } else {
3182 /*
3183 * No page at cur_object, cur_offset... m == NULL
3184 */
3185 if (cur_object->pager_created) {
3186 if (MUST_ASK_PAGER(cur_object, cur_offset) == TRUE) {
3187 /*
3188 * May have to talk to a pager...
3189 * take the slow path.
3190 */
3191 break;
3192 }
3193 /*
3194 * existence map present and indicates
3195 * that the pager doesn't have this page
3196 */
3197 }
3198 if (cur_object->shadow == VM_OBJECT_NULL) {
3199 /*
3200 * Zero fill fault. Page gets
3201 * inserted into the original object.
3202 */
3203 if (cur_object->shadow_severed ||
3204 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object))
3205 {
3206 if (object != cur_object)
3207 vm_object_unlock(cur_object);
3208 vm_object_unlock(object);
3209
3210 vm_map_unlock_read(map);
3211 if (real_map != map)
3212 vm_map_unlock(real_map);
3213
3214 kr = KERN_MEMORY_ERROR;
3215 goto done;
3216 }
3217 if (vm_page_throttled()) {
3218 /*
3219 * drop all of our locks...
3220 * wait until the free queue is
3221 * pumped back up and then
3222 * redrive the fault
3223 */
3224 if (object != cur_object)
3225 vm_object_unlock(cur_object);
3226 vm_object_unlock(object);
3227 vm_map_unlock_read(map);
3228 if (real_map != map)
3229 vm_map_unlock(real_map);
3230
3231 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
3232 delay(HARD_THROTTLE_DELAY);
3233
3234 if (!current_thread_aborted() && vm_page_wait((change_wiring) ?
3235 THREAD_UNINT :
3236 THREAD_ABORTSAFE))
3237 goto RetryFault;
3238 kr = KERN_ABORTED;
3239 goto done;
3240 }
3241 if (vm_backing_store_low) {
3242 /*
3243 * we are protecting the system from
3244 * backing store exhaustion...
3245 * must take the slow path if we're
3246 * not privileged
3247 */
3248 if (!(current_task()->priv_flags & VM_BACKING_STORE_PRIV))
3249 break;
3250 }
3251 if (cur_object != object) {
3252 vm_object_unlock(cur_object);
3253
3254 cur_object = object;
3255 }
3256 if (object_lock_type == OBJECT_LOCK_SHARED) {
3257
3258 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
3259
3260 if (vm_object_lock_upgrade(object) == FALSE) {
3261 /*
3262 * couldn't upgrade so do a full retry on the fault
3263 * since we dropped the object lock which
3264 * could allow another thread to insert
3265 * a page at this offset
3266 */
3267 vm_map_unlock_read(map);
3268 if (real_map != map)
3269 vm_map_unlock(real_map);
3270
3271 goto RetryFault;
3272 }
3273 }
3274 m = vm_page_alloc(object, offset);
3275
3276 if (m == VM_PAGE_NULL) {
3277 /*
3278 * no free page currently available...
3279 * must take the slow path
3280 */
3281 break;
3282 }
3283
3284 /*
3285 * Now zero fill page...
3286 * the page is probably going to
3287 * be written soon, so don't bother
3288 * to clear the modified bit
3289 *
3290 * NOTE: This code holds the map
3291 * lock across the zero fill.
3292 */
3293 type_of_fault = vm_fault_zero_page(m, map->no_zero_fill);
3294
3295 goto FastPmapEnter;
3296 }
3297 /*
3298 * On to the next level in the shadow chain
3299 */
3300 cur_offset += cur_object->shadow_offset;
3301 new_object = cur_object->shadow;
3302
3303 /*
3304 * take the new_object's lock with the indicated state
3305 */
3306 if (cur_object_lock_type == OBJECT_LOCK_SHARED)
3307 vm_object_lock_shared(new_object);
3308 else
3309 vm_object_lock(new_object);
3310
3311 if (cur_object != object)
3312 vm_object_unlock(cur_object);
3313
3314 cur_object = new_object;
3315
3316 continue;
3317 }
3318 }
3319 /*
3320 * Cleanup from fast fault failure. Drop any object
3321 * lock other than original and drop map lock.
3322 */
3323 if (object != cur_object)
3324 vm_object_unlock(cur_object);
3325
3326 /*
3327 * must own the object lock exclusively at this point
3328 */
3329 if (object_lock_type == OBJECT_LOCK_SHARED) {
3330 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
3331
3332 if (vm_object_lock_upgrade(object) == FALSE) {
3333 /*
3334 * couldn't upgrade, so explictly
3335 * take the lock exclusively
3336 * no need to retry the fault at this
3337 * point since "vm_fault_page" will
3338 * completely re-evaluate the state
3339 */
3340 vm_object_lock(object);
3341 }
3342 }
3343
3344 handle_copy_delay:
3345 vm_map_unlock_read(map);
3346 if (real_map != map)
3347 vm_map_unlock(real_map);
3348
3349 /*
3350 * Make a reference to this object to
3351 * prevent its disposal while we are messing with
3352 * it. Once we have the reference, the map is free
3353 * to be diddled. Since objects reference their
3354 * shadows (and copies), they will stay around as well.
3355 */
3356 vm_object_reference_locked(object);
3357 vm_object_paging_begin(object);
3358
3359 XPR(XPR_VM_FAULT,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
3360
3361 error_code = 0;
3362
3363 kr = vm_fault_page(object, offset, fault_type,
3364 (change_wiring && !wired),
3365 &prot, &result_page, &top_page,
3366 &type_of_fault,
3367 &error_code, map->no_zero_fill,
3368 FALSE, &fault_info);
3369
3370 /*
3371 * if kr != VM_FAULT_SUCCESS, then the paging reference
3372 * has been dropped and the object unlocked... the ref_count
3373 * is still held
3374 *
3375 * if kr == VM_FAULT_SUCCESS, then the paging reference
3376 * is still held along with the ref_count on the original object
3377 *
3378 * the object is returned locked with a paging reference
3379 *
3380 * if top_page != NULL, then it's BUSY and the
3381 * object it belongs to has a paging reference
3382 * but is returned unlocked
3383 */
3384 if (kr != VM_FAULT_SUCCESS &&
3385 kr != VM_FAULT_SUCCESS_NO_VM_PAGE) {
3386 /*
3387 * we didn't succeed, lose the object reference immediately.
3388 */
3389 vm_object_deallocate(object);
3390
3391 /*
3392 * See why we failed, and take corrective action.
3393 */
3394 switch (kr) {
3395 case VM_FAULT_MEMORY_SHORTAGE:
3396 if (vm_page_wait((change_wiring) ?
3397 THREAD_UNINT :
3398 THREAD_ABORTSAFE))
3399 goto RetryFault;
3400 /*
3401 * fall thru
3402 */
3403 case VM_FAULT_INTERRUPTED:
3404 kr = KERN_ABORTED;
3405 goto done;
3406 case VM_FAULT_RETRY:
3407 goto RetryFault;
3408 case VM_FAULT_MEMORY_ERROR:
3409 if (error_code)
3410 kr = error_code;
3411 else
3412 kr = KERN_MEMORY_ERROR;
3413 goto done;
3414 default:
3415 panic("vm_fault: unexpected error 0x%x from "
3416 "vm_fault_page()\n", kr);
3417 }
3418 }
3419 m = result_page;
3420
3421 if (m != VM_PAGE_NULL) {
3422 assert((change_wiring && !wired) ?
3423 (top_page == VM_PAGE_NULL) :
3424 ((top_page == VM_PAGE_NULL) == (m->object == object)));
3425 }
3426
3427 /*
3428 * What to do with the resulting page from vm_fault_page
3429 * if it doesn't get entered into the physical map:
3430 */
3431 #define RELEASE_PAGE(m) \
3432 MACRO_BEGIN \
3433 PAGE_WAKEUP_DONE(m); \
3434 if (!m->active && !m->inactive && !m->throttled) { \
3435 vm_page_lockspin_queues(); \
3436 if (!m->active && !m->inactive && !m->throttled) \
3437 vm_page_activate(m); \
3438 vm_page_unlock_queues(); \
3439 } \
3440 MACRO_END
3441
3442 /*
3443 * We must verify that the maps have not changed
3444 * since our last lookup.
3445 */
3446 if (m != VM_PAGE_NULL) {
3447 old_copy_object = m->object->copy;
3448 vm_object_unlock(m->object);
3449 } else {
3450 old_copy_object = VM_OBJECT_NULL;
3451 vm_object_unlock(object);
3452 }
3453
3454 /*
3455 * no object locks are held at this point
3456 */
3457 if ((map != original_map) || !vm_map_verify(map, &version)) {
3458 vm_object_t retry_object;
3459 vm_object_offset_t retry_offset;
3460 vm_prot_t retry_prot;
3461
3462 /*
3463 * To avoid trying to write_lock the map while another
3464 * thread has it read_locked (in vm_map_pageable), we
3465 * do not try for write permission. If the page is
3466 * still writable, we will get write permission. If it
3467 * is not, or has been marked needs_copy, we enter the
3468 * mapping without write permission, and will merely
3469 * take another fault.
3470 */
3471 map = original_map;
3472 vm_map_lock_read(map);
3473
3474 kr = vm_map_lookup_locked(&map, vaddr,
3475 fault_type & ~VM_PROT_WRITE,
3476 OBJECT_LOCK_EXCLUSIVE, &version,
3477 &retry_object, &retry_offset, &retry_prot,
3478 &wired,
3479 &fault_info,
3480 &real_map);
3481 pmap = real_map->pmap;
3482
3483 if (kr != KERN_SUCCESS) {
3484 vm_map_unlock_read(map);
3485
3486 if (m != VM_PAGE_NULL) {
3487 /*
3488 * retake the lock so that
3489 * we can drop the paging reference
3490 * in vm_fault_cleanup and do the
3491 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3492 */
3493 vm_object_lock(m->object);
3494
3495 RELEASE_PAGE(m);
3496
3497 vm_fault_cleanup(m->object, top_page);
3498 } else {
3499 /*
3500 * retake the lock so that
3501 * we can drop the paging reference
3502 * in vm_fault_cleanup
3503 */
3504 vm_object_lock(object);
3505
3506 vm_fault_cleanup(object, top_page);
3507 }
3508 vm_object_deallocate(object);
3509
3510 goto done;
3511 }
3512 vm_object_unlock(retry_object);
3513
3514 if ((retry_object != object) || (retry_offset != offset)) {
3515
3516 vm_map_unlock_read(map);
3517 if (real_map != map)
3518 vm_map_unlock(real_map);
3519
3520 if (m != VM_PAGE_NULL) {
3521 /*
3522 * retake the lock so that
3523 * we can drop the paging reference
3524 * in vm_fault_cleanup and do the
3525 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3526 */
3527 vm_object_lock(m->object);
3528
3529 RELEASE_PAGE(m);
3530
3531 vm_fault_cleanup(m->object, top_page);
3532 } else {
3533 /*
3534 * retake the lock so that
3535 * we can drop the paging reference
3536 * in vm_fault_cleanup
3537 */
3538 vm_object_lock(object);
3539
3540 vm_fault_cleanup(object, top_page);
3541 }
3542 vm_object_deallocate(object);
3543
3544 goto RetryFault;
3545 }
3546 /*
3547 * Check whether the protection has changed or the object
3548 * has been copied while we left the map unlocked.
3549 */
3550 prot &= retry_prot;
3551 }
3552 if (m != VM_PAGE_NULL) {
3553 vm_object_lock(m->object);
3554
3555 if (m->object->copy != old_copy_object) {
3556 /*
3557 * The copy object changed while the top-level object
3558 * was unlocked, so take away write permission.
3559 */
3560 prot &= ~VM_PROT_WRITE;
3561 }
3562 } else
3563 vm_object_lock(object);
3564
3565 /*
3566 * If we want to wire down this page, but no longer have
3567 * adequate permissions, we must start all over.
3568 */
3569 if (wired && (fault_type != (prot | VM_PROT_WRITE))) {
3570
3571 vm_map_verify_done(map, &version);
3572 if (real_map != map)
3573 vm_map_unlock(real_map);
3574
3575 if (m != VM_PAGE_NULL) {
3576 RELEASE_PAGE(m);
3577
3578 vm_fault_cleanup(m->object, top_page);
3579 } else
3580 vm_fault_cleanup(object, top_page);
3581
3582 vm_object_deallocate(object);
3583
3584 goto RetryFault;
3585 }
3586 if (m != VM_PAGE_NULL) {
3587 /*
3588 * Put this page into the physical map.
3589 * We had to do the unlock above because pmap_enter
3590 * may cause other faults. The page may be on
3591 * the pageout queues. If the pageout daemon comes
3592 * across the page, it will remove it from the queues.
3593 */
3594 if (caller_pmap) {
3595 kr = vm_fault_enter(m,
3596 caller_pmap,
3597 caller_pmap_addr,
3598 prot,
3599 wired,
3600 change_wiring,
3601 fault_info.no_cache,
3602 &type_of_fault);
3603 } else {
3604 kr = vm_fault_enter(m,
3605 pmap,
3606 vaddr,
3607 prot,
3608 wired,
3609 change_wiring,
3610 fault_info.no_cache,
3611 &type_of_fault);
3612 }
3613 if (kr != KERN_SUCCESS) {
3614 /* abort this page fault */
3615 vm_map_verify_done(map, &version);
3616 if (real_map != map)
3617 vm_map_unlock(real_map);
3618 PAGE_WAKEUP_DONE(m);
3619 vm_fault_cleanup(m->object, top_page);
3620 vm_object_deallocate(object);
3621 goto done;
3622 }
3623 } else {
3624
3625 vm_map_entry_t entry;
3626 vm_map_offset_t laddr;
3627 vm_map_offset_t ldelta, hdelta;
3628
3629 /*
3630 * do a pmap block mapping from the physical address
3631 * in the object
3632 */
3633
3634 #ifdef ppc
3635 /* While we do not worry about execution protection in */
3636 /* general, certian pages may have instruction execution */
3637 /* disallowed. We will check here, and if not allowed */
3638 /* to execute, we return with a protection failure. */
3639
3640 if ((fault_type & VM_PROT_EXECUTE) &&
3641 (!pmap_eligible_for_execute((ppnum_t)(object->shadow_offset >> 12)))) {
3642
3643 vm_map_verify_done(map, &version);
3644
3645 if (real_map != map)
3646 vm_map_unlock(real_map);
3647
3648 vm_fault_cleanup(object, top_page);
3649 vm_object_deallocate(object);
3650
3651 kr = KERN_PROTECTION_FAILURE;
3652 goto done;
3653 }
3654 #endif /* ppc */
3655
3656 if (real_map != map)
3657 vm_map_unlock(real_map);
3658
3659 if (original_map != map) {
3660 vm_map_unlock_read(map);
3661 vm_map_lock_read(original_map);
3662 map = original_map;
3663 }
3664 real_map = map;
3665
3666 laddr = vaddr;
3667 hdelta = 0xFFFFF000;
3668 ldelta = 0xFFFFF000;
3669
3670 while (vm_map_lookup_entry(map, laddr, &entry)) {
3671 if (ldelta > (laddr - entry->vme_start))
3672 ldelta = laddr - entry->vme_start;
3673 if (hdelta > (entry->vme_end - laddr))
3674 hdelta = entry->vme_end - laddr;
3675 if (entry->is_sub_map) {
3676
3677 laddr = (laddr - entry->vme_start)
3678 + entry->offset;
3679 vm_map_lock_read(entry->object.sub_map);
3680
3681 if (map != real_map)
3682 vm_map_unlock_read(map);
3683 if (entry->use_pmap) {
3684 vm_map_unlock_read(real_map);
3685 real_map = entry->object.sub_map;
3686 }
3687 map = entry->object.sub_map;
3688
3689 } else {
3690 break;
3691 }
3692 }
3693
3694 if (vm_map_lookup_entry(map, laddr, &entry) &&
3695 (entry->object.vm_object != NULL) &&
3696 (entry->object.vm_object == object)) {
3697
3698 int superpage = (!object->pager_created && object->phys_contiguous)? VM_MEM_SUPERPAGE : 0;
3699 if (caller_pmap) {
3700 /*
3701 * Set up a block mapped area
3702 */
3703 assert((uint32_t)((ldelta + hdelta) >> 12) == ((ldelta + hdelta) >> 12));
3704 pmap_map_block(caller_pmap,
3705 (addr64_t)(caller_pmap_addr - ldelta),
3706 (ppnum_t)((((vm_map_offset_t) (entry->object.vm_object->shadow_offset)) +
3707 entry->offset + (laddr - entry->vme_start) - ldelta) >> 12),
3708 (uint32_t)((ldelta + hdelta) >> 12), prot,
3709 (VM_WIMG_MASK & (int)object->wimg_bits) | superpage, 0);
3710 } else {
3711 /*
3712 * Set up a block mapped area
3713 */
3714 assert((uint32_t)((ldelta + hdelta) >> 12) == ((ldelta + hdelta) >> 12));
3715 pmap_map_block(real_map->pmap,
3716 (addr64_t)(vaddr - ldelta),
3717 (ppnum_t)((((vm_map_offset_t)(entry->object.vm_object->shadow_offset)) +
3718 entry->offset + (laddr - entry->vme_start) - ldelta) >> 12),
3719 (uint32_t)((ldelta + hdelta) >> 12), prot,
3720 (VM_WIMG_MASK & (int)object->wimg_bits) | superpage, 0);
3721 }
3722 }
3723 }
3724
3725 /*
3726 * Unlock everything, and return
3727 */
3728 vm_map_verify_done(map, &version);
3729 if (real_map != map)
3730 vm_map_unlock(real_map);
3731
3732 if (m != VM_PAGE_NULL) {
3733 PAGE_WAKEUP_DONE(m);
3734
3735 vm_fault_cleanup(m->object, top_page);
3736 } else
3737 vm_fault_cleanup(object, top_page);
3738
3739 vm_object_deallocate(object);
3740
3741 #undef RELEASE_PAGE
3742
3743 kr = KERN_SUCCESS;
3744 done:
3745 thread_interrupt_level(interruptible_state);
3746
3747 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END,
3748 (int)((uint64_t)vaddr >> 32),
3749 (int)vaddr,
3750 kr,
3751 type_of_fault,
3752 0);
3753
3754 return (kr);
3755 }
3756
3757 /*
3758 * vm_fault_wire:
3759 *
3760 * Wire down a range of virtual addresses in a map.
3761 */
3762 kern_return_t
3763 vm_fault_wire(
3764 vm_map_t map,
3765 vm_map_entry_t entry,
3766 pmap_t pmap,
3767 vm_map_offset_t pmap_addr)
3768 {
3769
3770 register vm_map_offset_t va;
3771 register vm_map_offset_t end_addr = entry->vme_end;
3772 register kern_return_t rc;
3773
3774 assert(entry->in_transition);
3775
3776 if ((entry->object.vm_object != NULL) &&
3777 !entry->is_sub_map &&
3778 entry->object.vm_object->phys_contiguous) {
3779 return KERN_SUCCESS;
3780 }
3781
3782 /*
3783 * Inform the physical mapping system that the
3784 * range of addresses may not fault, so that
3785 * page tables and such can be locked down as well.
3786 */
3787
3788 pmap_pageable(pmap, pmap_addr,
3789 pmap_addr + (end_addr - entry->vme_start), FALSE);
3790
3791 /*
3792 * We simulate a fault to get the page and enter it
3793 * in the physical map.
3794 */
3795
3796 for (va = entry->vme_start; va < end_addr; va += PAGE_SIZE) {
3797 if ((rc = vm_fault_wire_fast(
3798 map, va, entry, pmap,
3799 pmap_addr + (va - entry->vme_start)
3800 )) != KERN_SUCCESS) {
3801 rc = vm_fault(map, va, VM_PROT_NONE, TRUE,
3802 (pmap == kernel_pmap) ?
3803 THREAD_UNINT : THREAD_ABORTSAFE,
3804 pmap, pmap_addr + (va - entry->vme_start));
3805 DTRACE_VM2(softlock, int, 1, (uint64_t *), NULL);
3806 }
3807
3808 if (rc != KERN_SUCCESS) {
3809 struct vm_map_entry tmp_entry = *entry;
3810
3811 /* unwire wired pages */
3812 tmp_entry.vme_end = va;
3813 vm_fault_unwire(map,
3814 &tmp_entry, FALSE, pmap, pmap_addr);
3815
3816 return rc;
3817 }
3818 }
3819 return KERN_SUCCESS;
3820 }
3821
3822 /*
3823 * vm_fault_unwire:
3824 *
3825 * Unwire a range of virtual addresses in a map.
3826 */
3827 void
3828 vm_fault_unwire(
3829 vm_map_t map,
3830 vm_map_entry_t entry,
3831 boolean_t deallocate,
3832 pmap_t pmap,
3833 vm_map_offset_t pmap_addr)
3834 {
3835 register vm_map_offset_t va;
3836 register vm_map_offset_t end_addr = entry->vme_end;
3837 vm_object_t object;
3838 struct vm_object_fault_info fault_info;
3839
3840 object = (entry->is_sub_map)
3841 ? VM_OBJECT_NULL : entry->object.vm_object;
3842
3843 /*
3844 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
3845 * do anything since such memory is wired by default. So we don't have
3846 * anything to undo here.
3847 */
3848
3849 if (object != VM_OBJECT_NULL && object->phys_contiguous)
3850 return;
3851
3852 fault_info.interruptible = THREAD_UNINT;
3853 fault_info.behavior = entry->behavior;
3854 fault_info.user_tag = entry->alias;
3855 fault_info.lo_offset = entry->offset;
3856 fault_info.hi_offset = (entry->vme_end - entry->vme_start) + entry->offset;
3857 fault_info.no_cache = entry->no_cache;
3858 fault_info.stealth = TRUE;
3859
3860 /*
3861 * Since the pages are wired down, we must be able to
3862 * get their mappings from the physical map system.
3863 */
3864
3865 for (va = entry->vme_start; va < end_addr; va += PAGE_SIZE) {
3866
3867 if (object == VM_OBJECT_NULL) {
3868 if (pmap) {
3869 pmap_change_wiring(pmap,
3870 pmap_addr + (va - entry->vme_start), FALSE);
3871 }
3872 (void) vm_fault(map, va, VM_PROT_NONE,
3873 TRUE, THREAD_UNINT, pmap, pmap_addr);
3874 } else {
3875 vm_prot_t prot;
3876 vm_page_t result_page;
3877 vm_page_t top_page;
3878 vm_object_t result_object;
3879 vm_fault_return_t result;
3880
3881 if (end_addr - va > (vm_size_t) -1) {
3882 /* 32-bit overflow */
3883 fault_info.cluster_size = (vm_size_t) (0 - PAGE_SIZE);
3884 } else {
3885 fault_info.cluster_size = (vm_size_t) (end_addr - va);
3886 assert(fault_info.cluster_size == end_addr - va);
3887 }
3888
3889 do {
3890 prot = VM_PROT_NONE;
3891
3892 vm_object_lock(object);
3893 vm_object_paging_begin(object);
3894 XPR(XPR_VM_FAULT,
3895 "vm_fault_unwire -> vm_fault_page\n",
3896 0,0,0,0,0);
3897 result = vm_fault_page(
3898 object,
3899 entry->offset + (va - entry->vme_start),
3900 VM_PROT_NONE, TRUE,
3901 &prot, &result_page, &top_page,
3902 (int *)0,
3903 NULL, map->no_zero_fill,
3904 FALSE, &fault_info);
3905 } while (result == VM_FAULT_RETRY);
3906
3907 /*
3908 * If this was a mapping to a file on a device that has been forcibly
3909 * unmounted, then we won't get a page back from vm_fault_page(). Just
3910 * move on to the next one in case the remaining pages are mapped from
3911 * different objects. During a forced unmount, the object is terminated
3912 * so the alive flag will be false if this happens. A forced unmount will
3913 * will occur when an external disk is unplugged before the user does an
3914 * eject, so we don't want to panic in that situation.
3915 */
3916
3917 if (result == VM_FAULT_MEMORY_ERROR && !object->alive)
3918 continue;
3919
3920 if (result != VM_FAULT_SUCCESS)
3921 panic("vm_fault_unwire: failure");
3922
3923 result_object = result_page->object;
3924
3925 if ((pmap) && (result_page->phys_page != vm_page_guard_addr)) {
3926 pmap_change_wiring(pmap,
3927 pmap_addr + (va - entry->vme_start), FALSE);
3928 }
3929 if (deallocate) {
3930 assert(result_page->phys_page !=
3931 vm_page_fictitious_addr);
3932 pmap_disconnect(result_page->phys_page);
3933 VM_PAGE_FREE(result_page);
3934 } else {
3935 if (VM_PAGE_WIRED(result_page)) {
3936 vm_page_lockspin_queues();
3937 vm_page_unwire(result_page);
3938 vm_page_unlock_queues();
3939 }
3940 if(entry->zero_wired_pages) {
3941 pmap_zero_page(result_page->phys_page);
3942 entry->zero_wired_pages = FALSE;
3943 }
3944
3945 PAGE_WAKEUP_DONE(result_page);
3946 }
3947 vm_fault_cleanup(result_object, top_page);
3948 }
3949 }
3950
3951 /*
3952 * Inform the physical mapping system that the range
3953 * of addresses may fault, so that page tables and
3954 * such may be unwired themselves.
3955 */
3956
3957 pmap_pageable(pmap, pmap_addr,
3958 pmap_addr + (end_addr - entry->vme_start), TRUE);
3959
3960 }
3961
3962 /*
3963 * vm_fault_wire_fast:
3964 *
3965 * Handle common case of a wire down page fault at the given address.
3966 * If successful, the page is inserted into the associated physical map.
3967 * The map entry is passed in to avoid the overhead of a map lookup.
3968 *
3969 * NOTE: the given address should be truncated to the
3970 * proper page address.
3971 *
3972 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
3973 * a standard error specifying why the fault is fatal is returned.
3974 *
3975 * The map in question must be referenced, and remains so.
3976 * Caller has a read lock on the map.
3977 *
3978 * This is a stripped version of vm_fault() for wiring pages. Anything
3979 * other than the common case will return KERN_FAILURE, and the caller
3980 * is expected to call vm_fault().
3981 */
3982 kern_return_t
3983 vm_fault_wire_fast(
3984 __unused vm_map_t map,
3985 vm_map_offset_t va,
3986 vm_map_entry_t entry,
3987 pmap_t pmap,
3988 vm_map_offset_t pmap_addr)
3989 {
3990 vm_object_t object;
3991 vm_object_offset_t offset;
3992 register vm_page_t m;
3993 vm_prot_t prot;
3994 thread_t thread = current_thread();
3995 int type_of_fault;
3996 kern_return_t kr;
3997
3998 VM_STAT_INCR(faults);
3999
4000 if (thread != THREAD_NULL && thread->task != TASK_NULL)
4001 thread->task->faults++;
4002
4003 /*
4004 * Recovery actions
4005 */
4006
4007 #undef RELEASE_PAGE
4008 #define RELEASE_PAGE(m) { \
4009 PAGE_WAKEUP_DONE(m); \
4010 vm_page_lockspin_queues(); \
4011 vm_page_unwire(m); \
4012 vm_page_unlock_queues(); \
4013 }
4014
4015
4016 #undef UNLOCK_THINGS
4017 #define UNLOCK_THINGS { \
4018 vm_object_paging_end(object); \
4019 vm_object_unlock(object); \
4020 }
4021
4022 #undef UNLOCK_AND_DEALLOCATE
4023 #define UNLOCK_AND_DEALLOCATE { \
4024 UNLOCK_THINGS; \
4025 vm_object_deallocate(object); \
4026 }
4027 /*
4028 * Give up and have caller do things the hard way.
4029 */
4030
4031 #define GIVE_UP { \
4032 UNLOCK_AND_DEALLOCATE; \
4033 return(KERN_FAILURE); \
4034 }
4035
4036
4037 /*
4038 * If this entry is not directly to a vm_object, bail out.
4039 */
4040 if (entry->is_sub_map)
4041 return(KERN_FAILURE);
4042
4043 /*
4044 * Find the backing store object and offset into it.
4045 */
4046
4047 object = entry->object.vm_object;
4048 offset = (va - entry->vme_start) + entry->offset;
4049 prot = entry->protection;
4050
4051 /*
4052 * Make a reference to this object to prevent its
4053 * disposal while we are messing with it.
4054 */
4055
4056 vm_object_lock(object);
4057 vm_object_reference_locked(object);
4058 vm_object_paging_begin(object);
4059
4060 /*
4061 * INVARIANTS (through entire routine):
4062 *
4063 * 1) At all times, we must either have the object
4064 * lock or a busy page in some object to prevent
4065 * some other thread from trying to bring in
4066 * the same page.
4067 *
4068 * 2) Once we have a busy page, we must remove it from
4069 * the pageout queues, so that the pageout daemon
4070 * will not grab it away.
4071 *
4072 */
4073
4074 /*
4075 * Look for page in top-level object. If it's not there or
4076 * there's something going on, give up.
4077 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
4078 * decrypt the page before wiring it down.
4079 */
4080 m = vm_page_lookup(object, offset);
4081 if ((m == VM_PAGE_NULL) || (m->busy) || (m->encrypted) ||
4082 (m->unusual && ( m->error || m->restart || m->absent))) {
4083
4084 GIVE_UP;
4085 }
4086 ASSERT_PAGE_DECRYPTED(m);
4087
4088 if (m->fictitious &&
4089 m->phys_page == vm_page_guard_addr) {
4090 /*
4091 * Guard pages are fictitious pages and are never
4092 * entered into a pmap, so let's say it's been wired...
4093 */
4094 kr = KERN_SUCCESS;
4095 goto done;
4096 }
4097
4098 /*
4099 * Wire the page down now. All bail outs beyond this
4100 * point must unwire the page.
4101 */
4102
4103 vm_page_lockspin_queues();
4104 vm_page_wire(m);
4105 vm_page_unlock_queues();
4106
4107 /*
4108 * Mark page busy for other threads.
4109 */
4110 assert(!m->busy);
4111 m->busy = TRUE;
4112 assert(!m->absent);
4113
4114 /*
4115 * Give up if the page is being written and there's a copy object
4116 */
4117 if ((object->copy != VM_OBJECT_NULL) && (prot & VM_PROT_WRITE)) {
4118 RELEASE_PAGE(m);
4119 GIVE_UP;
4120 }
4121
4122 /*
4123 * Put this page into the physical map.
4124 */
4125 type_of_fault = DBG_CACHE_HIT_FAULT;
4126 kr = vm_fault_enter(m,
4127 pmap,
4128 pmap_addr,
4129 prot,
4130 TRUE,
4131 FALSE,
4132 FALSE,
4133 &type_of_fault);
4134
4135 done:
4136 /*
4137 * Unlock everything, and return
4138 */
4139
4140 PAGE_WAKEUP_DONE(m);
4141 UNLOCK_AND_DEALLOCATE;
4142
4143 return kr;
4144
4145 }
4146
4147 /*
4148 * Routine: vm_fault_copy_cleanup
4149 * Purpose:
4150 * Release a page used by vm_fault_copy.
4151 */
4152
4153 void
4154 vm_fault_copy_cleanup(
4155 vm_page_t page,
4156 vm_page_t top_page)
4157 {
4158 vm_object_t object = page->object;
4159
4160 vm_object_lock(object);
4161 PAGE_WAKEUP_DONE(page);
4162 if (!page->active && !page->inactive && !page->throttled) {
4163 vm_page_lockspin_queues();
4164 if (!page->active && !page->inactive && !page->throttled)
4165 vm_page_activate(page);
4166 vm_page_unlock_queues();
4167 }
4168 vm_fault_cleanup(object, top_page);
4169 }
4170
4171 void
4172 vm_fault_copy_dst_cleanup(
4173 vm_page_t page)
4174 {
4175 vm_object_t object;
4176
4177 if (page != VM_PAGE_NULL) {
4178 object = page->object;
4179 vm_object_lock(object);
4180 vm_page_lockspin_queues();
4181 vm_page_unwire(page);
4182 vm_page_unlock_queues();
4183 vm_object_paging_end(object);
4184 vm_object_unlock(object);
4185 }
4186 }
4187
4188 /*
4189 * Routine: vm_fault_copy
4190 *
4191 * Purpose:
4192 * Copy pages from one virtual memory object to another --
4193 * neither the source nor destination pages need be resident.
4194 *
4195 * Before actually copying a page, the version associated with
4196 * the destination address map wil be verified.
4197 *
4198 * In/out conditions:
4199 * The caller must hold a reference, but not a lock, to
4200 * each of the source and destination objects and to the
4201 * destination map.
4202 *
4203 * Results:
4204 * Returns KERN_SUCCESS if no errors were encountered in
4205 * reading or writing the data. Returns KERN_INTERRUPTED if
4206 * the operation was interrupted (only possible if the
4207 * "interruptible" argument is asserted). Other return values
4208 * indicate a permanent error in copying the data.
4209 *
4210 * The actual amount of data copied will be returned in the
4211 * "copy_size" argument. In the event that the destination map
4212 * verification failed, this amount may be less than the amount
4213 * requested.
4214 */
4215 kern_return_t
4216 vm_fault_copy(
4217 vm_object_t src_object,
4218 vm_object_offset_t src_offset,
4219 vm_map_size_t *copy_size, /* INOUT */
4220 vm_object_t dst_object,
4221 vm_object_offset_t dst_offset,
4222 vm_map_t dst_map,
4223 vm_map_version_t *dst_version,
4224 int interruptible)
4225 {
4226 vm_page_t result_page;
4227
4228 vm_page_t src_page;
4229 vm_page_t src_top_page;
4230 vm_prot_t src_prot;
4231
4232 vm_page_t dst_page;
4233 vm_page_t dst_top_page;
4234 vm_prot_t dst_prot;
4235
4236 vm_map_size_t amount_left;
4237 vm_object_t old_copy_object;
4238 kern_return_t error = 0;
4239 vm_fault_return_t result;
4240
4241 vm_map_size_t part_size;
4242 struct vm_object_fault_info fault_info_src;
4243 struct vm_object_fault_info fault_info_dst;
4244
4245 /*
4246 * In order not to confuse the clustered pageins, align
4247 * the different offsets on a page boundary.
4248 */
4249
4250 #define RETURN(x) \
4251 MACRO_BEGIN \
4252 *copy_size -= amount_left; \
4253 MACRO_RETURN(x); \
4254 MACRO_END
4255
4256 amount_left = *copy_size;
4257
4258 fault_info_src.interruptible = interruptible;
4259 fault_info_src.behavior = VM_BEHAVIOR_SEQUENTIAL;
4260 fault_info_src.user_tag = 0;
4261 fault_info_src.lo_offset = vm_object_trunc_page(src_offset);
4262 fault_info_src.hi_offset = fault_info_src.lo_offset + amount_left;
4263 fault_info_src.no_cache = FALSE;
4264 fault_info_src.stealth = TRUE;
4265
4266 fault_info_dst.interruptible = interruptible;
4267 fault_info_dst.behavior = VM_BEHAVIOR_SEQUENTIAL;
4268 fault_info_dst.user_tag = 0;
4269 fault_info_dst.lo_offset = vm_object_trunc_page(dst_offset);
4270 fault_info_dst.hi_offset = fault_info_dst.lo_offset + amount_left;
4271 fault_info_dst.no_cache = FALSE;
4272 fault_info_dst.stealth = TRUE;
4273
4274 do { /* while (amount_left > 0) */
4275 /*
4276 * There may be a deadlock if both source and destination
4277 * pages are the same. To avoid this deadlock, the copy must
4278 * start by getting the destination page in order to apply
4279 * COW semantics if any.
4280 */
4281
4282 RetryDestinationFault: ;
4283
4284 dst_prot = VM_PROT_WRITE|VM_PROT_READ;
4285
4286 vm_object_lock(dst_object);
4287 vm_object_paging_begin(dst_object);
4288
4289 if (amount_left > (vm_size_t) -1) {
4290 /* 32-bit overflow */
4291 fault_info_dst.cluster_size = (vm_size_t) (0 - PAGE_SIZE);
4292 } else {
4293 fault_info_dst.cluster_size = (vm_size_t) amount_left;
4294 assert(fault_info_dst.cluster_size == amount_left);
4295 }
4296
4297 XPR(XPR_VM_FAULT,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
4298 result = vm_fault_page(dst_object,
4299 vm_object_trunc_page(dst_offset),
4300 VM_PROT_WRITE|VM_PROT_READ,
4301 FALSE,
4302 &dst_prot, &dst_page, &dst_top_page,
4303 (int *)0,
4304 &error,
4305 dst_map->no_zero_fill,
4306 FALSE, &fault_info_dst);
4307 switch (result) {
4308 case VM_FAULT_SUCCESS:
4309 break;
4310 case VM_FAULT_RETRY:
4311 goto RetryDestinationFault;
4312 case VM_FAULT_MEMORY_SHORTAGE:
4313 if (vm_page_wait(interruptible))
4314 goto RetryDestinationFault;
4315 /* fall thru */
4316 case VM_FAULT_INTERRUPTED:
4317 RETURN(MACH_SEND_INTERRUPTED);
4318 case VM_FAULT_SUCCESS_NO_VM_PAGE:
4319 /* success but no VM page: fail the copy */
4320 vm_object_paging_end(dst_object);
4321 vm_object_unlock(dst_object);
4322 /*FALLTHROUGH*/
4323 case VM_FAULT_MEMORY_ERROR:
4324 if (error)
4325 return (error);
4326 else
4327 return(KERN_MEMORY_ERROR);
4328 default:
4329 panic("vm_fault_copy: unexpected error 0x%x from "
4330 "vm_fault_page()\n", result);
4331 }
4332 assert ((dst_prot & VM_PROT_WRITE) != VM_PROT_NONE);
4333
4334 old_copy_object = dst_page->object->copy;
4335
4336 /*
4337 * There exists the possiblity that the source and
4338 * destination page are the same. But we can't
4339 * easily determine that now. If they are the
4340 * same, the call to vm_fault_page() for the
4341 * destination page will deadlock. To prevent this we
4342 * wire the page so we can drop busy without having
4343 * the page daemon steal the page. We clean up the
4344 * top page but keep the paging reference on the object
4345 * holding the dest page so it doesn't go away.
4346 */
4347
4348 vm_page_lockspin_queues();
4349 vm_page_wire(dst_page);
4350 vm_page_unlock_queues();
4351 PAGE_WAKEUP_DONE(dst_page);
4352 vm_object_unlock(dst_page->object);
4353
4354 if (dst_top_page != VM_PAGE_NULL) {
4355 vm_object_lock(dst_object);
4356 VM_PAGE_FREE(dst_top_page);
4357 vm_object_paging_end(dst_object);
4358 vm_object_unlock(dst_object);
4359 }
4360
4361 RetrySourceFault: ;
4362
4363 if (src_object == VM_OBJECT_NULL) {
4364 /*
4365 * No source object. We will just
4366 * zero-fill the page in dst_object.
4367 */
4368 src_page = VM_PAGE_NULL;
4369 result_page = VM_PAGE_NULL;
4370 } else {
4371 vm_object_lock(src_object);
4372 src_page = vm_page_lookup(src_object,
4373 vm_object_trunc_page(src_offset));
4374 if (src_page == dst_page) {
4375 src_prot = dst_prot;
4376 result_page = VM_PAGE_NULL;
4377 } else {
4378 src_prot = VM_PROT_READ;
4379 vm_object_paging_begin(src_object);
4380
4381 if (amount_left > (vm_size_t) -1) {
4382 /* 32-bit overflow */
4383 fault_info_src.cluster_size = (vm_size_t) (0 - PAGE_SIZE);
4384 } else {
4385 fault_info_src.cluster_size = (vm_size_t) amount_left;
4386 assert(fault_info_src.cluster_size == amount_left);
4387 }
4388
4389 XPR(XPR_VM_FAULT,
4390 "vm_fault_copy(2) -> vm_fault_page\n",
4391 0,0,0,0,0);
4392 result = vm_fault_page(
4393 src_object,
4394 vm_object_trunc_page(src_offset),
4395 VM_PROT_READ, FALSE,
4396 &src_prot,
4397 &result_page, &src_top_page,
4398 (int *)0, &error, FALSE,
4399 FALSE, &fault_info_src);
4400
4401 switch (result) {
4402 case VM_FAULT_SUCCESS:
4403 break;
4404 case VM_FAULT_RETRY:
4405 goto RetrySourceFault;
4406 case VM_FAULT_MEMORY_SHORTAGE:
4407 if (vm_page_wait(interruptible))
4408 goto RetrySourceFault;
4409 /* fall thru */
4410 case VM_FAULT_INTERRUPTED:
4411 vm_fault_copy_dst_cleanup(dst_page);
4412 RETURN(MACH_SEND_INTERRUPTED);
4413 case VM_FAULT_SUCCESS_NO_VM_PAGE:
4414 /* success but no VM page: fail */
4415 vm_object_paging_end(src_object);
4416 vm_object_unlock(src_object);
4417 /*FALLTHROUGH*/
4418 case VM_FAULT_MEMORY_ERROR:
4419 vm_fault_copy_dst_cleanup(dst_page);
4420 if (error)
4421 return (error);
4422 else
4423 return(KERN_MEMORY_ERROR);
4424 default:
4425 panic("vm_fault_copy(2): unexpected "
4426 "error 0x%x from "
4427 "vm_fault_page()\n", result);
4428 }
4429
4430
4431 assert((src_top_page == VM_PAGE_NULL) ==
4432 (result_page->object == src_object));
4433 }
4434 assert ((src_prot & VM_PROT_READ) != VM_PROT_NONE);
4435 vm_object_unlock(result_page->object);
4436 }
4437
4438 if (!vm_map_verify(dst_map, dst_version)) {
4439 if (result_page != VM_PAGE_NULL && src_page != dst_page)
4440 vm_fault_copy_cleanup(result_page, src_top_page);
4441 vm_fault_copy_dst_cleanup(dst_page);
4442 break;
4443 }
4444
4445 vm_object_lock(dst_page->object);
4446
4447 if (dst_page->object->copy != old_copy_object) {
4448 vm_object_unlock(dst_page->object);
4449 vm_map_verify_done(dst_map, dst_version);
4450 if (result_page != VM_PAGE_NULL && src_page != dst_page)
4451 vm_fault_copy_cleanup(result_page, src_top_page);
4452 vm_fault_copy_dst_cleanup(dst_page);
4453 break;
4454 }
4455 vm_object_unlock(dst_page->object);
4456
4457 /*
4458 * Copy the page, and note that it is dirty
4459 * immediately.
4460 */
4461
4462 if (!page_aligned(src_offset) ||
4463 !page_aligned(dst_offset) ||
4464 !page_aligned(amount_left)) {
4465
4466 vm_object_offset_t src_po,
4467 dst_po;
4468
4469 src_po = src_offset - vm_object_trunc_page(src_offset);
4470 dst_po = dst_offset - vm_object_trunc_page(dst_offset);
4471
4472 if (dst_po > src_po) {
4473 part_size = PAGE_SIZE - dst_po;
4474 } else {
4475 part_size = PAGE_SIZE - src_po;
4476 }
4477 if (part_size > (amount_left)){
4478 part_size = amount_left;
4479 }
4480
4481 if (result_page == VM_PAGE_NULL) {
4482 assert((vm_offset_t) dst_po == dst_po);
4483 assert((vm_size_t) part_size == part_size);
4484 vm_page_part_zero_fill(dst_page,
4485 (vm_offset_t) dst_po,
4486 (vm_size_t) part_size);
4487 } else {
4488 assert((vm_offset_t) src_po == src_po);
4489 assert((vm_offset_t) dst_po == dst_po);
4490 assert((vm_size_t) part_size == part_size);
4491 vm_page_part_copy(result_page,
4492 (vm_offset_t) src_po,
4493 dst_page,
4494 (vm_offset_t) dst_po,
4495 (vm_size_t)part_size);
4496 if(!dst_page->dirty){
4497 vm_object_lock(dst_object);
4498 dst_page->dirty = TRUE;
4499 vm_object_unlock(dst_page->object);
4500 }
4501
4502 }
4503 } else {
4504 part_size = PAGE_SIZE;
4505
4506 if (result_page == VM_PAGE_NULL)
4507 vm_page_zero_fill(dst_page);
4508 else{
4509 vm_page_copy(result_page, dst_page);
4510 if(!dst_page->dirty){
4511 vm_object_lock(dst_object);
4512 dst_page->dirty = TRUE;
4513 vm_object_unlock(dst_page->object);
4514 }
4515 }
4516
4517 }
4518
4519 /*
4520 * Unlock everything, and return
4521 */
4522
4523 vm_map_verify_done(dst_map, dst_version);
4524
4525 if (result_page != VM_PAGE_NULL && src_page != dst_page)
4526 vm_fault_copy_cleanup(result_page, src_top_page);
4527 vm_fault_copy_dst_cleanup(dst_page);
4528
4529 amount_left -= part_size;
4530 src_offset += part_size;
4531 dst_offset += part_size;
4532 } while (amount_left > 0);
4533
4534 RETURN(KERN_SUCCESS);
4535 #undef RETURN
4536
4537 /*NOTREACHED*/
4538 }
4539
4540 #if VM_FAULT_CLASSIFY
4541 /*
4542 * Temporary statistics gathering support.
4543 */
4544
4545 /*
4546 * Statistics arrays:
4547 */
4548 #define VM_FAULT_TYPES_MAX 5
4549 #define VM_FAULT_LEVEL_MAX 8
4550
4551 int vm_fault_stats[VM_FAULT_TYPES_MAX][VM_FAULT_LEVEL_MAX];
4552
4553 #define VM_FAULT_TYPE_ZERO_FILL 0
4554 #define VM_FAULT_TYPE_MAP_IN 1
4555 #define VM_FAULT_TYPE_PAGER 2
4556 #define VM_FAULT_TYPE_COPY 3
4557 #define VM_FAULT_TYPE_OTHER 4
4558
4559
4560 void
4561 vm_fault_classify(vm_object_t object,
4562 vm_object_offset_t offset,
4563 vm_prot_t fault_type)
4564 {
4565 int type, level = 0;
4566 vm_page_t m;
4567
4568 while (TRUE) {
4569 m = vm_page_lookup(object, offset);
4570 if (m != VM_PAGE_NULL) {
4571 if (m->busy || m->error || m->restart || m->absent) {
4572 type = VM_FAULT_TYPE_OTHER;
4573 break;
4574 }
4575 if (((fault_type & VM_PROT_WRITE) == 0) ||
4576 ((level == 0) && object->copy == VM_OBJECT_NULL)) {
4577 type = VM_FAULT_TYPE_MAP_IN;
4578 break;
4579 }
4580 type = VM_FAULT_TYPE_COPY;
4581 break;
4582 }
4583 else {
4584 if (object->pager_created) {
4585 type = VM_FAULT_TYPE_PAGER;
4586 break;
4587 }
4588 if (object->shadow == VM_OBJECT_NULL) {
4589 type = VM_FAULT_TYPE_ZERO_FILL;
4590 break;
4591 }
4592
4593 offset += object->shadow_offset;
4594 object = object->shadow;
4595 level++;
4596 continue;
4597 }
4598 }
4599
4600 if (level > VM_FAULT_LEVEL_MAX)
4601 level = VM_FAULT_LEVEL_MAX;
4602
4603 vm_fault_stats[type][level] += 1;
4604
4605 return;
4606 }
4607
4608 /* cleanup routine to call from debugger */
4609
4610 void
4611 vm_fault_classify_init(void)
4612 {
4613 int type, level;
4614
4615 for (type = 0; type < VM_FAULT_TYPES_MAX; type++) {
4616 for (level = 0; level < VM_FAULT_LEVEL_MAX; level++) {
4617 vm_fault_stats[type][level] = 0;
4618 }
4619 }
4620
4621 return;
4622 }
4623 #endif /* VM_FAULT_CLASSIFY */
4624
4625
4626 extern int cs_validation;
4627
4628 void
4629 vm_page_validate_cs_mapped(
4630 vm_page_t page,
4631 const void *kaddr)
4632 {
4633 vm_object_t object;
4634 vm_object_offset_t offset;
4635 kern_return_t kr;
4636 memory_object_t pager;
4637 void *blobs;
4638 boolean_t validated, tainted;
4639
4640 assert(page->busy);
4641 vm_object_lock_assert_exclusive(page->object);
4642
4643 if (!cs_validation) {
4644 return;
4645 }
4646
4647 if (page->wpmapped && !page->cs_tainted) {
4648 /*
4649 * This page was mapped for "write" access sometime in the
4650 * past and could still be modifiable in the future.
4651 * Consider it tainted.
4652 * [ If the page was already found to be "tainted", no
4653 * need to re-validate. ]
4654 */
4655 page->cs_validated = TRUE;
4656 page->cs_tainted = TRUE;
4657 if (cs_debug) {
4658 printf("CODESIGNING: vm_page_validate_cs: "
4659 "page %p obj %p off 0x%llx "
4660 "was modified\n",
4661 page, page->object, page->offset);
4662 }
4663 vm_cs_validated_dirtied++;
4664 }
4665
4666 if (page->cs_validated) {
4667 return;
4668 }
4669
4670 vm_cs_validates++;
4671
4672 object = page->object;
4673 assert(object->code_signed);
4674 offset = page->offset;
4675
4676 if (!object->alive || object->terminating || object->pager == NULL) {
4677 /*
4678 * The object is terminating and we don't have its pager
4679 * so we can't validate the data...
4680 */
4681 return;
4682 }
4683 /*
4684 * Since we get here to validate a page that was brought in by
4685 * the pager, we know that this pager is all setup and ready
4686 * by now.
4687 */
4688 assert(!object->internal);
4689 assert(object->pager != NULL);
4690 assert(object->pager_ready);
4691
4692 pager = object->pager;
4693 assert(object->paging_in_progress);
4694 kr = vnode_pager_get_object_cs_blobs(pager, &blobs);
4695 if (kr != KERN_SUCCESS) {
4696 blobs = NULL;
4697 }
4698
4699 /* verify the SHA1 hash for this page */
4700 validated = cs_validate_page(blobs,
4701 offset + object->paging_offset,
4702 (const void *)kaddr,
4703 &tainted);
4704
4705 page->cs_validated = validated;
4706 if (validated) {
4707 page->cs_tainted = tainted;
4708 }
4709 }
4710
4711 void
4712 vm_page_validate_cs(
4713 vm_page_t page)
4714 {
4715 vm_object_t object;
4716 vm_object_offset_t offset;
4717 vm_map_offset_t koffset;
4718 vm_map_size_t ksize;
4719 vm_offset_t kaddr;
4720 kern_return_t kr;
4721 boolean_t busy_page;
4722
4723 vm_object_lock_assert_held(page->object);
4724
4725 if (!cs_validation) {
4726 return;
4727 }
4728
4729 if (page->wpmapped && !page->cs_tainted) {
4730 vm_object_lock_assert_exclusive(page->object);
4731
4732 /*
4733 * This page was mapped for "write" access sometime in the
4734 * past and could still be modifiable in the future.
4735 * Consider it tainted.
4736 * [ If the page was already found to be "tainted", no
4737 * need to re-validate. ]
4738 */
4739 page->cs_validated = TRUE;
4740 page->cs_tainted = TRUE;
4741 if (cs_debug) {
4742 printf("CODESIGNING: vm_page_validate_cs: "
4743 "page %p obj %p off 0x%llx "
4744 "was modified\n",
4745 page, page->object, page->offset);
4746 }
4747 vm_cs_validated_dirtied++;
4748 }
4749
4750 if (page->cs_validated) {
4751 return;
4752 }
4753
4754 vm_object_lock_assert_exclusive(page->object);
4755
4756 object = page->object;
4757 assert(object->code_signed);
4758 offset = page->offset;
4759
4760 busy_page = page->busy;
4761 if (!busy_page) {
4762 /* keep page busy while we map (and unlock) the VM object */
4763 page->busy = TRUE;
4764 }
4765
4766 /*
4767 * Take a paging reference on the VM object
4768 * to protect it from collapse or bypass,
4769 * and keep it from disappearing too.
4770 */
4771 vm_object_paging_begin(object);
4772
4773 /* map the page in the kernel address space */
4774 koffset = 0;
4775 ksize = PAGE_SIZE_64;
4776 kr = vm_paging_map_object(&koffset,
4777 page,
4778 object,
4779 offset,
4780 &ksize,
4781 VM_PROT_READ,
4782 FALSE); /* can't unlock object ! */
4783 if (kr != KERN_SUCCESS) {
4784 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr);
4785 }
4786 kaddr = CAST_DOWN(vm_offset_t, koffset);
4787
4788 /* validate the mapped page */
4789 vm_page_validate_cs_mapped(page, (const void *) kaddr);
4790
4791 assert(page->busy);
4792 assert(object == page->object);
4793 vm_object_lock_assert_exclusive(object);
4794
4795 if (!busy_page) {
4796 PAGE_WAKEUP_DONE(page);
4797 }
4798 if (koffset != 0) {
4799 /* unmap the map from the kernel address space */
4800 vm_paging_unmap_object(object, koffset, koffset + ksize);
4801 koffset = 0;
4802 ksize = 0;
4803 kaddr = 0;
4804 }
4805 vm_object_paging_end(object);
4806 }