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
;
740 if (no_zero_fill
== TRUE
) {
741 my_fault
= DBG_NZF_PAGE_FAULT
;
743 if (m
->absent
&& m
->busy
)
746 vm_page_zero_fill(m
);
748 VM_STAT_INCR(zero_fill_count
);
749 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
752 assert(m
->object
!= kernel_object
);
753 //assert(m->pageq.next == NULL && m->pageq.prev == NULL);
755 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) &&
756 (m
->object
->purgable
== VM_PURGABLE_DENY
||
757 m
->object
->purgable
== VM_PURGABLE_NONVOLATILE
||
758 m
->object
->purgable
== VM_PURGABLE_VOLATILE
)) {
760 vm_page_lockspin_queues();
762 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
)) {
763 assert(!VM_PAGE_WIRED(m
));
766 * can't be on the pageout queue since we don't
767 * have a pager to try and clean to
769 assert(!m
->pageout_queue
);
771 VM_PAGE_QUEUES_REMOVE(m
);
773 queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
775 vm_page_throttled_count
++;
777 vm_page_unlock_queues();
784 * Routine: vm_fault_page
786 * Find the resident page for the virtual memory
787 * specified by the given virtual memory object
789 * Additional arguments:
790 * The required permissions for the page is given
791 * in "fault_type". Desired permissions are included
793 * fault_info is passed along to determine pagein cluster
794 * limits... it contains the expected reference pattern,
795 * cluster size if available, etc...
797 * If the desired page is known to be resident (for
798 * example, because it was previously wired down), asserting
799 * the "unwiring" parameter will speed the search.
801 * If the operation can be interrupted (by thread_abort
802 * or thread_terminate), then the "interruptible"
803 * parameter should be asserted.
806 * The page containing the proper data is returned
810 * The source object must be locked and referenced,
811 * and must donate one paging reference. The reference
812 * is not affected. The paging reference and lock are
815 * If the call succeeds, the object in which "result_page"
816 * resides is left locked and holding a paging reference.
817 * If this is not the original object, a busy page in the
818 * original object is returned in "top_page", to prevent other
819 * callers from pursuing this same data, along with a paging
820 * reference for the original object. The "top_page" should
821 * be destroyed when this guarantee is no longer required.
822 * The "result_page" is also left busy. It is not removed
823 * from the pageout queues.
825 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
826 * fault succeeded but there's no VM page (i.e. the VM object
827 * does not actually hold VM pages, but device memory or
828 * large pages). The object is still locked and we still hold a
829 * paging_in_progress reference.
831 unsigned int vm_fault_page_blocked_access
= 0;
832 unsigned int vm_fault_page_forced_retry
= 0;
837 vm_object_t first_object
, /* Object to begin search */
838 vm_object_offset_t first_offset
, /* Offset into object */
839 vm_prot_t fault_type
, /* What access is requested */
840 boolean_t must_be_resident
,/* Must page be resident? */
841 boolean_t caller_lookup
, /* caller looked up page */
842 /* Modifies in place: */
843 vm_prot_t
*protection
, /* Protection for mapping */
844 vm_page_t
*result_page
, /* Page found, if successful */
846 vm_page_t
*top_page
, /* Page in top object, if
847 * not result_page. */
848 int *type_of_fault
, /* if non-null, fill in with type of fault
849 * COW, zero-fill, etc... returned in trace point */
850 /* More arguments: */
851 kern_return_t
*error_code
, /* code if page is in error */
852 boolean_t no_zero_fill
, /* don't zero fill absent pages */
853 boolean_t data_supply
, /* treat as data_supply if
854 * it is a write fault and a full
855 * page is provided */
856 vm_object_fault_info_t fault_info
)
860 vm_object_offset_t offset
;
862 vm_object_t next_object
;
863 vm_object_t copy_object
;
864 boolean_t look_for_page
;
865 boolean_t force_fault_retry
= FALSE
;
866 vm_prot_t access_required
= fault_type
;
867 vm_prot_t wants_copy_flag
;
868 CLUSTER_STAT(int pages_at_higher_offsets
;)
869 CLUSTER_STAT(int pages_at_lower_offsets
;)
870 kern_return_t wait_result
;
871 boolean_t interruptible_state
;
872 boolean_t data_already_requested
= FALSE
;
873 vm_behavior_t orig_behavior
;
874 vm_size_t orig_cluster_size
;
875 vm_fault_return_t error
;
877 uint32_t try_failed_count
;
878 int interruptible
; /* how may fault be interrupted? */
879 int external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
880 memory_object_t pager
;
881 vm_fault_return_t retval
;
884 * MACH page map - an optional optimization where a bit map is maintained
885 * by the VM subsystem for internal objects to indicate which pages of
886 * the object currently reside on backing store. This existence map
887 * duplicates information maintained by the vnode pager. It is
888 * created at the time of the first pageout against the object, i.e.
889 * at the same time pager for the object is created. The optimization
890 * is designed to eliminate pager interaction overhead, if it is
891 * 'known' that the page does not exist on backing store.
893 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
894 * either marked as paged out in the existence map for the object or no
895 * existence map exists for the object. MUST_ASK_PAGER() is one of the
896 * criteria in the decision to invoke the pager. It is also used as one
897 * of the criteria to terminate the scan for adjacent pages in a clustered
898 * pagein operation. Note that MUST_ASK_PAGER() always evaluates to TRUE for
899 * permanent objects. Note also that if the pager for an internal object
900 * has not been created, the pager is not invoked regardless of the value
901 * of MUST_ASK_PAGER() and that clustered pagein scans are only done on an object
902 * for which a pager has been created.
904 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
905 * is marked as paged out in the existence map for the object. PAGED_OUT()
906 * PAGED_OUT() is used to determine if a page has already been pushed
907 * into a copy object in order to avoid a redundant page out operation.
910 #define MUST_ASK_PAGER(o, f, s) \
911 ((vm_external_state_get((o)->existence_map, (f)) \
912 != VM_EXTERNAL_STATE_ABSENT) && \
913 (s = (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)))) \
914 != VM_EXTERNAL_STATE_ABSENT)
915 #define PAGED_OUT(o, f) \
916 ((vm_external_state_get((o)->existence_map, (f)) \
917 == VM_EXTERNAL_STATE_EXISTS) || \
918 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) \
919 == VM_EXTERNAL_STATE_EXISTS))
920 #else /* MACH_PAGEMAP */
921 #define MUST_ASK_PAGER(o, f, s) \
922 ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
923 #define PAGED_OUT(o, f) \
924 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
925 #endif /* MACH_PAGEMAP */
930 #define RELEASE_PAGE(m) \
932 PAGE_WAKEUP_DONE(m); \
933 if (!m->active && !m->inactive && !m->throttled) { \
934 vm_page_lockspin_queues(); \
935 if (!m->active && !m->inactive && !m->throttled) { \
936 if (COMPRESSED_PAGER_IS_ACTIVE) \
937 vm_page_deactivate(m); \
939 vm_page_activate(m); \
941 vm_page_unlock_queues(); \
946 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
949 interruptible
= fault_info
->interruptible
;
950 interruptible_state
= thread_interrupt_level(interruptible
);
953 * INVARIANTS (through entire routine):
955 * 1) At all times, we must either have the object
956 * lock or a busy page in some object to prevent
957 * some other thread from trying to bring in
960 * Note that we cannot hold any locks during the
961 * pager access or when waiting for memory, so
962 * we use a busy page then.
964 * 2) To prevent another thread from racing us down the
965 * shadow chain and entering a new page in the top
966 * object before we do, we must keep a busy page in
967 * the top object while following the shadow chain.
969 * 3) We must increment paging_in_progress on any object
970 * for which we have a busy page before dropping
973 * 4) We leave busy pages on the pageout queues.
974 * If the pageout daemon comes across a busy page,
975 * it will remove the page from the pageout queues.
978 object
= first_object
;
979 offset
= first_offset
;
980 first_m
= VM_PAGE_NULL
;
981 access_required
= fault_type
;
985 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
986 object
, offset
, fault_type
, *protection
, 0);
989 * default type of fault
991 my_fault
= DBG_CACHE_HIT_FAULT
;
995 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
997 if (!object
->alive
) {
999 * object is no longer valid
1000 * clean up and return error
1002 vm_fault_cleanup(object
, first_m
);
1003 thread_interrupt_level(interruptible_state
);
1005 return (VM_FAULT_MEMORY_ERROR
);
1008 if (!object
->pager_created
&& object
->phys_contiguous
) {
1010 * A physically-contiguous object without a pager:
1011 * must be a "large page" object. We do not deal
1012 * with VM pages for this object.
1014 caller_lookup
= FALSE
;
1016 goto phys_contig_object
;
1019 if (object
->blocked_access
) {
1021 * Access to this VM object has been blocked.
1022 * Replace our "paging_in_progress" reference with
1023 * a "activity_in_progress" reference and wait for
1024 * access to be unblocked.
1026 caller_lookup
= FALSE
; /* no longer valid after sleep */
1027 vm_object_activity_begin(object
);
1028 vm_object_paging_end(object
);
1029 while (object
->blocked_access
) {
1030 vm_object_sleep(object
,
1031 VM_OBJECT_EVENT_UNBLOCKED
,
1034 vm_fault_page_blocked_access
++;
1035 vm_object_paging_begin(object
);
1036 vm_object_activity_end(object
);
1040 * See whether the page at 'offset' is resident
1042 if (caller_lookup
== TRUE
) {
1044 * The caller has already looked up the page
1045 * and gave us the result in "result_page".
1046 * We can use this for the first lookup but
1047 * it loses its validity as soon as we unlock
1051 caller_lookup
= FALSE
; /* no longer valid after that */
1053 m
= vm_page_lookup(object
, offset
);
1056 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1058 if (m
!= VM_PAGE_NULL
) {
1062 * The page is being brought in,
1063 * wait for it and then retry.
1066 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1068 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
1071 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
1074 counter(c_vm_fault_page_block_busy_kernel
++);
1076 if (wait_result
!= THREAD_AWAKENED
) {
1077 vm_fault_cleanup(object
, first_m
);
1078 thread_interrupt_level(interruptible_state
);
1080 if (wait_result
== THREAD_RESTART
)
1081 return (VM_FAULT_RETRY
);
1083 return (VM_FAULT_INTERRUPTED
);
1091 vm_pageout_steal_laundry(m
, FALSE
);
1093 if (m
->phys_page
== vm_page_guard_addr
) {
1095 * Guard page: off limits !
1097 if (fault_type
== VM_PROT_NONE
) {
1099 * The fault is not requesting any
1100 * access to the guard page, so it must
1101 * be just to wire or unwire it.
1102 * Let's pretend it succeeded...
1106 assert(first_m
== VM_PAGE_NULL
);
1107 *top_page
= first_m
;
1109 *type_of_fault
= DBG_GUARD_FAULT
;
1110 thread_interrupt_level(interruptible_state
);
1111 return VM_FAULT_SUCCESS
;
1114 * The fault requests access to the
1115 * guard page: let's deny that !
1117 vm_fault_cleanup(object
, first_m
);
1118 thread_interrupt_level(interruptible_state
);
1119 return VM_FAULT_MEMORY_ERROR
;
1125 * The page is in error, give up now.
1128 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1131 *error_code
= KERN_MEMORY_ERROR
;
1134 vm_fault_cleanup(object
, first_m
);
1135 thread_interrupt_level(interruptible_state
);
1137 return (VM_FAULT_MEMORY_ERROR
);
1141 * The pager wants us to restart
1142 * at the top of the chain,
1143 * typically because it has moved the
1144 * page to another pager, then do so.
1147 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1151 vm_fault_cleanup(object
, first_m
);
1152 thread_interrupt_level(interruptible_state
);
1154 return (VM_FAULT_RETRY
);
1158 * The page isn't busy, but is absent,
1159 * therefore it's deemed "unavailable".
1161 * Remove the non-existent page (unless it's
1162 * in the top object) and move on down to the
1163 * next object (if there is one).
1166 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1168 next_object
= object
->shadow
;
1170 if (next_object
== VM_OBJECT_NULL
) {
1172 * Absent page at bottom of shadow
1173 * chain; zero fill the page we left
1174 * busy in the first object, and free
1177 assert(!must_be_resident
);
1180 * check for any conditions that prevent
1181 * us from creating a new zero-fill page
1182 * vm_fault_check will do all of the
1183 * fault cleanup in the case of an error condition
1184 * including resetting the thread_interrupt_level
1186 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1188 if (error
!= VM_FAULT_SUCCESS
)
1192 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
1197 if (object
!= first_object
) {
1199 * free the absent page we just found
1204 * drop reference and lock on current object
1206 vm_object_paging_end(object
);
1207 vm_object_unlock(object
);
1210 * grab the original page we
1211 * 'soldered' in place and
1212 * retake lock on 'first_object'
1215 first_m
= VM_PAGE_NULL
;
1217 object
= first_object
;
1218 offset
= first_offset
;
1220 vm_object_lock(object
);
1223 * we're going to use the absent page we just found
1224 * so convert it to a 'busy' page
1229 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1232 * zero-fill the page and put it on
1233 * the correct paging queue
1235 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1239 if (must_be_resident
)
1240 vm_object_paging_end(object
);
1241 else if (object
!= first_object
) {
1242 vm_object_paging_end(object
);
1249 vm_page_lockspin_queues();
1251 assert(!m
->pageout_queue
);
1252 VM_PAGE_QUEUES_REMOVE(m
);
1254 vm_page_unlock_queues();
1257 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1260 offset
+object
->vo_shadow_offset
,0);
1262 offset
+= object
->vo_shadow_offset
;
1263 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1264 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1265 access_required
= VM_PROT_READ
;
1267 vm_object_lock(next_object
);
1268 vm_object_unlock(object
);
1269 object
= next_object
;
1270 vm_object_paging_begin(object
);
1273 * reset to default type of fault
1275 my_fault
= DBG_CACHE_HIT_FAULT
;
1281 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1282 && (fault_type
& VM_PROT_WRITE
)) {
1284 * This is a copy-on-write fault that will
1285 * cause us to revoke access to this page, but
1286 * this page is in the process of being cleaned
1287 * in a clustered pageout. We must wait until
1288 * the cleaning operation completes before
1289 * revoking access to the original page,
1290 * otherwise we might attempt to remove a
1294 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1297 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1301 * take an extra ref so that object won't die
1303 vm_object_reference_locked(object
);
1305 vm_fault_cleanup(object
, first_m
);
1307 counter(c_vm_fault_page_block_backoff_kernel
++);
1308 vm_object_lock(object
);
1309 assert(object
->ref_count
> 0);
1311 m
= vm_page_lookup(object
, offset
);
1313 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
1314 PAGE_ASSERT_WAIT(m
, interruptible
);
1316 vm_object_unlock(object
);
1317 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1318 vm_object_deallocate(object
);
1322 vm_object_unlock(object
);
1324 vm_object_deallocate(object
);
1325 thread_interrupt_level(interruptible_state
);
1327 return (VM_FAULT_RETRY
);
1330 if (type_of_fault
== NULL
&& m
->speculative
&&
1331 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1333 * If we were passed a non-NULL pointer for
1334 * "type_of_fault", than we came from
1335 * vm_fault... we'll let it deal with
1336 * this condition, since it
1337 * needs to see m->speculative to correctly
1338 * account the pageins, otherwise...
1339 * take it off the speculative queue, we'll
1340 * let the caller of vm_fault_page deal
1341 * with getting it onto the correct queue
1343 * If the caller specified in fault_info that
1344 * it wants a "stealth" fault, we also leave
1345 * the page in the speculative queue.
1347 vm_page_lockspin_queues();
1349 VM_PAGE_QUEUES_REMOVE(m
);
1350 vm_page_unlock_queues();
1356 * the user needs access to a page that we
1357 * encrypted before paging it out.
1358 * Decrypt the page now.
1359 * Keep it busy to prevent anyone from
1360 * accessing it during the decryption.
1363 vm_page_decrypt(m
, 0);
1364 assert(object
== m
->object
);
1366 PAGE_WAKEUP_DONE(m
);
1369 * Retry from the top, in case
1370 * something changed while we were
1375 ASSERT_PAGE_DECRYPTED(m
);
1377 if (m
->object
->code_signed
) {
1380 * We just paged in a page from a signed
1381 * memory object but we don't need to
1382 * validate it now. We'll validate it if
1383 * when it gets mapped into a user address
1384 * space for the first time or when the page
1385 * gets copied to another object as a result
1386 * of a copy-on-write.
1391 * We mark the page busy and leave it on
1392 * the pageout queues. If the pageout
1393 * deamon comes across it, then it will
1394 * remove the page from the queue, but not the object
1397 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1400 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1401 object
, offset
, m
, 0, 0);
1411 * we get here when there is no page present in the object at
1412 * the offset we're interested in... we'll allocate a page
1413 * at this point if the pager associated with
1414 * this object can provide the data or we're the top object...
1415 * object is locked; m == NULL
1417 if (must_be_resident
) {
1418 if (fault_type
== VM_PROT_NONE
&&
1419 object
== kernel_object
) {
1421 * We've been called from vm_fault_unwire()
1422 * while removing a map entry that was allocated
1423 * with KMA_KOBJECT and KMA_VAONLY. This page
1424 * is not present and there's nothing more to
1425 * do here (nothing to unwire).
1427 vm_fault_cleanup(object
, first_m
);
1428 thread_interrupt_level(interruptible_state
);
1430 return VM_FAULT_MEMORY_ERROR
;
1433 goto dont_look_for_page
;
1437 data_supply
= FALSE
;
1438 #endif /* !MACH_PAGEMAP */
1440 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
, external_state
) == TRUE
) && !data_supply
);
1443 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1445 if (!look_for_page
&& object
== first_object
&& !object
->phys_contiguous
) {
1447 * Allocate a new page for this object/offset pair as a placeholder
1451 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1453 if (m
== VM_PAGE_NULL
) {
1455 vm_fault_cleanup(object
, first_m
);
1456 thread_interrupt_level(interruptible_state
);
1458 return (VM_FAULT_MEMORY_SHORTAGE
);
1461 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1462 vm_page_insert_internal(m
, object
, offset
, FALSE
, TRUE
, TRUE
);
1464 vm_page_insert(m
, object
, offset
);
1467 if (look_for_page
) {
1472 * If the memory manager is not ready, we
1473 * cannot make requests.
1475 if (!object
->pager_ready
) {
1477 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1479 if (m
!= VM_PAGE_NULL
)
1483 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1484 object
, offset
, 0, 0, 0);
1487 * take an extra ref so object won't die
1489 vm_object_reference_locked(object
);
1490 vm_fault_cleanup(object
, first_m
);
1491 counter(c_vm_fault_page_block_backoff_kernel
++);
1493 vm_object_lock(object
);
1494 assert(object
->ref_count
> 0);
1496 if (!object
->pager_ready
) {
1497 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1499 vm_object_unlock(object
);
1500 if (wait_result
== THREAD_WAITING
)
1501 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1502 vm_object_deallocate(object
);
1506 vm_object_unlock(object
);
1507 vm_object_deallocate(object
);
1508 thread_interrupt_level(interruptible_state
);
1510 return (VM_FAULT_RETRY
);
1513 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1515 * If there are too many outstanding page
1516 * requests pending on this external object, we
1517 * wait for them to be resolved now.
1520 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1522 if (m
!= VM_PAGE_NULL
)
1525 * take an extra ref so object won't die
1527 vm_object_reference_locked(object
);
1529 vm_fault_cleanup(object
, first_m
);
1531 counter(c_vm_fault_page_block_backoff_kernel
++);
1533 vm_object_lock(object
);
1534 assert(object
->ref_count
> 0);
1536 if (object
->paging_in_progress
>= vm_object_pagein_throttle
) {
1537 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS
, interruptible
);
1539 vm_object_unlock(object
);
1540 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1541 vm_object_deallocate(object
);
1545 vm_object_unlock(object
);
1546 vm_object_deallocate(object
);
1547 thread_interrupt_level(interruptible_state
);
1549 return (VM_FAULT_RETRY
);
1552 if (object
->internal
&&
1553 (COMPRESSED_PAGER_IS_ACTIVE
1554 || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
)) {
1555 int compressed_count_delta
;
1557 if (m
== VM_PAGE_NULL
) {
1559 * Allocate a new page for this object/offset pair as a placeholder
1563 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1565 if (m
== VM_PAGE_NULL
) {
1567 vm_fault_cleanup(object
, first_m
);
1568 thread_interrupt_level(interruptible_state
);
1570 return (VM_FAULT_MEMORY_SHORTAGE
);
1574 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1575 vm_page_insert_internal(m
, object
, offset
, FALSE
, TRUE
, TRUE
);
1577 vm_page_insert(m
, object
, offset
);
1583 pager
= object
->pager
;
1585 assert(object
->paging_in_progress
> 0);
1586 vm_object_unlock(object
);
1588 rc
= vm_compressor_pager_get(
1590 offset
+ object
->paging_offset
,
1594 &compressed_count_delta
);
1596 if (type_of_fault
== NULL
) {
1600 * we weren't called from vm_fault, so we
1601 * need to apply page creation throttling
1602 * do it before we re-acquire any locks
1604 if (my_fault_type
== DBG_COMPRESSOR_FAULT
) {
1605 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
1606 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 1, 0);
1607 delay(throttle_delay
);
1611 vm_object_lock(object
);
1612 assert(object
->paging_in_progress
> 0);
1614 vm_compressor_pager_count(
1616 compressed_count_delta
,
1617 FALSE
, /* shared_lock */
1624 if ((m
->object
->wimg_bits
&
1626 VM_WIMG_USE_DEFAULT
) {
1628 * If the page is not cacheable,
1629 * we can't let its contents
1630 * linger in the data cache
1631 * after the decompression.
1633 pmap_sync_page_attributes_phys(
1636 m
->written_by_kernel
= TRUE
;
1640 * If the object is purgeable, its
1641 * owner's purgeable ledgers have been
1642 * updated in vm_page_insert() but the
1643 * page was also accounted for in a
1644 * "compressed purgeable" ledger, so
1647 if ((object
->purgable
!=
1648 VM_PURGABLE_DENY
) &&
1649 (object
->vo_purgeable_owner
!=
1652 * One less compressed
1655 vm_purgeable_compressed_update(
1661 case KERN_MEMORY_FAILURE
:
1666 case KERN_MEMORY_ERROR
:
1670 panic("vm_fault_page(): unexpected "
1672 "vm_compressor_pager_get()\n",
1675 PAGE_WAKEUP_DONE(m
);
1678 goto data_requested
;
1680 my_fault_type
= DBG_PAGEIN_FAULT
;
1682 if (m
!= VM_PAGE_NULL
) {
1688 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1692 * It's possible someone called vm_object_destroy while we weren't
1693 * holding the object lock. If that has happened, then bail out
1697 pager
= object
->pager
;
1699 if (pager
== MEMORY_OBJECT_NULL
) {
1700 vm_fault_cleanup(object
, first_m
);
1701 thread_interrupt_level(interruptible_state
);
1702 return VM_FAULT_MEMORY_ERROR
;
1706 * We have an absent page in place for the faulting offset,
1707 * so we can release the object lock.
1710 vm_object_unlock(object
);
1713 * If this object uses a copy_call strategy,
1714 * and we are interested in a copy of this object
1715 * (having gotten here only by following a
1716 * shadow chain), then tell the memory manager
1717 * via a flag added to the desired_access
1718 * parameter, so that it can detect a race
1719 * between our walking down the shadow chain
1720 * and its pushing pages up into a copy of
1721 * the object that it manages.
1723 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
)
1724 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1726 wants_copy_flag
= VM_PROT_NONE
;
1729 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1731 access_required
| wants_copy_flag
, 0);
1733 if (object
->copy
== first_object
) {
1735 * if we issue the memory_object_data_request in
1736 * this state, we are subject to a deadlock with
1737 * the underlying filesystem if it is trying to
1738 * shrink the file resulting in a push of pages
1739 * into the copy object... that push will stall
1740 * on the placeholder page, and if the pushing thread
1741 * is holding a lock that is required on the pagein
1742 * path (such as a truncate lock), we'll deadlock...
1743 * to avoid this potential deadlock, we throw away
1744 * our placeholder page before calling memory_object_data_request
1745 * and force this thread to retry the vm_fault_page after
1746 * we have issued the I/O. the second time through this path
1747 * we will find the page already in the cache (presumably still
1748 * busy waiting for the I/O to complete) and then complete
1749 * the fault w/o having to go through memory_object_data_request again
1751 assert(first_m
!= VM_PAGE_NULL
);
1752 assert(first_m
->object
== first_object
);
1754 vm_object_lock(first_object
);
1755 VM_PAGE_FREE(first_m
);
1756 vm_object_paging_end(first_object
);
1757 vm_object_unlock(first_object
);
1759 first_m
= VM_PAGE_NULL
;
1760 force_fault_retry
= TRUE
;
1762 vm_fault_page_forced_retry
++;
1765 if (data_already_requested
== TRUE
) {
1766 orig_behavior
= fault_info
->behavior
;
1767 orig_cluster_size
= fault_info
->cluster_size
;
1769 fault_info
->behavior
= VM_BEHAVIOR_RANDOM
;
1770 fault_info
->cluster_size
= PAGE_SIZE
;
1773 * Call the memory manager to retrieve the data.
1775 rc
= memory_object_data_request(
1777 offset
+ object
->paging_offset
,
1779 access_required
| wants_copy_flag
,
1780 (memory_object_fault_info_t
)fault_info
);
1782 if (data_already_requested
== TRUE
) {
1783 fault_info
->behavior
= orig_behavior
;
1784 fault_info
->cluster_size
= orig_cluster_size
;
1786 data_already_requested
= TRUE
;
1788 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1790 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1792 vm_object_lock(object
);
1795 if (rc
!= KERN_SUCCESS
) {
1797 vm_fault_cleanup(object
, first_m
);
1798 thread_interrupt_level(interruptible_state
);
1800 return ((rc
== MACH_SEND_INTERRUPTED
) ?
1801 VM_FAULT_INTERRUPTED
:
1802 VM_FAULT_MEMORY_ERROR
);
1805 clock_usec_t tv_usec
;
1807 if (my_fault_type
== DBG_PAGEIN_FAULT
) {
1808 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1809 current_thread()->t_page_creation_time
= tv_sec
;
1810 current_thread()->t_page_creation_count
= 0;
1813 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_flags
& TH_SFLAG_ABORT
)) {
1815 vm_fault_cleanup(object
, first_m
);
1816 thread_interrupt_level(interruptible_state
);
1818 return (VM_FAULT_INTERRUPTED
);
1820 if (force_fault_retry
== TRUE
) {
1822 vm_fault_cleanup(object
, first_m
);
1823 thread_interrupt_level(interruptible_state
);
1825 return (VM_FAULT_RETRY
);
1827 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1829 * No page here means that the object we
1830 * initially looked up was "physically
1831 * contiguous" (i.e. device memory). However,
1832 * with Virtual VRAM, the object might not
1833 * be backed by that device memory anymore,
1834 * so we're done here only if the object is
1835 * still "phys_contiguous".
1836 * Otherwise, if the object is no longer
1837 * "phys_contiguous", we need to retry the
1838 * page fault against the object's new backing
1839 * store (different memory object).
1845 * potentially a pagein fault
1846 * if we make it through the state checks
1847 * above, than we'll count it as such
1849 my_fault
= my_fault_type
;
1852 * Retry with same object/offset, since new data may
1853 * be in a different page (i.e., m is meaningless at
1860 * We get here if the object has no pager, or an existence map
1861 * exists and indicates the page isn't present on the pager
1862 * or we're unwiring a page. If a pager exists, but there
1863 * is no existence map, then the m->absent case above handles
1864 * the ZF case when the pager can't provide the page
1867 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1869 if (object
== first_object
)
1872 assert(m
== VM_PAGE_NULL
);
1875 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1879 next_object
= object
->shadow
;
1881 if (next_object
== VM_OBJECT_NULL
) {
1883 * we've hit the bottom of the shadown chain,
1884 * fill the page in the top object with zeros.
1886 assert(!must_be_resident
);
1888 if (object
!= first_object
) {
1889 vm_object_paging_end(object
);
1890 vm_object_unlock(object
);
1892 object
= first_object
;
1893 offset
= first_offset
;
1894 vm_object_lock(object
);
1897 assert(m
->object
== object
);
1898 first_m
= VM_PAGE_NULL
;
1901 * check for any conditions that prevent
1902 * us from creating a new zero-fill page
1903 * vm_fault_check will do all of the
1904 * fault cleanup in the case of an error condition
1905 * including resetting the thread_interrupt_level
1907 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1909 if (error
!= VM_FAULT_SUCCESS
)
1912 if (m
== VM_PAGE_NULL
) {
1915 if (m
== VM_PAGE_NULL
) {
1916 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1917 thread_interrupt_level(interruptible_state
);
1919 return (VM_FAULT_MEMORY_SHORTAGE
);
1921 vm_page_insert(m
, object
, offset
);
1923 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1926 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1932 * Move on to the next object. Lock the next
1933 * object before unlocking the current one.
1935 if ((object
!= first_object
) || must_be_resident
)
1936 vm_object_paging_end(object
);
1938 offset
+= object
->vo_shadow_offset
;
1939 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1940 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1941 access_required
= VM_PROT_READ
;
1943 vm_object_lock(next_object
);
1944 vm_object_unlock(object
);
1946 object
= next_object
;
1947 vm_object_paging_begin(object
);
1952 * PAGE HAS BEEN FOUND.
1955 * busy, so that we can play with it;
1956 * not absent, so that nobody else will fill it;
1957 * possibly eligible for pageout;
1959 * The top-level page (first_m) is:
1960 * VM_PAGE_NULL if the page was found in the
1962 * busy, not absent, and ineligible for pageout.
1964 * The current object (object) is locked. A paging
1965 * reference is held for the current and top-level
1970 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1972 #if EXTRA_ASSERTIONS
1973 assert(m
->busy
&& !m
->absent
);
1974 assert((first_m
== VM_PAGE_NULL
) ||
1975 (first_m
->busy
&& !first_m
->absent
&&
1976 !first_m
->active
&& !first_m
->inactive
));
1977 #endif /* EXTRA_ASSERTIONS */
1981 * If we found a page, we must have decrypted it before we
1984 ASSERT_PAGE_DECRYPTED(m
);
1987 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1989 first_object
, first_m
);
1992 * If the page is being written, but isn't
1993 * already owned by the top-level object,
1994 * we have to copy it into a new page owned
1995 * by the top-level object.
1997 if (object
!= first_object
) {
2000 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2002 if (fault_type
& VM_PROT_WRITE
) {
2006 * We only really need to copy if we
2009 assert(!must_be_resident
);
2012 * are we protecting the system from
2013 * backing store exhaustion. If so
2014 * sleep unless we are privileged.
2016 if (vm_backing_store_low
) {
2017 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
2020 vm_fault_cleanup(object
, first_m
);
2022 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
2024 thread_block(THREAD_CONTINUE_NULL
);
2025 thread_interrupt_level(interruptible_state
);
2027 return (VM_FAULT_RETRY
);
2031 * If we try to collapse first_object at this
2032 * point, we may deadlock when we try to get
2033 * the lock on an intermediate object (since we
2034 * have the bottom object locked). We can't
2035 * unlock the bottom object, because the page
2036 * we found may move (by collapse) if we do.
2038 * Instead, we first copy the page. Then, when
2039 * we have no more use for the bottom object,
2040 * we unlock it and try to collapse.
2042 * Note that we copy the page even if we didn't
2043 * need to... that's the breaks.
2047 * Allocate a page for the copy
2049 copy_m
= vm_page_grab();
2051 if (copy_m
== VM_PAGE_NULL
) {
2054 vm_fault_cleanup(object
, first_m
);
2055 thread_interrupt_level(interruptible_state
);
2057 return (VM_FAULT_MEMORY_SHORTAGE
);
2060 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
2064 vm_page_copy(m
, copy_m
);
2067 * If another map is truly sharing this
2068 * page with us, we have to flush all
2069 * uses of the original page, since we
2070 * can't distinguish those which want the
2071 * original from those which need the
2074 * XXXO If we know that only one map has
2075 * access to this page, then we could
2076 * avoid the pmap_disconnect() call.
2079 pmap_disconnect(m
->phys_page
);
2082 VM_PAGE_COUNT_AS_PAGEIN(m
);
2083 VM_PAGE_CONSUME_CLUSTERED(m
);
2085 assert(!m
->cleaning
);
2088 * We no longer need the old page or object.
2092 vm_object_paging_end(object
);
2093 vm_object_unlock(object
);
2095 my_fault
= DBG_COW_FAULT
;
2096 VM_STAT_INCR(cow_faults
);
2097 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2098 current_task()->cow_faults
++;
2100 object
= first_object
;
2101 offset
= first_offset
;
2103 vm_object_lock(object
);
2105 * get rid of the place holder
2106 * page that we soldered in earlier
2108 VM_PAGE_FREE(first_m
);
2109 first_m
= VM_PAGE_NULL
;
2112 * and replace it with the
2113 * page we just copied into
2115 assert(copy_m
->busy
);
2116 vm_page_insert(copy_m
, object
, offset
);
2117 SET_PAGE_DIRTY(copy_m
, TRUE
);
2121 * Now that we've gotten the copy out of the
2122 * way, let's try to collapse the top object.
2123 * But we have to play ugly games with
2124 * paging_in_progress to do that...
2126 vm_object_paging_end(object
);
2127 vm_object_collapse(object
, offset
, TRUE
);
2128 vm_object_paging_begin(object
);
2131 *protection
&= (~VM_PROT_WRITE
);
2134 * Now check whether the page needs to be pushed into the
2135 * copy object. The use of asymmetric copy on write for
2136 * shared temporary objects means that we may do two copies to
2137 * satisfy the fault; one above to get the page from a
2138 * shadowed object, and one here to push it into the copy.
2140 try_failed_count
= 0;
2142 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
2143 vm_object_offset_t copy_offset
;
2147 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2150 * If the page is being written, but hasn't been
2151 * copied to the copy-object, we have to copy it there.
2153 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2154 *protection
&= ~VM_PROT_WRITE
;
2159 * If the page was guaranteed to be resident,
2160 * we must have already performed the copy.
2162 if (must_be_resident
)
2166 * Try to get the lock on the copy_object.
2168 if (!vm_object_lock_try(copy_object
)) {
2170 vm_object_unlock(object
);
2173 mutex_pause(try_failed_count
); /* wait a bit */
2174 vm_object_lock(object
);
2178 try_failed_count
= 0;
2181 * Make another reference to the copy-object,
2182 * to keep it from disappearing during the
2185 vm_object_reference_locked(copy_object
);
2188 * Does the page exist in the copy?
2190 copy_offset
= first_offset
- copy_object
->vo_shadow_offset
;
2192 if (copy_object
->vo_size
<= copy_offset
)
2194 * Copy object doesn't cover this page -- do nothing.
2197 else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
2199 * Page currently exists in the copy object
2203 * If the page is being brought
2204 * in, wait for it and then retry.
2209 * take an extra ref so object won't die
2211 vm_object_reference_locked(copy_object
);
2212 vm_object_unlock(copy_object
);
2213 vm_fault_cleanup(object
, first_m
);
2214 counter(c_vm_fault_page_block_backoff_kernel
++);
2216 vm_object_lock(copy_object
);
2217 assert(copy_object
->ref_count
> 0);
2218 VM_OBJ_RES_DECR(copy_object
);
2219 vm_object_lock_assert_exclusive(copy_object
);
2220 copy_object
->ref_count
--;
2221 assert(copy_object
->ref_count
> 0);
2222 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
2225 * it's OK if the "copy_m" page is encrypted,
2226 * because we're not moving it nor handling its
2229 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
2230 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
2232 vm_object_unlock(copy_object
);
2233 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2234 vm_object_deallocate(copy_object
);
2238 vm_object_unlock(copy_object
);
2239 vm_object_deallocate(copy_object
);
2240 thread_interrupt_level(interruptible_state
);
2242 return (VM_FAULT_RETRY
);
2246 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
2248 * If PAGED_OUT is TRUE, then the page used to exist
2249 * in the copy-object, and has already been paged out.
2250 * We don't need to repeat this. If PAGED_OUT is
2251 * FALSE, then either we don't know (!pager_created,
2252 * for example) or it hasn't been paged out.
2253 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2254 * We must copy the page to the copy object.
2257 if (vm_backing_store_low
) {
2259 * we are protecting the system from
2260 * backing store exhaustion. If so
2261 * sleep unless we are privileged.
2263 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
2264 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
2267 VM_OBJ_RES_DECR(copy_object
);
2268 vm_object_lock_assert_exclusive(copy_object
);
2269 copy_object
->ref_count
--;
2270 assert(copy_object
->ref_count
> 0);
2272 vm_object_unlock(copy_object
);
2273 vm_fault_cleanup(object
, first_m
);
2274 thread_block(THREAD_CONTINUE_NULL
);
2275 thread_interrupt_level(interruptible_state
);
2277 return (VM_FAULT_RETRY
);
2281 * Allocate a page for the copy
2283 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
2285 if (copy_m
== VM_PAGE_NULL
) {
2288 VM_OBJ_RES_DECR(copy_object
);
2289 vm_object_lock_assert_exclusive(copy_object
);
2290 copy_object
->ref_count
--;
2291 assert(copy_object
->ref_count
> 0);
2293 vm_object_unlock(copy_object
);
2294 vm_fault_cleanup(object
, first_m
);
2295 thread_interrupt_level(interruptible_state
);
2297 return (VM_FAULT_MEMORY_SHORTAGE
);
2300 * Must copy page into copy-object.
2302 vm_page_copy(m
, copy_m
);
2305 * If the old page was in use by any users
2306 * of the copy-object, it must be removed
2307 * from all pmaps. (We can't know which
2311 pmap_disconnect(m
->phys_page
);
2314 VM_PAGE_COUNT_AS_PAGEIN(m
);
2315 VM_PAGE_CONSUME_CLUSTERED(m
);
2318 * If there's a pager, then immediately
2319 * page out this page, using the "initialize"
2320 * option. Else, we use the copy.
2322 if ((!copy_object
->pager_ready
)
2324 || vm_external_state_get(copy_object
->existence_map
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2326 || VM_COMPRESSOR_PAGER_STATE_GET(copy_object
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2329 vm_page_lockspin_queues();
2330 assert(!m
->cleaning
);
2331 vm_page_activate(copy_m
);
2332 vm_page_unlock_queues();
2334 SET_PAGE_DIRTY(copy_m
, TRUE
);
2335 PAGE_WAKEUP_DONE(copy_m
);
2337 } else if (copy_object
->internal
&&
2338 (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
)) {
2340 * For internal objects check with the pager to see
2341 * if the page already exists in the backing store.
2342 * If yes, then we can drop the copy page. If not,
2343 * then we'll activate it, mark it dirty and keep it
2347 kern_return_t kr
= KERN_SUCCESS
;
2349 memory_object_t copy_pager
= copy_object
->pager
;
2350 assert(copy_pager
!= MEMORY_OBJECT_NULL
);
2351 vm_object_paging_begin(copy_object
);
2353 vm_object_unlock(copy_object
);
2355 kr
= memory_object_data_request(
2357 copy_offset
+ copy_object
->paging_offset
,
2358 0, /* Only query the pager. */
2362 vm_object_lock(copy_object
);
2364 vm_object_paging_end(copy_object
);
2367 * Since we dropped the copy_object's lock,
2368 * check whether we'll have to deallocate
2371 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2372 vm_object_unlock(copy_object
);
2373 vm_object_deallocate(copy_object
);
2374 vm_object_lock(object
);
2378 if (kr
== KERN_SUCCESS
) {
2380 * The pager has the page. We don't want to overwrite
2381 * that page by sending this one out to the backing store.
2382 * So we drop the copy page.
2384 VM_PAGE_FREE(copy_m
);
2388 * The pager doesn't have the page. We'll keep this one
2389 * around in the copy object. It might get sent out to
2390 * the backing store under memory pressure.
2392 vm_page_lockspin_queues();
2393 assert(!m
->cleaning
);
2394 vm_page_activate(copy_m
);
2395 vm_page_unlock_queues();
2397 SET_PAGE_DIRTY(copy_m
, TRUE
);
2398 PAGE_WAKEUP_DONE(copy_m
);
2402 assert(copy_m
->busy
== TRUE
);
2403 assert(!m
->cleaning
);
2406 * dirty is protected by the object lock
2408 SET_PAGE_DIRTY(copy_m
, TRUE
);
2411 * The page is already ready for pageout:
2412 * not on pageout queues and busy.
2413 * Unlock everything except the
2414 * copy_object itself.
2416 vm_object_unlock(object
);
2419 * Write the page to the copy-object,
2420 * flushing it from the kernel.
2422 vm_pageout_initialize_page(copy_m
);
2425 * Since the pageout may have
2426 * temporarily dropped the
2427 * copy_object's lock, we
2428 * check whether we'll have
2429 * to deallocate the hard way.
2431 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2432 vm_object_unlock(copy_object
);
2433 vm_object_deallocate(copy_object
);
2434 vm_object_lock(object
);
2439 * Pick back up the old object's
2440 * lock. [It is safe to do so,
2441 * since it must be deeper in the
2444 vm_object_lock(object
);
2448 * Because we're pushing a page upward
2449 * in the object tree, we must restart
2450 * any faults that are waiting here.
2451 * [Note that this is an expansion of
2452 * PAGE_WAKEUP that uses the THREAD_RESTART
2453 * wait result]. Can't turn off the page's
2454 * busy bit because we're not done with it.
2458 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2462 * The reference count on copy_object must be
2463 * at least 2: one for our extra reference,
2464 * and at least one from the outside world
2465 * (we checked that when we last locked
2468 vm_object_lock_assert_exclusive(copy_object
);
2469 copy_object
->ref_count
--;
2470 assert(copy_object
->ref_count
> 0);
2472 VM_OBJ_RES_DECR(copy_object
);
2473 vm_object_unlock(copy_object
);
2480 *top_page
= first_m
;
2483 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2484 object
, offset
, m
, first_m
, 0);
2486 if (m
!= VM_PAGE_NULL
) {
2487 retval
= VM_FAULT_SUCCESS
;
2489 if (my_fault
== DBG_PAGEIN_FAULT
) {
2491 VM_PAGE_COUNT_AS_PAGEIN(m
);
2493 if (m
->object
->internal
)
2494 my_fault
= DBG_PAGEIND_FAULT
;
2496 my_fault
= DBG_PAGEINV_FAULT
;
2499 * evaluate access pattern and update state
2500 * vm_fault_deactivate_behind depends on the
2501 * state being up to date
2503 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2505 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2506 } else if (my_fault
== DBG_COMPRESSOR_FAULT
|| my_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
) {
2508 VM_STAT_INCR(decompressions
);
2511 *type_of_fault
= my_fault
;
2513 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2514 assert(first_m
== VM_PAGE_NULL
);
2515 assert(object
== first_object
);
2518 thread_interrupt_level(interruptible_state
);
2521 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2526 thread_interrupt_level(interruptible_state
);
2528 if (wait_result
== THREAD_INTERRUPTED
)
2529 return (VM_FAULT_INTERRUPTED
);
2530 return (VM_FAULT_RETRY
);
2539 * When soft faulting a page, we have to validate the page if:
2540 * 1. the page is being mapped in user space
2541 * 2. the page hasn't already been found to be "tainted"
2542 * 3. the page belongs to a code-signed object
2543 * 4. the page has not been validated yet or has been mapped for write.
2545 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page) \
2546 ((pmap) != kernel_pmap /*1*/ && \
2547 !(page)->cs_tainted /*2*/ && \
2548 (page)->object->code_signed /*3*/ && \
2549 (!(page)->cs_validated || (page)->wpmapped /*4*/))
2553 * page queue lock must NOT be held
2554 * m->object must be locked
2556 * NOTE: m->object could be locked "shared" only if we are called
2557 * from vm_fault() as part of a soft fault. If so, we must be
2558 * careful not to modify the VM object in any way that is not
2559 * legal under a shared lock...
2561 extern int proc_selfpid(void);
2562 extern char *proc_name_address(void *p
);
2563 unsigned long cs_enter_tainted_rejected
= 0;
2564 unsigned long cs_enter_tainted_accepted
= 0;
2566 vm_fault_enter(vm_page_t m
,
2568 vm_map_offset_t vaddr
,
2570 vm_prot_t fault_type
,
2572 boolean_t change_wiring
,
2574 boolean_t cs_bypass
,
2575 __unused
int user_tag
,
2577 boolean_t
*need_retry
,
2580 kern_return_t kr
, pe_result
;
2581 boolean_t previously_pmapped
= m
->pmapped
;
2582 boolean_t must_disconnect
= 0;
2583 boolean_t map_is_switched
, map_is_switch_protected
;
2584 int cs_enforcement_enabled
;
2586 vm_object_lock_assert_held(m
->object
);
2588 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2591 if (m
->phys_page
== vm_page_guard_addr
) {
2592 assert(m
->fictitious
);
2593 return KERN_SUCCESS
;
2596 if (*type_of_fault
== DBG_ZERO_FILL_FAULT
) {
2598 vm_object_lock_assert_exclusive(m
->object
);
2600 } else if ((fault_type
& VM_PROT_WRITE
) == 0) {
2602 * This is not a "write" fault, so we
2603 * might not have taken the object lock
2604 * exclusively and we might not be able
2605 * to update the "wpmapped" bit in
2607 * Let's just grant read access to
2608 * the page for now and we'll
2609 * soft-fault again if we need write
2612 prot
&= ~VM_PROT_WRITE
;
2614 if (m
->pmapped
== FALSE
) {
2617 if (*type_of_fault
== DBG_CACHE_HIT_FAULT
) {
2619 * found it in the cache, but this
2620 * is the first fault-in of the page (m->pmapped == FALSE)
2621 * so it must have come in as part of
2622 * a cluster... account 1 pagein against it
2624 if (m
->object
->internal
)
2625 *type_of_fault
= DBG_PAGEIND_FAULT
;
2627 *type_of_fault
= DBG_PAGEINV_FAULT
;
2629 VM_PAGE_COUNT_AS_PAGEIN(m
);
2631 VM_PAGE_CONSUME_CLUSTERED(m
);
2635 if (*type_of_fault
!= DBG_COW_FAULT
) {
2636 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2638 if (pmap
== kernel_pmap
) {
2639 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2643 /* Validate code signature if necessary. */
2644 if (VM_FAULT_NEED_CS_VALIDATION(pmap
, m
)) {
2645 vm_object_lock_assert_exclusive(m
->object
);
2647 if (m
->cs_validated
) {
2648 vm_cs_revalidates
++;
2651 /* VM map is locked, so 1 ref will remain on VM object -
2652 * so no harm if vm_page_validate_cs drops the object lock */
2653 vm_page_validate_cs(m
);
2656 #define page_immutable(m,prot) ((m)->cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/)
2657 #define page_nx(m) ((m)->cs_nx)
2659 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2660 (pmap
== vm_map_pmap(current_thread()->map
)));
2661 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2663 /* If the map is switched, and is switch-protected, we must protect
2664 * some pages from being write-faulted: immutable pages because by
2665 * definition they may not be written, and executable pages because that
2666 * would provide a way to inject unsigned code.
2667 * If the page is immutable, we can simply return. However, we can't
2668 * immediately determine whether a page is executable anywhere. But,
2669 * we can disconnect it everywhere and remove the executable protection
2670 * from the current map. We do that below right before we do the
2673 cs_enforcement_enabled
= cs_enforcement(NULL
);
2675 if(cs_enforcement_enabled
&& map_is_switched
&&
2676 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2677 (prot
& VM_PROT_WRITE
))
2679 return KERN_CODESIGN_ERROR
;
2682 if (cs_enforcement_enabled
&& page_nx(m
) && (prot
& VM_PROT_EXECUTE
)) {
2684 printf("page marked to be NX, not letting it be mapped EXEC\n");
2685 return KERN_CODESIGN_ERROR
;
2688 /* A page could be tainted, or pose a risk of being tainted later.
2689 * Check whether the receiving process wants it, and make it feel
2690 * the consequences (that hapens in cs_invalid_page()).
2691 * For CS Enforcement, two other conditions will
2692 * cause that page to be tainted as well:
2693 * - pmapping an unsigned page executable - this means unsigned code;
2694 * - writeable mapping of a validated page - the content of that page
2695 * can be changed without the kernel noticing, therefore unsigned
2696 * code can be created
2698 if (m
->cs_tainted
||
2699 ((cs_enforcement_enabled
&& !cs_bypass
) &&
2700 (/* The page is unsigned and wants to be executable */
2701 (!m
->cs_validated
&& (prot
& VM_PROT_EXECUTE
)) ||
2702 /* The page should be immutable, but is in danger of being modified
2703 * This is the case where we want policy from the code directory -
2704 * is the page immutable or not? For now we have to assume that
2705 * code pages will be immutable, data pages not.
2706 * We'll assume a page is a code page if it has a code directory
2707 * and we fault for execution.
2708 * That is good enough since if we faulted the code page for
2709 * writing in another map before, it is wpmapped; if we fault
2710 * it for writing in this map later it will also be faulted for executing
2711 * at the same time; and if we fault for writing in another map
2712 * later, we will disconnect it from this pmap so we'll notice
2715 (page_immutable(m
, prot
) && ((prot
& VM_PROT_WRITE
) || m
->wpmapped
))
2719 /* We will have a tainted page. Have to handle the special case
2720 * of a switched map now. If the map is not switched, standard
2721 * procedure applies - call cs_invalid_page().
2722 * If the map is switched, the real owner is invalid already.
2723 * There is no point in invalidating the switching process since
2724 * it will not be executing from the map. So we don't call
2725 * cs_invalid_page() in that case. */
2726 boolean_t reject_page
;
2727 if(map_is_switched
) {
2728 assert(pmap
==vm_map_pmap(current_thread()->map
));
2729 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2730 reject_page
= FALSE
;
2733 printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s slid: %s prot: 0x%x\n",
2734 m
->object
->code_signed
? "yes" : "no",
2735 m
->cs_validated
? "yes" : "no",
2736 m
->cs_tainted
? "yes" : "no",
2737 m
->wpmapped
? "yes" : "no",
2738 m
->slid
? "yes" : "no",
2740 reject_page
= cs_invalid_page((addr64_t
) vaddr
);
2744 /* reject the invalid page: abort the page fault */
2746 const char *procname
;
2748 vm_object_t file_object
, shadow
;
2749 vm_object_offset_t file_offset
;
2750 char *pathname
, *filename
;
2751 vm_size_t pathname_len
, filename_len
;
2752 boolean_t truncated_path
;
2753 #define __PATH_MAX 1024
2754 struct timespec mtime
, cs_mtime
;
2756 kr
= KERN_CODESIGN_ERROR
;
2757 cs_enter_tainted_rejected
++;
2759 /* get process name and pid */
2761 task
= current_task();
2762 pid
= proc_selfpid();
2763 if (task
->bsd_info
!= NULL
)
2764 procname
= proc_name_address(task
->bsd_info
);
2766 /* get file's VM object */
2767 file_object
= m
->object
;
2768 file_offset
= m
->offset
;
2769 for (shadow
= file_object
->shadow
;
2770 shadow
!= VM_OBJECT_NULL
;
2771 shadow
= file_object
->shadow
) {
2772 vm_object_lock_shared(shadow
);
2773 if (file_object
!= m
->object
) {
2774 vm_object_unlock(file_object
);
2776 file_offset
+= file_object
->vo_shadow_offset
;
2777 file_object
= shadow
;
2782 cs_mtime
.tv_sec
= 0;
2783 cs_mtime
.tv_nsec
= 0;
2785 /* get file's pathname and/or filename */
2790 truncated_path
= FALSE
;
2791 if (file_object
->pager
== NULL
) {
2792 /* no pager -> no file -> no pathname */
2793 pathname
= (char *) "<nil>";
2795 pathname
= (char *)kalloc(__PATH_MAX
* 2);
2798 pathname_len
= __PATH_MAX
;
2799 filename
= pathname
+ pathname_len
;
2800 filename_len
= __PATH_MAX
;
2802 vnode_pager_get_object_name(file_object
->pager
,
2808 vnode_pager_get_object_mtime(file_object
->pager
,
2812 printf("CODE SIGNING: process %d[%s]: "
2813 "rejecting invalid page at address 0x%llx "
2814 "from offset 0x%llx in file \"%s%s%s\" "
2815 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2816 "(signed:%d validated:%d tainted:%d "
2817 "wpmapped:%d slid:%d)\n",
2818 pid
, procname
, (addr64_t
) vaddr
,
2820 (pathname
? pathname
: ""),
2821 (truncated_path
? "/.../" : ""),
2822 (truncated_path
? filename
: ""),
2823 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2824 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2825 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2828 mtime
.tv_sec
, mtime
.tv_nsec
,
2829 m
->object
->code_signed
,
2834 if (file_object
!= m
->object
) {
2835 vm_object_unlock(file_object
);
2837 if (pathname_len
!= 0) {
2838 kfree(pathname
, __PATH_MAX
* 2);
2843 /* proceed with the invalid page */
2845 if (!m
->cs_validated
) {
2847 * This page has not been validated, so it
2848 * must not belong to a code-signed object
2849 * and should not be forcefully considered
2851 * We're just concerned about it here because
2852 * we've been asked to "execute" it but that
2853 * does not mean that it should cause other
2855 * This happens when a debugger sets a
2856 * breakpoint and we then execute code in
2857 * that page. Marking the page as "tainted"
2858 * would cause any inspection tool ("leaks",
2859 * "vmmap", "CrashReporter", ...) to get killed
2860 * due to code-signing violation on that page,
2861 * even though they're just reading it and not
2862 * executing from it.
2864 assert(!m
->object
->code_signed
);
2867 * Page might have been tainted before or not;
2868 * now it definitively is. If the page wasn't
2869 * tainted, we must disconnect it from all
2870 * pmaps later, to force existing mappings
2871 * through that code path for re-consideration
2872 * of the validity of that page.
2874 must_disconnect
= !m
->cs_tainted
;
2875 m
->cs_tainted
= TRUE
;
2877 cs_enter_tainted_accepted
++;
2879 if (kr
!= KERN_SUCCESS
) {
2881 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2882 "*** INVALID PAGE ***\n",
2886 if (cs_enforcement_panic
) {
2887 panic("CODESIGNING: panicking on invalid page\n");
2893 /* proceed with the valid page */
2897 boolean_t page_queues_locked
= FALSE
;
2898 #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \
2900 if (! page_queues_locked) { \
2901 page_queues_locked = TRUE; \
2902 vm_page_lockspin_queues(); \
2905 #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \
2907 if (page_queues_locked) { \
2908 page_queues_locked = FALSE; \
2909 vm_page_unlock_queues(); \
2914 * Hold queues lock to manipulate
2915 * the page queues. Change wiring
2918 assert(m
->compressor
|| m
->object
!= compressor_object
);
2919 if (m
->compressor
) {
2921 * Compressor pages are neither wired
2922 * nor pageable and should never change.
2924 assert(m
->object
== compressor_object
);
2925 } else if (change_wiring
) {
2926 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
2929 if (kr
== KERN_SUCCESS
) {
2933 vm_page_unwire(m
, TRUE
);
2935 /* we keep the page queues lock, if we need it later */
2938 if (kr
!= KERN_SUCCESS
) {
2939 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
2940 vm_page_deactivate(m
);
2941 /* we keep the page queues lock, if we need it later */
2942 } else if (((!m
->active
&& !m
->inactive
) ||
2945 !VM_PAGE_WIRED(m
) && !m
->throttled
) {
2947 if (vm_page_local_q
&&
2949 (*type_of_fault
== DBG_COW_FAULT
||
2950 *type_of_fault
== DBG_ZERO_FILL_FAULT
) ) {
2954 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
2955 vm_object_lock_assert_exclusive(m
->object
);
2958 * we got a local queue to stuff this
2960 * its safe to manipulate local and
2961 * local_id at this point since we're
2962 * behind an exclusive object lock and
2963 * the page is not on any global queue.
2965 * we'll use the current cpu number to
2966 * select the queue note that we don't
2967 * need to disable preemption... we're
2968 * going to behind the local queue's
2969 * lock to do the real work
2973 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
2975 VPL_LOCK(&lq
->vpl_lock
);
2977 queue_enter(&lq
->vpl_queue
, m
,
2983 if (m
->object
->internal
)
2984 lq
->vpl_internal_count
++;
2986 lq
->vpl_external_count
++;
2988 VPL_UNLOCK(&lq
->vpl_lock
);
2990 if (lq
->vpl_count
> vm_page_local_q_soft_limit
)
2993 * we're beyond the soft limit
2994 * for the local queue
2995 * vm_page_reactivate_local will
2996 * 'try' to take the global page
2997 * queue lock... if it can't
2998 * that's ok... we'll let the
2999 * queue continue to grow up
3000 * to the hard limit... at that
3001 * point we'll wait for the
3002 * lock... once we've got the
3003 * lock, we'll transfer all of
3004 * the pages from the local
3005 * queue to the global active
3008 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
3012 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3015 * test again now that we hold the
3018 if (!VM_PAGE_WIRED(m
)) {
3019 if (m
->clean_queue
) {
3020 VM_PAGE_QUEUES_REMOVE(m
);
3022 vm_pageout_cleaned_reactivated
++;
3023 vm_pageout_cleaned_fault_reactivated
++;
3030 * If this is a no_cache mapping
3031 * and the page has never been
3032 * mapped before or was
3033 * previously a no_cache page,
3034 * then we want to leave pages
3035 * in the speculative state so
3036 * that they can be readily
3037 * recycled if free memory runs
3038 * low. Otherwise the page is
3039 * activated as normal.
3043 (!previously_pmapped
||
3047 if (!m
->speculative
)
3048 vm_page_speculate(m
, FALSE
);
3050 } else if (!m
->active
&&
3053 vm_page_activate(m
);
3057 /* we keep the page queues lock, if we need it later */
3061 /* we're done with the page queues lock, if we ever took it */
3062 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3065 /* If we have a KERN_SUCCESS from the previous checks, we either have
3066 * a good page, or a tainted page that has been accepted by the process.
3067 * In both cases the page will be entered into the pmap.
3068 * If the page is writeable, we need to disconnect it from other pmaps
3069 * now so those processes can take note.
3071 if (kr
== KERN_SUCCESS
) {
3074 * NOTE: we may only hold the vm_object lock SHARED
3075 * at this point, so we need the phys_page lock to
3076 * properly serialize updating the pmapped and
3079 if ((prot
& VM_PROT_EXECUTE
) && !m
->xpmapped
) {
3081 pmap_lock_phys_page(m
->phys_page
);
3083 * go ahead and take the opportunity
3084 * to set 'pmapped' here so that we don't
3085 * need to grab this lock a 2nd time
3094 pmap_unlock_phys_page(m
->phys_page
);
3096 if (!m
->object
->internal
)
3097 OSAddAtomic(1, &vm_page_xpmapped_external_count
);
3099 if ((COMPRESSED_PAGER_IS_ACTIVE
) &&
3100 m
->object
->internal
&&
3101 m
->object
->pager
!= NULL
) {
3103 * This page could have been
3104 * uncompressed by the
3105 * compressor pager and its
3106 * contents might be only in
3108 * Since it's being mapped for
3109 * "execute" for the fist time,
3110 * make sure the icache is in
3113 pmap_sync_page_data_phys(m
->phys_page
);
3116 pmap_unlock_phys_page(m
->phys_page
);
3118 if (m
->pmapped
== FALSE
) {
3119 pmap_lock_phys_page(m
->phys_page
);
3121 pmap_unlock_phys_page(m
->phys_page
);
3124 if (vm_page_is_slideable(m
)) {
3125 boolean_t was_busy
= m
->busy
;
3127 vm_object_lock_assert_exclusive(m
->object
);
3130 kr
= vm_page_slide(m
, 0);
3133 PAGE_WAKEUP_DONE(m
);
3135 if (kr
!= KERN_SUCCESS
) {
3137 * This page has not been slid correctly,
3138 * do not do the pmap_enter() !
3139 * Let vm_fault_enter() return the error
3140 * so the caller can fail the fault.
3142 goto after_the_pmap_enter
;
3146 if (fault_type
& VM_PROT_WRITE
) {
3148 if (m
->wpmapped
== FALSE
) {
3149 vm_object_lock_assert_exclusive(m
->object
);
3153 if (must_disconnect
) {
3155 * We can only get here
3156 * because of the CSE logic
3158 assert(cs_enforcement_enabled
);
3159 pmap_disconnect(m
->phys_page
);
3161 * If we are faulting for a write, we can clear
3162 * the execute bit - that will ensure the page is
3163 * checked again before being executable, which
3164 * protects against a map switch.
3165 * This only happens the first time the page
3166 * gets tainted, so we won't get stuck here
3167 * to make an already writeable page executable.
3170 prot
&= ~VM_PROT_EXECUTE
;
3175 /* Prevent a deadlock by not
3176 * holding the object lock if we need to wait for a page in
3177 * pmap_enter() - <rdar://problem/7138958> */
3178 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
, 0,
3180 pmap_options
| PMAP_OPTIONS_NOWAIT
,
3183 if(pe_result
== KERN_RESOURCE_SHORTAGE
) {
3187 * this will be non-null in the case where we hold the lock
3188 * on the top-object in this chain... we can't just drop
3189 * the lock on the object we're inserting the page into
3190 * and recall the PMAP_ENTER since we can still cause
3191 * a deadlock if one of the critical paths tries to
3192 * acquire the lock on the top-object and we're blocked
3193 * in PMAP_ENTER waiting for memory... our only recourse
3194 * is to deal with it at a higher level where we can
3198 vm_pmap_enter_retried
++;
3199 goto after_the_pmap_enter
;
3201 /* The nonblocking version of pmap_enter did not succeed.
3202 * and we don't need to drop other locks and retry
3203 * at the level above us, so
3204 * use the blocking version instead. Requires marking
3205 * the page busy and unlocking the object */
3206 boolean_t was_busy
= m
->busy
;
3208 vm_object_lock_assert_exclusive(m
->object
);
3211 vm_object_unlock(m
->object
);
3213 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
,
3215 pmap_options
, pe_result
);
3217 /* Take the object lock again. */
3218 vm_object_lock(m
->object
);
3220 /* If the page was busy, someone else will wake it up.
3221 * Otherwise, we have to do it now. */
3224 PAGE_WAKEUP_DONE(m
);
3226 vm_pmap_enter_blocked
++;
3230 after_the_pmap_enter
:
3235 vm_pre_fault(vm_map_offset_t vaddr
)
3237 if (pmap_find_phys(current_map()->pmap
, vaddr
) == 0) {
3239 vm_fault(current_map(), /* map */
3241 VM_PROT_READ
, /* fault_type */
3242 FALSE
, /* change_wiring */
3243 THREAD_UNINT
, /* interruptible */
3244 NULL
, /* caller_pmap */
3245 0 /* caller_pmap_addr */);
3253 * Handle page faults, including pseudo-faults
3254 * used to change the wiring status of pages.
3256 * Explicit continuations have been removed.
3258 * vm_fault and vm_fault_page save mucho state
3259 * in the moral equivalent of a closure. The state
3260 * structure is allocated when first entering vm_fault
3261 * and deallocated when leaving vm_fault.
3264 extern int _map_enter_debug
;
3266 unsigned long vm_fault_collapse_total
= 0;
3267 unsigned long vm_fault_collapse_skipped
= 0;
3273 vm_map_offset_t vaddr
,
3274 vm_prot_t fault_type
,
3275 boolean_t change_wiring
,
3278 vm_map_offset_t caller_pmap_addr
)
3280 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
,
3281 interruptible
, caller_pmap
, caller_pmap_addr
,
3288 vm_map_offset_t vaddr
,
3289 vm_prot_t fault_type
,
3290 boolean_t change_wiring
,
3293 vm_map_offset_t caller_pmap_addr
,
3294 ppnum_t
*physpage_p
)
3296 vm_map_version_t version
; /* Map version for verificiation */
3297 boolean_t wired
; /* Should mapping be wired down? */
3298 vm_object_t object
; /* Top-level object */
3299 vm_object_offset_t offset
; /* Top-level offset */
3300 vm_prot_t prot
; /* Protection for mapping */
3301 vm_object_t old_copy_object
; /* Saved copy object */
3302 vm_page_t result_page
; /* Result of vm_fault_page */
3303 vm_page_t top_page
; /* Placeholder page */
3306 vm_page_t m
; /* Fast access to result_page */
3307 kern_return_t error_code
;
3308 vm_object_t cur_object
;
3309 vm_object_offset_t cur_offset
;
3311 vm_object_t new_object
;
3314 boolean_t interruptible_state
;
3315 vm_map_t real_map
= map
;
3316 vm_map_t original_map
= map
;
3317 vm_prot_t original_fault_type
;
3318 struct vm_object_fault_info fault_info
;
3319 boolean_t need_collapse
= FALSE
;
3320 boolean_t need_retry
= FALSE
;
3321 boolean_t
*need_retry_ptr
= NULL
;
3322 int object_lock_type
= 0;
3323 int cur_object_lock_type
;
3324 vm_object_t top_object
= VM_OBJECT_NULL
;
3326 int compressed_count_delta
;
3329 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3330 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
3331 ((uint64_t)vaddr
>> 32),
3333 (map
== kernel_map
),
3337 if (get_preemption_level() != 0) {
3338 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3339 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3340 ((uint64_t)vaddr
>> 32),
3346 return (KERN_FAILURE
);
3349 interruptible_state
= thread_interrupt_level(interruptible
);
3351 VM_STAT_INCR(faults
);
3352 current_task()->faults
++;
3353 original_fault_type
= fault_type
;
3355 if (fault_type
& VM_PROT_WRITE
)
3356 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3358 object_lock_type
= OBJECT_LOCK_SHARED
;
3360 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
3364 * assume we will hit a page in the cache
3365 * otherwise, explicitly override with
3366 * the real fault type once we determine it
3368 type_of_fault
= DBG_CACHE_HIT_FAULT
;
3371 * Find the backing store object and offset into
3372 * it to begin the search.
3374 fault_type
= original_fault_type
;
3376 vm_map_lock_read(map
);
3378 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
3379 object_lock_type
, &version
,
3380 &object
, &offset
, &prot
, &wired
,
3384 if (kr
!= KERN_SUCCESS
) {
3385 vm_map_unlock_read(map
);
3388 pmap
= real_map
->pmap
;
3389 fault_info
.interruptible
= interruptible
;
3390 fault_info
.stealth
= FALSE
;
3391 fault_info
.io_sync
= FALSE
;
3392 fault_info
.mark_zf_absent
= FALSE
;
3393 fault_info
.batch_pmap_op
= FALSE
;
3396 * If the page is wired, we must fault for the current protection
3397 * value, to avoid further faults.
3400 fault_type
= prot
| VM_PROT_WRITE
;
3402 * since we're treating this fault as a 'write'
3403 * we must hold the top object lock exclusively
3405 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3407 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3409 if (vm_object_lock_upgrade(object
) == FALSE
) {
3411 * couldn't upgrade, so explictly
3412 * take the lock exclusively
3414 vm_object_lock(object
);
3419 #if VM_FAULT_CLASSIFY
3421 * Temporary data gathering code
3423 vm_fault_classify(object
, offset
, fault_type
);
3426 * Fast fault code. The basic idea is to do as much as
3427 * possible while holding the map lock and object locks.
3428 * Busy pages are not used until the object lock has to
3429 * be dropped to do something (copy, zero fill, pmap enter).
3430 * Similarly, paging references aren't acquired until that
3431 * point, and object references aren't used.
3433 * If we can figure out what to do
3434 * (zero fill, copy on write, pmap enter) while holding
3435 * the locks, then it gets done. Otherwise, we give up,
3436 * and use the original fault path (which doesn't hold
3437 * the map lock, and relies on busy pages).
3438 * The give up cases include:
3439 * - Have to talk to pager.
3440 * - Page is busy, absent or in error.
3441 * - Pager has locked out desired access.
3442 * - Fault needs to be restarted.
3443 * - Have to push page into copy object.
3445 * The code is an infinite loop that moves one level down
3446 * the shadow chain each time. cur_object and cur_offset
3447 * refer to the current object being examined. object and offset
3448 * are the original object from the map. The loop is at the
3449 * top level if and only if object and cur_object are the same.
3451 * Invariants: Map lock is held throughout. Lock is held on
3452 * original object and cur_object (if different) when
3453 * continuing or exiting loop.
3459 * If this page is to be inserted in a copy delay object
3460 * for writing, and if the object has a copy, then the
3461 * copy delay strategy is implemented in the slow fault page.
3463 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
3464 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
))
3465 goto handle_copy_delay
;
3467 cur_object
= object
;
3468 cur_offset
= offset
;
3471 if (!cur_object
->pager_created
&&
3472 cur_object
->phys_contiguous
) /* superpage */
3475 if (cur_object
->blocked_access
) {
3477 * Access to this VM object has been blocked.
3478 * Let the slow path handle it.
3483 m
= vm_page_lookup(cur_object
, cur_offset
);
3485 if (m
!= VM_PAGE_NULL
) {
3487 wait_result_t result
;
3490 * in order to do the PAGE_ASSERT_WAIT, we must
3491 * have object that 'm' belongs to locked exclusively
3493 if (object
!= cur_object
) {
3495 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3497 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3499 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3501 * couldn't upgrade so go do a full retry
3502 * immediately since we can no longer be
3503 * certain about cur_object (since we
3504 * don't hold a reference on it)...
3505 * first drop the top object lock
3507 vm_object_unlock(object
);
3509 vm_map_unlock_read(map
);
3510 if (real_map
!= map
)
3511 vm_map_unlock(real_map
);
3516 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3518 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3520 if (vm_object_lock_upgrade(object
) == FALSE
) {
3522 * couldn't upgrade, so explictly take the lock
3523 * exclusively and go relookup the page since we
3524 * will have dropped the object lock and
3525 * a different thread could have inserted
3526 * a page at this offset
3527 * no need for a full retry since we're
3528 * at the top level of the object chain
3530 vm_object_lock(object
);
3535 if (m
->pageout_queue
&& m
->object
->internal
&& COMPRESSED_PAGER_IS_ACTIVE
) {
3537 * m->busy == TRUE and the object is locked exclusively
3538 * if m->pageout_queue == TRUE after we acquire the
3539 * queues lock, we are guaranteed that it is stable on
3540 * the pageout queue and therefore reclaimable
3542 * NOTE: this is only true for the internal pageout queue
3543 * in the compressor world
3545 vm_page_lock_queues();
3547 if (m
->pageout_queue
) {
3548 vm_pageout_throttle_up(m
);
3549 vm_page_unlock_queues();
3551 PAGE_WAKEUP_DONE(m
);
3552 goto reclaimed_from_pageout
;
3554 vm_page_unlock_queues();
3556 if (object
!= cur_object
)
3557 vm_object_unlock(object
);
3559 vm_map_unlock_read(map
);
3560 if (real_map
!= map
)
3561 vm_map_unlock(real_map
);
3563 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
3565 vm_object_unlock(cur_object
);
3567 if (result
== THREAD_WAITING
) {
3568 result
= thread_block(THREAD_CONTINUE_NULL
);
3570 counter(c_vm_fault_page_block_busy_kernel
++);
3572 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
3578 reclaimed_from_pageout
:
3580 if (object
!= cur_object
) {
3581 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3582 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3584 vm_object_unlock(object
);
3585 vm_object_unlock(cur_object
);
3587 vm_map_unlock_read(map
);
3588 if (real_map
!= map
)
3589 vm_map_unlock(real_map
);
3594 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3596 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3598 if (vm_object_lock_upgrade(object
) == FALSE
) {
3600 * couldn't upgrade, so explictly take the lock
3601 * exclusively and go relookup the page since we
3602 * will have dropped the object lock and
3603 * a different thread could have inserted
3604 * a page at this offset
3605 * no need for a full retry since we're
3606 * at the top level of the object chain
3608 vm_object_lock(object
);
3615 vm_pageout_steal_laundry(m
, FALSE
);
3618 if (m
->phys_page
== vm_page_guard_addr
) {
3620 * Guard page: let the slow path deal with it
3624 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
)) {
3626 * Unusual case... let the slow path deal with it
3630 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m
->object
)) {
3631 if (object
!= cur_object
)
3632 vm_object_unlock(object
);
3633 vm_map_unlock_read(map
);
3634 if (real_map
!= map
)
3635 vm_map_unlock(real_map
);
3636 vm_object_unlock(cur_object
);
3637 kr
= KERN_MEMORY_ERROR
;
3644 * We've soft-faulted (because it's not in the page
3645 * table) on an encrypted page.
3646 * Keep the page "busy" so that no one messes with
3647 * it during the decryption.
3648 * Release the extra locks we're holding, keep only
3649 * the page's VM object lock.
3651 * in order to set 'busy' on 'm', we must
3652 * have object that 'm' belongs to locked exclusively
3654 if (object
!= cur_object
) {
3655 vm_object_unlock(object
);
3657 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3659 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3661 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3663 * couldn't upgrade so go do a full retry
3664 * immediately since we've already dropped
3665 * the top object lock associated with this page
3666 * and the current one got dropped due to the
3667 * failed upgrade... the state is no longer valid
3669 vm_map_unlock_read(map
);
3670 if (real_map
!= map
)
3671 vm_map_unlock(real_map
);
3676 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3678 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3680 if (vm_object_lock_upgrade(object
) == FALSE
) {
3682 * couldn't upgrade, so explictly take the lock
3683 * exclusively and go relookup the page since we
3684 * will have dropped the object lock and
3685 * a different thread could have inserted
3686 * a page at this offset
3687 * no need for a full retry since we're
3688 * at the top level of the object chain
3690 vm_object_lock(object
);
3697 vm_map_unlock_read(map
);
3698 if (real_map
!= map
)
3699 vm_map_unlock(real_map
);
3701 vm_page_decrypt(m
, 0);
3704 PAGE_WAKEUP_DONE(m
);
3706 vm_object_unlock(cur_object
);
3708 * Retry from the top, in case anything
3709 * changed while we were decrypting...
3713 ASSERT_PAGE_DECRYPTED(m
);
3715 if(vm_page_is_slideable(m
)) {
3717 * We might need to slide this page, and so,
3718 * we want to hold the VM object exclusively.
3720 if (object
!= cur_object
) {
3721 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3722 vm_object_unlock(object
);
3723 vm_object_unlock(cur_object
);
3725 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3727 vm_map_unlock_read(map
);
3728 if (real_map
!= map
)
3729 vm_map_unlock(real_map
);
3733 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3735 vm_object_unlock(object
);
3736 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3737 vm_map_unlock_read(map
);
3742 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
) ||
3743 (physpage_p
!= NULL
&& (prot
& VM_PROT_WRITE
))) {
3744 upgrade_for_validation
:
3746 * We might need to validate this page
3747 * against its code signature, so we
3748 * want to hold the VM object exclusively.
3750 if (object
!= cur_object
) {
3751 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3752 vm_object_unlock(object
);
3753 vm_object_unlock(cur_object
);
3755 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3757 vm_map_unlock_read(map
);
3758 if (real_map
!= map
)
3759 vm_map_unlock(real_map
);
3764 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3766 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3768 if (vm_object_lock_upgrade(object
) == FALSE
) {
3770 * couldn't upgrade, so explictly take the lock
3771 * exclusively and go relookup the page since we
3772 * will have dropped the object lock and
3773 * a different thread could have inserted
3774 * a page at this offset
3775 * no need for a full retry since we're
3776 * at the top level of the object chain
3778 vm_object_lock(object
);
3785 * Two cases of map in faults:
3786 * - At top level w/o copy object.
3787 * - Read fault anywhere.
3788 * --> must disallow write.
3791 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
3796 if ((fault_type
& VM_PROT_WRITE
) == 0) {
3798 if (object
!= cur_object
) {
3800 * We still need to hold the top object
3801 * lock here to prevent a race between
3802 * a read fault (taking only "shared"
3803 * locks) and a write fault (taking
3804 * an "exclusive" lock on the top
3806 * Otherwise, as soon as we release the
3807 * top lock, the write fault could
3808 * proceed and actually complete before
3809 * the read fault, and the copied page's
3810 * translation could then be overwritten
3811 * by the read fault's translation for
3812 * the original page.
3814 * Let's just record what the top object
3815 * is and we'll release it later.
3817 top_object
= object
;
3820 * switch to the object that has the new page
3822 object
= cur_object
;
3823 object_lock_type
= cur_object_lock_type
;
3827 * prepare for the pmap_enter...
3828 * object and map are both locked
3829 * m contains valid data
3830 * object == m->object
3831 * cur_object == NULL or it's been unlocked
3832 * no paging references on either object or cur_object
3834 if (top_object
!= VM_OBJECT_NULL
|| object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
)
3835 need_retry_ptr
= &need_retry
;
3837 need_retry_ptr
= NULL
;
3840 kr
= vm_fault_enter(m
,
3847 fault_info
.no_cache
,
3848 fault_info
.cs_bypass
,
3849 fault_info
.user_tag
,
3850 fault_info
.pmap_options
,
3854 kr
= vm_fault_enter(m
,
3861 fault_info
.no_cache
,
3862 fault_info
.cs_bypass
,
3863 fault_info
.user_tag
,
3864 fault_info
.pmap_options
,
3869 if (kr
== KERN_SUCCESS
&&
3870 physpage_p
!= NULL
) {
3871 /* for vm_map_wire_and_extract() */
3872 *physpage_p
= m
->phys_page
;
3873 if (prot
& VM_PROT_WRITE
) {
3874 vm_object_lock_assert_exclusive(
3880 if (top_object
!= VM_OBJECT_NULL
) {
3882 * It's safe to drop the top object
3883 * now that we've done our
3884 * vm_fault_enter(). Any other fault
3885 * in progress for that virtual
3886 * address will either find our page
3887 * and translation or put in a new page
3890 vm_object_unlock(top_object
);
3891 top_object
= VM_OBJECT_NULL
;
3894 if (need_collapse
== TRUE
)
3895 vm_object_collapse(object
, offset
, TRUE
);
3897 if (need_retry
== FALSE
&&
3898 (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
)) {
3900 * evaluate access pattern and update state
3901 * vm_fault_deactivate_behind depends on the
3902 * state being up to date
3904 vm_fault_is_sequential(object
, cur_offset
, fault_info
.behavior
);
3906 vm_fault_deactivate_behind(object
, cur_offset
, fault_info
.behavior
);
3909 * That's it, clean up and return.
3912 PAGE_WAKEUP_DONE(m
);
3914 vm_object_unlock(object
);
3916 vm_map_unlock_read(map
);
3917 if (real_map
!= map
)
3918 vm_map_unlock(real_map
);
3920 if (need_retry
== TRUE
) {
3922 * vm_fault_enter couldn't complete the PMAP_ENTER...
3923 * at this point we don't hold any locks so it's safe
3924 * to ask the pmap layer to expand the page table to
3925 * accommodate this mapping... once expanded, we'll
3926 * re-drive the fault which should result in vm_fault_enter
3927 * being able to successfully enter the mapping this time around
3929 (void)pmap_enter_options(
3930 pmap
, vaddr
, 0, 0, 0, 0, 0,
3931 PMAP_OPTIONS_NOENTER
, NULL
);
3939 * COPY ON WRITE FAULT
3941 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
3944 * If objects match, then
3945 * object->copy must not be NULL (else control
3946 * would be in previous code block), and we
3947 * have a potential push into the copy object
3948 * with which we can't cope with here.
3950 if (cur_object
== object
) {
3952 * must take the slow path to
3953 * deal with the copy push
3959 * This is now a shadow based copy on write
3960 * fault -- it requires a copy up the shadow
3964 if ((cur_object_lock_type
== OBJECT_LOCK_SHARED
) &&
3965 VM_FAULT_NEED_CS_VALIDATION(NULL
, m
)) {
3966 goto upgrade_for_validation
;
3970 * Allocate a page in the original top level
3971 * object. Give up if allocate fails. Also
3972 * need to remember current page, as it's the
3973 * source of the copy.
3975 * at this point we hold locks on both
3976 * object and cur_object... no need to take
3977 * paging refs or mark pages BUSY since
3978 * we don't drop either object lock until
3979 * the page has been copied and inserted
3984 if (m
== VM_PAGE_NULL
) {
3986 * no free page currently available...
3987 * must take the slow path
3992 * Now do the copy. Mark the source page busy...
3994 * NOTE: This code holds the map lock across
3997 vm_page_copy(cur_m
, m
);
3998 vm_page_insert(m
, object
, offset
);
3999 SET_PAGE_DIRTY(m
, FALSE
);
4002 * Now cope with the source page and object
4004 if (object
->ref_count
> 1 && cur_m
->pmapped
)
4005 pmap_disconnect(cur_m
->phys_page
);
4007 if (cur_m
->clustered
) {
4008 VM_PAGE_COUNT_AS_PAGEIN(cur_m
);
4009 VM_PAGE_CONSUME_CLUSTERED(cur_m
);
4011 need_collapse
= TRUE
;
4013 if (!cur_object
->internal
&&
4014 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
4016 * The object from which we've just
4017 * copied a page is most probably backed
4018 * by a vnode. We don't want to waste too
4019 * much time trying to collapse the VM objects
4020 * and create a bottleneck when several tasks
4021 * map the same file.
4023 if (cur_object
->copy
== object
) {
4025 * Shared mapping or no COW yet.
4026 * We can never collapse a copy
4027 * object into its backing object.
4029 need_collapse
= FALSE
;
4030 } else if (cur_object
->copy
== object
->shadow
&&
4031 object
->shadow
->resident_page_count
== 0) {
4033 * Shared mapping after a COW occurred.
4035 need_collapse
= FALSE
;
4038 vm_object_unlock(cur_object
);
4040 if (need_collapse
== FALSE
)
4041 vm_fault_collapse_skipped
++;
4042 vm_fault_collapse_total
++;
4044 type_of_fault
= DBG_COW_FAULT
;
4045 VM_STAT_INCR(cow_faults
);
4046 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
4047 current_task()->cow_faults
++;
4053 * No page at cur_object, cur_offset... m == NULL
4055 if (cur_object
->pager_created
) {
4056 int compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
4058 if (MUST_ASK_PAGER(cur_object
, cur_offset
, compressor_external_state
) == TRUE
) {
4060 int c_flags
= C_DONT_BLOCK
;
4061 boolean_t insert_cur_object
= FALSE
;
4064 * May have to talk to a pager...
4065 * if so, take the slow path by
4066 * doing a 'break' from the while (TRUE) loop
4068 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
4069 * if the compressor is active and the page exists there
4071 if (compressor_external_state
!= VM_EXTERNAL_STATE_EXISTS
)
4074 if (map
== kernel_map
|| real_map
== kernel_map
) {
4076 * can't call into the compressor with the kernel_map
4077 * lock held, since the compressor may try to operate
4078 * on the kernel map in order to return an empty c_segment
4082 if (object
!= cur_object
) {
4083 if (fault_type
& VM_PROT_WRITE
)
4086 insert_cur_object
= TRUE
;
4088 if (insert_cur_object
== TRUE
) {
4090 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4092 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4094 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
4096 * couldn't upgrade so go do a full retry
4097 * immediately since we can no longer be
4098 * certain about cur_object (since we
4099 * don't hold a reference on it)...
4100 * first drop the top object lock
4102 vm_object_unlock(object
);
4104 vm_map_unlock_read(map
);
4105 if (real_map
!= map
)
4106 vm_map_unlock(real_map
);
4111 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4113 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4115 if (object
!= cur_object
) {
4117 * we can't go for the upgrade on the top
4118 * lock since the upgrade may block waiting
4119 * for readers to drain... since we hold
4120 * cur_object locked at this point, waiting
4121 * for the readers to drain would represent
4122 * a lock order inversion since the lock order
4123 * for objects is the reference order in the
4126 vm_object_unlock(object
);
4127 vm_object_unlock(cur_object
);
4129 vm_map_unlock_read(map
);
4130 if (real_map
!= map
)
4131 vm_map_unlock(real_map
);
4135 if (vm_object_lock_upgrade(object
) == FALSE
) {
4137 * couldn't upgrade, so explictly take the lock
4138 * exclusively and go relookup the page since we
4139 * will have dropped the object lock and
4140 * a different thread could have inserted
4141 * a page at this offset
4142 * no need for a full retry since we're
4143 * at the top level of the object chain
4145 vm_object_lock(object
);
4152 if (m
== VM_PAGE_NULL
) {
4154 * no free page currently available...
4155 * must take the slow path
4161 * The object is and remains locked
4162 * so no need to take a
4163 * "paging_in_progress" reference.
4165 boolean_t shared_lock
;
4166 if ((object
== cur_object
&&
4167 object_lock_type
== OBJECT_LOCK_EXCLUSIVE
) ||
4168 (object
!= cur_object
&&
4169 cur_object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)) {
4170 shared_lock
= FALSE
;
4175 kr
= vm_compressor_pager_get(
4178 cur_object
->paging_offset
),
4182 &compressed_count_delta
);
4184 vm_compressor_pager_count(
4186 compressed_count_delta
,
4190 if (kr
!= KERN_SUCCESS
) {
4197 * If the object is purgeable, its
4198 * owner's purgeable ledgers will be
4199 * updated in vm_page_insert() but the
4200 * page was also accounted for in a
4201 * "compressed purgeable" ledger, so
4204 if (object
!= cur_object
&&
4205 !insert_cur_object
) {
4207 * We're not going to insert
4208 * the decompressed page into
4209 * the object it came from.
4211 * We're dealing with a
4212 * copy-on-write fault on
4214 * We're going to decompress
4215 * the page directly into the
4216 * target "object" while
4217 * keepin the compressed
4218 * page for "cur_object", so
4219 * no ledger update in that
4222 } else if ((cur_object
->purgable
==
4223 VM_PURGABLE_DENY
) ||
4224 (cur_object
->vo_purgeable_owner
==
4227 * "cur_object" is not purgeable
4228 * or is not owned, so no
4229 * purgeable ledgers to update.
4233 * One less compressed
4234 * purgeable page for
4235 * cur_object's owner.
4237 vm_purgeable_compressed_update(
4242 if (insert_cur_object
) {
4243 vm_page_insert(m
, cur_object
, cur_offset
);
4245 vm_page_insert(m
, object
, offset
);
4248 if ((m
->object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_USE_DEFAULT
) {
4250 * If the page is not cacheable,
4251 * we can't let its contents
4252 * linger in the data cache
4253 * after the decompression.
4255 pmap_sync_page_attributes_phys(m
->phys_page
);
4258 type_of_fault
= my_fault_type
;
4260 VM_STAT_INCR(decompressions
);
4262 if (cur_object
!= object
) {
4263 if (insert_cur_object
) {
4264 top_object
= object
;
4266 * switch to the object that has the new page
4268 object
= cur_object
;
4269 object_lock_type
= cur_object_lock_type
;
4271 vm_object_unlock(cur_object
);
4272 cur_object
= object
;
4278 * existence map present and indicates
4279 * that the pager doesn't have this page
4282 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
4284 * Zero fill fault. Page gets
4285 * inserted into the original object.
4287 if (cur_object
->shadow_severed
||
4288 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
))
4290 if (object
!= cur_object
)
4291 vm_object_unlock(cur_object
);
4292 vm_object_unlock(object
);
4294 vm_map_unlock_read(map
);
4295 if (real_map
!= map
)
4296 vm_map_unlock(real_map
);
4298 kr
= KERN_MEMORY_ERROR
;
4301 if (vm_backing_store_low
) {
4303 * we are protecting the system from
4304 * backing store exhaustion...
4305 * must take the slow path if we're
4308 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
))
4311 if (cur_object
!= object
) {
4312 vm_object_unlock(cur_object
);
4314 cur_object
= object
;
4316 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4318 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4320 if (vm_object_lock_upgrade(object
) == FALSE
) {
4322 * couldn't upgrade so do a full retry on the fault
4323 * since we dropped the object lock which
4324 * could allow another thread to insert
4325 * a page at this offset
4327 vm_map_unlock_read(map
);
4328 if (real_map
!= map
)
4329 vm_map_unlock(real_map
);
4334 m
= vm_page_alloc(object
, offset
);
4336 if (m
== VM_PAGE_NULL
) {
4338 * no free page currently available...
4339 * must take the slow path
4345 * Now zero fill page...
4346 * the page is probably going to
4347 * be written soon, so don't bother
4348 * to clear the modified bit
4350 * NOTE: This code holds the map
4351 * lock across the zero fill.
4353 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
4358 * On to the next level in the shadow chain
4360 cur_offset
+= cur_object
->vo_shadow_offset
;
4361 new_object
= cur_object
->shadow
;
4364 * take the new_object's lock with the indicated state
4366 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
)
4367 vm_object_lock_shared(new_object
);
4369 vm_object_lock(new_object
);
4371 if (cur_object
!= object
)
4372 vm_object_unlock(cur_object
);
4374 cur_object
= new_object
;
4380 * Cleanup from fast fault failure. Drop any object
4381 * lock other than original and drop map lock.
4383 if (object
!= cur_object
)
4384 vm_object_unlock(cur_object
);
4387 * must own the object lock exclusively at this point
4389 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4390 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4392 if (vm_object_lock_upgrade(object
) == FALSE
) {
4394 * couldn't upgrade, so explictly
4395 * take the lock exclusively
4396 * no need to retry the fault at this
4397 * point since "vm_fault_page" will
4398 * completely re-evaluate the state
4400 vm_object_lock(object
);
4405 vm_map_unlock_read(map
);
4406 if (real_map
!= map
)
4407 vm_map_unlock(real_map
);
4410 * Make a reference to this object to
4411 * prevent its disposal while we are messing with
4412 * it. Once we have the reference, the map is free
4413 * to be diddled. Since objects reference their
4414 * shadows (and copies), they will stay around as well.
4416 vm_object_reference_locked(object
);
4417 vm_object_paging_begin(object
);
4419 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
4423 result_page
= VM_PAGE_NULL
;
4424 kr
= vm_fault_page(object
, offset
, fault_type
,
4425 (change_wiring
&& !wired
),
4426 FALSE
, /* page not looked up */
4427 &prot
, &result_page
, &top_page
,
4429 &error_code
, map
->no_zero_fill
,
4430 FALSE
, &fault_info
);
4433 * if kr != VM_FAULT_SUCCESS, then the paging reference
4434 * has been dropped and the object unlocked... the ref_count
4437 * if kr == VM_FAULT_SUCCESS, then the paging reference
4438 * is still held along with the ref_count on the original object
4440 * the object is returned locked with a paging reference
4442 * if top_page != NULL, then it's BUSY and the
4443 * object it belongs to has a paging reference
4444 * but is returned unlocked
4446 if (kr
!= VM_FAULT_SUCCESS
&&
4447 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
4449 * we didn't succeed, lose the object reference immediately.
4451 vm_object_deallocate(object
);
4454 * See why we failed, and take corrective action.
4457 case VM_FAULT_MEMORY_SHORTAGE
:
4458 if (vm_page_wait((change_wiring
) ?
4465 case VM_FAULT_INTERRUPTED
:
4468 case VM_FAULT_RETRY
:
4470 case VM_FAULT_MEMORY_ERROR
:
4474 kr
= KERN_MEMORY_ERROR
;
4477 panic("vm_fault: unexpected error 0x%x from "
4478 "vm_fault_page()\n", kr
);
4483 if (m
!= VM_PAGE_NULL
) {
4484 assert((change_wiring
&& !wired
) ?
4485 (top_page
== VM_PAGE_NULL
) :
4486 ((top_page
== VM_PAGE_NULL
) == (m
->object
== object
)));
4490 * What to do with the resulting page from vm_fault_page
4491 * if it doesn't get entered into the physical map:
4493 #define RELEASE_PAGE(m) \
4495 PAGE_WAKEUP_DONE(m); \
4496 if (!m->active && !m->inactive && !m->throttled) { \
4497 vm_page_lockspin_queues(); \
4498 if (!m->active && !m->inactive && !m->throttled) \
4499 vm_page_activate(m); \
4500 vm_page_unlock_queues(); \
4505 * We must verify that the maps have not changed
4506 * since our last lookup.
4508 if (m
!= VM_PAGE_NULL
) {
4509 old_copy_object
= m
->object
->copy
;
4510 vm_object_unlock(m
->object
);
4512 old_copy_object
= VM_OBJECT_NULL
;
4513 vm_object_unlock(object
);
4517 * no object locks are held at this point
4519 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
4520 vm_object_t retry_object
;
4521 vm_object_offset_t retry_offset
;
4522 vm_prot_t retry_prot
;
4525 * To avoid trying to write_lock the map while another
4526 * thread has it read_locked (in vm_map_pageable), we
4527 * do not try for write permission. If the page is
4528 * still writable, we will get write permission. If it
4529 * is not, or has been marked needs_copy, we enter the
4530 * mapping without write permission, and will merely
4531 * take another fault.
4534 vm_map_lock_read(map
);
4536 kr
= vm_map_lookup_locked(&map
, vaddr
,
4537 fault_type
& ~VM_PROT_WRITE
,
4538 OBJECT_LOCK_EXCLUSIVE
, &version
,
4539 &retry_object
, &retry_offset
, &retry_prot
,
4543 pmap
= real_map
->pmap
;
4545 if (kr
!= KERN_SUCCESS
) {
4546 vm_map_unlock_read(map
);
4548 if (m
!= VM_PAGE_NULL
) {
4550 * retake the lock so that
4551 * we can drop the paging reference
4552 * in vm_fault_cleanup and do the
4553 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4555 vm_object_lock(m
->object
);
4559 vm_fault_cleanup(m
->object
, top_page
);
4562 * retake the lock so that
4563 * we can drop the paging reference
4564 * in vm_fault_cleanup
4566 vm_object_lock(object
);
4568 vm_fault_cleanup(object
, top_page
);
4570 vm_object_deallocate(object
);
4574 vm_object_unlock(retry_object
);
4576 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
4578 vm_map_unlock_read(map
);
4579 if (real_map
!= map
)
4580 vm_map_unlock(real_map
);
4582 if (m
!= VM_PAGE_NULL
) {
4584 * retake the lock so that
4585 * we can drop the paging reference
4586 * in vm_fault_cleanup and do the
4587 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4589 vm_object_lock(m
->object
);
4593 vm_fault_cleanup(m
->object
, top_page
);
4596 * retake the lock so that
4597 * we can drop the paging reference
4598 * in vm_fault_cleanup
4600 vm_object_lock(object
);
4602 vm_fault_cleanup(object
, top_page
);
4604 vm_object_deallocate(object
);
4609 * Check whether the protection has changed or the object
4610 * has been copied while we left the map unlocked.
4614 if (m
!= VM_PAGE_NULL
) {
4615 vm_object_lock(m
->object
);
4617 if (m
->object
->copy
!= old_copy_object
) {
4619 * The copy object changed while the top-level object
4620 * was unlocked, so take away write permission.
4622 prot
&= ~VM_PROT_WRITE
;
4625 vm_object_lock(object
);
4628 * If we want to wire down this page, but no longer have
4629 * adequate permissions, we must start all over.
4631 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
4633 vm_map_verify_done(map
, &version
);
4634 if (real_map
!= map
)
4635 vm_map_unlock(real_map
);
4637 if (m
!= VM_PAGE_NULL
) {
4640 vm_fault_cleanup(m
->object
, top_page
);
4642 vm_fault_cleanup(object
, top_page
);
4644 vm_object_deallocate(object
);
4648 if (m
!= VM_PAGE_NULL
) {
4650 * Put this page into the physical map.
4651 * We had to do the unlock above because pmap_enter
4652 * may cause other faults. The page may be on
4653 * the pageout queues. If the pageout daemon comes
4654 * across the page, it will remove it from the queues.
4657 kr
= vm_fault_enter(m
,
4664 fault_info
.no_cache
,
4665 fault_info
.cs_bypass
,
4666 fault_info
.user_tag
,
4667 fault_info
.pmap_options
,
4671 kr
= vm_fault_enter(m
,
4678 fault_info
.no_cache
,
4679 fault_info
.cs_bypass
,
4680 fault_info
.user_tag
,
4681 fault_info
.pmap_options
,
4685 if (kr
!= KERN_SUCCESS
) {
4686 /* abort this page fault */
4687 vm_map_verify_done(map
, &version
);
4688 if (real_map
!= map
)
4689 vm_map_unlock(real_map
);
4690 PAGE_WAKEUP_DONE(m
);
4691 vm_fault_cleanup(m
->object
, top_page
);
4692 vm_object_deallocate(object
);
4695 if (physpage_p
!= NULL
) {
4696 /* for vm_map_wire_and_extract() */
4697 *physpage_p
= m
->phys_page
;
4698 if (prot
& VM_PROT_WRITE
) {
4699 vm_object_lock_assert_exclusive(m
->object
);
4705 vm_map_entry_t entry
;
4706 vm_map_offset_t laddr
;
4707 vm_map_offset_t ldelta
, hdelta
;
4710 * do a pmap block mapping from the physical address
4715 /* While we do not worry about execution protection in */
4716 /* general, certian pages may have instruction execution */
4717 /* disallowed. We will check here, and if not allowed */
4718 /* to execute, we return with a protection failure. */
4720 if ((fault_type
& VM_PROT_EXECUTE
) &&
4721 (!pmap_eligible_for_execute((ppnum_t
)(object
->vo_shadow_offset
>> 12)))) {
4723 vm_map_verify_done(map
, &version
);
4725 if (real_map
!= map
)
4726 vm_map_unlock(real_map
);
4728 vm_fault_cleanup(object
, top_page
);
4729 vm_object_deallocate(object
);
4731 kr
= KERN_PROTECTION_FAILURE
;
4736 if (real_map
!= map
)
4737 vm_map_unlock(real_map
);
4739 if (original_map
!= map
) {
4740 vm_map_unlock_read(map
);
4741 vm_map_lock_read(original_map
);
4747 hdelta
= 0xFFFFF000;
4748 ldelta
= 0xFFFFF000;
4750 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
4751 if (ldelta
> (laddr
- entry
->vme_start
))
4752 ldelta
= laddr
- entry
->vme_start
;
4753 if (hdelta
> (entry
->vme_end
- laddr
))
4754 hdelta
= entry
->vme_end
- laddr
;
4755 if (entry
->is_sub_map
) {
4757 laddr
= (laddr
- entry
->vme_start
)
4759 vm_map_lock_read(entry
->object
.sub_map
);
4761 if (map
!= real_map
)
4762 vm_map_unlock_read(map
);
4763 if (entry
->use_pmap
) {
4764 vm_map_unlock_read(real_map
);
4765 real_map
= entry
->object
.sub_map
;
4767 map
= entry
->object
.sub_map
;
4774 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
4775 (entry
->object
.vm_object
!= NULL
) &&
4776 (entry
->object
.vm_object
== object
)) {
4778 int superpage
= (!object
->pager_created
&& object
->phys_contiguous
)? VM_MEM_SUPERPAGE
: 0;
4780 if (superpage
&& physpage_p
) {
4781 /* for vm_map_wire_and_extract() */
4782 *physpage_p
= (ppnum_t
) ((((vm_map_offset_t
) entry
->object
.vm_object
->vo_shadow_offset
)
4784 + (laddr
- entry
->vme_start
))
4790 * Set up a block mapped area
4792 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
4793 pmap_map_block(caller_pmap
,
4794 (addr64_t
)(caller_pmap_addr
- ldelta
),
4795 (ppnum_t
)((((vm_map_offset_t
) (entry
->object
.vm_object
->vo_shadow_offset
)) +
4796 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
4797 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
4798 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
4801 * Set up a block mapped area
4803 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
4804 pmap_map_block(real_map
->pmap
,
4805 (addr64_t
)(vaddr
- ldelta
),
4806 (ppnum_t
)((((vm_map_offset_t
)(entry
->object
.vm_object
->vo_shadow_offset
)) +
4807 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
4808 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
4809 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
4815 * Unlock everything, and return
4817 vm_map_verify_done(map
, &version
);
4818 if (real_map
!= map
)
4819 vm_map_unlock(real_map
);
4821 if (m
!= VM_PAGE_NULL
) {
4822 PAGE_WAKEUP_DONE(m
);
4824 vm_fault_cleanup(m
->object
, top_page
);
4826 vm_fault_cleanup(object
, top_page
);
4828 vm_object_deallocate(object
);
4834 thread_interrupt_level(interruptible_state
);
4837 * Only I/O throttle on faults which cause a pagein/swapin.
4839 if ((type_of_fault
== DBG_PAGEIND_FAULT
) || (type_of_fault
== DBG_PAGEINV_FAULT
) || (type_of_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
)) {
4840 throttle_lowpri_io(1);
4842 if (kr
== KERN_SUCCESS
&& type_of_fault
!= DBG_CACHE_HIT_FAULT
&& type_of_fault
!= DBG_GUARD_FAULT
) {
4844 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
4846 if (vm_debug_events
) {
4847 if (type_of_fault
== DBG_COMPRESSOR_FAULT
)
4848 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
4849 else if (type_of_fault
== DBG_COW_FAULT
)
4850 VM_DEBUG_EVENT(vmf_cowdelay
, VMF_COWDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
4852 VM_DEBUG_EVENT(vmf_zfdelay
, VMF_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
4854 delay(throttle_delay
);
4858 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
4859 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
4860 ((uint64_t)vaddr
>> 32),
4872 * Wire down a range of virtual addresses in a map.
4877 vm_map_entry_t entry
,
4879 vm_map_offset_t pmap_addr
,
4880 ppnum_t
*physpage_p
)
4883 register vm_map_offset_t va
;
4884 register vm_map_offset_t end_addr
= entry
->vme_end
;
4885 register kern_return_t rc
;
4887 assert(entry
->in_transition
);
4889 if ((entry
->object
.vm_object
!= NULL
) &&
4890 !entry
->is_sub_map
&&
4891 entry
->object
.vm_object
->phys_contiguous
) {
4892 return KERN_SUCCESS
;
4896 * Inform the physical mapping system that the
4897 * range of addresses may not fault, so that
4898 * page tables and such can be locked down as well.
4901 pmap_pageable(pmap
, pmap_addr
,
4902 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
4905 * We simulate a fault to get the page and enter it
4906 * in the physical map.
4909 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
4910 rc
= vm_fault_wire_fast(map
, va
, entry
, pmap
,
4911 pmap_addr
+ (va
- entry
->vme_start
),
4913 if (rc
!= KERN_SUCCESS
) {
4914 rc
= vm_fault_internal(map
, va
, VM_PROT_NONE
, TRUE
,
4915 ((pmap
== kernel_pmap
)
4917 : THREAD_ABORTSAFE
),
4920 (va
- entry
->vme_start
)),
4922 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
4925 if (rc
!= KERN_SUCCESS
) {
4926 struct vm_map_entry tmp_entry
= *entry
;
4928 /* unwire wired pages */
4929 tmp_entry
.vme_end
= va
;
4930 vm_fault_unwire(map
,
4931 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
4936 return KERN_SUCCESS
;
4942 * Unwire a range of virtual addresses in a map.
4947 vm_map_entry_t entry
,
4948 boolean_t deallocate
,
4950 vm_map_offset_t pmap_addr
)
4952 register vm_map_offset_t va
;
4953 register vm_map_offset_t end_addr
= entry
->vme_end
;
4955 struct vm_object_fault_info fault_info
;
4957 object
= (entry
->is_sub_map
)
4958 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
4961 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
4962 * do anything since such memory is wired by default. So we don't have
4963 * anything to undo here.
4966 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
)
4969 fault_info
.interruptible
= THREAD_UNINT
;
4970 fault_info
.behavior
= entry
->behavior
;
4971 fault_info
.user_tag
= entry
->alias
;
4972 fault_info
.pmap_options
= 0;
4973 if (entry
->iokit_acct
||
4974 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
4975 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
4977 fault_info
.lo_offset
= entry
->offset
;
4978 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
4979 fault_info
.no_cache
= entry
->no_cache
;
4980 fault_info
.stealth
= TRUE
;
4981 fault_info
.io_sync
= FALSE
;
4982 fault_info
.cs_bypass
= FALSE
;
4983 fault_info
.mark_zf_absent
= FALSE
;
4984 fault_info
.batch_pmap_op
= FALSE
;
4987 * Since the pages are wired down, we must be able to
4988 * get their mappings from the physical map system.
4991 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
4993 if (object
== VM_OBJECT_NULL
) {
4995 pmap_change_wiring(pmap
,
4996 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
4998 (void) vm_fault(map
, va
, VM_PROT_NONE
,
4999 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
5002 vm_page_t result_page
;
5004 vm_object_t result_object
;
5005 vm_fault_return_t result
;
5007 if (end_addr
- va
> (vm_size_t
) -1) {
5008 /* 32-bit overflow */
5009 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5011 fault_info
.cluster_size
= (vm_size_t
) (end_addr
- va
);
5012 assert(fault_info
.cluster_size
== end_addr
- va
);
5016 prot
= VM_PROT_NONE
;
5018 vm_object_lock(object
);
5019 vm_object_paging_begin(object
);
5021 "vm_fault_unwire -> vm_fault_page\n",
5023 result_page
= VM_PAGE_NULL
;
5024 result
= vm_fault_page(
5026 entry
->offset
+ (va
- entry
->vme_start
),
5028 FALSE
, /* page not looked up */
5029 &prot
, &result_page
, &top_page
,
5031 NULL
, map
->no_zero_fill
,
5032 FALSE
, &fault_info
);
5033 } while (result
== VM_FAULT_RETRY
);
5036 * If this was a mapping to a file on a device that has been forcibly
5037 * unmounted, then we won't get a page back from vm_fault_page(). Just
5038 * move on to the next one in case the remaining pages are mapped from
5039 * different objects. During a forced unmount, the object is terminated
5040 * so the alive flag will be false if this happens. A forced unmount will
5041 * will occur when an external disk is unplugged before the user does an
5042 * eject, so we don't want to panic in that situation.
5045 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
)
5048 if (result
== VM_FAULT_MEMORY_ERROR
&&
5049 object
== kernel_object
) {
5051 * This must have been allocated with
5052 * KMA_KOBJECT and KMA_VAONLY and there's
5053 * no physical page at this offset.
5054 * We're done (no page to free).
5060 if (result
!= VM_FAULT_SUCCESS
)
5061 panic("vm_fault_unwire: failure");
5063 result_object
= result_page
->object
;
5066 assert(result_page
->phys_page
!=
5067 vm_page_fictitious_addr
);
5068 pmap_disconnect(result_page
->phys_page
);
5069 VM_PAGE_FREE(result_page
);
5071 if ((pmap
) && (result_page
->phys_page
!= vm_page_guard_addr
))
5072 pmap_change_wiring(pmap
,
5073 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5076 if (VM_PAGE_WIRED(result_page
)) {
5077 vm_page_lockspin_queues();
5078 vm_page_unwire(result_page
, TRUE
);
5079 vm_page_unlock_queues();
5081 if(entry
->zero_wired_pages
) {
5082 pmap_zero_page(result_page
->phys_page
);
5083 entry
->zero_wired_pages
= FALSE
;
5086 PAGE_WAKEUP_DONE(result_page
);
5088 vm_fault_cleanup(result_object
, top_page
);
5093 * Inform the physical mapping system that the range
5094 * of addresses may fault, so that page tables and
5095 * such may be unwired themselves.
5098 pmap_pageable(pmap
, pmap_addr
,
5099 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
5104 * vm_fault_wire_fast:
5106 * Handle common case of a wire down page fault at the given address.
5107 * If successful, the page is inserted into the associated physical map.
5108 * The map entry is passed in to avoid the overhead of a map lookup.
5110 * NOTE: the given address should be truncated to the
5111 * proper page address.
5113 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
5114 * a standard error specifying why the fault is fatal is returned.
5116 * The map in question must be referenced, and remains so.
5117 * Caller has a read lock on the map.
5119 * This is a stripped version of vm_fault() for wiring pages. Anything
5120 * other than the common case will return KERN_FAILURE, and the caller
5121 * is expected to call vm_fault().
5125 __unused vm_map_t map
,
5127 vm_map_entry_t entry
,
5129 vm_map_offset_t pmap_addr
,
5130 ppnum_t
*physpage_p
)
5133 vm_object_offset_t offset
;
5134 register vm_page_t m
;
5136 thread_t thread
= current_thread();
5140 VM_STAT_INCR(faults
);
5142 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
)
5143 thread
->task
->faults
++;
5150 #define RELEASE_PAGE(m) { \
5151 PAGE_WAKEUP_DONE(m); \
5152 vm_page_lockspin_queues(); \
5153 vm_page_unwire(m, TRUE); \
5154 vm_page_unlock_queues(); \
5158 #undef UNLOCK_THINGS
5159 #define UNLOCK_THINGS { \
5160 vm_object_paging_end(object); \
5161 vm_object_unlock(object); \
5164 #undef UNLOCK_AND_DEALLOCATE
5165 #define UNLOCK_AND_DEALLOCATE { \
5167 vm_object_deallocate(object); \
5170 * Give up and have caller do things the hard way.
5174 UNLOCK_AND_DEALLOCATE; \
5175 return(KERN_FAILURE); \
5180 * If this entry is not directly to a vm_object, bail out.
5182 if (entry
->is_sub_map
) {
5183 assert(physpage_p
== NULL
);
5184 return(KERN_FAILURE
);
5188 * Find the backing store object and offset into it.
5191 object
= entry
->object
.vm_object
;
5192 offset
= (va
- entry
->vme_start
) + entry
->offset
;
5193 prot
= entry
->protection
;
5196 * Make a reference to this object to prevent its
5197 * disposal while we are messing with it.
5200 vm_object_lock(object
);
5201 vm_object_reference_locked(object
);
5202 vm_object_paging_begin(object
);
5205 * INVARIANTS (through entire routine):
5207 * 1) At all times, we must either have the object
5208 * lock or a busy page in some object to prevent
5209 * some other thread from trying to bring in
5212 * 2) Once we have a busy page, we must remove it from
5213 * the pageout queues, so that the pageout daemon
5214 * will not grab it away.
5219 * Look for page in top-level object. If it's not there or
5220 * there's something going on, give up.
5221 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
5222 * decrypt the page before wiring it down.
5224 m
= vm_page_lookup(object
, offset
);
5225 if ((m
== VM_PAGE_NULL
) || (m
->busy
) || (m
->encrypted
) ||
5226 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
5230 ASSERT_PAGE_DECRYPTED(m
);
5232 if (m
->fictitious
&&
5233 m
->phys_page
== vm_page_guard_addr
) {
5235 * Guard pages are fictitious pages and are never
5236 * entered into a pmap, so let's say it's been wired...
5243 * Wire the page down now. All bail outs beyond this
5244 * point must unwire the page.
5247 vm_page_lockspin_queues();
5249 vm_page_unlock_queues();
5252 * Mark page busy for other threads.
5259 * Give up if the page is being written and there's a copy object
5261 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
5267 * Put this page into the physical map.
5269 type_of_fault
= DBG_CACHE_HIT_FAULT
;
5270 kr
= vm_fault_enter(m
,
5280 ((entry
->iokit_acct
||
5281 (!entry
->is_sub_map
&& !entry
->use_pmap
))
5282 ? PMAP_OPTIONS_ALT_ACCT
5289 * Unlock everything, and return
5293 /* for vm_map_wire_and_extract() */
5294 if (kr
== KERN_SUCCESS
) {
5295 *physpage_p
= m
->phys_page
;
5296 if (prot
& VM_PROT_WRITE
) {
5297 vm_object_lock_assert_exclusive(m
->object
);
5305 PAGE_WAKEUP_DONE(m
);
5306 UNLOCK_AND_DEALLOCATE
;
5313 * Routine: vm_fault_copy_cleanup
5315 * Release a page used by vm_fault_copy.
5319 vm_fault_copy_cleanup(
5323 vm_object_t object
= page
->object
;
5325 vm_object_lock(object
);
5326 PAGE_WAKEUP_DONE(page
);
5327 if (!page
->active
&& !page
->inactive
&& !page
->throttled
) {
5328 vm_page_lockspin_queues();
5329 if (!page
->active
&& !page
->inactive
&& !page
->throttled
)
5330 vm_page_activate(page
);
5331 vm_page_unlock_queues();
5333 vm_fault_cleanup(object
, top_page
);
5337 vm_fault_copy_dst_cleanup(
5342 if (page
!= VM_PAGE_NULL
) {
5343 object
= page
->object
;
5344 vm_object_lock(object
);
5345 vm_page_lockspin_queues();
5346 vm_page_unwire(page
, TRUE
);
5347 vm_page_unlock_queues();
5348 vm_object_paging_end(object
);
5349 vm_object_unlock(object
);
5354 * Routine: vm_fault_copy
5357 * Copy pages from one virtual memory object to another --
5358 * neither the source nor destination pages need be resident.
5360 * Before actually copying a page, the version associated with
5361 * the destination address map wil be verified.
5363 * In/out conditions:
5364 * The caller must hold a reference, but not a lock, to
5365 * each of the source and destination objects and to the
5369 * Returns KERN_SUCCESS if no errors were encountered in
5370 * reading or writing the data. Returns KERN_INTERRUPTED if
5371 * the operation was interrupted (only possible if the
5372 * "interruptible" argument is asserted). Other return values
5373 * indicate a permanent error in copying the data.
5375 * The actual amount of data copied will be returned in the
5376 * "copy_size" argument. In the event that the destination map
5377 * verification failed, this amount may be less than the amount
5382 vm_object_t src_object
,
5383 vm_object_offset_t src_offset
,
5384 vm_map_size_t
*copy_size
, /* INOUT */
5385 vm_object_t dst_object
,
5386 vm_object_offset_t dst_offset
,
5388 vm_map_version_t
*dst_version
,
5391 vm_page_t result_page
;
5394 vm_page_t src_top_page
;
5398 vm_page_t dst_top_page
;
5401 vm_map_size_t amount_left
;
5402 vm_object_t old_copy_object
;
5403 kern_return_t error
= 0;
5404 vm_fault_return_t result
;
5406 vm_map_size_t part_size
;
5407 struct vm_object_fault_info fault_info_src
;
5408 struct vm_object_fault_info fault_info_dst
;
5411 * In order not to confuse the clustered pageins, align
5412 * the different offsets on a page boundary.
5417 *copy_size -= amount_left; \
5421 amount_left
= *copy_size
;
5423 fault_info_src
.interruptible
= interruptible
;
5424 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5425 fault_info_src
.user_tag
= 0;
5426 fault_info_src
.pmap_options
= 0;
5427 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
5428 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
5429 fault_info_src
.no_cache
= FALSE
;
5430 fault_info_src
.stealth
= TRUE
;
5431 fault_info_src
.io_sync
= FALSE
;
5432 fault_info_src
.cs_bypass
= FALSE
;
5433 fault_info_src
.mark_zf_absent
= FALSE
;
5434 fault_info_src
.batch_pmap_op
= FALSE
;
5436 fault_info_dst
.interruptible
= interruptible
;
5437 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5438 fault_info_dst
.user_tag
= 0;
5439 fault_info_dst
.pmap_options
= 0;
5440 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
5441 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
5442 fault_info_dst
.no_cache
= FALSE
;
5443 fault_info_dst
.stealth
= TRUE
;
5444 fault_info_dst
.io_sync
= FALSE
;
5445 fault_info_dst
.cs_bypass
= FALSE
;
5446 fault_info_dst
.mark_zf_absent
= FALSE
;
5447 fault_info_dst
.batch_pmap_op
= FALSE
;
5449 do { /* while (amount_left > 0) */
5451 * There may be a deadlock if both source and destination
5452 * pages are the same. To avoid this deadlock, the copy must
5453 * start by getting the destination page in order to apply
5454 * COW semantics if any.
5457 RetryDestinationFault
: ;
5459 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
5461 vm_object_lock(dst_object
);
5462 vm_object_paging_begin(dst_object
);
5464 if (amount_left
> (vm_size_t
) -1) {
5465 /* 32-bit overflow */
5466 fault_info_dst
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5468 fault_info_dst
.cluster_size
= (vm_size_t
) amount_left
;
5469 assert(fault_info_dst
.cluster_size
== amount_left
);
5472 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
5473 dst_page
= VM_PAGE_NULL
;
5474 result
= vm_fault_page(dst_object
,
5475 vm_object_trunc_page(dst_offset
),
5476 VM_PROT_WRITE
|VM_PROT_READ
,
5478 FALSE
, /* page not looked up */
5479 &dst_prot
, &dst_page
, &dst_top_page
,
5482 dst_map
->no_zero_fill
,
5483 FALSE
, &fault_info_dst
);
5485 case VM_FAULT_SUCCESS
:
5487 case VM_FAULT_RETRY
:
5488 goto RetryDestinationFault
;
5489 case VM_FAULT_MEMORY_SHORTAGE
:
5490 if (vm_page_wait(interruptible
))
5491 goto RetryDestinationFault
;
5493 case VM_FAULT_INTERRUPTED
:
5494 RETURN(MACH_SEND_INTERRUPTED
);
5495 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5496 /* success but no VM page: fail the copy */
5497 vm_object_paging_end(dst_object
);
5498 vm_object_unlock(dst_object
);
5500 case VM_FAULT_MEMORY_ERROR
:
5504 return(KERN_MEMORY_ERROR
);
5506 panic("vm_fault_copy: unexpected error 0x%x from "
5507 "vm_fault_page()\n", result
);
5509 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
5511 old_copy_object
= dst_page
->object
->copy
;
5514 * There exists the possiblity that the source and
5515 * destination page are the same. But we can't
5516 * easily determine that now. If they are the
5517 * same, the call to vm_fault_page() for the
5518 * destination page will deadlock. To prevent this we
5519 * wire the page so we can drop busy without having
5520 * the page daemon steal the page. We clean up the
5521 * top page but keep the paging reference on the object
5522 * holding the dest page so it doesn't go away.
5525 vm_page_lockspin_queues();
5526 vm_page_wire(dst_page
);
5527 vm_page_unlock_queues();
5528 PAGE_WAKEUP_DONE(dst_page
);
5529 vm_object_unlock(dst_page
->object
);
5531 if (dst_top_page
!= VM_PAGE_NULL
) {
5532 vm_object_lock(dst_object
);
5533 VM_PAGE_FREE(dst_top_page
);
5534 vm_object_paging_end(dst_object
);
5535 vm_object_unlock(dst_object
);
5540 if (src_object
== VM_OBJECT_NULL
) {
5542 * No source object. We will just
5543 * zero-fill the page in dst_object.
5545 src_page
= VM_PAGE_NULL
;
5546 result_page
= VM_PAGE_NULL
;
5548 vm_object_lock(src_object
);
5549 src_page
= vm_page_lookup(src_object
,
5550 vm_object_trunc_page(src_offset
));
5551 if (src_page
== dst_page
) {
5552 src_prot
= dst_prot
;
5553 result_page
= VM_PAGE_NULL
;
5555 src_prot
= VM_PROT_READ
;
5556 vm_object_paging_begin(src_object
);
5558 if (amount_left
> (vm_size_t
) -1) {
5559 /* 32-bit overflow */
5560 fault_info_src
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5562 fault_info_src
.cluster_size
= (vm_size_t
) amount_left
;
5563 assert(fault_info_src
.cluster_size
== amount_left
);
5567 "vm_fault_copy(2) -> vm_fault_page\n",
5569 result_page
= VM_PAGE_NULL
;
5570 result
= vm_fault_page(
5572 vm_object_trunc_page(src_offset
),
5573 VM_PROT_READ
, FALSE
,
5574 FALSE
, /* page not looked up */
5576 &result_page
, &src_top_page
,
5577 (int *)0, &error
, FALSE
,
5578 FALSE
, &fault_info_src
);
5581 case VM_FAULT_SUCCESS
:
5583 case VM_FAULT_RETRY
:
5584 goto RetrySourceFault
;
5585 case VM_FAULT_MEMORY_SHORTAGE
:
5586 if (vm_page_wait(interruptible
))
5587 goto RetrySourceFault
;
5589 case VM_FAULT_INTERRUPTED
:
5590 vm_fault_copy_dst_cleanup(dst_page
);
5591 RETURN(MACH_SEND_INTERRUPTED
);
5592 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5593 /* success but no VM page: fail */
5594 vm_object_paging_end(src_object
);
5595 vm_object_unlock(src_object
);
5597 case VM_FAULT_MEMORY_ERROR
:
5598 vm_fault_copy_dst_cleanup(dst_page
);
5602 return(KERN_MEMORY_ERROR
);
5604 panic("vm_fault_copy(2): unexpected "
5606 "vm_fault_page()\n", result
);
5610 assert((src_top_page
== VM_PAGE_NULL
) ==
5611 (result_page
->object
== src_object
));
5613 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
5614 vm_object_unlock(result_page
->object
);
5617 if (!vm_map_verify(dst_map
, dst_version
)) {
5618 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5619 vm_fault_copy_cleanup(result_page
, src_top_page
);
5620 vm_fault_copy_dst_cleanup(dst_page
);
5624 vm_object_lock(dst_page
->object
);
5626 if (dst_page
->object
->copy
!= old_copy_object
) {
5627 vm_object_unlock(dst_page
->object
);
5628 vm_map_verify_done(dst_map
, dst_version
);
5629 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5630 vm_fault_copy_cleanup(result_page
, src_top_page
);
5631 vm_fault_copy_dst_cleanup(dst_page
);
5634 vm_object_unlock(dst_page
->object
);
5637 * Copy the page, and note that it is dirty
5641 if (!page_aligned(src_offset
) ||
5642 !page_aligned(dst_offset
) ||
5643 !page_aligned(amount_left
)) {
5645 vm_object_offset_t src_po
,
5648 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
5649 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
5651 if (dst_po
> src_po
) {
5652 part_size
= PAGE_SIZE
- dst_po
;
5654 part_size
= PAGE_SIZE
- src_po
;
5656 if (part_size
> (amount_left
)){
5657 part_size
= amount_left
;
5660 if (result_page
== VM_PAGE_NULL
) {
5661 assert((vm_offset_t
) dst_po
== dst_po
);
5662 assert((vm_size_t
) part_size
== part_size
);
5663 vm_page_part_zero_fill(dst_page
,
5664 (vm_offset_t
) dst_po
,
5665 (vm_size_t
) part_size
);
5667 assert((vm_offset_t
) src_po
== src_po
);
5668 assert((vm_offset_t
) dst_po
== dst_po
);
5669 assert((vm_size_t
) part_size
== part_size
);
5670 vm_page_part_copy(result_page
,
5671 (vm_offset_t
) src_po
,
5673 (vm_offset_t
) dst_po
,
5674 (vm_size_t
)part_size
);
5675 if(!dst_page
->dirty
){
5676 vm_object_lock(dst_object
);
5677 SET_PAGE_DIRTY(dst_page
, TRUE
);
5678 vm_object_unlock(dst_page
->object
);
5683 part_size
= PAGE_SIZE
;
5685 if (result_page
== VM_PAGE_NULL
)
5686 vm_page_zero_fill(dst_page
);
5688 vm_object_lock(result_page
->object
);
5689 vm_page_copy(result_page
, dst_page
);
5690 vm_object_unlock(result_page
->object
);
5692 if(!dst_page
->dirty
){
5693 vm_object_lock(dst_object
);
5694 SET_PAGE_DIRTY(dst_page
, TRUE
);
5695 vm_object_unlock(dst_page
->object
);
5702 * Unlock everything, and return
5705 vm_map_verify_done(dst_map
, dst_version
);
5707 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5708 vm_fault_copy_cleanup(result_page
, src_top_page
);
5709 vm_fault_copy_dst_cleanup(dst_page
);
5711 amount_left
-= part_size
;
5712 src_offset
+= part_size
;
5713 dst_offset
+= part_size
;
5714 } while (amount_left
> 0);
5716 RETURN(KERN_SUCCESS
);
5722 #if VM_FAULT_CLASSIFY
5724 * Temporary statistics gathering support.
5728 * Statistics arrays:
5730 #define VM_FAULT_TYPES_MAX 5
5731 #define VM_FAULT_LEVEL_MAX 8
5733 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
5735 #define VM_FAULT_TYPE_ZERO_FILL 0
5736 #define VM_FAULT_TYPE_MAP_IN 1
5737 #define VM_FAULT_TYPE_PAGER 2
5738 #define VM_FAULT_TYPE_COPY 3
5739 #define VM_FAULT_TYPE_OTHER 4
5743 vm_fault_classify(vm_object_t object
,
5744 vm_object_offset_t offset
,
5745 vm_prot_t fault_type
)
5747 int type
, level
= 0;
5751 m
= vm_page_lookup(object
, offset
);
5752 if (m
!= VM_PAGE_NULL
) {
5753 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
) {
5754 type
= VM_FAULT_TYPE_OTHER
;
5757 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
5758 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
5759 type
= VM_FAULT_TYPE_MAP_IN
;
5762 type
= VM_FAULT_TYPE_COPY
;
5766 if (object
->pager_created
) {
5767 type
= VM_FAULT_TYPE_PAGER
;
5770 if (object
->shadow
== VM_OBJECT_NULL
) {
5771 type
= VM_FAULT_TYPE_ZERO_FILL
;
5775 offset
+= object
->vo_shadow_offset
;
5776 object
= object
->shadow
;
5782 if (level
> VM_FAULT_LEVEL_MAX
)
5783 level
= VM_FAULT_LEVEL_MAX
;
5785 vm_fault_stats
[type
][level
] += 1;
5790 /* cleanup routine to call from debugger */
5793 vm_fault_classify_init(void)
5797 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
5798 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
5799 vm_fault_stats
[type
][level
] = 0;
5805 #endif /* VM_FAULT_CLASSIFY */
5809 vm_page_validate_cs_mapped(
5814 vm_object_offset_t offset
;
5816 memory_object_t pager
;
5818 boolean_t validated
;
5822 vm_object_lock_assert_exclusive(page
->object
);
5824 if (!cs_validation
) {
5828 if (page
->wpmapped
&& !page
->cs_tainted
) {
5830 * This page was mapped for "write" access sometime in the
5831 * past and could still be modifiable in the future.
5832 * Consider it tainted.
5833 * [ If the page was already found to be "tainted", no
5834 * need to re-validate. ]
5836 page
->cs_validated
= TRUE
;
5837 page
->cs_tainted
= TRUE
;
5839 printf("CODESIGNING: vm_page_validate_cs: "
5840 "page %p obj %p off 0x%llx "
5842 page
, page
->object
, page
->offset
);
5844 vm_cs_validated_dirtied
++;
5847 if (page
->cs_validated
) {
5853 object
= page
->object
;
5854 assert(object
->code_signed
);
5855 offset
= page
->offset
;
5857 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
5859 * The object is terminating and we don't have its pager
5860 * so we can't validate the data...
5865 * Since we get here to validate a page that was brought in by
5866 * the pager, we know that this pager is all setup and ready
5869 assert(!object
->internal
);
5870 assert(object
->pager
!= NULL
);
5871 assert(object
->pager_ready
);
5873 pager
= object
->pager
;
5874 assert(object
->paging_in_progress
);
5875 kr
= vnode_pager_get_object_cs_blobs(pager
, &blobs
);
5876 if (kr
!= KERN_SUCCESS
) {
5880 /* verify the SHA1 hash for this page */
5882 validated
= cs_validate_page(blobs
,
5884 offset
+ object
->paging_offset
,
5885 (const void *)kaddr
,
5888 page
->cs_validated
= validated
;
5890 page
->cs_tainted
= !!(tainted
& CS_VALIDATE_TAINTED
);
5891 page
->cs_nx
= !!(tainted
& CS_VALIDATE_NX
);
5896 vm_page_validate_cs(
5900 vm_object_offset_t offset
;
5901 vm_map_offset_t koffset
;
5902 vm_map_size_t ksize
;
5905 boolean_t busy_page
;
5906 boolean_t need_unmap
;
5908 vm_object_lock_assert_held(page
->object
);
5910 if (!cs_validation
) {
5914 if (page
->wpmapped
&& !page
->cs_tainted
) {
5915 vm_object_lock_assert_exclusive(page
->object
);
5918 * This page was mapped for "write" access sometime in the
5919 * past and could still be modifiable in the future.
5920 * Consider it tainted.
5921 * [ If the page was already found to be "tainted", no
5922 * need to re-validate. ]
5924 page
->cs_validated
= TRUE
;
5925 page
->cs_tainted
= TRUE
;
5927 printf("CODESIGNING: vm_page_validate_cs: "
5928 "page %p obj %p off 0x%llx "
5930 page
, page
->object
, page
->offset
);
5932 vm_cs_validated_dirtied
++;
5935 if (page
->cs_validated
) {
5940 panic("vm_page_validate_cs(%p): page is slid\n", page
);
5942 assert(!page
->slid
);
5944 #if CHECK_CS_VALIDATION_BITMAP
5945 if ( vnode_pager_cs_check_validation_bitmap( page
->object
->pager
, trunc_page(page
->offset
+ page
->object
->paging_offset
), CS_BITMAP_CHECK
) == KERN_SUCCESS
) {
5946 page
->cs_validated
= TRUE
;
5947 page
->cs_tainted
= FALSE
;
5948 vm_cs_bitmap_validated
++;
5952 vm_object_lock_assert_exclusive(page
->object
);
5954 object
= page
->object
;
5955 assert(object
->code_signed
);
5956 offset
= page
->offset
;
5958 busy_page
= page
->busy
;
5960 /* keep page busy while we map (and unlock) the VM object */
5965 * Take a paging reference on the VM object
5966 * to protect it from collapse or bypass,
5967 * and keep it from disappearing too.
5969 vm_object_paging_begin(object
);
5971 /* map the page in the kernel address space */
5972 ksize
= PAGE_SIZE_64
;
5975 kr
= vm_paging_map_object(page
,
5979 FALSE
, /* can't unlock object ! */
5983 if (kr
!= KERN_SUCCESS
) {
5984 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr
);
5986 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
5988 /* validate the mapped page */
5989 vm_page_validate_cs_mapped(page
, (const void *) kaddr
);
5991 #if CHECK_CS_VALIDATION_BITMAP
5992 if ( page
->cs_validated
== TRUE
&& page
->cs_tainted
== FALSE
) {
5993 vnode_pager_cs_check_validation_bitmap( object
->pager
, trunc_page( offset
+ object
->paging_offset
), CS_BITMAP_SET
);
5997 assert(object
== page
->object
);
5998 vm_object_lock_assert_exclusive(object
);
6001 PAGE_WAKEUP_DONE(page
);
6004 /* unmap the map from the kernel address space */
6005 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
6010 vm_object_paging_end(object
);