2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Page fault handling module.
65 #include <mach_cluster_stats.h>
66 #include <mach_pagemap.h>
67 #include <libkern/OSAtomic.h>
69 #include <mach/mach_types.h>
70 #include <mach/kern_return.h>
71 #include <mach/message.h> /* for error codes */
72 #include <mach/vm_param.h>
73 #include <mach/vm_behavior.h>
74 #include <mach/memory_object.h>
75 /* For memory_object_data_{request,unlock} */
78 #include <kern/kern_types.h>
79 #include <kern/host_statistics.h>
80 #include <kern/counters.h>
81 #include <kern/task.h>
82 #include <kern/thread.h>
83 #include <kern/sched_prim.h>
84 #include <kern/host.h>
86 #include <kern/mach_param.h>
87 #include <kern/macro_help.h>
88 #include <kern/zalloc.h>
89 #include <kern/misc_protos.h>
91 #include <vm/vm_compressor.h>
92 #include <vm/vm_compressor_pager.h>
93 #include <vm/vm_fault.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_kern.h>
99 #include <vm/vm_pageout.h>
100 #include <vm/vm_protos.h>
101 #include <vm/vm_external.h>
102 #include <vm/memory_object.h>
103 #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
104 #include <vm/vm_shared_region.h>
106 #include <sys/codesign.h>
108 #include <libsa/sys/timers.h> /* for struct timespec */
110 #define VM_FAULT_CLASSIFY 0
112 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
114 unsigned int vm_object_pagein_throttle
= 16;
117 * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
118 * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts
119 * of memory if they're buggy and can run the system completely out of swap space. If this happens, we
120 * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps
121 * keep the UI active so that the user has a chance to kill the offending task before the system
124 * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
125 * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold
126 * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a
127 * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
130 extern void throttle_lowpri_io(int);
132 uint64_t vm_hard_throttle_threshold
;
136 #define NEED_TO_HARD_THROTTLE_THIS_TASK() (vm_wants_task_throttled(current_task()) || \
137 (vm_page_free_count < vm_page_throttle_limit && \
138 proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO) > THROTTLE_LEVEL_THROTTLED))
141 #define HARD_THROTTLE_DELAY 5000 /* 5000 us == 5 ms */
142 #define SOFT_THROTTLE_DELAY 200 /* 200 us == .2 ms */
144 #define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS 6
145 #define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC 20000
148 boolean_t
current_thread_aborted(void);
150 /* Forward declarations of internal routines. */
151 extern kern_return_t
vm_fault_wire_fast(
154 vm_map_entry_t entry
,
156 vm_map_offset_t pmap_addr
,
157 ppnum_t
*physpage_p
);
159 extern void vm_fault_continue(void);
161 extern void vm_fault_copy_cleanup(
165 extern void vm_fault_copy_dst_cleanup(
168 #if VM_FAULT_CLASSIFY
169 extern void vm_fault_classify(vm_object_t object
,
170 vm_object_offset_t offset
,
171 vm_prot_t fault_type
);
173 extern void vm_fault_classify_init(void);
176 unsigned long vm_pmap_enter_blocked
= 0;
177 unsigned long vm_pmap_enter_retried
= 0;
179 unsigned long vm_cs_validates
= 0;
180 unsigned long vm_cs_revalidates
= 0;
181 unsigned long vm_cs_query_modified
= 0;
182 unsigned long vm_cs_validated_dirtied
= 0;
183 unsigned long vm_cs_bitmap_validated
= 0;
185 void vm_pre_fault(vm_map_offset_t
);
188 * Routine: vm_fault_init
190 * Initialize our private data structures.
195 int i
, vm_compressor_temp
;
196 boolean_t need_default_val
= TRUE
;
198 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
199 * computed as a percentage of available memory, and the percentage used is scaled inversely with
200 * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems
201 * and reduce the value down to 10% for very large memory configurations. This helps give us a
202 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
203 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
206 vm_hard_throttle_threshold
= sane_size
* (35 - MIN((int)(sane_size
/ (1024*1024*1024)), 25)) / 100;
209 * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry.
212 if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp
, sizeof (vm_compressor_temp
))) {
213 for ( i
= 0; i
< VM_PAGER_MAX_MODES
; i
++) {
214 if (vm_compressor_temp
> 0 &&
215 ((vm_compressor_temp
& ( 1 << i
)) == vm_compressor_temp
)) {
216 need_default_val
= FALSE
;
217 vm_compressor_mode
= vm_compressor_temp
;
221 if (need_default_val
)
222 printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp
);
224 if (need_default_val
) {
225 /* If no boot arg or incorrect boot arg, try device tree. */
226 PE_get_default("kern.vm_compressor", &vm_compressor_mode
, sizeof(vm_compressor_mode
));
228 PE_parse_boot_argn("vm_compressor_threads", &vm_compressor_thread_count
, sizeof (vm_compressor_thread_count
));
229 printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode
);
233 * Routine: vm_fault_cleanup
235 * Clean up the result of vm_fault_page.
237 * The paging reference for "object" is released.
238 * "object" is unlocked.
239 * If "top_page" is not null, "top_page" is
240 * freed and the paging reference for the object
241 * containing it is released.
244 * "object" must be locked.
248 register vm_object_t object
,
249 register vm_page_t top_page
)
251 vm_object_paging_end(object
);
252 vm_object_unlock(object
);
254 if (top_page
!= VM_PAGE_NULL
) {
255 object
= top_page
->object
;
257 vm_object_lock(object
);
258 VM_PAGE_FREE(top_page
);
259 vm_object_paging_end(object
);
260 vm_object_unlock(object
);
264 #if MACH_CLUSTER_STATS
265 #define MAXCLUSTERPAGES 16
267 unsigned long pages_in_cluster
;
268 unsigned long pages_at_higher_offsets
;
269 unsigned long pages_at_lower_offsets
;
270 } cluster_stats_in
[MAXCLUSTERPAGES
];
271 #define CLUSTER_STAT(clause) clause
272 #define CLUSTER_STAT_HIGHER(x) \
273 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
274 #define CLUSTER_STAT_LOWER(x) \
275 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
276 #define CLUSTER_STAT_CLUSTER(x) \
277 ((cluster_stats_in[(x)].pages_in_cluster)++)
278 #else /* MACH_CLUSTER_STATS */
279 #define CLUSTER_STAT(clause)
280 #endif /* MACH_CLUSTER_STATS */
282 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
285 boolean_t vm_page_deactivate_behind
= TRUE
;
287 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
289 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
290 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
291 /* we use it to size an array on the stack */
293 int vm_default_behind
= VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW
;
295 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
298 * vm_page_is_sequential
300 * Determine if sequential access is in progress
301 * in accordance with the behavior specified.
302 * Update state to indicate current access pattern.
304 * object must have at least the shared lock held
308 vm_fault_is_sequential(
310 vm_object_offset_t offset
,
311 vm_behavior_t behavior
)
313 vm_object_offset_t last_alloc
;
317 last_alloc
= object
->last_alloc
;
318 sequential
= object
->sequential
;
319 orig_sequential
= sequential
;
322 case VM_BEHAVIOR_RANDOM
:
324 * reset indicator of sequential behavior
329 case VM_BEHAVIOR_SEQUENTIAL
:
330 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
332 * advance indicator of sequential behavior
334 if (sequential
< MAX_SEQUENTIAL_RUN
)
335 sequential
+= PAGE_SIZE
;
338 * reset indicator of sequential behavior
344 case VM_BEHAVIOR_RSEQNTL
:
345 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
347 * advance indicator of sequential behavior
349 if (sequential
> -MAX_SEQUENTIAL_RUN
)
350 sequential
-= PAGE_SIZE
;
353 * reset indicator of sequential behavior
359 case VM_BEHAVIOR_DEFAULT
:
361 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
363 * advance indicator of sequential behavior
367 if (sequential
< MAX_SEQUENTIAL_RUN
)
368 sequential
+= PAGE_SIZE
;
370 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
372 * advance indicator of sequential behavior
376 if (sequential
> -MAX_SEQUENTIAL_RUN
)
377 sequential
-= PAGE_SIZE
;
380 * reset indicator of sequential behavior
386 if (sequential
!= orig_sequential
) {
387 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
389 * if someone else has already updated object->sequential
390 * don't bother trying to update it or object->last_alloc
396 * I'd like to do this with a OSCompareAndSwap64, but that
397 * doesn't exist for PPC... however, it shouldn't matter
398 * that much... last_alloc is maintained so that we can determine
399 * if a sequential access pattern is taking place... if only
400 * one thread is banging on this object, no problem with the unprotected
401 * update... if 2 or more threads are banging away, we run the risk of
402 * someone seeing a mangled update... however, in the face of multiple
403 * accesses, no sequential access pattern can develop anyway, so we
404 * haven't lost any real info.
406 object
->last_alloc
= offset
;
410 int vm_page_deactivate_behind_count
= 0;
413 * vm_page_deactivate_behind
415 * Determine if sequential access is in progress
416 * in accordance with the behavior specified. If
417 * so, compute a potential page to deactivate and
420 * object must be locked.
422 * return TRUE if we actually deactivate a page
426 vm_fault_deactivate_behind(
428 vm_object_offset_t offset
,
429 vm_behavior_t behavior
)
432 int pages_in_run
= 0;
433 int max_pages_in_run
= 0;
435 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
436 vm_object_offset_t run_offset
= 0;
437 vm_object_offset_t pg_offset
= 0;
439 vm_page_t page_run
[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
];
443 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
446 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
448 * Do not deactivate pages from the kernel object: they
449 * are not intended to become pageable.
450 * or we've disabled the deactivate behind mechanism
454 if ((sequential_run
= object
->sequential
)) {
455 if (sequential_run
< 0) {
456 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
457 sequential_run
= 0 - sequential_run
;
459 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
463 case VM_BEHAVIOR_RANDOM
:
465 case VM_BEHAVIOR_SEQUENTIAL
:
466 if (sequential_run
>= (int)PAGE_SIZE
) {
467 run_offset
= 0 - PAGE_SIZE_64
;
468 max_pages_in_run
= 1;
471 case VM_BEHAVIOR_RSEQNTL
:
472 if (sequential_run
>= (int)PAGE_SIZE
) {
473 run_offset
= PAGE_SIZE_64
;
474 max_pages_in_run
= 1;
477 case VM_BEHAVIOR_DEFAULT
:
479 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
482 * determine if the run of sequential accesss has been
483 * long enough on an object with default access behavior
484 * to consider it for deactivation
486 if ((uint64_t)sequential_run
>= behind
&& (sequential_run
% (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
* PAGE_SIZE
)) == 0) {
488 * the comparisons between offset and behind are done
489 * in this kind of odd fashion in order to prevent wrap around
492 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
493 if (offset
>= behind
) {
494 run_offset
= 0 - behind
;
495 pg_offset
= PAGE_SIZE_64
;
496 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
499 if (offset
< -behind
) {
501 pg_offset
= 0 - PAGE_SIZE_64
;
502 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
509 for (n
= 0; n
< max_pages_in_run
; n
++) {
510 m
= vm_page_lookup(object
, offset
+ run_offset
+ (n
* pg_offset
));
512 if (m
&& !m
->laundry
&& !m
->busy
&& !m
->no_cache
&& !m
->throttled
&& !m
->fictitious
&& !m
->absent
) {
513 page_run
[pages_in_run
++] = m
;
516 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
518 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
519 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
520 * new reference happens. If no futher references happen on the page after that remote TLB flushes
521 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
522 * by pageout_scan, which is just fine since the last reference would have happened quite far
523 * in the past (TLB caches don't hang around for very long), and of course could just as easily
524 * have happened before we did the deactivate_behind.
526 pmap_clear_refmod_options(m
->phys_page
, VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
530 vm_page_lockspin_queues();
532 for (n
= 0; n
< pages_in_run
; n
++) {
536 vm_page_deactivate_internal(m
, FALSE
);
538 vm_page_deactivate_behind_count
++;
540 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
543 vm_page_unlock_queues();
551 #if (DEVELOPMENT || DEBUG)
552 uint32_t vm_page_creation_throttled_hard
= 0;
553 uint32_t vm_page_creation_throttled_soft
= 0;
554 #endif /* DEVELOPMENT || DEBUG */
557 vm_page_throttled(boolean_t page_kept
)
559 clock_sec_t elapsed_sec
;
561 clock_usec_t tv_usec
;
563 thread_t thread
= current_thread();
565 if (thread
->options
& TH_OPT_VMPRIV
)
568 if (thread
->t_page_creation_throttled
) {
569 thread
->t_page_creation_throttled
= 0;
571 if (page_kept
== FALSE
)
574 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
575 #if (DEVELOPMENT || DEBUG)
576 thread
->t_page_creation_throttled_hard
++;
577 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
578 #endif /* DEVELOPMENT || DEBUG */
579 return (HARD_THROTTLE_DELAY
);
582 if ((vm_page_free_count
< vm_page_throttle_limit
|| ((COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) && SWAPPER_NEEDS_TO_UNTHROTTLE())) &&
583 thread
->t_page_creation_count
> (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
* VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
)) {
585 clock_get_system_microtime(&tv_sec
, &tv_usec
);
587 elapsed_sec
= tv_sec
- thread
->t_page_creation_time
;
589 if (elapsed_sec
<= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
||
590 (thread
->t_page_creation_count
/ elapsed_sec
) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
) {
592 if (elapsed_sec
>= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
)) {
594 * we'll reset our stats to give a well behaved app
595 * that was unlucky enough to accumulate a bunch of pages
596 * over a long period of time a chance to get out of
597 * the throttled state... we reset the counter and timestamp
598 * so that if it stays under the rate limit for the next second
599 * it will be back in our good graces... if it exceeds it, it
600 * will remain in the throttled state
602 thread
->t_page_creation_time
= tv_sec
;
603 thread
->t_page_creation_count
= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
* (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
- 1);
605 ++vm_page_throttle_count
;
607 thread
->t_page_creation_throttled
= 1;
609 if ((COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) && HARD_THROTTLE_LIMIT_REACHED()) {
610 #if (DEVELOPMENT || DEBUG)
611 thread
->t_page_creation_throttled_hard
++;
612 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
613 #endif /* DEVELOPMENT || DEBUG */
614 return (HARD_THROTTLE_DELAY
);
616 #if (DEVELOPMENT || DEBUG)
617 thread
->t_page_creation_throttled_soft
++;
618 OSAddAtomic(1, &vm_page_creation_throttled_soft
);
619 #endif /* DEVELOPMENT || DEBUG */
620 return (SOFT_THROTTLE_DELAY
);
623 thread
->t_page_creation_time
= tv_sec
;
624 thread
->t_page_creation_count
= 0;
627 thread
->t_page_creation_count
++;
633 * check for various conditions that would
634 * prevent us from creating a ZF page...
635 * cleanup is based on being called from vm_fault_page
637 * object must be locked
638 * object == m->object
640 static vm_fault_return_t
641 vm_fault_check(vm_object_t object
, vm_page_t m
, vm_page_t first_m
, boolean_t interruptible_state
, boolean_t page_throttle
)
645 if (object
->shadow_severed
||
646 VM_OBJECT_PURGEABLE_FAULT_ERROR(object
)) {
649 * 1. the shadow chain was severed,
650 * 2. the purgeable object is volatile or empty and is marked
651 * to fault on access while volatile.
652 * Just have to return an error at this point
654 if (m
!= VM_PAGE_NULL
)
656 vm_fault_cleanup(object
, first_m
);
658 thread_interrupt_level(interruptible_state
);
660 return (VM_FAULT_MEMORY_ERROR
);
662 if (vm_backing_store_low
) {
664 * are we protecting the system from
665 * backing store exhaustion. If so
666 * sleep unless we are privileged.
668 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
670 if (m
!= VM_PAGE_NULL
)
672 vm_fault_cleanup(object
, first_m
);
674 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
676 thread_block(THREAD_CONTINUE_NULL
);
677 thread_interrupt_level(interruptible_state
);
679 return (VM_FAULT_RETRY
);
682 if (page_throttle
== TRUE
&& (throttle_delay
= vm_page_throttled(FALSE
))) {
684 * we're throttling zero-fills...
685 * treat this as if we couldn't grab a page
687 if (m
!= VM_PAGE_NULL
)
689 vm_fault_cleanup(object
, first_m
);
691 VM_DEBUG_EVENT(vmf_check_zfdelay
, VMF_CHECK_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
693 delay(throttle_delay
);
695 if (current_thread_aborted()) {
696 thread_interrupt_level(interruptible_state
);
697 return VM_FAULT_INTERRUPTED
;
699 thread_interrupt_level(interruptible_state
);
701 return (VM_FAULT_MEMORY_SHORTAGE
);
703 return (VM_FAULT_SUCCESS
);
708 * do the work to zero fill a page and
709 * inject it into the correct paging queue
711 * m->object must be locked
712 * page queue lock must NOT be held
715 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
717 int my_fault
= DBG_ZERO_FILL_FAULT
;
720 * This is is a zero-fill page fault...
722 * Checking the page lock is a waste of
723 * time; this page was absent, so
724 * it can't be page locked by a pager.
726 * we also consider it undefined
727 * with respect to instruction
728 * execution. i.e. it is the responsibility
729 * of higher layers to call for an instruction
730 * sync after changing the contents and before
731 * sending a program into this area. We
732 * choose this approach for performance
736 m
->cs_validated
= FALSE
;
737 m
->cs_tainted
= FALSE
;
739 if (no_zero_fill
== TRUE
) {
740 my_fault
= DBG_NZF_PAGE_FAULT
;
742 if (m
->absent
&& m
->busy
)
745 vm_page_zero_fill(m
);
747 VM_STAT_INCR(zero_fill_count
);
748 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
751 assert(m
->object
!= kernel_object
);
752 //assert(m->pageq.next == NULL && m->pageq.prev == NULL);
754 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) &&
755 (m
->object
->purgable
== VM_PURGABLE_DENY
||
756 m
->object
->purgable
== VM_PURGABLE_NONVOLATILE
||
757 m
->object
->purgable
== VM_PURGABLE_VOLATILE
)) {
759 vm_page_lockspin_queues();
761 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
)) {
762 assert(!VM_PAGE_WIRED(m
));
765 * can't be on the pageout queue since we don't
766 * have a pager to try and clean to
768 assert(!m
->pageout_queue
);
770 VM_PAGE_QUEUES_REMOVE(m
);
772 queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
774 vm_page_throttled_count
++;
776 vm_page_unlock_queues();
783 * Routine: vm_fault_page
785 * Find the resident page for the virtual memory
786 * specified by the given virtual memory object
788 * Additional arguments:
789 * The required permissions for the page is given
790 * in "fault_type". Desired permissions are included
792 * fault_info is passed along to determine pagein cluster
793 * limits... it contains the expected reference pattern,
794 * cluster size if available, etc...
796 * If the desired page is known to be resident (for
797 * example, because it was previously wired down), asserting
798 * the "unwiring" parameter will speed the search.
800 * If the operation can be interrupted (by thread_abort
801 * or thread_terminate), then the "interruptible"
802 * parameter should be asserted.
805 * The page containing the proper data is returned
809 * The source object must be locked and referenced,
810 * and must donate one paging reference. The reference
811 * is not affected. The paging reference and lock are
814 * If the call succeeds, the object in which "result_page"
815 * resides is left locked and holding a paging reference.
816 * If this is not the original object, a busy page in the
817 * original object is returned in "top_page", to prevent other
818 * callers from pursuing this same data, along with a paging
819 * reference for the original object. The "top_page" should
820 * be destroyed when this guarantee is no longer required.
821 * The "result_page" is also left busy. It is not removed
822 * from the pageout queues.
824 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
825 * fault succeeded but there's no VM page (i.e. the VM object
826 * does not actually hold VM pages, but device memory or
827 * large pages). The object is still locked and we still hold a
828 * paging_in_progress reference.
830 unsigned int vm_fault_page_blocked_access
= 0;
831 unsigned int vm_fault_page_forced_retry
= 0;
836 vm_object_t first_object
, /* Object to begin search */
837 vm_object_offset_t first_offset
, /* Offset into object */
838 vm_prot_t fault_type
, /* What access is requested */
839 boolean_t must_be_resident
,/* Must page be resident? */
840 boolean_t caller_lookup
, /* caller looked up page */
841 /* Modifies in place: */
842 vm_prot_t
*protection
, /* Protection for mapping */
843 vm_page_t
*result_page
, /* Page found, if successful */
845 vm_page_t
*top_page
, /* Page in top object, if
846 * not result_page. */
847 int *type_of_fault
, /* if non-null, fill in with type of fault
848 * COW, zero-fill, etc... returned in trace point */
849 /* More arguments: */
850 kern_return_t
*error_code
, /* code if page is in error */
851 boolean_t no_zero_fill
, /* don't zero fill absent pages */
852 boolean_t data_supply
, /* treat as data_supply if
853 * it is a write fault and a full
854 * page is provided */
855 vm_object_fault_info_t fault_info
)
859 vm_object_offset_t offset
;
861 vm_object_t next_object
;
862 vm_object_t copy_object
;
863 boolean_t look_for_page
;
864 boolean_t force_fault_retry
= FALSE
;
865 vm_prot_t access_required
= fault_type
;
866 vm_prot_t wants_copy_flag
;
867 CLUSTER_STAT(int pages_at_higher_offsets
;)
868 CLUSTER_STAT(int pages_at_lower_offsets
;)
869 kern_return_t wait_result
;
870 boolean_t interruptible_state
;
871 boolean_t data_already_requested
= FALSE
;
872 vm_behavior_t orig_behavior
;
873 vm_size_t orig_cluster_size
;
874 vm_fault_return_t error
;
876 uint32_t try_failed_count
;
877 int interruptible
; /* how may fault be interrupted? */
878 int external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
879 memory_object_t pager
;
880 vm_fault_return_t retval
;
883 * MACH page map - an optional optimization where a bit map is maintained
884 * by the VM subsystem for internal objects to indicate which pages of
885 * the object currently reside on backing store. This existence map
886 * duplicates information maintained by the vnode pager. It is
887 * created at the time of the first pageout against the object, i.e.
888 * at the same time pager for the object is created. The optimization
889 * is designed to eliminate pager interaction overhead, if it is
890 * 'known' that the page does not exist on backing store.
892 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
893 * either marked as paged out in the existence map for the object or no
894 * existence map exists for the object. MUST_ASK_PAGER() is one of the
895 * criteria in the decision to invoke the pager. It is also used as one
896 * of the criteria to terminate the scan for adjacent pages in a clustered
897 * pagein operation. Note that MUST_ASK_PAGER() always evaluates to TRUE for
898 * permanent objects. Note also that if the pager for an internal object
899 * has not been created, the pager is not invoked regardless of the value
900 * of MUST_ASK_PAGER() and that clustered pagein scans are only done on an object
901 * for which a pager has been created.
903 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
904 * is marked as paged out in the existence map for the object. PAGED_OUT()
905 * PAGED_OUT() is used to determine if a page has already been pushed
906 * into a copy object in order to avoid a redundant page out operation.
909 #define MUST_ASK_PAGER(o, f, s) \
910 ((vm_external_state_get((o)->existence_map, (f)) \
911 != VM_EXTERNAL_STATE_ABSENT) && \
912 (s = (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)))) \
913 != VM_EXTERNAL_STATE_ABSENT)
914 #define PAGED_OUT(o, f) \
915 ((vm_external_state_get((o)->existence_map, (f)) \
916 == VM_EXTERNAL_STATE_EXISTS) || \
917 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) \
918 == VM_EXTERNAL_STATE_EXISTS))
919 #else /* MACH_PAGEMAP */
920 #define MUST_ASK_PAGER(o, f, s) \
921 ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
922 #define PAGED_OUT(o, f) \
923 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
924 #endif /* MACH_PAGEMAP */
929 #define RELEASE_PAGE(m) \
931 PAGE_WAKEUP_DONE(m); \
932 if (!m->active && !m->inactive && !m->throttled) { \
933 vm_page_lockspin_queues(); \
934 if (!m->active && !m->inactive && !m->throttled) { \
935 if (COMPRESSED_PAGER_IS_ACTIVE) \
936 vm_page_deactivate(m); \
938 vm_page_activate(m); \
940 vm_page_unlock_queues(); \
945 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
948 interruptible
= fault_info
->interruptible
;
949 interruptible_state
= thread_interrupt_level(interruptible
);
952 * INVARIANTS (through entire routine):
954 * 1) At all times, we must either have the object
955 * lock or a busy page in some object to prevent
956 * some other thread from trying to bring in
959 * Note that we cannot hold any locks during the
960 * pager access or when waiting for memory, so
961 * we use a busy page then.
963 * 2) To prevent another thread from racing us down the
964 * shadow chain and entering a new page in the top
965 * object before we do, we must keep a busy page in
966 * the top object while following the shadow chain.
968 * 3) We must increment paging_in_progress on any object
969 * for which we have a busy page before dropping
972 * 4) We leave busy pages on the pageout queues.
973 * If the pageout daemon comes across a busy page,
974 * it will remove the page from the pageout queues.
977 object
= first_object
;
978 offset
= first_offset
;
979 first_m
= VM_PAGE_NULL
;
980 access_required
= fault_type
;
984 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
985 object
, offset
, fault_type
, *protection
, 0);
988 * default type of fault
990 my_fault
= DBG_CACHE_HIT_FAULT
;
994 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
996 if (!object
->alive
) {
998 * object is no longer valid
999 * clean up and return error
1001 vm_fault_cleanup(object
, first_m
);
1002 thread_interrupt_level(interruptible_state
);
1004 return (VM_FAULT_MEMORY_ERROR
);
1007 if (!object
->pager_created
&& object
->phys_contiguous
) {
1009 * A physically-contiguous object without a pager:
1010 * must be a "large page" object. We do not deal
1011 * with VM pages for this object.
1013 caller_lookup
= FALSE
;
1015 goto phys_contig_object
;
1018 if (object
->blocked_access
) {
1020 * Access to this VM object has been blocked.
1021 * Replace our "paging_in_progress" reference with
1022 * a "activity_in_progress" reference and wait for
1023 * access to be unblocked.
1025 caller_lookup
= FALSE
; /* no longer valid after sleep */
1026 vm_object_activity_begin(object
);
1027 vm_object_paging_end(object
);
1028 while (object
->blocked_access
) {
1029 vm_object_sleep(object
,
1030 VM_OBJECT_EVENT_UNBLOCKED
,
1033 vm_fault_page_blocked_access
++;
1034 vm_object_paging_begin(object
);
1035 vm_object_activity_end(object
);
1039 * See whether the page at 'offset' is resident
1041 if (caller_lookup
== TRUE
) {
1043 * The caller has already looked up the page
1044 * and gave us the result in "result_page".
1045 * We can use this for the first lookup but
1046 * it loses its validity as soon as we unlock
1050 caller_lookup
= FALSE
; /* no longer valid after that */
1052 m
= vm_page_lookup(object
, offset
);
1055 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1057 if (m
!= VM_PAGE_NULL
) {
1061 * The page is being brought in,
1062 * wait for it and then retry.
1065 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1067 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
1070 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
1073 counter(c_vm_fault_page_block_busy_kernel
++);
1075 if (wait_result
!= THREAD_AWAKENED
) {
1076 vm_fault_cleanup(object
, first_m
);
1077 thread_interrupt_level(interruptible_state
);
1079 if (wait_result
== THREAD_RESTART
)
1080 return (VM_FAULT_RETRY
);
1082 return (VM_FAULT_INTERRUPTED
);
1090 vm_pageout_steal_laundry(m
, FALSE
);
1092 if (m
->phys_page
== vm_page_guard_addr
) {
1094 * Guard page: off limits !
1096 if (fault_type
== VM_PROT_NONE
) {
1098 * The fault is not requesting any
1099 * access to the guard page, so it must
1100 * be just to wire or unwire it.
1101 * Let's pretend it succeeded...
1105 assert(first_m
== VM_PAGE_NULL
);
1106 *top_page
= first_m
;
1108 *type_of_fault
= DBG_GUARD_FAULT
;
1109 thread_interrupt_level(interruptible_state
);
1110 return VM_FAULT_SUCCESS
;
1113 * The fault requests access to the
1114 * guard page: let's deny that !
1116 vm_fault_cleanup(object
, first_m
);
1117 thread_interrupt_level(interruptible_state
);
1118 return VM_FAULT_MEMORY_ERROR
;
1124 * The page is in error, give up now.
1127 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1130 *error_code
= KERN_MEMORY_ERROR
;
1133 vm_fault_cleanup(object
, first_m
);
1134 thread_interrupt_level(interruptible_state
);
1136 return (VM_FAULT_MEMORY_ERROR
);
1140 * The pager wants us to restart
1141 * at the top of the chain,
1142 * typically because it has moved the
1143 * page to another pager, then do so.
1146 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1150 vm_fault_cleanup(object
, first_m
);
1151 thread_interrupt_level(interruptible_state
);
1153 return (VM_FAULT_RETRY
);
1157 * The page isn't busy, but is absent,
1158 * therefore it's deemed "unavailable".
1160 * Remove the non-existent page (unless it's
1161 * in the top object) and move on down to the
1162 * next object (if there is one).
1165 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1167 next_object
= object
->shadow
;
1169 if (next_object
== VM_OBJECT_NULL
) {
1171 * Absent page at bottom of shadow
1172 * chain; zero fill the page we left
1173 * busy in the first object, and free
1176 assert(!must_be_resident
);
1179 * check for any conditions that prevent
1180 * us from creating a new zero-fill page
1181 * vm_fault_check will do all of the
1182 * fault cleanup in the case of an error condition
1183 * including resetting the thread_interrupt_level
1185 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1187 if (error
!= VM_FAULT_SUCCESS
)
1191 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
1196 if (object
!= first_object
) {
1198 * free the absent page we just found
1203 * drop reference and lock on current object
1205 vm_object_paging_end(object
);
1206 vm_object_unlock(object
);
1209 * grab the original page we
1210 * 'soldered' in place and
1211 * retake lock on 'first_object'
1214 first_m
= VM_PAGE_NULL
;
1216 object
= first_object
;
1217 offset
= first_offset
;
1219 vm_object_lock(object
);
1222 * we're going to use the absent page we just found
1223 * so convert it to a 'busy' page
1228 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1231 * zero-fill the page and put it on
1232 * the correct paging queue
1234 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1238 if (must_be_resident
)
1239 vm_object_paging_end(object
);
1240 else if (object
!= first_object
) {
1241 vm_object_paging_end(object
);
1248 vm_page_lockspin_queues();
1250 assert(!m
->pageout_queue
);
1251 VM_PAGE_QUEUES_REMOVE(m
);
1253 vm_page_unlock_queues();
1256 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1259 offset
+object
->vo_shadow_offset
,0);
1261 offset
+= object
->vo_shadow_offset
;
1262 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1263 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1264 access_required
= VM_PROT_READ
;
1266 vm_object_lock(next_object
);
1267 vm_object_unlock(object
);
1268 object
= next_object
;
1269 vm_object_paging_begin(object
);
1272 * reset to default type of fault
1274 my_fault
= DBG_CACHE_HIT_FAULT
;
1280 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1281 && (fault_type
& VM_PROT_WRITE
)) {
1283 * This is a copy-on-write fault that will
1284 * cause us to revoke access to this page, but
1285 * this page is in the process of being cleaned
1286 * in a clustered pageout. We must wait until
1287 * the cleaning operation completes before
1288 * revoking access to the original page,
1289 * otherwise we might attempt to remove a
1293 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1296 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1300 * take an extra ref so that object won't die
1302 vm_object_reference_locked(object
);
1304 vm_fault_cleanup(object
, first_m
);
1306 counter(c_vm_fault_page_block_backoff_kernel
++);
1307 vm_object_lock(object
);
1308 assert(object
->ref_count
> 0);
1310 m
= vm_page_lookup(object
, offset
);
1312 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
1313 PAGE_ASSERT_WAIT(m
, interruptible
);
1315 vm_object_unlock(object
);
1316 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1317 vm_object_deallocate(object
);
1321 vm_object_unlock(object
);
1323 vm_object_deallocate(object
);
1324 thread_interrupt_level(interruptible_state
);
1326 return (VM_FAULT_RETRY
);
1329 if (type_of_fault
== NULL
&& m
->speculative
&&
1330 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1332 * If we were passed a non-NULL pointer for
1333 * "type_of_fault", than we came from
1334 * vm_fault... we'll let it deal with
1335 * this condition, since it
1336 * needs to see m->speculative to correctly
1337 * account the pageins, otherwise...
1338 * take it off the speculative queue, we'll
1339 * let the caller of vm_fault_page deal
1340 * with getting it onto the correct queue
1342 * If the caller specified in fault_info that
1343 * it wants a "stealth" fault, we also leave
1344 * the page in the speculative queue.
1346 vm_page_lockspin_queues();
1348 VM_PAGE_QUEUES_REMOVE(m
);
1349 vm_page_unlock_queues();
1355 * the user needs access to a page that we
1356 * encrypted before paging it out.
1357 * Decrypt the page now.
1358 * Keep it busy to prevent anyone from
1359 * accessing it during the decryption.
1362 vm_page_decrypt(m
, 0);
1363 assert(object
== m
->object
);
1365 PAGE_WAKEUP_DONE(m
);
1368 * Retry from the top, in case
1369 * something changed while we were
1374 ASSERT_PAGE_DECRYPTED(m
);
1376 if (m
->object
->code_signed
) {
1379 * We just paged in a page from a signed
1380 * memory object but we don't need to
1381 * validate it now. We'll validate it if
1382 * when it gets mapped into a user address
1383 * space for the first time or when the page
1384 * gets copied to another object as a result
1385 * of a copy-on-write.
1390 * We mark the page busy and leave it on
1391 * the pageout queues. If the pageout
1392 * deamon comes across it, then it will
1393 * remove the page from the queue, but not the object
1396 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1399 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1400 object
, offset
, m
, 0, 0);
1410 * we get here when there is no page present in the object at
1411 * the offset we're interested in... we'll allocate a page
1412 * at this point if the pager associated with
1413 * this object can provide the data or we're the top object...
1414 * object is locked; m == NULL
1416 if (must_be_resident
) {
1417 if (fault_type
== VM_PROT_NONE
&&
1418 object
== kernel_object
) {
1420 * We've been called from vm_fault_unwire()
1421 * while removing a map entry that was allocated
1422 * with KMA_KOBJECT and KMA_VAONLY. This page
1423 * is not present and there's nothing more to
1424 * do here (nothing to unwire).
1426 vm_fault_cleanup(object
, first_m
);
1427 thread_interrupt_level(interruptible_state
);
1429 return VM_FAULT_MEMORY_ERROR
;
1432 goto dont_look_for_page
;
1436 data_supply
= FALSE
;
1437 #endif /* !MACH_PAGEMAP */
1439 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
, external_state
) == TRUE
) && !data_supply
);
1442 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1444 if (!look_for_page
&& object
== first_object
&& !object
->phys_contiguous
) {
1446 * Allocate a new page for this object/offset pair as a placeholder
1450 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1452 if (m
== VM_PAGE_NULL
) {
1454 vm_fault_cleanup(object
, first_m
);
1455 thread_interrupt_level(interruptible_state
);
1457 return (VM_FAULT_MEMORY_SHORTAGE
);
1460 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1461 vm_page_insert_internal(m
, object
, offset
, FALSE
, TRUE
, TRUE
);
1463 vm_page_insert(m
, object
, offset
);
1466 if (look_for_page
) {
1471 * If the memory manager is not ready, we
1472 * cannot make requests.
1474 if (!object
->pager_ready
) {
1476 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1478 if (m
!= VM_PAGE_NULL
)
1482 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1483 object
, offset
, 0, 0, 0);
1486 * take an extra ref so object won't die
1488 vm_object_reference_locked(object
);
1489 vm_fault_cleanup(object
, first_m
);
1490 counter(c_vm_fault_page_block_backoff_kernel
++);
1492 vm_object_lock(object
);
1493 assert(object
->ref_count
> 0);
1495 if (!object
->pager_ready
) {
1496 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1498 vm_object_unlock(object
);
1499 if (wait_result
== THREAD_WAITING
)
1500 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1501 vm_object_deallocate(object
);
1505 vm_object_unlock(object
);
1506 vm_object_deallocate(object
);
1507 thread_interrupt_level(interruptible_state
);
1509 return (VM_FAULT_RETRY
);
1512 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1514 * If there are too many outstanding page
1515 * requests pending on this external object, we
1516 * wait for them to be resolved now.
1519 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1521 if (m
!= VM_PAGE_NULL
)
1524 * take an extra ref so object won't die
1526 vm_object_reference_locked(object
);
1528 vm_fault_cleanup(object
, first_m
);
1530 counter(c_vm_fault_page_block_backoff_kernel
++);
1532 vm_object_lock(object
);
1533 assert(object
->ref_count
> 0);
1535 if (object
->paging_in_progress
>= vm_object_pagein_throttle
) {
1536 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS
, interruptible
);
1538 vm_object_unlock(object
);
1539 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1540 vm_object_deallocate(object
);
1544 vm_object_unlock(object
);
1545 vm_object_deallocate(object
);
1546 thread_interrupt_level(interruptible_state
);
1548 return (VM_FAULT_RETRY
);
1551 if (object
->internal
&&
1552 (COMPRESSED_PAGER_IS_ACTIVE
1553 || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
)) {
1554 int compressed_count_delta
;
1556 if (m
== VM_PAGE_NULL
) {
1558 * Allocate a new page for this object/offset pair as a placeholder
1562 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1564 if (m
== VM_PAGE_NULL
) {
1566 vm_fault_cleanup(object
, first_m
);
1567 thread_interrupt_level(interruptible_state
);
1569 return (VM_FAULT_MEMORY_SHORTAGE
);
1573 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1574 vm_page_insert_internal(m
, object
, offset
, FALSE
, TRUE
, TRUE
);
1576 vm_page_insert(m
, object
, offset
);
1582 pager
= object
->pager
;
1584 assert(object
->paging_in_progress
> 0);
1585 vm_object_unlock(object
);
1587 rc
= vm_compressor_pager_get(
1589 offset
+ object
->paging_offset
,
1593 &compressed_count_delta
);
1595 if (type_of_fault
== NULL
) {
1599 * we weren't called from vm_fault, so we
1600 * need to apply page creation throttling
1601 * do it before we re-acquire any locks
1603 if (my_fault_type
== DBG_COMPRESSOR_FAULT
) {
1604 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
1605 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 1, 0);
1606 delay(throttle_delay
);
1610 vm_object_lock(object
);
1611 assert(object
->paging_in_progress
> 0);
1613 vm_compressor_pager_count(
1615 compressed_count_delta
,
1616 FALSE
, /* shared_lock */
1623 if ((m
->object
->wimg_bits
&
1625 VM_WIMG_USE_DEFAULT
) {
1627 * If the page is not cacheable,
1628 * we can't let its contents
1629 * linger in the data cache
1630 * after the decompression.
1632 pmap_sync_page_attributes_phys(
1635 m
->written_by_kernel
= TRUE
;
1639 * If the object is purgeable, its
1640 * owner's purgeable ledgers have been
1641 * updated in vm_page_insert() but the
1642 * page was also accounted for in a
1643 * "compressed purgeable" ledger, so
1646 if ((object
->purgable
!=
1647 VM_PURGABLE_DENY
) &&
1648 (object
->vo_purgeable_owner
!=
1651 * One less compressed
1654 vm_purgeable_compressed_update(
1660 case KERN_MEMORY_FAILURE
:
1665 case KERN_MEMORY_ERROR
:
1669 panic("vm_fault_page(): unexpected "
1671 "vm_compressor_pager_get()\n",
1674 PAGE_WAKEUP_DONE(m
);
1677 goto data_requested
;
1679 my_fault_type
= DBG_PAGEIN_FAULT
;
1681 if (m
!= VM_PAGE_NULL
) {
1687 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1691 * It's possible someone called vm_object_destroy while we weren't
1692 * holding the object lock. If that has happened, then bail out
1696 pager
= object
->pager
;
1698 if (pager
== MEMORY_OBJECT_NULL
) {
1699 vm_fault_cleanup(object
, first_m
);
1700 thread_interrupt_level(interruptible_state
);
1701 return VM_FAULT_MEMORY_ERROR
;
1705 * We have an absent page in place for the faulting offset,
1706 * so we can release the object lock.
1709 vm_object_unlock(object
);
1712 * If this object uses a copy_call strategy,
1713 * and we are interested in a copy of this object
1714 * (having gotten here only by following a
1715 * shadow chain), then tell the memory manager
1716 * via a flag added to the desired_access
1717 * parameter, so that it can detect a race
1718 * between our walking down the shadow chain
1719 * and its pushing pages up into a copy of
1720 * the object that it manages.
1722 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
)
1723 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1725 wants_copy_flag
= VM_PROT_NONE
;
1728 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1730 access_required
| wants_copy_flag
, 0);
1732 if (object
->copy
== first_object
) {
1734 * if we issue the memory_object_data_request in
1735 * this state, we are subject to a deadlock with
1736 * the underlying filesystem if it is trying to
1737 * shrink the file resulting in a push of pages
1738 * into the copy object... that push will stall
1739 * on the placeholder page, and if the pushing thread
1740 * is holding a lock that is required on the pagein
1741 * path (such as a truncate lock), we'll deadlock...
1742 * to avoid this potential deadlock, we throw away
1743 * our placeholder page before calling memory_object_data_request
1744 * and force this thread to retry the vm_fault_page after
1745 * we have issued the I/O. the second time through this path
1746 * we will find the page already in the cache (presumably still
1747 * busy waiting for the I/O to complete) and then complete
1748 * the fault w/o having to go through memory_object_data_request again
1750 assert(first_m
!= VM_PAGE_NULL
);
1751 assert(first_m
->object
== first_object
);
1753 vm_object_lock(first_object
);
1754 VM_PAGE_FREE(first_m
);
1755 vm_object_paging_end(first_object
);
1756 vm_object_unlock(first_object
);
1758 first_m
= VM_PAGE_NULL
;
1759 force_fault_retry
= TRUE
;
1761 vm_fault_page_forced_retry
++;
1764 if (data_already_requested
== TRUE
) {
1765 orig_behavior
= fault_info
->behavior
;
1766 orig_cluster_size
= fault_info
->cluster_size
;
1768 fault_info
->behavior
= VM_BEHAVIOR_RANDOM
;
1769 fault_info
->cluster_size
= PAGE_SIZE
;
1772 * Call the memory manager to retrieve the data.
1774 rc
= memory_object_data_request(
1776 offset
+ object
->paging_offset
,
1778 access_required
| wants_copy_flag
,
1779 (memory_object_fault_info_t
)fault_info
);
1781 if (data_already_requested
== TRUE
) {
1782 fault_info
->behavior
= orig_behavior
;
1783 fault_info
->cluster_size
= orig_cluster_size
;
1785 data_already_requested
= TRUE
;
1787 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1789 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1791 vm_object_lock(object
);
1794 if (rc
!= KERN_SUCCESS
) {
1796 vm_fault_cleanup(object
, first_m
);
1797 thread_interrupt_level(interruptible_state
);
1799 return ((rc
== MACH_SEND_INTERRUPTED
) ?
1800 VM_FAULT_INTERRUPTED
:
1801 VM_FAULT_MEMORY_ERROR
);
1804 clock_usec_t tv_usec
;
1806 if (my_fault_type
== DBG_PAGEIN_FAULT
) {
1807 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1808 current_thread()->t_page_creation_time
= tv_sec
;
1809 current_thread()->t_page_creation_count
= 0;
1812 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_flags
& TH_SFLAG_ABORT
)) {
1814 vm_fault_cleanup(object
, first_m
);
1815 thread_interrupt_level(interruptible_state
);
1817 return (VM_FAULT_INTERRUPTED
);
1819 if (force_fault_retry
== TRUE
) {
1821 vm_fault_cleanup(object
, first_m
);
1822 thread_interrupt_level(interruptible_state
);
1824 return (VM_FAULT_RETRY
);
1826 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1828 * No page here means that the object we
1829 * initially looked up was "physically
1830 * contiguous" (i.e. device memory). However,
1831 * with Virtual VRAM, the object might not
1832 * be backed by that device memory anymore,
1833 * so we're done here only if the object is
1834 * still "phys_contiguous".
1835 * Otherwise, if the object is no longer
1836 * "phys_contiguous", we need to retry the
1837 * page fault against the object's new backing
1838 * store (different memory object).
1844 * potentially a pagein fault
1845 * if we make it through the state checks
1846 * above, than we'll count it as such
1848 my_fault
= my_fault_type
;
1851 * Retry with same object/offset, since new data may
1852 * be in a different page (i.e., m is meaningless at
1859 * We get here if the object has no pager, or an existence map
1860 * exists and indicates the page isn't present on the pager
1861 * or we're unwiring a page. If a pager exists, but there
1862 * is no existence map, then the m->absent case above handles
1863 * the ZF case when the pager can't provide the page
1866 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1868 if (object
== first_object
)
1871 assert(m
== VM_PAGE_NULL
);
1874 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1878 next_object
= object
->shadow
;
1880 if (next_object
== VM_OBJECT_NULL
) {
1882 * we've hit the bottom of the shadown chain,
1883 * fill the page in the top object with zeros.
1885 assert(!must_be_resident
);
1887 if (object
!= first_object
) {
1888 vm_object_paging_end(object
);
1889 vm_object_unlock(object
);
1891 object
= first_object
;
1892 offset
= first_offset
;
1893 vm_object_lock(object
);
1896 assert(m
->object
== object
);
1897 first_m
= VM_PAGE_NULL
;
1900 * check for any conditions that prevent
1901 * us from creating a new zero-fill page
1902 * vm_fault_check will do all of the
1903 * fault cleanup in the case of an error condition
1904 * including resetting the thread_interrupt_level
1906 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1908 if (error
!= VM_FAULT_SUCCESS
)
1911 if (m
== VM_PAGE_NULL
) {
1914 if (m
== VM_PAGE_NULL
) {
1915 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1916 thread_interrupt_level(interruptible_state
);
1918 return (VM_FAULT_MEMORY_SHORTAGE
);
1920 vm_page_insert(m
, object
, offset
);
1922 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1925 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1931 * Move on to the next object. Lock the next
1932 * object before unlocking the current one.
1934 if ((object
!= first_object
) || must_be_resident
)
1935 vm_object_paging_end(object
);
1937 offset
+= object
->vo_shadow_offset
;
1938 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1939 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1940 access_required
= VM_PROT_READ
;
1942 vm_object_lock(next_object
);
1943 vm_object_unlock(object
);
1945 object
= next_object
;
1946 vm_object_paging_begin(object
);
1951 * PAGE HAS BEEN FOUND.
1954 * busy, so that we can play with it;
1955 * not absent, so that nobody else will fill it;
1956 * possibly eligible for pageout;
1958 * The top-level page (first_m) is:
1959 * VM_PAGE_NULL if the page was found in the
1961 * busy, not absent, and ineligible for pageout.
1963 * The current object (object) is locked. A paging
1964 * reference is held for the current and top-level
1969 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1971 #if EXTRA_ASSERTIONS
1972 assert(m
->busy
&& !m
->absent
);
1973 assert((first_m
== VM_PAGE_NULL
) ||
1974 (first_m
->busy
&& !first_m
->absent
&&
1975 !first_m
->active
&& !first_m
->inactive
));
1976 #endif /* EXTRA_ASSERTIONS */
1980 * If we found a page, we must have decrypted it before we
1983 ASSERT_PAGE_DECRYPTED(m
);
1986 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1988 first_object
, first_m
);
1991 * If the page is being written, but isn't
1992 * already owned by the top-level object,
1993 * we have to copy it into a new page owned
1994 * by the top-level object.
1996 if (object
!= first_object
) {
1999 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2001 if (fault_type
& VM_PROT_WRITE
) {
2005 * We only really need to copy if we
2008 assert(!must_be_resident
);
2011 * are we protecting the system from
2012 * backing store exhaustion. If so
2013 * sleep unless we are privileged.
2015 if (vm_backing_store_low
) {
2016 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
2019 vm_fault_cleanup(object
, first_m
);
2021 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
2023 thread_block(THREAD_CONTINUE_NULL
);
2024 thread_interrupt_level(interruptible_state
);
2026 return (VM_FAULT_RETRY
);
2030 * If we try to collapse first_object at this
2031 * point, we may deadlock when we try to get
2032 * the lock on an intermediate object (since we
2033 * have the bottom object locked). We can't
2034 * unlock the bottom object, because the page
2035 * we found may move (by collapse) if we do.
2037 * Instead, we first copy the page. Then, when
2038 * we have no more use for the bottom object,
2039 * we unlock it and try to collapse.
2041 * Note that we copy the page even if we didn't
2042 * need to... that's the breaks.
2046 * Allocate a page for the copy
2048 copy_m
= vm_page_grab();
2050 if (copy_m
== VM_PAGE_NULL
) {
2053 vm_fault_cleanup(object
, first_m
);
2054 thread_interrupt_level(interruptible_state
);
2056 return (VM_FAULT_MEMORY_SHORTAGE
);
2059 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
2063 vm_page_copy(m
, copy_m
);
2066 * If another map is truly sharing this
2067 * page with us, we have to flush all
2068 * uses of the original page, since we
2069 * can't distinguish those which want the
2070 * original from those which need the
2073 * XXXO If we know that only one map has
2074 * access to this page, then we could
2075 * avoid the pmap_disconnect() call.
2078 pmap_disconnect(m
->phys_page
);
2081 VM_PAGE_COUNT_AS_PAGEIN(m
);
2082 VM_PAGE_CONSUME_CLUSTERED(m
);
2084 assert(!m
->cleaning
);
2087 * We no longer need the old page or object.
2091 vm_object_paging_end(object
);
2092 vm_object_unlock(object
);
2094 my_fault
= DBG_COW_FAULT
;
2095 VM_STAT_INCR(cow_faults
);
2096 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2097 current_task()->cow_faults
++;
2099 object
= first_object
;
2100 offset
= first_offset
;
2102 vm_object_lock(object
);
2104 * get rid of the place holder
2105 * page that we soldered in earlier
2107 VM_PAGE_FREE(first_m
);
2108 first_m
= VM_PAGE_NULL
;
2111 * and replace it with the
2112 * page we just copied into
2114 assert(copy_m
->busy
);
2115 vm_page_insert(copy_m
, object
, offset
);
2116 SET_PAGE_DIRTY(copy_m
, TRUE
);
2120 * Now that we've gotten the copy out of the
2121 * way, let's try to collapse the top object.
2122 * But we have to play ugly games with
2123 * paging_in_progress to do that...
2125 vm_object_paging_end(object
);
2126 vm_object_collapse(object
, offset
, TRUE
);
2127 vm_object_paging_begin(object
);
2130 *protection
&= (~VM_PROT_WRITE
);
2133 * Now check whether the page needs to be pushed into the
2134 * copy object. The use of asymmetric copy on write for
2135 * shared temporary objects means that we may do two copies to
2136 * satisfy the fault; one above to get the page from a
2137 * shadowed object, and one here to push it into the copy.
2139 try_failed_count
= 0;
2141 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
2142 vm_object_offset_t copy_offset
;
2146 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2149 * If the page is being written, but hasn't been
2150 * copied to the copy-object, we have to copy it there.
2152 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2153 *protection
&= ~VM_PROT_WRITE
;
2158 * If the page was guaranteed to be resident,
2159 * we must have already performed the copy.
2161 if (must_be_resident
)
2165 * Try to get the lock on the copy_object.
2167 if (!vm_object_lock_try(copy_object
)) {
2169 vm_object_unlock(object
);
2172 mutex_pause(try_failed_count
); /* wait a bit */
2173 vm_object_lock(object
);
2177 try_failed_count
= 0;
2180 * Make another reference to the copy-object,
2181 * to keep it from disappearing during the
2184 vm_object_reference_locked(copy_object
);
2187 * Does the page exist in the copy?
2189 copy_offset
= first_offset
- copy_object
->vo_shadow_offset
;
2191 if (copy_object
->vo_size
<= copy_offset
)
2193 * Copy object doesn't cover this page -- do nothing.
2196 else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
2198 * Page currently exists in the copy object
2202 * If the page is being brought
2203 * in, wait for it and then retry.
2208 * take an extra ref so object won't die
2210 vm_object_reference_locked(copy_object
);
2211 vm_object_unlock(copy_object
);
2212 vm_fault_cleanup(object
, first_m
);
2213 counter(c_vm_fault_page_block_backoff_kernel
++);
2215 vm_object_lock(copy_object
);
2216 assert(copy_object
->ref_count
> 0);
2217 VM_OBJ_RES_DECR(copy_object
);
2218 vm_object_lock_assert_exclusive(copy_object
);
2219 copy_object
->ref_count
--;
2220 assert(copy_object
->ref_count
> 0);
2221 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
2224 * it's OK if the "copy_m" page is encrypted,
2225 * because we're not moving it nor handling its
2228 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
2229 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
2231 vm_object_unlock(copy_object
);
2232 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2233 vm_object_deallocate(copy_object
);
2237 vm_object_unlock(copy_object
);
2238 vm_object_deallocate(copy_object
);
2239 thread_interrupt_level(interruptible_state
);
2241 return (VM_FAULT_RETRY
);
2245 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
2247 * If PAGED_OUT is TRUE, then the page used to exist
2248 * in the copy-object, and has already been paged out.
2249 * We don't need to repeat this. If PAGED_OUT is
2250 * FALSE, then either we don't know (!pager_created,
2251 * for example) or it hasn't been paged out.
2252 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2253 * We must copy the page to the copy object.
2256 if (vm_backing_store_low
) {
2258 * we are protecting the system from
2259 * backing store exhaustion. If so
2260 * sleep unless we are privileged.
2262 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
2263 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
2266 VM_OBJ_RES_DECR(copy_object
);
2267 vm_object_lock_assert_exclusive(copy_object
);
2268 copy_object
->ref_count
--;
2269 assert(copy_object
->ref_count
> 0);
2271 vm_object_unlock(copy_object
);
2272 vm_fault_cleanup(object
, first_m
);
2273 thread_block(THREAD_CONTINUE_NULL
);
2274 thread_interrupt_level(interruptible_state
);
2276 return (VM_FAULT_RETRY
);
2280 * Allocate a page for the copy
2282 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
2284 if (copy_m
== VM_PAGE_NULL
) {
2287 VM_OBJ_RES_DECR(copy_object
);
2288 vm_object_lock_assert_exclusive(copy_object
);
2289 copy_object
->ref_count
--;
2290 assert(copy_object
->ref_count
> 0);
2292 vm_object_unlock(copy_object
);
2293 vm_fault_cleanup(object
, first_m
);
2294 thread_interrupt_level(interruptible_state
);
2296 return (VM_FAULT_MEMORY_SHORTAGE
);
2299 * Must copy page into copy-object.
2301 vm_page_copy(m
, copy_m
);
2304 * If the old page was in use by any users
2305 * of the copy-object, it must be removed
2306 * from all pmaps. (We can't know which
2310 pmap_disconnect(m
->phys_page
);
2313 VM_PAGE_COUNT_AS_PAGEIN(m
);
2314 VM_PAGE_CONSUME_CLUSTERED(m
);
2317 * If there's a pager, then immediately
2318 * page out this page, using the "initialize"
2319 * option. Else, we use the copy.
2321 if ((!copy_object
->pager_ready
)
2323 || vm_external_state_get(copy_object
->existence_map
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2325 || VM_COMPRESSOR_PAGER_STATE_GET(copy_object
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2328 vm_page_lockspin_queues();
2329 assert(!m
->cleaning
);
2330 vm_page_activate(copy_m
);
2331 vm_page_unlock_queues();
2333 SET_PAGE_DIRTY(copy_m
, TRUE
);
2334 PAGE_WAKEUP_DONE(copy_m
);
2336 } else if (copy_object
->internal
&&
2337 (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
)) {
2339 * For internal objects check with the pager to see
2340 * if the page already exists in the backing store.
2341 * If yes, then we can drop the copy page. If not,
2342 * then we'll activate it, mark it dirty and keep it
2346 kern_return_t kr
= KERN_SUCCESS
;
2348 memory_object_t copy_pager
= copy_object
->pager
;
2349 assert(copy_pager
!= MEMORY_OBJECT_NULL
);
2350 vm_object_paging_begin(copy_object
);
2352 vm_object_unlock(copy_object
);
2354 kr
= memory_object_data_request(
2356 copy_offset
+ copy_object
->paging_offset
,
2357 0, /* Only query the pager. */
2361 vm_object_lock(copy_object
);
2363 vm_object_paging_end(copy_object
);
2366 * Since we dropped the copy_object's lock,
2367 * check whether we'll have to deallocate
2370 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2371 vm_object_unlock(copy_object
);
2372 vm_object_deallocate(copy_object
);
2373 vm_object_lock(object
);
2377 if (kr
== KERN_SUCCESS
) {
2379 * The pager has the page. We don't want to overwrite
2380 * that page by sending this one out to the backing store.
2381 * So we drop the copy page.
2383 VM_PAGE_FREE(copy_m
);
2387 * The pager doesn't have the page. We'll keep this one
2388 * around in the copy object. It might get sent out to
2389 * the backing store under memory pressure.
2391 vm_page_lockspin_queues();
2392 assert(!m
->cleaning
);
2393 vm_page_activate(copy_m
);
2394 vm_page_unlock_queues();
2396 SET_PAGE_DIRTY(copy_m
, TRUE
);
2397 PAGE_WAKEUP_DONE(copy_m
);
2401 assert(copy_m
->busy
== TRUE
);
2402 assert(!m
->cleaning
);
2405 * dirty is protected by the object lock
2407 SET_PAGE_DIRTY(copy_m
, TRUE
);
2410 * The page is already ready for pageout:
2411 * not on pageout queues and busy.
2412 * Unlock everything except the
2413 * copy_object itself.
2415 vm_object_unlock(object
);
2418 * Write the page to the copy-object,
2419 * flushing it from the kernel.
2421 vm_pageout_initialize_page(copy_m
);
2424 * Since the pageout may have
2425 * temporarily dropped the
2426 * copy_object's lock, we
2427 * check whether we'll have
2428 * to deallocate the hard way.
2430 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2431 vm_object_unlock(copy_object
);
2432 vm_object_deallocate(copy_object
);
2433 vm_object_lock(object
);
2438 * Pick back up the old object's
2439 * lock. [It is safe to do so,
2440 * since it must be deeper in the
2443 vm_object_lock(object
);
2447 * Because we're pushing a page upward
2448 * in the object tree, we must restart
2449 * any faults that are waiting here.
2450 * [Note that this is an expansion of
2451 * PAGE_WAKEUP that uses the THREAD_RESTART
2452 * wait result]. Can't turn off the page's
2453 * busy bit because we're not done with it.
2457 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2461 * The reference count on copy_object must be
2462 * at least 2: one for our extra reference,
2463 * and at least one from the outside world
2464 * (we checked that when we last locked
2467 vm_object_lock_assert_exclusive(copy_object
);
2468 copy_object
->ref_count
--;
2469 assert(copy_object
->ref_count
> 0);
2471 VM_OBJ_RES_DECR(copy_object
);
2472 vm_object_unlock(copy_object
);
2479 *top_page
= first_m
;
2482 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2483 object
, offset
, m
, first_m
, 0);
2485 if (m
!= VM_PAGE_NULL
) {
2486 retval
= VM_FAULT_SUCCESS
;
2488 if (my_fault
== DBG_PAGEIN_FAULT
) {
2490 VM_PAGE_COUNT_AS_PAGEIN(m
);
2492 if (m
->object
->internal
)
2493 my_fault
= DBG_PAGEIND_FAULT
;
2495 my_fault
= DBG_PAGEINV_FAULT
;
2498 * evaluate access pattern and update state
2499 * vm_fault_deactivate_behind depends on the
2500 * state being up to date
2502 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2504 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2505 } else if (my_fault
== DBG_COMPRESSOR_FAULT
|| my_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
) {
2507 VM_STAT_INCR(decompressions
);
2510 *type_of_fault
= my_fault
;
2512 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2513 assert(first_m
== VM_PAGE_NULL
);
2514 assert(object
== first_object
);
2517 thread_interrupt_level(interruptible_state
);
2520 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2525 thread_interrupt_level(interruptible_state
);
2527 if (wait_result
== THREAD_INTERRUPTED
)
2528 return (VM_FAULT_INTERRUPTED
);
2529 return (VM_FAULT_RETRY
);
2538 * When soft faulting a page, we have to validate the page if:
2539 * 1. the page is being mapped in user space
2540 * 2. the page hasn't already been found to be "tainted"
2541 * 3. the page belongs to a code-signed object
2542 * 4. the page has not been validated yet or has been mapped for write.
2544 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page) \
2545 ((pmap) != kernel_pmap /*1*/ && \
2546 !(page)->cs_tainted /*2*/ && \
2547 (page)->object->code_signed /*3*/ && \
2548 (!(page)->cs_validated || (page)->wpmapped /*4*/))
2552 * page queue lock must NOT be held
2553 * m->object must be locked
2555 * NOTE: m->object could be locked "shared" only if we are called
2556 * from vm_fault() as part of a soft fault. If so, we must be
2557 * careful not to modify the VM object in any way that is not
2558 * legal under a shared lock...
2560 extern int proc_selfpid(void);
2561 extern char *proc_name_address(void *p
);
2562 unsigned long cs_enter_tainted_rejected
= 0;
2563 unsigned long cs_enter_tainted_accepted
= 0;
2565 vm_fault_enter(vm_page_t m
,
2567 vm_map_offset_t vaddr
,
2569 vm_prot_t fault_type
,
2571 boolean_t change_wiring
,
2573 boolean_t cs_bypass
,
2574 __unused
int user_tag
,
2576 boolean_t
*need_retry
,
2579 kern_return_t kr
, pe_result
;
2580 boolean_t previously_pmapped
= m
->pmapped
;
2581 boolean_t must_disconnect
= 0;
2582 boolean_t map_is_switched
, map_is_switch_protected
;
2583 int cs_enforcement_enabled
;
2585 vm_object_lock_assert_held(m
->object
);
2587 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2590 if (m
->phys_page
== vm_page_guard_addr
) {
2591 assert(m
->fictitious
);
2592 return KERN_SUCCESS
;
2595 if (*type_of_fault
== DBG_ZERO_FILL_FAULT
) {
2597 vm_object_lock_assert_exclusive(m
->object
);
2599 } else if ((fault_type
& VM_PROT_WRITE
) == 0) {
2601 * This is not a "write" fault, so we
2602 * might not have taken the object lock
2603 * exclusively and we might not be able
2604 * to update the "wpmapped" bit in
2606 * Let's just grant read access to
2607 * the page for now and we'll
2608 * soft-fault again if we need write
2611 prot
&= ~VM_PROT_WRITE
;
2613 if (m
->pmapped
== FALSE
) {
2616 if (*type_of_fault
== DBG_CACHE_HIT_FAULT
) {
2618 * found it in the cache, but this
2619 * is the first fault-in of the page (m->pmapped == FALSE)
2620 * so it must have come in as part of
2621 * a cluster... account 1 pagein against it
2623 if (m
->object
->internal
)
2624 *type_of_fault
= DBG_PAGEIND_FAULT
;
2626 *type_of_fault
= DBG_PAGEINV_FAULT
;
2628 VM_PAGE_COUNT_AS_PAGEIN(m
);
2630 VM_PAGE_CONSUME_CLUSTERED(m
);
2634 if (*type_of_fault
!= DBG_COW_FAULT
) {
2635 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2637 if (pmap
== kernel_pmap
) {
2638 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2642 /* Validate code signature if necessary. */
2643 if (VM_FAULT_NEED_CS_VALIDATION(pmap
, m
)) {
2644 vm_object_lock_assert_exclusive(m
->object
);
2646 if (m
->cs_validated
) {
2647 vm_cs_revalidates
++;
2650 /* VM map is locked, so 1 ref will remain on VM object -
2651 * so no harm if vm_page_validate_cs drops the object lock */
2652 vm_page_validate_cs(m
);
2655 #define page_immutable(m,prot) ((m)->cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/)
2657 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2658 (pmap
== vm_map_pmap(current_thread()->map
)));
2659 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2661 /* If the map is switched, and is switch-protected, we must protect
2662 * some pages from being write-faulted: immutable pages because by
2663 * definition they may not be written, and executable pages because that
2664 * would provide a way to inject unsigned code.
2665 * If the page is immutable, we can simply return. However, we can't
2666 * immediately determine whether a page is executable anywhere. But,
2667 * we can disconnect it everywhere and remove the executable protection
2668 * from the current map. We do that below right before we do the
2671 cs_enforcement_enabled
= cs_enforcement(NULL
);
2673 if(cs_enforcement_enabled
&& map_is_switched
&&
2674 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2675 (prot
& VM_PROT_WRITE
))
2677 return KERN_CODESIGN_ERROR
;
2680 /* A page could be tainted, or pose a risk of being tainted later.
2681 * Check whether the receiving process wants it, and make it feel
2682 * the consequences (that hapens in cs_invalid_page()).
2683 * For CS Enforcement, two other conditions will
2684 * cause that page to be tainted as well:
2685 * - pmapping an unsigned page executable - this means unsigned code;
2686 * - writeable mapping of a validated page - the content of that page
2687 * can be changed without the kernel noticing, therefore unsigned
2688 * code can be created
2690 if (m
->cs_tainted
||
2691 ((cs_enforcement_enabled
&& !cs_bypass
) &&
2692 (/* The page is unsigned and wants to be executable */
2693 (!m
->cs_validated
&& (prot
& VM_PROT_EXECUTE
)) ||
2694 /* The page should be immutable, but is in danger of being modified
2695 * This is the case where we want policy from the code directory -
2696 * is the page immutable or not? For now we have to assume that
2697 * code pages will be immutable, data pages not.
2698 * We'll assume a page is a code page if it has a code directory
2699 * and we fault for execution.
2700 * That is good enough since if we faulted the code page for
2701 * writing in another map before, it is wpmapped; if we fault
2702 * it for writing in this map later it will also be faulted for executing
2703 * at the same time; and if we fault for writing in another map
2704 * later, we will disconnect it from this pmap so we'll notice
2707 (page_immutable(m
, prot
) && ((prot
& VM_PROT_WRITE
) || m
->wpmapped
))
2711 /* We will have a tainted page. Have to handle the special case
2712 * of a switched map now. If the map is not switched, standard
2713 * procedure applies - call cs_invalid_page().
2714 * If the map is switched, the real owner is invalid already.
2715 * There is no point in invalidating the switching process since
2716 * it will not be executing from the map. So we don't call
2717 * cs_invalid_page() in that case. */
2718 boolean_t reject_page
;
2719 if(map_is_switched
) {
2720 assert(pmap
==vm_map_pmap(current_thread()->map
));
2721 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2722 reject_page
= FALSE
;
2725 printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s slid: %s prot: 0x%x\n",
2726 m
->object
->code_signed
? "yes" : "no",
2727 m
->cs_validated
? "yes" : "no",
2728 m
->cs_tainted
? "yes" : "no",
2729 m
->wpmapped
? "yes" : "no",
2730 m
->slid
? "yes" : "no",
2732 reject_page
= cs_invalid_page((addr64_t
) vaddr
);
2736 /* reject the invalid page: abort the page fault */
2738 const char *procname
;
2740 vm_object_t file_object
, shadow
;
2741 vm_object_offset_t file_offset
;
2742 char *pathname
, *filename
;
2743 vm_size_t pathname_len
, filename_len
;
2744 boolean_t truncated_path
;
2745 #define __PATH_MAX 1024
2746 struct timespec mtime
, cs_mtime
;
2748 kr
= KERN_CODESIGN_ERROR
;
2749 cs_enter_tainted_rejected
++;
2751 /* get process name and pid */
2753 task
= current_task();
2754 pid
= proc_selfpid();
2755 if (task
->bsd_info
!= NULL
)
2756 procname
= proc_name_address(task
->bsd_info
);
2758 /* get file's VM object */
2759 file_object
= m
->object
;
2760 file_offset
= m
->offset
;
2761 for (shadow
= file_object
->shadow
;
2762 shadow
!= VM_OBJECT_NULL
;
2763 shadow
= file_object
->shadow
) {
2764 vm_object_lock_shared(shadow
);
2765 if (file_object
!= m
->object
) {
2766 vm_object_unlock(file_object
);
2768 file_offset
+= file_object
->vo_shadow_offset
;
2769 file_object
= shadow
;
2774 cs_mtime
.tv_sec
= 0;
2775 cs_mtime
.tv_nsec
= 0;
2777 /* get file's pathname and/or filename */
2782 truncated_path
= FALSE
;
2783 if (file_object
->pager
== NULL
) {
2784 /* no pager -> no file -> no pathname */
2785 pathname
= (char *) "<nil>";
2787 pathname
= (char *)kalloc(__PATH_MAX
* 2);
2790 pathname_len
= __PATH_MAX
;
2791 filename
= pathname
+ pathname_len
;
2792 filename_len
= __PATH_MAX
;
2794 vnode_pager_get_object_name(file_object
->pager
,
2800 vnode_pager_get_object_mtime(file_object
->pager
,
2804 printf("CODE SIGNING: process %d[%s]: "
2805 "rejecting invalid page at address 0x%llx "
2806 "from offset 0x%llx in file \"%s%s%s\" "
2807 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2808 "(signed:%d validated:%d tainted:%d "
2809 "wpmapped:%d slid:%d)\n",
2810 pid
, procname
, (addr64_t
) vaddr
,
2812 (pathname
? pathname
: ""),
2813 (truncated_path
? "/.../" : ""),
2814 (truncated_path
? filename
: ""),
2815 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2816 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2817 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2820 mtime
.tv_sec
, mtime
.tv_nsec
,
2821 m
->object
->code_signed
,
2826 if (file_object
!= m
->object
) {
2827 vm_object_unlock(file_object
);
2829 if (pathname_len
!= 0) {
2830 kfree(pathname
, __PATH_MAX
* 2);
2835 /* proceed with the invalid page */
2837 if (!m
->cs_validated
) {
2839 * This page has not been validated, so it
2840 * must not belong to a code-signed object
2841 * and should not be forcefully considered
2843 * We're just concerned about it here because
2844 * we've been asked to "execute" it but that
2845 * does not mean that it should cause other
2847 * This happens when a debugger sets a
2848 * breakpoint and we then execute code in
2849 * that page. Marking the page as "tainted"
2850 * would cause any inspection tool ("leaks",
2851 * "vmmap", "CrashReporter", ...) to get killed
2852 * due to code-signing violation on that page,
2853 * even though they're just reading it and not
2854 * executing from it.
2856 assert(!m
->object
->code_signed
);
2859 * Page might have been tainted before or not;
2860 * now it definitively is. If the page wasn't
2861 * tainted, we must disconnect it from all
2862 * pmaps later, to force existing mappings
2863 * through that code path for re-consideration
2864 * of the validity of that page.
2866 must_disconnect
= !m
->cs_tainted
;
2867 m
->cs_tainted
= TRUE
;
2869 cs_enter_tainted_accepted
++;
2871 if (kr
!= KERN_SUCCESS
) {
2873 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2874 "*** INVALID PAGE ***\n",
2878 if (cs_enforcement_panic
) {
2879 panic("CODESIGNING: panicking on invalid page\n");
2885 /* proceed with the valid page */
2889 boolean_t page_queues_locked
= FALSE
;
2890 #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \
2892 if (! page_queues_locked) { \
2893 page_queues_locked = TRUE; \
2894 vm_page_lockspin_queues(); \
2897 #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \
2899 if (page_queues_locked) { \
2900 page_queues_locked = FALSE; \
2901 vm_page_unlock_queues(); \
2906 * Hold queues lock to manipulate
2907 * the page queues. Change wiring
2910 assert(m
->compressor
|| m
->object
!= compressor_object
);
2911 if (m
->compressor
) {
2913 * Compressor pages are neither wired
2914 * nor pageable and should never change.
2916 assert(m
->object
== compressor_object
);
2917 } else if (change_wiring
) {
2918 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
2921 if (kr
== KERN_SUCCESS
) {
2925 vm_page_unwire(m
, TRUE
);
2927 /* we keep the page queues lock, if we need it later */
2930 if (kr
!= KERN_SUCCESS
) {
2931 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
2932 vm_page_deactivate(m
);
2933 /* we keep the page queues lock, if we need it later */
2934 } else if (((!m
->active
&& !m
->inactive
) ||
2937 !VM_PAGE_WIRED(m
) && !m
->throttled
) {
2939 if (vm_page_local_q
&&
2941 (*type_of_fault
== DBG_COW_FAULT
||
2942 *type_of_fault
== DBG_ZERO_FILL_FAULT
) ) {
2946 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
2947 vm_object_lock_assert_exclusive(m
->object
);
2950 * we got a local queue to stuff this
2952 * its safe to manipulate local and
2953 * local_id at this point since we're
2954 * behind an exclusive object lock and
2955 * the page is not on any global queue.
2957 * we'll use the current cpu number to
2958 * select the queue note that we don't
2959 * need to disable preemption... we're
2960 * going to behind the local queue's
2961 * lock to do the real work
2965 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
2967 VPL_LOCK(&lq
->vpl_lock
);
2969 queue_enter(&lq
->vpl_queue
, m
,
2975 if (m
->object
->internal
)
2976 lq
->vpl_internal_count
++;
2978 lq
->vpl_external_count
++;
2980 VPL_UNLOCK(&lq
->vpl_lock
);
2982 if (lq
->vpl_count
> vm_page_local_q_soft_limit
)
2985 * we're beyond the soft limit
2986 * for the local queue
2987 * vm_page_reactivate_local will
2988 * 'try' to take the global page
2989 * queue lock... if it can't
2990 * that's ok... we'll let the
2991 * queue continue to grow up
2992 * to the hard limit... at that
2993 * point we'll wait for the
2994 * lock... once we've got the
2995 * lock, we'll transfer all of
2996 * the pages from the local
2997 * queue to the global active
3000 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
3004 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3007 * test again now that we hold the
3010 if (!VM_PAGE_WIRED(m
)) {
3011 if (m
->clean_queue
) {
3012 VM_PAGE_QUEUES_REMOVE(m
);
3014 vm_pageout_cleaned_reactivated
++;
3015 vm_pageout_cleaned_fault_reactivated
++;
3022 * If this is a no_cache mapping
3023 * and the page has never been
3024 * mapped before or was
3025 * previously a no_cache page,
3026 * then we want to leave pages
3027 * in the speculative state so
3028 * that they can be readily
3029 * recycled if free memory runs
3030 * low. Otherwise the page is
3031 * activated as normal.
3035 (!previously_pmapped
||
3039 if (!m
->speculative
)
3040 vm_page_speculate(m
, FALSE
);
3042 } else if (!m
->active
&&
3045 vm_page_activate(m
);
3049 /* we keep the page queues lock, if we need it later */
3053 /* we're done with the page queues lock, if we ever took it */
3054 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3057 /* If we have a KERN_SUCCESS from the previous checks, we either have
3058 * a good page, or a tainted page that has been accepted by the process.
3059 * In both cases the page will be entered into the pmap.
3060 * If the page is writeable, we need to disconnect it from other pmaps
3061 * now so those processes can take note.
3063 if (kr
== KERN_SUCCESS
) {
3066 * NOTE: we may only hold the vm_object lock SHARED
3067 * at this point, so we need the phys_page lock to
3068 * properly serialize updating the pmapped and
3071 if ((prot
& VM_PROT_EXECUTE
) && !m
->xpmapped
) {
3073 pmap_lock_phys_page(m
->phys_page
);
3075 * go ahead and take the opportunity
3076 * to set 'pmapped' here so that we don't
3077 * need to grab this lock a 2nd time
3086 pmap_unlock_phys_page(m
->phys_page
);
3088 if (!m
->object
->internal
)
3089 OSAddAtomic(1, &vm_page_xpmapped_external_count
);
3091 if ((COMPRESSED_PAGER_IS_ACTIVE
) &&
3092 m
->object
->internal
&&
3093 m
->object
->pager
!= NULL
) {
3095 * This page could have been
3096 * uncompressed by the
3097 * compressor pager and its
3098 * contents might be only in
3100 * Since it's being mapped for
3101 * "execute" for the fist time,
3102 * make sure the icache is in
3105 pmap_sync_page_data_phys(m
->phys_page
);
3108 pmap_unlock_phys_page(m
->phys_page
);
3110 if (m
->pmapped
== FALSE
) {
3111 pmap_lock_phys_page(m
->phys_page
);
3113 pmap_unlock_phys_page(m
->phys_page
);
3116 if (vm_page_is_slideable(m
)) {
3117 boolean_t was_busy
= m
->busy
;
3119 vm_object_lock_assert_exclusive(m
->object
);
3122 kr
= vm_page_slide(m
, 0);
3125 PAGE_WAKEUP_DONE(m
);
3127 if (kr
!= KERN_SUCCESS
) {
3129 * This page has not been slid correctly,
3130 * do not do the pmap_enter() !
3131 * Let vm_fault_enter() return the error
3132 * so the caller can fail the fault.
3134 goto after_the_pmap_enter
;
3138 if (fault_type
& VM_PROT_WRITE
) {
3140 if (m
->wpmapped
== FALSE
) {
3141 vm_object_lock_assert_exclusive(m
->object
);
3145 if (must_disconnect
) {
3147 * We can only get here
3148 * because of the CSE logic
3150 assert(cs_enforcement_enabled
);
3151 pmap_disconnect(m
->phys_page
);
3153 * If we are faulting for a write, we can clear
3154 * the execute bit - that will ensure the page is
3155 * checked again before being executable, which
3156 * protects against a map switch.
3157 * This only happens the first time the page
3158 * gets tainted, so we won't get stuck here
3159 * to make an already writeable page executable.
3162 prot
&= ~VM_PROT_EXECUTE
;
3167 /* Prevent a deadlock by not
3168 * holding the object lock if we need to wait for a page in
3169 * pmap_enter() - <rdar://problem/7138958> */
3170 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
, 0,
3172 pmap_options
| PMAP_OPTIONS_NOWAIT
,
3175 if(pe_result
== KERN_RESOURCE_SHORTAGE
) {
3179 * this will be non-null in the case where we hold the lock
3180 * on the top-object in this chain... we can't just drop
3181 * the lock on the object we're inserting the page into
3182 * and recall the PMAP_ENTER since we can still cause
3183 * a deadlock if one of the critical paths tries to
3184 * acquire the lock on the top-object and we're blocked
3185 * in PMAP_ENTER waiting for memory... our only recourse
3186 * is to deal with it at a higher level where we can
3190 vm_pmap_enter_retried
++;
3191 goto after_the_pmap_enter
;
3193 /* The nonblocking version of pmap_enter did not succeed.
3194 * and we don't need to drop other locks and retry
3195 * at the level above us, so
3196 * use the blocking version instead. Requires marking
3197 * the page busy and unlocking the object */
3198 boolean_t was_busy
= m
->busy
;
3200 vm_object_lock_assert_exclusive(m
->object
);
3203 vm_object_unlock(m
->object
);
3205 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
,
3207 pmap_options
, pe_result
);
3209 /* Take the object lock again. */
3210 vm_object_lock(m
->object
);
3212 /* If the page was busy, someone else will wake it up.
3213 * Otherwise, we have to do it now. */
3216 PAGE_WAKEUP_DONE(m
);
3218 vm_pmap_enter_blocked
++;
3222 after_the_pmap_enter
:
3227 vm_pre_fault(vm_map_offset_t vaddr
)
3229 if (pmap_find_phys(current_map()->pmap
, vaddr
) == 0) {
3231 vm_fault(current_map(), /* map */
3233 VM_PROT_READ
, /* fault_type */
3234 FALSE
, /* change_wiring */
3235 THREAD_UNINT
, /* interruptible */
3236 NULL
, /* caller_pmap */
3237 0 /* caller_pmap_addr */);
3245 * Handle page faults, including pseudo-faults
3246 * used to change the wiring status of pages.
3248 * Explicit continuations have been removed.
3250 * vm_fault and vm_fault_page save mucho state
3251 * in the moral equivalent of a closure. The state
3252 * structure is allocated when first entering vm_fault
3253 * and deallocated when leaving vm_fault.
3256 extern int _map_enter_debug
;
3258 unsigned long vm_fault_collapse_total
= 0;
3259 unsigned long vm_fault_collapse_skipped
= 0;
3265 vm_map_offset_t vaddr
,
3266 vm_prot_t fault_type
,
3267 boolean_t change_wiring
,
3270 vm_map_offset_t caller_pmap_addr
)
3272 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
,
3273 interruptible
, caller_pmap
, caller_pmap_addr
,
3280 vm_map_offset_t vaddr
,
3281 vm_prot_t fault_type
,
3282 boolean_t change_wiring
,
3285 vm_map_offset_t caller_pmap_addr
,
3286 ppnum_t
*physpage_p
)
3288 vm_map_version_t version
; /* Map version for verificiation */
3289 boolean_t wired
; /* Should mapping be wired down? */
3290 vm_object_t object
; /* Top-level object */
3291 vm_object_offset_t offset
; /* Top-level offset */
3292 vm_prot_t prot
; /* Protection for mapping */
3293 vm_object_t old_copy_object
; /* Saved copy object */
3294 vm_page_t result_page
; /* Result of vm_fault_page */
3295 vm_page_t top_page
; /* Placeholder page */
3298 vm_page_t m
; /* Fast access to result_page */
3299 kern_return_t error_code
;
3300 vm_object_t cur_object
;
3301 vm_object_offset_t cur_offset
;
3303 vm_object_t new_object
;
3306 boolean_t interruptible_state
;
3307 vm_map_t real_map
= map
;
3308 vm_map_t original_map
= map
;
3309 vm_prot_t original_fault_type
;
3310 struct vm_object_fault_info fault_info
;
3311 boolean_t need_collapse
= FALSE
;
3312 boolean_t need_retry
= FALSE
;
3313 boolean_t
*need_retry_ptr
= NULL
;
3314 int object_lock_type
= 0;
3315 int cur_object_lock_type
;
3316 vm_object_t top_object
= VM_OBJECT_NULL
;
3318 int compressed_count_delta
;
3321 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3322 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
3323 ((uint64_t)vaddr
>> 32),
3325 (map
== kernel_map
),
3329 if (get_preemption_level() != 0) {
3330 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3331 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3332 ((uint64_t)vaddr
>> 32),
3338 return (KERN_FAILURE
);
3341 interruptible_state
= thread_interrupt_level(interruptible
);
3343 VM_STAT_INCR(faults
);
3344 current_task()->faults
++;
3345 original_fault_type
= fault_type
;
3347 if (fault_type
& VM_PROT_WRITE
)
3348 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3350 object_lock_type
= OBJECT_LOCK_SHARED
;
3352 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
3356 * assume we will hit a page in the cache
3357 * otherwise, explicitly override with
3358 * the real fault type once we determine it
3360 type_of_fault
= DBG_CACHE_HIT_FAULT
;
3363 * Find the backing store object and offset into
3364 * it to begin the search.
3366 fault_type
= original_fault_type
;
3368 vm_map_lock_read(map
);
3370 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
3371 object_lock_type
, &version
,
3372 &object
, &offset
, &prot
, &wired
,
3376 if (kr
!= KERN_SUCCESS
) {
3377 vm_map_unlock_read(map
);
3380 pmap
= real_map
->pmap
;
3381 fault_info
.interruptible
= interruptible
;
3382 fault_info
.stealth
= FALSE
;
3383 fault_info
.io_sync
= FALSE
;
3384 fault_info
.mark_zf_absent
= FALSE
;
3385 fault_info
.batch_pmap_op
= FALSE
;
3388 * If the page is wired, we must fault for the current protection
3389 * value, to avoid further faults.
3392 fault_type
= prot
| VM_PROT_WRITE
;
3394 * since we're treating this fault as a 'write'
3395 * we must hold the top object lock exclusively
3397 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3399 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3401 if (vm_object_lock_upgrade(object
) == FALSE
) {
3403 * couldn't upgrade, so explictly
3404 * take the lock exclusively
3406 vm_object_lock(object
);
3411 #if VM_FAULT_CLASSIFY
3413 * Temporary data gathering code
3415 vm_fault_classify(object
, offset
, fault_type
);
3418 * Fast fault code. The basic idea is to do as much as
3419 * possible while holding the map lock and object locks.
3420 * Busy pages are not used until the object lock has to
3421 * be dropped to do something (copy, zero fill, pmap enter).
3422 * Similarly, paging references aren't acquired until that
3423 * point, and object references aren't used.
3425 * If we can figure out what to do
3426 * (zero fill, copy on write, pmap enter) while holding
3427 * the locks, then it gets done. Otherwise, we give up,
3428 * and use the original fault path (which doesn't hold
3429 * the map lock, and relies on busy pages).
3430 * The give up cases include:
3431 * - Have to talk to pager.
3432 * - Page is busy, absent or in error.
3433 * - Pager has locked out desired access.
3434 * - Fault needs to be restarted.
3435 * - Have to push page into copy object.
3437 * The code is an infinite loop that moves one level down
3438 * the shadow chain each time. cur_object and cur_offset
3439 * refer to the current object being examined. object and offset
3440 * are the original object from the map. The loop is at the
3441 * top level if and only if object and cur_object are the same.
3443 * Invariants: Map lock is held throughout. Lock is held on
3444 * original object and cur_object (if different) when
3445 * continuing or exiting loop.
3451 * If this page is to be inserted in a copy delay object
3452 * for writing, and if the object has a copy, then the
3453 * copy delay strategy is implemented in the slow fault page.
3455 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
3456 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
))
3457 goto handle_copy_delay
;
3459 cur_object
= object
;
3460 cur_offset
= offset
;
3463 if (!cur_object
->pager_created
&&
3464 cur_object
->phys_contiguous
) /* superpage */
3467 if (cur_object
->blocked_access
) {
3469 * Access to this VM object has been blocked.
3470 * Let the slow path handle it.
3475 m
= vm_page_lookup(cur_object
, cur_offset
);
3477 if (m
!= VM_PAGE_NULL
) {
3479 wait_result_t result
;
3482 * in order to do the PAGE_ASSERT_WAIT, we must
3483 * have object that 'm' belongs to locked exclusively
3485 if (object
!= cur_object
) {
3487 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3489 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3491 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3493 * couldn't upgrade so go do a full retry
3494 * immediately since we can no longer be
3495 * certain about cur_object (since we
3496 * don't hold a reference on it)...
3497 * first drop the top object lock
3499 vm_object_unlock(object
);
3501 vm_map_unlock_read(map
);
3502 if (real_map
!= map
)
3503 vm_map_unlock(real_map
);
3508 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3510 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3512 if (vm_object_lock_upgrade(object
) == FALSE
) {
3514 * couldn't upgrade, so explictly take the lock
3515 * exclusively and go relookup the page since we
3516 * will have dropped the object lock and
3517 * a different thread could have inserted
3518 * a page at this offset
3519 * no need for a full retry since we're
3520 * at the top level of the object chain
3522 vm_object_lock(object
);
3527 if (m
->pageout_queue
&& m
->object
->internal
&& COMPRESSED_PAGER_IS_ACTIVE
) {
3529 * m->busy == TRUE and the object is locked exclusively
3530 * if m->pageout_queue == TRUE after we acquire the
3531 * queues lock, we are guaranteed that it is stable on
3532 * the pageout queue and therefore reclaimable
3534 * NOTE: this is only true for the internal pageout queue
3535 * in the compressor world
3537 vm_page_lock_queues();
3539 if (m
->pageout_queue
) {
3540 vm_pageout_throttle_up(m
);
3541 vm_page_unlock_queues();
3543 PAGE_WAKEUP_DONE(m
);
3544 goto reclaimed_from_pageout
;
3546 vm_page_unlock_queues();
3548 if (object
!= cur_object
)
3549 vm_object_unlock(object
);
3551 vm_map_unlock_read(map
);
3552 if (real_map
!= map
)
3553 vm_map_unlock(real_map
);
3555 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
3557 vm_object_unlock(cur_object
);
3559 if (result
== THREAD_WAITING
) {
3560 result
= thread_block(THREAD_CONTINUE_NULL
);
3562 counter(c_vm_fault_page_block_busy_kernel
++);
3564 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
3570 reclaimed_from_pageout
:
3572 if (object
!= cur_object
) {
3573 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3574 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3576 vm_object_unlock(object
);
3577 vm_object_unlock(cur_object
);
3579 vm_map_unlock_read(map
);
3580 if (real_map
!= map
)
3581 vm_map_unlock(real_map
);
3586 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3588 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3590 if (vm_object_lock_upgrade(object
) == FALSE
) {
3592 * couldn't upgrade, so explictly take the lock
3593 * exclusively and go relookup the page since we
3594 * will have dropped the object lock and
3595 * a different thread could have inserted
3596 * a page at this offset
3597 * no need for a full retry since we're
3598 * at the top level of the object chain
3600 vm_object_lock(object
);
3607 vm_pageout_steal_laundry(m
, FALSE
);
3610 if (m
->phys_page
== vm_page_guard_addr
) {
3612 * Guard page: let the slow path deal with it
3616 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
)) {
3618 * Unusual case... let the slow path deal with it
3622 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m
->object
)) {
3623 if (object
!= cur_object
)
3624 vm_object_unlock(object
);
3625 vm_map_unlock_read(map
);
3626 if (real_map
!= map
)
3627 vm_map_unlock(real_map
);
3628 vm_object_unlock(cur_object
);
3629 kr
= KERN_MEMORY_ERROR
;
3636 * We've soft-faulted (because it's not in the page
3637 * table) on an encrypted page.
3638 * Keep the page "busy" so that no one messes with
3639 * it during the decryption.
3640 * Release the extra locks we're holding, keep only
3641 * the page's VM object lock.
3643 * in order to set 'busy' on 'm', we must
3644 * have object that 'm' belongs to locked exclusively
3646 if (object
!= cur_object
) {
3647 vm_object_unlock(object
);
3649 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3651 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3653 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3655 * couldn't upgrade so go do a full retry
3656 * immediately since we've already dropped
3657 * the top object lock associated with this page
3658 * and the current one got dropped due to the
3659 * failed upgrade... the state is no longer valid
3661 vm_map_unlock_read(map
);
3662 if (real_map
!= map
)
3663 vm_map_unlock(real_map
);
3668 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3670 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3672 if (vm_object_lock_upgrade(object
) == FALSE
) {
3674 * couldn't upgrade, so explictly take the lock
3675 * exclusively and go relookup the page since we
3676 * will have dropped the object lock and
3677 * a different thread could have inserted
3678 * a page at this offset
3679 * no need for a full retry since we're
3680 * at the top level of the object chain
3682 vm_object_lock(object
);
3689 vm_map_unlock_read(map
);
3690 if (real_map
!= map
)
3691 vm_map_unlock(real_map
);
3693 vm_page_decrypt(m
, 0);
3696 PAGE_WAKEUP_DONE(m
);
3698 vm_object_unlock(cur_object
);
3700 * Retry from the top, in case anything
3701 * changed while we were decrypting...
3705 ASSERT_PAGE_DECRYPTED(m
);
3707 if(vm_page_is_slideable(m
)) {
3709 * We might need to slide this page, and so,
3710 * we want to hold the VM object exclusively.
3712 if (object
!= cur_object
) {
3713 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3714 vm_object_unlock(object
);
3715 vm_object_unlock(cur_object
);
3717 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3719 vm_map_unlock_read(map
);
3720 if (real_map
!= map
)
3721 vm_map_unlock(real_map
);
3725 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3727 vm_object_unlock(object
);
3728 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3729 vm_map_unlock_read(map
);
3734 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
) ||
3735 (physpage_p
!= NULL
&& (prot
& VM_PROT_WRITE
))) {
3736 upgrade_for_validation
:
3738 * We might need to validate this page
3739 * against its code signature, so we
3740 * want to hold the VM object exclusively.
3742 if (object
!= cur_object
) {
3743 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3744 vm_object_unlock(object
);
3745 vm_object_unlock(cur_object
);
3747 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3749 vm_map_unlock_read(map
);
3750 if (real_map
!= map
)
3751 vm_map_unlock(real_map
);
3756 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3758 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3760 if (vm_object_lock_upgrade(object
) == FALSE
) {
3762 * couldn't upgrade, so explictly take the lock
3763 * exclusively and go relookup the page since we
3764 * will have dropped the object lock and
3765 * a different thread could have inserted
3766 * a page at this offset
3767 * no need for a full retry since we're
3768 * at the top level of the object chain
3770 vm_object_lock(object
);
3777 * Two cases of map in faults:
3778 * - At top level w/o copy object.
3779 * - Read fault anywhere.
3780 * --> must disallow write.
3783 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
3788 if ((fault_type
& VM_PROT_WRITE
) == 0) {
3790 if (object
!= cur_object
) {
3792 * We still need to hold the top object
3793 * lock here to prevent a race between
3794 * a read fault (taking only "shared"
3795 * locks) and a write fault (taking
3796 * an "exclusive" lock on the top
3798 * Otherwise, as soon as we release the
3799 * top lock, the write fault could
3800 * proceed and actually complete before
3801 * the read fault, and the copied page's
3802 * translation could then be overwritten
3803 * by the read fault's translation for
3804 * the original page.
3806 * Let's just record what the top object
3807 * is and we'll release it later.
3809 top_object
= object
;
3812 * switch to the object that has the new page
3814 object
= cur_object
;
3815 object_lock_type
= cur_object_lock_type
;
3819 * prepare for the pmap_enter...
3820 * object and map are both locked
3821 * m contains valid data
3822 * object == m->object
3823 * cur_object == NULL or it's been unlocked
3824 * no paging references on either object or cur_object
3826 if (top_object
!= VM_OBJECT_NULL
|| object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
)
3827 need_retry_ptr
= &need_retry
;
3829 need_retry_ptr
= NULL
;
3832 kr
= vm_fault_enter(m
,
3839 fault_info
.no_cache
,
3840 fault_info
.cs_bypass
,
3841 fault_info
.user_tag
,
3842 fault_info
.pmap_options
,
3846 kr
= vm_fault_enter(m
,
3853 fault_info
.no_cache
,
3854 fault_info
.cs_bypass
,
3855 fault_info
.user_tag
,
3856 fault_info
.pmap_options
,
3861 if (kr
== KERN_SUCCESS
&&
3862 physpage_p
!= NULL
) {
3863 /* for vm_map_wire_and_extract() */
3864 *physpage_p
= m
->phys_page
;
3865 if (prot
& VM_PROT_WRITE
) {
3866 vm_object_lock_assert_exclusive(
3872 if (top_object
!= VM_OBJECT_NULL
) {
3874 * It's safe to drop the top object
3875 * now that we've done our
3876 * vm_fault_enter(). Any other fault
3877 * in progress for that virtual
3878 * address will either find our page
3879 * and translation or put in a new page
3882 vm_object_unlock(top_object
);
3883 top_object
= VM_OBJECT_NULL
;
3886 if (need_collapse
== TRUE
)
3887 vm_object_collapse(object
, offset
, TRUE
);
3889 if (need_retry
== FALSE
&&
3890 (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
)) {
3892 * evaluate access pattern and update state
3893 * vm_fault_deactivate_behind depends on the
3894 * state being up to date
3896 vm_fault_is_sequential(object
, cur_offset
, fault_info
.behavior
);
3898 vm_fault_deactivate_behind(object
, cur_offset
, fault_info
.behavior
);
3901 * That's it, clean up and return.
3904 PAGE_WAKEUP_DONE(m
);
3906 vm_object_unlock(object
);
3908 vm_map_unlock_read(map
);
3909 if (real_map
!= map
)
3910 vm_map_unlock(real_map
);
3912 if (need_retry
== TRUE
) {
3914 * vm_fault_enter couldn't complete the PMAP_ENTER...
3915 * at this point we don't hold any locks so it's safe
3916 * to ask the pmap layer to expand the page table to
3917 * accommodate this mapping... once expanded, we'll
3918 * re-drive the fault which should result in vm_fault_enter
3919 * being able to successfully enter the mapping this time around
3921 (void)pmap_enter_options(
3922 pmap
, vaddr
, 0, 0, 0, 0, 0,
3923 PMAP_OPTIONS_NOENTER
, NULL
);
3931 * COPY ON WRITE FAULT
3933 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
3936 * If objects match, then
3937 * object->copy must not be NULL (else control
3938 * would be in previous code block), and we
3939 * have a potential push into the copy object
3940 * with which we can't cope with here.
3942 if (cur_object
== object
) {
3944 * must take the slow path to
3945 * deal with the copy push
3951 * This is now a shadow based copy on write
3952 * fault -- it requires a copy up the shadow
3956 if ((cur_object_lock_type
== OBJECT_LOCK_SHARED
) &&
3957 VM_FAULT_NEED_CS_VALIDATION(NULL
, m
)) {
3958 goto upgrade_for_validation
;
3962 * Allocate a page in the original top level
3963 * object. Give up if allocate fails. Also
3964 * need to remember current page, as it's the
3965 * source of the copy.
3967 * at this point we hold locks on both
3968 * object and cur_object... no need to take
3969 * paging refs or mark pages BUSY since
3970 * we don't drop either object lock until
3971 * the page has been copied and inserted
3976 if (m
== VM_PAGE_NULL
) {
3978 * no free page currently available...
3979 * must take the slow path
3984 * Now do the copy. Mark the source page busy...
3986 * NOTE: This code holds the map lock across
3989 vm_page_copy(cur_m
, m
);
3990 vm_page_insert(m
, object
, offset
);
3991 SET_PAGE_DIRTY(m
, FALSE
);
3994 * Now cope with the source page and object
3996 if (object
->ref_count
> 1 && cur_m
->pmapped
)
3997 pmap_disconnect(cur_m
->phys_page
);
3999 if (cur_m
->clustered
) {
4000 VM_PAGE_COUNT_AS_PAGEIN(cur_m
);
4001 VM_PAGE_CONSUME_CLUSTERED(cur_m
);
4003 need_collapse
= TRUE
;
4005 if (!cur_object
->internal
&&
4006 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
4008 * The object from which we've just
4009 * copied a page is most probably backed
4010 * by a vnode. We don't want to waste too
4011 * much time trying to collapse the VM objects
4012 * and create a bottleneck when several tasks
4013 * map the same file.
4015 if (cur_object
->copy
== object
) {
4017 * Shared mapping or no COW yet.
4018 * We can never collapse a copy
4019 * object into its backing object.
4021 need_collapse
= FALSE
;
4022 } else if (cur_object
->copy
== object
->shadow
&&
4023 object
->shadow
->resident_page_count
== 0) {
4025 * Shared mapping after a COW occurred.
4027 need_collapse
= FALSE
;
4030 vm_object_unlock(cur_object
);
4032 if (need_collapse
== FALSE
)
4033 vm_fault_collapse_skipped
++;
4034 vm_fault_collapse_total
++;
4036 type_of_fault
= DBG_COW_FAULT
;
4037 VM_STAT_INCR(cow_faults
);
4038 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
4039 current_task()->cow_faults
++;
4045 * No page at cur_object, cur_offset... m == NULL
4047 if (cur_object
->pager_created
) {
4048 int compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
4050 if (MUST_ASK_PAGER(cur_object
, cur_offset
, compressor_external_state
) == TRUE
) {
4052 int c_flags
= C_DONT_BLOCK
;
4053 boolean_t insert_cur_object
= FALSE
;
4056 * May have to talk to a pager...
4057 * if so, take the slow path by
4058 * doing a 'break' from the while (TRUE) loop
4060 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
4061 * if the compressor is active and the page exists there
4063 if (compressor_external_state
!= VM_EXTERNAL_STATE_EXISTS
)
4066 if (map
== kernel_map
|| real_map
== kernel_map
) {
4068 * can't call into the compressor with the kernel_map
4069 * lock held, since the compressor may try to operate
4070 * on the kernel map in order to return an empty c_segment
4074 if (object
!= cur_object
) {
4075 if (fault_type
& VM_PROT_WRITE
)
4078 insert_cur_object
= TRUE
;
4080 if (insert_cur_object
== TRUE
) {
4082 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4084 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4086 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
4088 * couldn't upgrade so go do a full retry
4089 * immediately since we can no longer be
4090 * certain about cur_object (since we
4091 * don't hold a reference on it)...
4092 * first drop the top object lock
4094 vm_object_unlock(object
);
4096 vm_map_unlock_read(map
);
4097 if (real_map
!= map
)
4098 vm_map_unlock(real_map
);
4103 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4105 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4107 if (object
!= cur_object
) {
4109 * we can't go for the upgrade on the top
4110 * lock since the upgrade may block waiting
4111 * for readers to drain... since we hold
4112 * cur_object locked at this point, waiting
4113 * for the readers to drain would represent
4114 * a lock order inversion since the lock order
4115 * for objects is the reference order in the
4118 vm_object_unlock(object
);
4119 vm_object_unlock(cur_object
);
4121 vm_map_unlock_read(map
);
4122 if (real_map
!= map
)
4123 vm_map_unlock(real_map
);
4127 if (vm_object_lock_upgrade(object
) == FALSE
) {
4129 * couldn't upgrade, so explictly take the lock
4130 * exclusively and go relookup the page since we
4131 * will have dropped the object lock and
4132 * a different thread could have inserted
4133 * a page at this offset
4134 * no need for a full retry since we're
4135 * at the top level of the object chain
4137 vm_object_lock(object
);
4144 if (m
== VM_PAGE_NULL
) {
4146 * no free page currently available...
4147 * must take the slow path
4153 * The object is and remains locked
4154 * so no need to take a
4155 * "paging_in_progress" reference.
4157 boolean_t shared_lock
;
4158 if ((object
== cur_object
&&
4159 object_lock_type
== OBJECT_LOCK_EXCLUSIVE
) ||
4160 (object
!= cur_object
&&
4161 cur_object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)) {
4162 shared_lock
= FALSE
;
4167 kr
= vm_compressor_pager_get(
4170 cur_object
->paging_offset
),
4174 &compressed_count_delta
);
4176 vm_compressor_pager_count(
4178 compressed_count_delta
,
4182 if (kr
!= KERN_SUCCESS
) {
4189 * If the object is purgeable, its
4190 * owner's purgeable ledgers will be
4191 * updated in vm_page_insert() but the
4192 * page was also accounted for in a
4193 * "compressed purgeable" ledger, so
4196 if (object
!= cur_object
&&
4197 !insert_cur_object
) {
4199 * We're not going to insert
4200 * the decompressed page into
4201 * the object it came from.
4203 * We're dealing with a
4204 * copy-on-write fault on
4206 * We're going to decompress
4207 * the page directly into the
4208 * target "object" while
4209 * keepin the compressed
4210 * page for "cur_object", so
4211 * no ledger update in that
4214 } else if ((cur_object
->purgable
==
4215 VM_PURGABLE_DENY
) ||
4216 (cur_object
->vo_purgeable_owner
==
4219 * "cur_object" is not purgeable
4220 * or is not owned, so no
4221 * purgeable ledgers to update.
4225 * One less compressed
4226 * purgeable page for
4227 * cur_object's owner.
4229 vm_purgeable_compressed_update(
4234 if (insert_cur_object
) {
4235 vm_page_insert(m
, cur_object
, cur_offset
);
4237 vm_page_insert(m
, object
, offset
);
4240 if ((m
->object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_USE_DEFAULT
) {
4242 * If the page is not cacheable,
4243 * we can't let its contents
4244 * linger in the data cache
4245 * after the decompression.
4247 pmap_sync_page_attributes_phys(m
->phys_page
);
4250 type_of_fault
= my_fault_type
;
4252 VM_STAT_INCR(decompressions
);
4254 if (cur_object
!= object
) {
4255 if (insert_cur_object
) {
4256 top_object
= object
;
4258 * switch to the object that has the new page
4260 object
= cur_object
;
4261 object_lock_type
= cur_object_lock_type
;
4263 vm_object_unlock(cur_object
);
4264 cur_object
= object
;
4270 * existence map present and indicates
4271 * that the pager doesn't have this page
4274 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
4276 * Zero fill fault. Page gets
4277 * inserted into the original object.
4279 if (cur_object
->shadow_severed
||
4280 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
))
4282 if (object
!= cur_object
)
4283 vm_object_unlock(cur_object
);
4284 vm_object_unlock(object
);
4286 vm_map_unlock_read(map
);
4287 if (real_map
!= map
)
4288 vm_map_unlock(real_map
);
4290 kr
= KERN_MEMORY_ERROR
;
4293 if (vm_backing_store_low
) {
4295 * we are protecting the system from
4296 * backing store exhaustion...
4297 * must take the slow path if we're
4300 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
))
4303 if (cur_object
!= object
) {
4304 vm_object_unlock(cur_object
);
4306 cur_object
= object
;
4308 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4310 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4312 if (vm_object_lock_upgrade(object
) == FALSE
) {
4314 * couldn't upgrade so do a full retry on the fault
4315 * since we dropped the object lock which
4316 * could allow another thread to insert
4317 * a page at this offset
4319 vm_map_unlock_read(map
);
4320 if (real_map
!= map
)
4321 vm_map_unlock(real_map
);
4326 m
= vm_page_alloc(object
, offset
);
4328 if (m
== VM_PAGE_NULL
) {
4330 * no free page currently available...
4331 * must take the slow path
4337 * Now zero fill page...
4338 * the page is probably going to
4339 * be written soon, so don't bother
4340 * to clear the modified bit
4342 * NOTE: This code holds the map
4343 * lock across the zero fill.
4345 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
4350 * On to the next level in the shadow chain
4352 cur_offset
+= cur_object
->vo_shadow_offset
;
4353 new_object
= cur_object
->shadow
;
4356 * take the new_object's lock with the indicated state
4358 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
)
4359 vm_object_lock_shared(new_object
);
4361 vm_object_lock(new_object
);
4363 if (cur_object
!= object
)
4364 vm_object_unlock(cur_object
);
4366 cur_object
= new_object
;
4372 * Cleanup from fast fault failure. Drop any object
4373 * lock other than original and drop map lock.
4375 if (object
!= cur_object
)
4376 vm_object_unlock(cur_object
);
4379 * must own the object lock exclusively at this point
4381 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4382 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4384 if (vm_object_lock_upgrade(object
) == FALSE
) {
4386 * couldn't upgrade, so explictly
4387 * take the lock exclusively
4388 * no need to retry the fault at this
4389 * point since "vm_fault_page" will
4390 * completely re-evaluate the state
4392 vm_object_lock(object
);
4397 vm_map_unlock_read(map
);
4398 if (real_map
!= map
)
4399 vm_map_unlock(real_map
);
4402 * Make a reference to this object to
4403 * prevent its disposal while we are messing with
4404 * it. Once we have the reference, the map is free
4405 * to be diddled. Since objects reference their
4406 * shadows (and copies), they will stay around as well.
4408 vm_object_reference_locked(object
);
4409 vm_object_paging_begin(object
);
4411 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
4415 result_page
= VM_PAGE_NULL
;
4416 kr
= vm_fault_page(object
, offset
, fault_type
,
4417 (change_wiring
&& !wired
),
4418 FALSE
, /* page not looked up */
4419 &prot
, &result_page
, &top_page
,
4421 &error_code
, map
->no_zero_fill
,
4422 FALSE
, &fault_info
);
4425 * if kr != VM_FAULT_SUCCESS, then the paging reference
4426 * has been dropped and the object unlocked... the ref_count
4429 * if kr == VM_FAULT_SUCCESS, then the paging reference
4430 * is still held along with the ref_count on the original object
4432 * the object is returned locked with a paging reference
4434 * if top_page != NULL, then it's BUSY and the
4435 * object it belongs to has a paging reference
4436 * but is returned unlocked
4438 if (kr
!= VM_FAULT_SUCCESS
&&
4439 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
4441 * we didn't succeed, lose the object reference immediately.
4443 vm_object_deallocate(object
);
4446 * See why we failed, and take corrective action.
4449 case VM_FAULT_MEMORY_SHORTAGE
:
4450 if (vm_page_wait((change_wiring
) ?
4457 case VM_FAULT_INTERRUPTED
:
4460 case VM_FAULT_RETRY
:
4462 case VM_FAULT_MEMORY_ERROR
:
4466 kr
= KERN_MEMORY_ERROR
;
4469 panic("vm_fault: unexpected error 0x%x from "
4470 "vm_fault_page()\n", kr
);
4475 if (m
!= VM_PAGE_NULL
) {
4476 assert((change_wiring
&& !wired
) ?
4477 (top_page
== VM_PAGE_NULL
) :
4478 ((top_page
== VM_PAGE_NULL
) == (m
->object
== object
)));
4482 * What to do with the resulting page from vm_fault_page
4483 * if it doesn't get entered into the physical map:
4485 #define RELEASE_PAGE(m) \
4487 PAGE_WAKEUP_DONE(m); \
4488 if (!m->active && !m->inactive && !m->throttled) { \
4489 vm_page_lockspin_queues(); \
4490 if (!m->active && !m->inactive && !m->throttled) \
4491 vm_page_activate(m); \
4492 vm_page_unlock_queues(); \
4497 * We must verify that the maps have not changed
4498 * since our last lookup.
4500 if (m
!= VM_PAGE_NULL
) {
4501 old_copy_object
= m
->object
->copy
;
4502 vm_object_unlock(m
->object
);
4504 old_copy_object
= VM_OBJECT_NULL
;
4505 vm_object_unlock(object
);
4509 * no object locks are held at this point
4511 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
4512 vm_object_t retry_object
;
4513 vm_object_offset_t retry_offset
;
4514 vm_prot_t retry_prot
;
4517 * To avoid trying to write_lock the map while another
4518 * thread has it read_locked (in vm_map_pageable), we
4519 * do not try for write permission. If the page is
4520 * still writable, we will get write permission. If it
4521 * is not, or has been marked needs_copy, we enter the
4522 * mapping without write permission, and will merely
4523 * take another fault.
4526 vm_map_lock_read(map
);
4528 kr
= vm_map_lookup_locked(&map
, vaddr
,
4529 fault_type
& ~VM_PROT_WRITE
,
4530 OBJECT_LOCK_EXCLUSIVE
, &version
,
4531 &retry_object
, &retry_offset
, &retry_prot
,
4535 pmap
= real_map
->pmap
;
4537 if (kr
!= KERN_SUCCESS
) {
4538 vm_map_unlock_read(map
);
4540 if (m
!= VM_PAGE_NULL
) {
4542 * retake the lock so that
4543 * we can drop the paging reference
4544 * in vm_fault_cleanup and do the
4545 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4547 vm_object_lock(m
->object
);
4551 vm_fault_cleanup(m
->object
, top_page
);
4554 * retake the lock so that
4555 * we can drop the paging reference
4556 * in vm_fault_cleanup
4558 vm_object_lock(object
);
4560 vm_fault_cleanup(object
, top_page
);
4562 vm_object_deallocate(object
);
4566 vm_object_unlock(retry_object
);
4568 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
4570 vm_map_unlock_read(map
);
4571 if (real_map
!= map
)
4572 vm_map_unlock(real_map
);
4574 if (m
!= VM_PAGE_NULL
) {
4576 * retake the lock so that
4577 * we can drop the paging reference
4578 * in vm_fault_cleanup and do the
4579 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4581 vm_object_lock(m
->object
);
4585 vm_fault_cleanup(m
->object
, top_page
);
4588 * retake the lock so that
4589 * we can drop the paging reference
4590 * in vm_fault_cleanup
4592 vm_object_lock(object
);
4594 vm_fault_cleanup(object
, top_page
);
4596 vm_object_deallocate(object
);
4601 * Check whether the protection has changed or the object
4602 * has been copied while we left the map unlocked.
4606 if (m
!= VM_PAGE_NULL
) {
4607 vm_object_lock(m
->object
);
4609 if (m
->object
->copy
!= old_copy_object
) {
4611 * The copy object changed while the top-level object
4612 * was unlocked, so take away write permission.
4614 prot
&= ~VM_PROT_WRITE
;
4617 vm_object_lock(object
);
4620 * If we want to wire down this page, but no longer have
4621 * adequate permissions, we must start all over.
4623 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
4625 vm_map_verify_done(map
, &version
);
4626 if (real_map
!= map
)
4627 vm_map_unlock(real_map
);
4629 if (m
!= VM_PAGE_NULL
) {
4632 vm_fault_cleanup(m
->object
, top_page
);
4634 vm_fault_cleanup(object
, top_page
);
4636 vm_object_deallocate(object
);
4640 if (m
!= VM_PAGE_NULL
) {
4642 * Put this page into the physical map.
4643 * We had to do the unlock above because pmap_enter
4644 * may cause other faults. The page may be on
4645 * the pageout queues. If the pageout daemon comes
4646 * across the page, it will remove it from the queues.
4649 kr
= vm_fault_enter(m
,
4656 fault_info
.no_cache
,
4657 fault_info
.cs_bypass
,
4658 fault_info
.user_tag
,
4659 fault_info
.pmap_options
,
4663 kr
= vm_fault_enter(m
,
4670 fault_info
.no_cache
,
4671 fault_info
.cs_bypass
,
4672 fault_info
.user_tag
,
4673 fault_info
.pmap_options
,
4677 if (kr
!= KERN_SUCCESS
) {
4678 /* abort this page fault */
4679 vm_map_verify_done(map
, &version
);
4680 if (real_map
!= map
)
4681 vm_map_unlock(real_map
);
4682 PAGE_WAKEUP_DONE(m
);
4683 vm_fault_cleanup(m
->object
, top_page
);
4684 vm_object_deallocate(object
);
4687 if (physpage_p
!= NULL
) {
4688 /* for vm_map_wire_and_extract() */
4689 *physpage_p
= m
->phys_page
;
4690 if (prot
& VM_PROT_WRITE
) {
4691 vm_object_lock_assert_exclusive(m
->object
);
4697 vm_map_entry_t entry
;
4698 vm_map_offset_t laddr
;
4699 vm_map_offset_t ldelta
, hdelta
;
4702 * do a pmap block mapping from the physical address
4707 /* While we do not worry about execution protection in */
4708 /* general, certian pages may have instruction execution */
4709 /* disallowed. We will check here, and if not allowed */
4710 /* to execute, we return with a protection failure. */
4712 if ((fault_type
& VM_PROT_EXECUTE
) &&
4713 (!pmap_eligible_for_execute((ppnum_t
)(object
->vo_shadow_offset
>> 12)))) {
4715 vm_map_verify_done(map
, &version
);
4717 if (real_map
!= map
)
4718 vm_map_unlock(real_map
);
4720 vm_fault_cleanup(object
, top_page
);
4721 vm_object_deallocate(object
);
4723 kr
= KERN_PROTECTION_FAILURE
;
4728 if (real_map
!= map
)
4729 vm_map_unlock(real_map
);
4731 if (original_map
!= map
) {
4732 vm_map_unlock_read(map
);
4733 vm_map_lock_read(original_map
);
4739 hdelta
= 0xFFFFF000;
4740 ldelta
= 0xFFFFF000;
4742 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
4743 if (ldelta
> (laddr
- entry
->vme_start
))
4744 ldelta
= laddr
- entry
->vme_start
;
4745 if (hdelta
> (entry
->vme_end
- laddr
))
4746 hdelta
= entry
->vme_end
- laddr
;
4747 if (entry
->is_sub_map
) {
4749 laddr
= (laddr
- entry
->vme_start
)
4751 vm_map_lock_read(entry
->object
.sub_map
);
4753 if (map
!= real_map
)
4754 vm_map_unlock_read(map
);
4755 if (entry
->use_pmap
) {
4756 vm_map_unlock_read(real_map
);
4757 real_map
= entry
->object
.sub_map
;
4759 map
= entry
->object
.sub_map
;
4766 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
4767 (entry
->object
.vm_object
!= NULL
) &&
4768 (entry
->object
.vm_object
== object
)) {
4770 int superpage
= (!object
->pager_created
&& object
->phys_contiguous
)? VM_MEM_SUPERPAGE
: 0;
4772 if (superpage
&& physpage_p
) {
4773 /* for vm_map_wire_and_extract() */
4774 *physpage_p
= (ppnum_t
) ((((vm_map_offset_t
) entry
->object
.vm_object
->vo_shadow_offset
)
4776 + (laddr
- entry
->vme_start
))
4782 * Set up a block mapped area
4784 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
4785 pmap_map_block(caller_pmap
,
4786 (addr64_t
)(caller_pmap_addr
- ldelta
),
4787 (ppnum_t
)((((vm_map_offset_t
) (entry
->object
.vm_object
->vo_shadow_offset
)) +
4788 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
4789 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
4790 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
4793 * Set up a block mapped area
4795 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
4796 pmap_map_block(real_map
->pmap
,
4797 (addr64_t
)(vaddr
- ldelta
),
4798 (ppnum_t
)((((vm_map_offset_t
)(entry
->object
.vm_object
->vo_shadow_offset
)) +
4799 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
4800 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
4801 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
4807 * Unlock everything, and return
4809 vm_map_verify_done(map
, &version
);
4810 if (real_map
!= map
)
4811 vm_map_unlock(real_map
);
4813 if (m
!= VM_PAGE_NULL
) {
4814 PAGE_WAKEUP_DONE(m
);
4816 vm_fault_cleanup(m
->object
, top_page
);
4818 vm_fault_cleanup(object
, top_page
);
4820 vm_object_deallocate(object
);
4826 thread_interrupt_level(interruptible_state
);
4829 * Only I/O throttle on faults which cause a pagein/swapin.
4831 if ((type_of_fault
== DBG_PAGEIND_FAULT
) || (type_of_fault
== DBG_PAGEINV_FAULT
) || (type_of_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
)) {
4832 throttle_lowpri_io(1);
4834 if (kr
== KERN_SUCCESS
&& type_of_fault
!= DBG_CACHE_HIT_FAULT
&& type_of_fault
!= DBG_GUARD_FAULT
) {
4836 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
4838 if (vm_debug_events
) {
4839 if (type_of_fault
== DBG_COMPRESSOR_FAULT
)
4840 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
4841 else if (type_of_fault
== DBG_COW_FAULT
)
4842 VM_DEBUG_EVENT(vmf_cowdelay
, VMF_COWDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
4844 VM_DEBUG_EVENT(vmf_zfdelay
, VMF_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
4846 delay(throttle_delay
);
4850 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
4851 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
4852 ((uint64_t)vaddr
>> 32),
4864 * Wire down a range of virtual addresses in a map.
4869 vm_map_entry_t entry
,
4871 vm_map_offset_t pmap_addr
,
4872 ppnum_t
*physpage_p
)
4875 register vm_map_offset_t va
;
4876 register vm_map_offset_t end_addr
= entry
->vme_end
;
4877 register kern_return_t rc
;
4879 assert(entry
->in_transition
);
4881 if ((entry
->object
.vm_object
!= NULL
) &&
4882 !entry
->is_sub_map
&&
4883 entry
->object
.vm_object
->phys_contiguous
) {
4884 return KERN_SUCCESS
;
4888 * Inform the physical mapping system that the
4889 * range of addresses may not fault, so that
4890 * page tables and such can be locked down as well.
4893 pmap_pageable(pmap
, pmap_addr
,
4894 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
4897 * We simulate a fault to get the page and enter it
4898 * in the physical map.
4901 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
4902 rc
= vm_fault_wire_fast(map
, va
, entry
, pmap
,
4903 pmap_addr
+ (va
- entry
->vme_start
),
4905 if (rc
!= KERN_SUCCESS
) {
4906 rc
= vm_fault_internal(map
, va
, VM_PROT_NONE
, TRUE
,
4907 ((pmap
== kernel_pmap
)
4909 : THREAD_ABORTSAFE
),
4912 (va
- entry
->vme_start
)),
4914 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
4917 if (rc
!= KERN_SUCCESS
) {
4918 struct vm_map_entry tmp_entry
= *entry
;
4920 /* unwire wired pages */
4921 tmp_entry
.vme_end
= va
;
4922 vm_fault_unwire(map
,
4923 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
4928 return KERN_SUCCESS
;
4934 * Unwire a range of virtual addresses in a map.
4939 vm_map_entry_t entry
,
4940 boolean_t deallocate
,
4942 vm_map_offset_t pmap_addr
)
4944 register vm_map_offset_t va
;
4945 register vm_map_offset_t end_addr
= entry
->vme_end
;
4947 struct vm_object_fault_info fault_info
;
4949 object
= (entry
->is_sub_map
)
4950 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
4953 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
4954 * do anything since such memory is wired by default. So we don't have
4955 * anything to undo here.
4958 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
)
4961 fault_info
.interruptible
= THREAD_UNINT
;
4962 fault_info
.behavior
= entry
->behavior
;
4963 fault_info
.user_tag
= entry
->alias
;
4964 fault_info
.pmap_options
= 0;
4965 if (entry
->iokit_acct
||
4966 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
4967 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
4969 fault_info
.lo_offset
= entry
->offset
;
4970 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
4971 fault_info
.no_cache
= entry
->no_cache
;
4972 fault_info
.stealth
= TRUE
;
4973 fault_info
.io_sync
= FALSE
;
4974 fault_info
.cs_bypass
= FALSE
;
4975 fault_info
.mark_zf_absent
= FALSE
;
4976 fault_info
.batch_pmap_op
= FALSE
;
4979 * Since the pages are wired down, we must be able to
4980 * get their mappings from the physical map system.
4983 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
4985 if (object
== VM_OBJECT_NULL
) {
4987 pmap_change_wiring(pmap
,
4988 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
4990 (void) vm_fault(map
, va
, VM_PROT_NONE
,
4991 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
4994 vm_page_t result_page
;
4996 vm_object_t result_object
;
4997 vm_fault_return_t result
;
4999 if (end_addr
- va
> (vm_size_t
) -1) {
5000 /* 32-bit overflow */
5001 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5003 fault_info
.cluster_size
= (vm_size_t
) (end_addr
- va
);
5004 assert(fault_info
.cluster_size
== end_addr
- va
);
5008 prot
= VM_PROT_NONE
;
5010 vm_object_lock(object
);
5011 vm_object_paging_begin(object
);
5013 "vm_fault_unwire -> vm_fault_page\n",
5015 result_page
= VM_PAGE_NULL
;
5016 result
= vm_fault_page(
5018 entry
->offset
+ (va
- entry
->vme_start
),
5020 FALSE
, /* page not looked up */
5021 &prot
, &result_page
, &top_page
,
5023 NULL
, map
->no_zero_fill
,
5024 FALSE
, &fault_info
);
5025 } while (result
== VM_FAULT_RETRY
);
5028 * If this was a mapping to a file on a device that has been forcibly
5029 * unmounted, then we won't get a page back from vm_fault_page(). Just
5030 * move on to the next one in case the remaining pages are mapped from
5031 * different objects. During a forced unmount, the object is terminated
5032 * so the alive flag will be false if this happens. A forced unmount will
5033 * will occur when an external disk is unplugged before the user does an
5034 * eject, so we don't want to panic in that situation.
5037 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
)
5040 if (result
== VM_FAULT_MEMORY_ERROR
&&
5041 object
== kernel_object
) {
5043 * This must have been allocated with
5044 * KMA_KOBJECT and KMA_VAONLY and there's
5045 * no physical page at this offset.
5046 * We're done (no page to free).
5052 if (result
!= VM_FAULT_SUCCESS
)
5053 panic("vm_fault_unwire: failure");
5055 result_object
= result_page
->object
;
5058 assert(result_page
->phys_page
!=
5059 vm_page_fictitious_addr
);
5060 pmap_disconnect(result_page
->phys_page
);
5061 VM_PAGE_FREE(result_page
);
5063 if ((pmap
) && (result_page
->phys_page
!= vm_page_guard_addr
))
5064 pmap_change_wiring(pmap
,
5065 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5068 if (VM_PAGE_WIRED(result_page
)) {
5069 vm_page_lockspin_queues();
5070 vm_page_unwire(result_page
, TRUE
);
5071 vm_page_unlock_queues();
5073 if(entry
->zero_wired_pages
) {
5074 pmap_zero_page(result_page
->phys_page
);
5075 entry
->zero_wired_pages
= FALSE
;
5078 PAGE_WAKEUP_DONE(result_page
);
5080 vm_fault_cleanup(result_object
, top_page
);
5085 * Inform the physical mapping system that the range
5086 * of addresses may fault, so that page tables and
5087 * such may be unwired themselves.
5090 pmap_pageable(pmap
, pmap_addr
,
5091 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
5096 * vm_fault_wire_fast:
5098 * Handle common case of a wire down page fault at the given address.
5099 * If successful, the page is inserted into the associated physical map.
5100 * The map entry is passed in to avoid the overhead of a map lookup.
5102 * NOTE: the given address should be truncated to the
5103 * proper page address.
5105 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
5106 * a standard error specifying why the fault is fatal is returned.
5108 * The map in question must be referenced, and remains so.
5109 * Caller has a read lock on the map.
5111 * This is a stripped version of vm_fault() for wiring pages. Anything
5112 * other than the common case will return KERN_FAILURE, and the caller
5113 * is expected to call vm_fault().
5117 __unused vm_map_t map
,
5119 vm_map_entry_t entry
,
5121 vm_map_offset_t pmap_addr
,
5122 ppnum_t
*physpage_p
)
5125 vm_object_offset_t offset
;
5126 register vm_page_t m
;
5128 thread_t thread
= current_thread();
5132 VM_STAT_INCR(faults
);
5134 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
)
5135 thread
->task
->faults
++;
5142 #define RELEASE_PAGE(m) { \
5143 PAGE_WAKEUP_DONE(m); \
5144 vm_page_lockspin_queues(); \
5145 vm_page_unwire(m, TRUE); \
5146 vm_page_unlock_queues(); \
5150 #undef UNLOCK_THINGS
5151 #define UNLOCK_THINGS { \
5152 vm_object_paging_end(object); \
5153 vm_object_unlock(object); \
5156 #undef UNLOCK_AND_DEALLOCATE
5157 #define UNLOCK_AND_DEALLOCATE { \
5159 vm_object_deallocate(object); \
5162 * Give up and have caller do things the hard way.
5166 UNLOCK_AND_DEALLOCATE; \
5167 return(KERN_FAILURE); \
5172 * If this entry is not directly to a vm_object, bail out.
5174 if (entry
->is_sub_map
) {
5175 assert(physpage_p
== NULL
);
5176 return(KERN_FAILURE
);
5180 * Find the backing store object and offset into it.
5183 object
= entry
->object
.vm_object
;
5184 offset
= (va
- entry
->vme_start
) + entry
->offset
;
5185 prot
= entry
->protection
;
5188 * Make a reference to this object to prevent its
5189 * disposal while we are messing with it.
5192 vm_object_lock(object
);
5193 vm_object_reference_locked(object
);
5194 vm_object_paging_begin(object
);
5197 * INVARIANTS (through entire routine):
5199 * 1) At all times, we must either have the object
5200 * lock or a busy page in some object to prevent
5201 * some other thread from trying to bring in
5204 * 2) Once we have a busy page, we must remove it from
5205 * the pageout queues, so that the pageout daemon
5206 * will not grab it away.
5211 * Look for page in top-level object. If it's not there or
5212 * there's something going on, give up.
5213 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
5214 * decrypt the page before wiring it down.
5216 m
= vm_page_lookup(object
, offset
);
5217 if ((m
== VM_PAGE_NULL
) || (m
->busy
) || (m
->encrypted
) ||
5218 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
5222 ASSERT_PAGE_DECRYPTED(m
);
5224 if (m
->fictitious
&&
5225 m
->phys_page
== vm_page_guard_addr
) {
5227 * Guard pages are fictitious pages and are never
5228 * entered into a pmap, so let's say it's been wired...
5235 * Wire the page down now. All bail outs beyond this
5236 * point must unwire the page.
5239 vm_page_lockspin_queues();
5241 vm_page_unlock_queues();
5244 * Mark page busy for other threads.
5251 * Give up if the page is being written and there's a copy object
5253 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
5259 * Put this page into the physical map.
5261 type_of_fault
= DBG_CACHE_HIT_FAULT
;
5262 kr
= vm_fault_enter(m
,
5272 ((entry
->iokit_acct
||
5273 (!entry
->is_sub_map
&& !entry
->use_pmap
))
5274 ? PMAP_OPTIONS_ALT_ACCT
5281 * Unlock everything, and return
5285 /* for vm_map_wire_and_extract() */
5286 if (kr
== KERN_SUCCESS
) {
5287 *physpage_p
= m
->phys_page
;
5288 if (prot
& VM_PROT_WRITE
) {
5289 vm_object_lock_assert_exclusive(m
->object
);
5297 PAGE_WAKEUP_DONE(m
);
5298 UNLOCK_AND_DEALLOCATE
;
5305 * Routine: vm_fault_copy_cleanup
5307 * Release a page used by vm_fault_copy.
5311 vm_fault_copy_cleanup(
5315 vm_object_t object
= page
->object
;
5317 vm_object_lock(object
);
5318 PAGE_WAKEUP_DONE(page
);
5319 if (!page
->active
&& !page
->inactive
&& !page
->throttled
) {
5320 vm_page_lockspin_queues();
5321 if (!page
->active
&& !page
->inactive
&& !page
->throttled
)
5322 vm_page_activate(page
);
5323 vm_page_unlock_queues();
5325 vm_fault_cleanup(object
, top_page
);
5329 vm_fault_copy_dst_cleanup(
5334 if (page
!= VM_PAGE_NULL
) {
5335 object
= page
->object
;
5336 vm_object_lock(object
);
5337 vm_page_lockspin_queues();
5338 vm_page_unwire(page
, TRUE
);
5339 vm_page_unlock_queues();
5340 vm_object_paging_end(object
);
5341 vm_object_unlock(object
);
5346 * Routine: vm_fault_copy
5349 * Copy pages from one virtual memory object to another --
5350 * neither the source nor destination pages need be resident.
5352 * Before actually copying a page, the version associated with
5353 * the destination address map wil be verified.
5355 * In/out conditions:
5356 * The caller must hold a reference, but not a lock, to
5357 * each of the source and destination objects and to the
5361 * Returns KERN_SUCCESS if no errors were encountered in
5362 * reading or writing the data. Returns KERN_INTERRUPTED if
5363 * the operation was interrupted (only possible if the
5364 * "interruptible" argument is asserted). Other return values
5365 * indicate a permanent error in copying the data.
5367 * The actual amount of data copied will be returned in the
5368 * "copy_size" argument. In the event that the destination map
5369 * verification failed, this amount may be less than the amount
5374 vm_object_t src_object
,
5375 vm_object_offset_t src_offset
,
5376 vm_map_size_t
*copy_size
, /* INOUT */
5377 vm_object_t dst_object
,
5378 vm_object_offset_t dst_offset
,
5380 vm_map_version_t
*dst_version
,
5383 vm_page_t result_page
;
5386 vm_page_t src_top_page
;
5390 vm_page_t dst_top_page
;
5393 vm_map_size_t amount_left
;
5394 vm_object_t old_copy_object
;
5395 kern_return_t error
= 0;
5396 vm_fault_return_t result
;
5398 vm_map_size_t part_size
;
5399 struct vm_object_fault_info fault_info_src
;
5400 struct vm_object_fault_info fault_info_dst
;
5403 * In order not to confuse the clustered pageins, align
5404 * the different offsets on a page boundary.
5409 *copy_size -= amount_left; \
5413 amount_left
= *copy_size
;
5415 fault_info_src
.interruptible
= interruptible
;
5416 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5417 fault_info_src
.user_tag
= 0;
5418 fault_info_src
.pmap_options
= 0;
5419 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
5420 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
5421 fault_info_src
.no_cache
= FALSE
;
5422 fault_info_src
.stealth
= TRUE
;
5423 fault_info_src
.io_sync
= FALSE
;
5424 fault_info_src
.cs_bypass
= FALSE
;
5425 fault_info_src
.mark_zf_absent
= FALSE
;
5426 fault_info_src
.batch_pmap_op
= FALSE
;
5428 fault_info_dst
.interruptible
= interruptible
;
5429 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5430 fault_info_dst
.user_tag
= 0;
5431 fault_info_dst
.pmap_options
= 0;
5432 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
5433 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
5434 fault_info_dst
.no_cache
= FALSE
;
5435 fault_info_dst
.stealth
= TRUE
;
5436 fault_info_dst
.io_sync
= FALSE
;
5437 fault_info_dst
.cs_bypass
= FALSE
;
5438 fault_info_dst
.mark_zf_absent
= FALSE
;
5439 fault_info_dst
.batch_pmap_op
= FALSE
;
5441 do { /* while (amount_left > 0) */
5443 * There may be a deadlock if both source and destination
5444 * pages are the same. To avoid this deadlock, the copy must
5445 * start by getting the destination page in order to apply
5446 * COW semantics if any.
5449 RetryDestinationFault
: ;
5451 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
5453 vm_object_lock(dst_object
);
5454 vm_object_paging_begin(dst_object
);
5456 if (amount_left
> (vm_size_t
) -1) {
5457 /* 32-bit overflow */
5458 fault_info_dst
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5460 fault_info_dst
.cluster_size
= (vm_size_t
) amount_left
;
5461 assert(fault_info_dst
.cluster_size
== amount_left
);
5464 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
5465 dst_page
= VM_PAGE_NULL
;
5466 result
= vm_fault_page(dst_object
,
5467 vm_object_trunc_page(dst_offset
),
5468 VM_PROT_WRITE
|VM_PROT_READ
,
5470 FALSE
, /* page not looked up */
5471 &dst_prot
, &dst_page
, &dst_top_page
,
5474 dst_map
->no_zero_fill
,
5475 FALSE
, &fault_info_dst
);
5477 case VM_FAULT_SUCCESS
:
5479 case VM_FAULT_RETRY
:
5480 goto RetryDestinationFault
;
5481 case VM_FAULT_MEMORY_SHORTAGE
:
5482 if (vm_page_wait(interruptible
))
5483 goto RetryDestinationFault
;
5485 case VM_FAULT_INTERRUPTED
:
5486 RETURN(MACH_SEND_INTERRUPTED
);
5487 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5488 /* success but no VM page: fail the copy */
5489 vm_object_paging_end(dst_object
);
5490 vm_object_unlock(dst_object
);
5492 case VM_FAULT_MEMORY_ERROR
:
5496 return(KERN_MEMORY_ERROR
);
5498 panic("vm_fault_copy: unexpected error 0x%x from "
5499 "vm_fault_page()\n", result
);
5501 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
5503 old_copy_object
= dst_page
->object
->copy
;
5506 * There exists the possiblity that the source and
5507 * destination page are the same. But we can't
5508 * easily determine that now. If they are the
5509 * same, the call to vm_fault_page() for the
5510 * destination page will deadlock. To prevent this we
5511 * wire the page so we can drop busy without having
5512 * the page daemon steal the page. We clean up the
5513 * top page but keep the paging reference on the object
5514 * holding the dest page so it doesn't go away.
5517 vm_page_lockspin_queues();
5518 vm_page_wire(dst_page
);
5519 vm_page_unlock_queues();
5520 PAGE_WAKEUP_DONE(dst_page
);
5521 vm_object_unlock(dst_page
->object
);
5523 if (dst_top_page
!= VM_PAGE_NULL
) {
5524 vm_object_lock(dst_object
);
5525 VM_PAGE_FREE(dst_top_page
);
5526 vm_object_paging_end(dst_object
);
5527 vm_object_unlock(dst_object
);
5532 if (src_object
== VM_OBJECT_NULL
) {
5534 * No source object. We will just
5535 * zero-fill the page in dst_object.
5537 src_page
= VM_PAGE_NULL
;
5538 result_page
= VM_PAGE_NULL
;
5540 vm_object_lock(src_object
);
5541 src_page
= vm_page_lookup(src_object
,
5542 vm_object_trunc_page(src_offset
));
5543 if (src_page
== dst_page
) {
5544 src_prot
= dst_prot
;
5545 result_page
= VM_PAGE_NULL
;
5547 src_prot
= VM_PROT_READ
;
5548 vm_object_paging_begin(src_object
);
5550 if (amount_left
> (vm_size_t
) -1) {
5551 /* 32-bit overflow */
5552 fault_info_src
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5554 fault_info_src
.cluster_size
= (vm_size_t
) amount_left
;
5555 assert(fault_info_src
.cluster_size
== amount_left
);
5559 "vm_fault_copy(2) -> vm_fault_page\n",
5561 result_page
= VM_PAGE_NULL
;
5562 result
= vm_fault_page(
5564 vm_object_trunc_page(src_offset
),
5565 VM_PROT_READ
, FALSE
,
5566 FALSE
, /* page not looked up */
5568 &result_page
, &src_top_page
,
5569 (int *)0, &error
, FALSE
,
5570 FALSE
, &fault_info_src
);
5573 case VM_FAULT_SUCCESS
:
5575 case VM_FAULT_RETRY
:
5576 goto RetrySourceFault
;
5577 case VM_FAULT_MEMORY_SHORTAGE
:
5578 if (vm_page_wait(interruptible
))
5579 goto RetrySourceFault
;
5581 case VM_FAULT_INTERRUPTED
:
5582 vm_fault_copy_dst_cleanup(dst_page
);
5583 RETURN(MACH_SEND_INTERRUPTED
);
5584 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5585 /* success but no VM page: fail */
5586 vm_object_paging_end(src_object
);
5587 vm_object_unlock(src_object
);
5589 case VM_FAULT_MEMORY_ERROR
:
5590 vm_fault_copy_dst_cleanup(dst_page
);
5594 return(KERN_MEMORY_ERROR
);
5596 panic("vm_fault_copy(2): unexpected "
5598 "vm_fault_page()\n", result
);
5602 assert((src_top_page
== VM_PAGE_NULL
) ==
5603 (result_page
->object
== src_object
));
5605 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
5606 vm_object_unlock(result_page
->object
);
5609 if (!vm_map_verify(dst_map
, dst_version
)) {
5610 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5611 vm_fault_copy_cleanup(result_page
, src_top_page
);
5612 vm_fault_copy_dst_cleanup(dst_page
);
5616 vm_object_lock(dst_page
->object
);
5618 if (dst_page
->object
->copy
!= old_copy_object
) {
5619 vm_object_unlock(dst_page
->object
);
5620 vm_map_verify_done(dst_map
, dst_version
);
5621 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5622 vm_fault_copy_cleanup(result_page
, src_top_page
);
5623 vm_fault_copy_dst_cleanup(dst_page
);
5626 vm_object_unlock(dst_page
->object
);
5629 * Copy the page, and note that it is dirty
5633 if (!page_aligned(src_offset
) ||
5634 !page_aligned(dst_offset
) ||
5635 !page_aligned(amount_left
)) {
5637 vm_object_offset_t src_po
,
5640 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
5641 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
5643 if (dst_po
> src_po
) {
5644 part_size
= PAGE_SIZE
- dst_po
;
5646 part_size
= PAGE_SIZE
- src_po
;
5648 if (part_size
> (amount_left
)){
5649 part_size
= amount_left
;
5652 if (result_page
== VM_PAGE_NULL
) {
5653 assert((vm_offset_t
) dst_po
== dst_po
);
5654 assert((vm_size_t
) part_size
== part_size
);
5655 vm_page_part_zero_fill(dst_page
,
5656 (vm_offset_t
) dst_po
,
5657 (vm_size_t
) part_size
);
5659 assert((vm_offset_t
) src_po
== src_po
);
5660 assert((vm_offset_t
) dst_po
== dst_po
);
5661 assert((vm_size_t
) part_size
== part_size
);
5662 vm_page_part_copy(result_page
,
5663 (vm_offset_t
) src_po
,
5665 (vm_offset_t
) dst_po
,
5666 (vm_size_t
)part_size
);
5667 if(!dst_page
->dirty
){
5668 vm_object_lock(dst_object
);
5669 SET_PAGE_DIRTY(dst_page
, TRUE
);
5670 vm_object_unlock(dst_page
->object
);
5675 part_size
= PAGE_SIZE
;
5677 if (result_page
== VM_PAGE_NULL
)
5678 vm_page_zero_fill(dst_page
);
5680 vm_object_lock(result_page
->object
);
5681 vm_page_copy(result_page
, dst_page
);
5682 vm_object_unlock(result_page
->object
);
5684 if(!dst_page
->dirty
){
5685 vm_object_lock(dst_object
);
5686 SET_PAGE_DIRTY(dst_page
, TRUE
);
5687 vm_object_unlock(dst_page
->object
);
5694 * Unlock everything, and return
5697 vm_map_verify_done(dst_map
, dst_version
);
5699 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5700 vm_fault_copy_cleanup(result_page
, src_top_page
);
5701 vm_fault_copy_dst_cleanup(dst_page
);
5703 amount_left
-= part_size
;
5704 src_offset
+= part_size
;
5705 dst_offset
+= part_size
;
5706 } while (amount_left
> 0);
5708 RETURN(KERN_SUCCESS
);
5714 #if VM_FAULT_CLASSIFY
5716 * Temporary statistics gathering support.
5720 * Statistics arrays:
5722 #define VM_FAULT_TYPES_MAX 5
5723 #define VM_FAULT_LEVEL_MAX 8
5725 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
5727 #define VM_FAULT_TYPE_ZERO_FILL 0
5728 #define VM_FAULT_TYPE_MAP_IN 1
5729 #define VM_FAULT_TYPE_PAGER 2
5730 #define VM_FAULT_TYPE_COPY 3
5731 #define VM_FAULT_TYPE_OTHER 4
5735 vm_fault_classify(vm_object_t object
,
5736 vm_object_offset_t offset
,
5737 vm_prot_t fault_type
)
5739 int type
, level
= 0;
5743 m
= vm_page_lookup(object
, offset
);
5744 if (m
!= VM_PAGE_NULL
) {
5745 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
) {
5746 type
= VM_FAULT_TYPE_OTHER
;
5749 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
5750 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
5751 type
= VM_FAULT_TYPE_MAP_IN
;
5754 type
= VM_FAULT_TYPE_COPY
;
5758 if (object
->pager_created
) {
5759 type
= VM_FAULT_TYPE_PAGER
;
5762 if (object
->shadow
== VM_OBJECT_NULL
) {
5763 type
= VM_FAULT_TYPE_ZERO_FILL
;
5767 offset
+= object
->vo_shadow_offset
;
5768 object
= object
->shadow
;
5774 if (level
> VM_FAULT_LEVEL_MAX
)
5775 level
= VM_FAULT_LEVEL_MAX
;
5777 vm_fault_stats
[type
][level
] += 1;
5782 /* cleanup routine to call from debugger */
5785 vm_fault_classify_init(void)
5789 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
5790 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
5791 vm_fault_stats
[type
][level
] = 0;
5797 #endif /* VM_FAULT_CLASSIFY */
5801 vm_page_validate_cs_mapped(
5806 vm_object_offset_t offset
;
5808 memory_object_t pager
;
5810 boolean_t validated
, tainted
;
5813 vm_object_lock_assert_exclusive(page
->object
);
5815 if (!cs_validation
) {
5819 if (page
->wpmapped
&& !page
->cs_tainted
) {
5821 * This page was mapped for "write" access sometime in the
5822 * past and could still be modifiable in the future.
5823 * Consider it tainted.
5824 * [ If the page was already found to be "tainted", no
5825 * need to re-validate. ]
5827 page
->cs_validated
= TRUE
;
5828 page
->cs_tainted
= TRUE
;
5830 printf("CODESIGNING: vm_page_validate_cs: "
5831 "page %p obj %p off 0x%llx "
5833 page
, page
->object
, page
->offset
);
5835 vm_cs_validated_dirtied
++;
5838 if (page
->cs_validated
) {
5844 object
= page
->object
;
5845 assert(object
->code_signed
);
5846 offset
= page
->offset
;
5848 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
5850 * The object is terminating and we don't have its pager
5851 * so we can't validate the data...
5856 * Since we get here to validate a page that was brought in by
5857 * the pager, we know that this pager is all setup and ready
5860 assert(!object
->internal
);
5861 assert(object
->pager
!= NULL
);
5862 assert(object
->pager_ready
);
5864 pager
= object
->pager
;
5865 assert(object
->paging_in_progress
);
5866 kr
= vnode_pager_get_object_cs_blobs(pager
, &blobs
);
5867 if (kr
!= KERN_SUCCESS
) {
5871 /* verify the SHA1 hash for this page */
5872 validated
= cs_validate_page(blobs
,
5874 offset
+ object
->paging_offset
,
5875 (const void *)kaddr
,
5878 page
->cs_validated
= validated
;
5880 page
->cs_tainted
= tainted
;
5885 vm_page_validate_cs(
5889 vm_object_offset_t offset
;
5890 vm_map_offset_t koffset
;
5891 vm_map_size_t ksize
;
5894 boolean_t busy_page
;
5895 boolean_t need_unmap
;
5897 vm_object_lock_assert_held(page
->object
);
5899 if (!cs_validation
) {
5903 if (page
->wpmapped
&& !page
->cs_tainted
) {
5904 vm_object_lock_assert_exclusive(page
->object
);
5907 * This page was mapped for "write" access sometime in the
5908 * past and could still be modifiable in the future.
5909 * Consider it tainted.
5910 * [ If the page was already found to be "tainted", no
5911 * need to re-validate. ]
5913 page
->cs_validated
= TRUE
;
5914 page
->cs_tainted
= TRUE
;
5916 printf("CODESIGNING: vm_page_validate_cs: "
5917 "page %p obj %p off 0x%llx "
5919 page
, page
->object
, page
->offset
);
5921 vm_cs_validated_dirtied
++;
5924 if (page
->cs_validated
) {
5929 panic("vm_page_validate_cs(%p): page is slid\n", page
);
5931 assert(!page
->slid
);
5933 #if CHECK_CS_VALIDATION_BITMAP
5934 if ( vnode_pager_cs_check_validation_bitmap( page
->object
->pager
, trunc_page(page
->offset
+ page
->object
->paging_offset
), CS_BITMAP_CHECK
) == KERN_SUCCESS
) {
5935 page
->cs_validated
= TRUE
;
5936 page
->cs_tainted
= FALSE
;
5937 vm_cs_bitmap_validated
++;
5941 vm_object_lock_assert_exclusive(page
->object
);
5943 object
= page
->object
;
5944 assert(object
->code_signed
);
5945 offset
= page
->offset
;
5947 busy_page
= page
->busy
;
5949 /* keep page busy while we map (and unlock) the VM object */
5954 * Take a paging reference on the VM object
5955 * to protect it from collapse or bypass,
5956 * and keep it from disappearing too.
5958 vm_object_paging_begin(object
);
5960 /* map the page in the kernel address space */
5961 ksize
= PAGE_SIZE_64
;
5964 kr
= vm_paging_map_object(page
,
5968 FALSE
, /* can't unlock object ! */
5972 if (kr
!= KERN_SUCCESS
) {
5973 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr
);
5975 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
5977 /* validate the mapped page */
5978 vm_page_validate_cs_mapped(page
, (const void *) kaddr
);
5980 #if CHECK_CS_VALIDATION_BITMAP
5981 if ( page
->cs_validated
== TRUE
&& page
->cs_tainted
== FALSE
) {
5982 vnode_pager_cs_check_validation_bitmap( object
->pager
, trunc_page( offset
+ object
->paging_offset
), CS_BITMAP_SET
);
5986 assert(object
== page
->object
);
5987 vm_object_lock_assert_exclusive(object
);
5990 PAGE_WAKEUP_DONE(page
);
5993 /* unmap the map from the kernel address space */
5994 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
5999 vm_object_paging_end(object
);