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>
90 #include <kern/policy_internal.h>
92 #include <vm/vm_compressor.h>
93 #include <vm/vm_compressor_pager.h>
94 #include <vm/vm_fault.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_kern.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_protos.h>
102 #include <vm/vm_external.h>
103 #include <vm/memory_object.h>
104 #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */
105 #include <vm/vm_shared_region.h>
107 #include <sys/codesign.h>
108 #include <sys/reason.h>
109 #include <sys/signalvar.h>
111 #include <libsa/sys/timers.h> /* for struct timespec */
113 #define VM_FAULT_CLASSIFY 0
115 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
117 unsigned int vm_object_pagein_throttle
= 16;
120 * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
121 * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts
122 * of memory if they're buggy and can run the system completely out of swap space. If this happens, we
123 * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps
124 * keep the UI active so that the user has a chance to kill the offending task before the system
127 * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
128 * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold
129 * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a
130 * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
133 extern void throttle_lowpri_io(int);
135 extern struct vnode
*vnode_pager_lookup_vnode(memory_object_t
);
137 uint64_t vm_hard_throttle_threshold
;
141 #define NEED_TO_HARD_THROTTLE_THIS_TASK() (vm_wants_task_throttled(current_task()) || \
142 (vm_page_free_count < vm_page_throttle_limit && \
143 proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO) > THROTTLE_LEVEL_THROTTLED))
146 #define HARD_THROTTLE_DELAY 5000 /* 5000 us == 5 ms */
147 #define SOFT_THROTTLE_DELAY 200 /* 200 us == .2 ms */
149 #define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS 6
150 #define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC 20000
153 boolean_t
current_thread_aborted(void);
155 /* Forward declarations of internal routines. */
156 static kern_return_t
vm_fault_wire_fast(
160 vm_map_entry_t entry
,
162 vm_map_offset_t pmap_addr
,
163 ppnum_t
*physpage_p
);
165 static kern_return_t
vm_fault_internal(
167 vm_map_offset_t vaddr
,
168 vm_prot_t caller_prot
,
169 boolean_t change_wiring
,
172 vm_map_offset_t pmap_addr
,
173 ppnum_t
*physpage_p
);
175 static void vm_fault_copy_cleanup(
179 static void vm_fault_copy_dst_cleanup(
182 #if VM_FAULT_CLASSIFY
183 extern void vm_fault_classify(vm_object_t object
,
184 vm_object_offset_t offset
,
185 vm_prot_t fault_type
);
187 extern void vm_fault_classify_init(void);
190 unsigned long vm_pmap_enter_blocked
= 0;
191 unsigned long vm_pmap_enter_retried
= 0;
193 unsigned long vm_cs_validates
= 0;
194 unsigned long vm_cs_revalidates
= 0;
195 unsigned long vm_cs_query_modified
= 0;
196 unsigned long vm_cs_validated_dirtied
= 0;
197 unsigned long vm_cs_bitmap_validated
= 0;
199 void vm_pre_fault(vm_map_offset_t
);
201 extern char *kdp_compressor_decompressed_page
;
202 extern addr64_t kdp_compressor_decompressed_page_paddr
;
203 extern ppnum_t kdp_compressor_decompressed_page_ppnum
;
206 * Routine: vm_fault_init
208 * Initialize our private data structures.
213 int i
, vm_compressor_temp
;
214 boolean_t need_default_val
= TRUE
;
216 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
217 * computed as a percentage of available memory, and the percentage used is scaled inversely with
218 * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems
219 * and reduce the value down to 10% for very large memory configurations. This helps give us a
220 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
221 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
224 vm_hard_throttle_threshold
= sane_size
* (35 - MIN((int)(sane_size
/ (1024*1024*1024)), 25)) / 100;
227 * Configure compressed pager behavior. A boot arg takes precedence over a device tree entry.
230 if (PE_parse_boot_argn("vm_compressor", &vm_compressor_temp
, sizeof (vm_compressor_temp
))) {
231 for ( i
= 0; i
< VM_PAGER_MAX_MODES
; i
++) {
232 if (vm_compressor_temp
> 0 &&
233 ((vm_compressor_temp
& ( 1 << i
)) == vm_compressor_temp
)) {
234 need_default_val
= FALSE
;
235 vm_compressor_mode
= vm_compressor_temp
;
239 if (need_default_val
)
240 printf("Ignoring \"vm_compressor\" boot arg %d\n", vm_compressor_temp
);
242 if (need_default_val
) {
243 /* If no boot arg or incorrect boot arg, try device tree. */
244 PE_get_default("kern.vm_compressor", &vm_compressor_mode
, sizeof(vm_compressor_mode
));
246 PE_parse_boot_argn("vm_compressor_threads", &vm_compressor_thread_count
, sizeof (vm_compressor_thread_count
));
248 if (PE_parse_boot_argn("vm_compressor_immediate", &vm_compressor_temp
, sizeof (vm_compressor_temp
)))
249 vm_compressor_immediate_preferred_override
= TRUE
;
251 if (PE_get_default("kern.vm_compressor_immediate", &vm_compressor_temp
, sizeof(vm_compressor_temp
)))
252 vm_compressor_immediate_preferred_override
= TRUE
;
254 if (vm_compressor_immediate_preferred_override
== TRUE
) {
255 if (vm_compressor_temp
)
256 vm_compressor_immediate_preferred
= TRUE
;
258 vm_compressor_immediate_preferred
= FALSE
;
260 printf("\"vm_compressor_mode\" is %d\n", vm_compressor_mode
);
264 * Routine: vm_fault_cleanup
266 * Clean up the result of vm_fault_page.
268 * The paging reference for "object" is released.
269 * "object" is unlocked.
270 * If "top_page" is not null, "top_page" is
271 * freed and the paging reference for the object
272 * containing it is released.
275 * "object" must be locked.
282 vm_object_paging_end(object
);
283 vm_object_unlock(object
);
285 if (top_page
!= VM_PAGE_NULL
) {
286 object
= VM_PAGE_OBJECT(top_page
);
288 vm_object_lock(object
);
289 VM_PAGE_FREE(top_page
);
290 vm_object_paging_end(object
);
291 vm_object_unlock(object
);
295 #if MACH_CLUSTER_STATS
296 #define MAXCLUSTERPAGES 16
298 unsigned long pages_in_cluster
;
299 unsigned long pages_at_higher_offsets
;
300 unsigned long pages_at_lower_offsets
;
301 } cluster_stats_in
[MAXCLUSTERPAGES
];
302 #define CLUSTER_STAT(clause) clause
303 #define CLUSTER_STAT_HIGHER(x) \
304 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
305 #define CLUSTER_STAT_LOWER(x) \
306 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
307 #define CLUSTER_STAT_CLUSTER(x) \
308 ((cluster_stats_in[(x)].pages_in_cluster)++)
309 #else /* MACH_CLUSTER_STATS */
310 #define CLUSTER_STAT(clause)
311 #endif /* MACH_CLUSTER_STATS */
313 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
316 boolean_t vm_page_deactivate_behind
= TRUE
;
318 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
320 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
321 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
322 /* we use it to size an array on the stack */
324 int vm_default_behind
= VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW
;
326 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
329 * vm_page_is_sequential
331 * Determine if sequential access is in progress
332 * in accordance with the behavior specified.
333 * Update state to indicate current access pattern.
335 * object must have at least the shared lock held
339 vm_fault_is_sequential(
341 vm_object_offset_t offset
,
342 vm_behavior_t behavior
)
344 vm_object_offset_t last_alloc
;
348 last_alloc
= object
->last_alloc
;
349 sequential
= object
->sequential
;
350 orig_sequential
= sequential
;
353 case VM_BEHAVIOR_RANDOM
:
355 * reset indicator of sequential behavior
360 case VM_BEHAVIOR_SEQUENTIAL
:
361 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
363 * advance indicator of sequential behavior
365 if (sequential
< MAX_SEQUENTIAL_RUN
)
366 sequential
+= PAGE_SIZE
;
369 * reset indicator of sequential behavior
375 case VM_BEHAVIOR_RSEQNTL
:
376 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
378 * advance indicator of sequential behavior
380 if (sequential
> -MAX_SEQUENTIAL_RUN
)
381 sequential
-= PAGE_SIZE
;
384 * reset indicator of sequential behavior
390 case VM_BEHAVIOR_DEFAULT
:
392 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
394 * advance indicator of sequential behavior
398 if (sequential
< MAX_SEQUENTIAL_RUN
)
399 sequential
+= PAGE_SIZE
;
401 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
403 * advance indicator of sequential behavior
407 if (sequential
> -MAX_SEQUENTIAL_RUN
)
408 sequential
-= PAGE_SIZE
;
411 * reset indicator of sequential behavior
417 if (sequential
!= orig_sequential
) {
418 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
420 * if someone else has already updated object->sequential
421 * don't bother trying to update it or object->last_alloc
427 * I'd like to do this with a OSCompareAndSwap64, but that
428 * doesn't exist for PPC... however, it shouldn't matter
429 * that much... last_alloc is maintained so that we can determine
430 * if a sequential access pattern is taking place... if only
431 * one thread is banging on this object, no problem with the unprotected
432 * update... if 2 or more threads are banging away, we run the risk of
433 * someone seeing a mangled update... however, in the face of multiple
434 * accesses, no sequential access pattern can develop anyway, so we
435 * haven't lost any real info.
437 object
->last_alloc
= offset
;
441 int vm_page_deactivate_behind_count
= 0;
444 * vm_page_deactivate_behind
446 * Determine if sequential access is in progress
447 * in accordance with the behavior specified. If
448 * so, compute a potential page to deactivate and
451 * object must be locked.
453 * return TRUE if we actually deactivate a page
457 vm_fault_deactivate_behind(
459 vm_object_offset_t offset
,
460 vm_behavior_t behavior
)
463 int pages_in_run
= 0;
464 int max_pages_in_run
= 0;
466 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
467 vm_object_offset_t run_offset
= 0;
468 vm_object_offset_t pg_offset
= 0;
470 vm_page_t page_run
[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
];
474 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
477 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
479 * Do not deactivate pages from the kernel object: they
480 * are not intended to become pageable.
481 * or we've disabled the deactivate behind mechanism
485 if ((sequential_run
= object
->sequential
)) {
486 if (sequential_run
< 0) {
487 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
488 sequential_run
= 0 - sequential_run
;
490 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
494 case VM_BEHAVIOR_RANDOM
:
496 case VM_BEHAVIOR_SEQUENTIAL
:
497 if (sequential_run
>= (int)PAGE_SIZE
) {
498 run_offset
= 0 - PAGE_SIZE_64
;
499 max_pages_in_run
= 1;
502 case VM_BEHAVIOR_RSEQNTL
:
503 if (sequential_run
>= (int)PAGE_SIZE
) {
504 run_offset
= PAGE_SIZE_64
;
505 max_pages_in_run
= 1;
508 case VM_BEHAVIOR_DEFAULT
:
510 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
513 * determine if the run of sequential accesss has been
514 * long enough on an object with default access behavior
515 * to consider it for deactivation
517 if ((uint64_t)sequential_run
>= behind
&& (sequential_run
% (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
* PAGE_SIZE
)) == 0) {
519 * the comparisons between offset and behind are done
520 * in this kind of odd fashion in order to prevent wrap around
523 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
524 if (offset
>= behind
) {
525 run_offset
= 0 - behind
;
526 pg_offset
= PAGE_SIZE_64
;
527 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
530 if (offset
< -behind
) {
532 pg_offset
= 0 - PAGE_SIZE_64
;
533 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
540 for (n
= 0; n
< max_pages_in_run
; n
++) {
541 m
= vm_page_lookup(object
, offset
+ run_offset
+ (n
* pg_offset
));
543 if (m
&& !m
->laundry
&& !m
->busy
&& !m
->no_cache
&& (m
->vm_page_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && !m
->fictitious
&& !m
->absent
) {
544 page_run
[pages_in_run
++] = m
;
547 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
549 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
550 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
551 * new reference happens. If no futher references happen on the page after that remote TLB flushes
552 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
553 * by pageout_scan, which is just fine since the last reference would have happened quite far
554 * in the past (TLB caches don't hang around for very long), and of course could just as easily
555 * have happened before we did the deactivate_behind.
557 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m
), VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
561 vm_page_lockspin_queues();
563 for (n
= 0; n
< pages_in_run
; n
++) {
567 vm_page_deactivate_internal(m
, FALSE
);
569 vm_page_deactivate_behind_count
++;
571 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
574 vm_page_unlock_queues();
582 #if (DEVELOPMENT || DEBUG)
583 uint32_t vm_page_creation_throttled_hard
= 0;
584 uint32_t vm_page_creation_throttled_soft
= 0;
585 uint64_t vm_page_creation_throttle_avoided
= 0;
586 #endif /* DEVELOPMENT || DEBUG */
589 vm_page_throttled(boolean_t page_kept
)
591 clock_sec_t elapsed_sec
;
593 clock_usec_t tv_usec
;
595 thread_t thread
= current_thread();
597 if (thread
->options
& TH_OPT_VMPRIV
)
600 if (thread
->t_page_creation_throttled
) {
601 thread
->t_page_creation_throttled
= 0;
603 if (page_kept
== FALSE
)
606 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
607 #if (DEVELOPMENT || DEBUG)
608 thread
->t_page_creation_throttled_hard
++;
609 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
610 #endif /* DEVELOPMENT || DEBUG */
611 return (HARD_THROTTLE_DELAY
);
614 if ((vm_page_free_count
< vm_page_throttle_limit
|| (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& SWAPPER_NEEDS_TO_UNTHROTTLE())) &&
615 thread
->t_page_creation_count
> (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
* VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
)) {
617 if (vm_page_free_wanted
== 0 && vm_page_free_wanted_privileged
== 0) {
618 #if (DEVELOPMENT || DEBUG)
619 OSAddAtomic64(1, &vm_page_creation_throttle_avoided
);
623 clock_get_system_microtime(&tv_sec
, &tv_usec
);
625 elapsed_sec
= tv_sec
- thread
->t_page_creation_time
;
627 if (elapsed_sec
<= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
||
628 (thread
->t_page_creation_count
/ elapsed_sec
) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
) {
630 if (elapsed_sec
>= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
)) {
632 * we'll reset our stats to give a well behaved app
633 * that was unlucky enough to accumulate a bunch of pages
634 * over a long period of time a chance to get out of
635 * the throttled state... we reset the counter and timestamp
636 * so that if it stays under the rate limit for the next second
637 * it will be back in our good graces... if it exceeds it, it
638 * will remain in the throttled state
640 thread
->t_page_creation_time
= tv_sec
;
641 thread
->t_page_creation_count
= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC
* (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS
- 1);
643 ++vm_page_throttle_count
;
645 thread
->t_page_creation_throttled
= 1;
647 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
&& HARD_THROTTLE_LIMIT_REACHED()) {
648 #if (DEVELOPMENT || DEBUG)
649 thread
->t_page_creation_throttled_hard
++;
650 OSAddAtomic(1, &vm_page_creation_throttled_hard
);
651 #endif /* DEVELOPMENT || DEBUG */
652 return (HARD_THROTTLE_DELAY
);
654 #if (DEVELOPMENT || DEBUG)
655 thread
->t_page_creation_throttled_soft
++;
656 OSAddAtomic(1, &vm_page_creation_throttled_soft
);
657 #endif /* DEVELOPMENT || DEBUG */
658 return (SOFT_THROTTLE_DELAY
);
661 thread
->t_page_creation_time
= tv_sec
;
662 thread
->t_page_creation_count
= 0;
665 thread
->t_page_creation_count
++;
672 * check for various conditions that would
673 * prevent us from creating a ZF page...
674 * cleanup is based on being called from vm_fault_page
676 * object must be locked
677 * object == m->object
679 static vm_fault_return_t
680 vm_fault_check(vm_object_t object
, vm_page_t m
, vm_page_t first_m
, boolean_t interruptible_state
, boolean_t page_throttle
)
684 if (object
->shadow_severed
||
685 VM_OBJECT_PURGEABLE_FAULT_ERROR(object
)) {
688 * 1. the shadow chain was severed,
689 * 2. the purgeable object is volatile or empty and is marked
690 * to fault on access while volatile.
691 * Just have to return an error at this point
693 if (m
!= VM_PAGE_NULL
)
695 vm_fault_cleanup(object
, first_m
);
697 thread_interrupt_level(interruptible_state
);
699 return (VM_FAULT_MEMORY_ERROR
);
701 if (vm_backing_store_low
) {
703 * are we protecting the system from
704 * backing store exhaustion. If so
705 * sleep unless we are privileged.
707 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
709 if (m
!= VM_PAGE_NULL
)
711 vm_fault_cleanup(object
, first_m
);
713 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
715 thread_block(THREAD_CONTINUE_NULL
);
716 thread_interrupt_level(interruptible_state
);
718 return (VM_FAULT_RETRY
);
721 if (page_throttle
== TRUE
) {
722 if ((throttle_delay
= vm_page_throttled(FALSE
))) {
724 * we're throttling zero-fills...
725 * treat this as if we couldn't grab a page
727 if (m
!= VM_PAGE_NULL
)
729 vm_fault_cleanup(object
, first_m
);
731 VM_DEBUG_EVENT(vmf_check_zfdelay
, VMF_CHECK_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
733 delay(throttle_delay
);
735 if (current_thread_aborted()) {
736 thread_interrupt_level(interruptible_state
);
737 return VM_FAULT_INTERRUPTED
;
739 thread_interrupt_level(interruptible_state
);
741 return (VM_FAULT_MEMORY_SHORTAGE
);
744 return (VM_FAULT_SUCCESS
);
749 * do the work to zero fill a page and
750 * inject it into the correct paging queue
752 * m->object must be locked
753 * page queue lock must NOT be held
756 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
758 int my_fault
= DBG_ZERO_FILL_FAULT
;
761 object
= VM_PAGE_OBJECT(m
);
764 * This is is a zero-fill page fault...
766 * Checking the page lock is a waste of
767 * time; this page was absent, so
768 * it can't be page locked by a pager.
770 * we also consider it undefined
771 * with respect to instruction
772 * execution. i.e. it is the responsibility
773 * of higher layers to call for an instruction
774 * sync after changing the contents and before
775 * sending a program into this area. We
776 * choose this approach for performance
780 m
->cs_validated
= FALSE
;
781 m
->cs_tainted
= FALSE
;
784 if (no_zero_fill
== TRUE
) {
785 my_fault
= DBG_NZF_PAGE_FAULT
;
787 if (m
->absent
&& m
->busy
)
790 vm_page_zero_fill(m
);
792 VM_STAT_INCR(zero_fill_count
);
793 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
796 assert(object
!= kernel_object
);
797 //assert(m->pageq.next == 0 && m->pageq.prev == 0);
799 if (!VM_DYNAMIC_PAGING_ENABLED() &&
800 (object
->purgable
== VM_PURGABLE_DENY
||
801 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
802 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
804 vm_page_lockspin_queues();
806 if (!VM_DYNAMIC_PAGING_ENABLED()) {
807 assert(!VM_PAGE_WIRED(m
));
810 * can't be on the pageout queue since we don't
811 * have a pager to try and clean to
813 vm_page_queues_remove(m
, TRUE
);
814 vm_page_check_pageable_safe(m
);
815 vm_page_queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
816 m
->vm_page_q_state
= VM_PAGE_ON_THROTTLED_Q
;
817 vm_page_throttled_count
++;
819 vm_page_unlock_queues();
826 * Routine: vm_fault_page
828 * Find the resident page for the virtual memory
829 * specified by the given virtual memory object
831 * Additional arguments:
832 * The required permissions for the page is given
833 * in "fault_type". Desired permissions are included
835 * fault_info is passed along to determine pagein cluster
836 * limits... it contains the expected reference pattern,
837 * cluster size if available, etc...
839 * If the desired page is known to be resident (for
840 * example, because it was previously wired down), asserting
841 * the "unwiring" parameter will speed the search.
843 * If the operation can be interrupted (by thread_abort
844 * or thread_terminate), then the "interruptible"
845 * parameter should be asserted.
848 * The page containing the proper data is returned
852 * The source object must be locked and referenced,
853 * and must donate one paging reference. The reference
854 * is not affected. The paging reference and lock are
857 * If the call succeeds, the object in which "result_page"
858 * resides is left locked and holding a paging reference.
859 * If this is not the original object, a busy page in the
860 * original object is returned in "top_page", to prevent other
861 * callers from pursuing this same data, along with a paging
862 * reference for the original object. The "top_page" should
863 * be destroyed when this guarantee is no longer required.
864 * The "result_page" is also left busy. It is not removed
865 * from the pageout queues.
867 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
868 * fault succeeded but there's no VM page (i.e. the VM object
869 * does not actually hold VM pages, but device memory or
870 * large pages). The object is still locked and we still hold a
871 * paging_in_progress reference.
873 unsigned int vm_fault_page_blocked_access
= 0;
874 unsigned int vm_fault_page_forced_retry
= 0;
879 vm_object_t first_object
, /* Object to begin search */
880 vm_object_offset_t first_offset
, /* Offset into object */
881 vm_prot_t fault_type
, /* What access is requested */
882 boolean_t must_be_resident
,/* Must page be resident? */
883 boolean_t caller_lookup
, /* caller looked up page */
884 /* Modifies in place: */
885 vm_prot_t
*protection
, /* Protection for mapping */
886 vm_page_t
*result_page
, /* Page found, if successful */
888 vm_page_t
*top_page
, /* Page in top object, if
889 * not result_page. */
890 int *type_of_fault
, /* if non-null, fill in with type of fault
891 * COW, zero-fill, etc... returned in trace point */
892 /* More arguments: */
893 kern_return_t
*error_code
, /* code if page is in error */
894 boolean_t no_zero_fill
, /* don't zero fill absent pages */
895 boolean_t data_supply
, /* treat as data_supply if
896 * it is a write fault and a full
897 * page is provided */
898 vm_object_fault_info_t fault_info
)
902 vm_object_offset_t offset
;
904 vm_object_t next_object
;
905 vm_object_t copy_object
;
906 boolean_t look_for_page
;
907 boolean_t force_fault_retry
= FALSE
;
908 vm_prot_t access_required
= fault_type
;
909 vm_prot_t wants_copy_flag
;
910 CLUSTER_STAT(int pages_at_higher_offsets
;)
911 CLUSTER_STAT(int pages_at_lower_offsets
;)
912 kern_return_t wait_result
;
913 boolean_t interruptible_state
;
914 boolean_t data_already_requested
= FALSE
;
915 vm_behavior_t orig_behavior
;
916 vm_size_t orig_cluster_size
;
917 vm_fault_return_t error
;
919 uint32_t try_failed_count
;
920 int interruptible
; /* how may fault be interrupted? */
921 int external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
922 memory_object_t pager
;
923 vm_fault_return_t retval
;
927 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
928 * marked as paged out in the compressor pager or the pager doesn't exist.
929 * Note also that if the pager for an internal object
930 * has not been created, the pager is not invoked regardless of the value
931 * of MUST_ASK_PAGER().
933 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
934 * is marked as paged out in the compressor pager.
935 * PAGED_OUT() is used to determine if a page has already been pushed
936 * into a copy object in order to avoid a redundant page out operation.
938 #define MUST_ASK_PAGER(o, f, s) \
939 ((s = VM_COMPRESSOR_PAGER_STATE_GET((o), (f))) != VM_EXTERNAL_STATE_ABSENT)
941 #define PAGED_OUT(o, f) \
942 (VM_COMPRESSOR_PAGER_STATE_GET((o), (f)) == VM_EXTERNAL_STATE_EXISTS)
947 #define RELEASE_PAGE(m) \
949 PAGE_WAKEUP_DONE(m); \
950 if ( !VM_PAGE_PAGEABLE(m)) { \
951 vm_page_lockspin_queues(); \
952 if ( !VM_PAGE_PAGEABLE(m)) { \
953 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE) \
954 vm_page_deactivate(m); \
956 vm_page_activate(m); \
958 vm_page_unlock_queues(); \
963 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
966 interruptible
= fault_info
->interruptible
;
967 interruptible_state
= thread_interrupt_level(interruptible
);
970 * INVARIANTS (through entire routine):
972 * 1) At all times, we must either have the object
973 * lock or a busy page in some object to prevent
974 * some other thread from trying to bring in
977 * Note that we cannot hold any locks during the
978 * pager access or when waiting for memory, so
979 * we use a busy page then.
981 * 2) To prevent another thread from racing us down the
982 * shadow chain and entering a new page in the top
983 * object before we do, we must keep a busy page in
984 * the top object while following the shadow chain.
986 * 3) We must increment paging_in_progress on any object
987 * for which we have a busy page before dropping
990 * 4) We leave busy pages on the pageout queues.
991 * If the pageout daemon comes across a busy page,
992 * it will remove the page from the pageout queues.
995 object
= first_object
;
996 offset
= first_offset
;
997 first_m
= VM_PAGE_NULL
;
998 access_required
= fault_type
;
1002 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
1003 object
, offset
, fault_type
, *protection
, 0);
1006 * default type of fault
1008 my_fault
= DBG_CACHE_HIT_FAULT
;
1012 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1016 #if CONFIG_SECLUDED_MEMORY
1017 if (object
->can_grab_secluded
) {
1018 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
1020 #endif /* CONFIG_SECLUDED_MEMORY */
1022 if (!object
->alive
) {
1024 * object is no longer valid
1025 * clean up and return error
1027 vm_fault_cleanup(object
, first_m
);
1028 thread_interrupt_level(interruptible_state
);
1030 return (VM_FAULT_MEMORY_ERROR
);
1033 if (!object
->pager_created
&& object
->phys_contiguous
) {
1035 * A physically-contiguous object without a pager:
1036 * must be a "large page" object. We do not deal
1037 * with VM pages for this object.
1039 caller_lookup
= FALSE
;
1041 goto phys_contig_object
;
1044 if (object
->blocked_access
) {
1046 * Access to this VM object has been blocked.
1047 * Replace our "paging_in_progress" reference with
1048 * a "activity_in_progress" reference and wait for
1049 * access to be unblocked.
1051 caller_lookup
= FALSE
; /* no longer valid after sleep */
1052 vm_object_activity_begin(object
);
1053 vm_object_paging_end(object
);
1054 while (object
->blocked_access
) {
1055 vm_object_sleep(object
,
1056 VM_OBJECT_EVENT_UNBLOCKED
,
1059 vm_fault_page_blocked_access
++;
1060 vm_object_paging_begin(object
);
1061 vm_object_activity_end(object
);
1065 * See whether the page at 'offset' is resident
1067 if (caller_lookup
== TRUE
) {
1069 * The caller has already looked up the page
1070 * and gave us the result in "result_page".
1071 * We can use this for the first lookup but
1072 * it loses its validity as soon as we unlock
1076 caller_lookup
= FALSE
; /* no longer valid after that */
1078 m
= vm_page_lookup(object
, offset
);
1081 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1083 if (m
!= VM_PAGE_NULL
) {
1087 * The page is being brought in,
1088 * wait for it and then retry.
1091 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1093 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
1096 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
1099 counter(c_vm_fault_page_block_busy_kernel
++);
1101 if (wait_result
!= THREAD_AWAKENED
) {
1102 vm_fault_cleanup(object
, first_m
);
1103 thread_interrupt_level(interruptible_state
);
1105 if (wait_result
== THREAD_RESTART
)
1106 return (VM_FAULT_RETRY
);
1108 return (VM_FAULT_INTERRUPTED
);
1113 m
->free_when_done
= FALSE
;
1116 vm_pageout_steal_laundry(m
, FALSE
);
1118 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
1120 * Guard page: off limits !
1122 if (fault_type
== VM_PROT_NONE
) {
1124 * The fault is not requesting any
1125 * access to the guard page, so it must
1126 * be just to wire or unwire it.
1127 * Let's pretend it succeeded...
1131 assert(first_m
== VM_PAGE_NULL
);
1132 *top_page
= first_m
;
1134 *type_of_fault
= DBG_GUARD_FAULT
;
1135 thread_interrupt_level(interruptible_state
);
1136 return VM_FAULT_SUCCESS
;
1139 * The fault requests access to the
1140 * guard page: let's deny that !
1142 vm_fault_cleanup(object
, first_m
);
1143 thread_interrupt_level(interruptible_state
);
1144 return VM_FAULT_MEMORY_ERROR
;
1150 * The page is in error, give up now.
1153 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1156 *error_code
= KERN_MEMORY_ERROR
;
1159 vm_fault_cleanup(object
, first_m
);
1160 thread_interrupt_level(interruptible_state
);
1162 return (VM_FAULT_MEMORY_ERROR
);
1166 * The pager wants us to restart
1167 * at the top of the chain,
1168 * typically because it has moved the
1169 * page to another pager, then do so.
1172 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1176 vm_fault_cleanup(object
, first_m
);
1177 thread_interrupt_level(interruptible_state
);
1179 return (VM_FAULT_RETRY
);
1183 * The page isn't busy, but is absent,
1184 * therefore it's deemed "unavailable".
1186 * Remove the non-existent page (unless it's
1187 * in the top object) and move on down to the
1188 * next object (if there is one).
1191 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1193 next_object
= object
->shadow
;
1195 if (next_object
== VM_OBJECT_NULL
) {
1197 * Absent page at bottom of shadow
1198 * chain; zero fill the page we left
1199 * busy in the first object, and free
1202 assert(!must_be_resident
);
1205 * check for any conditions that prevent
1206 * us from creating a new zero-fill page
1207 * vm_fault_check will do all of the
1208 * fault cleanup in the case of an error condition
1209 * including resetting the thread_interrupt_level
1211 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1213 if (error
!= VM_FAULT_SUCCESS
)
1217 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
1222 if (object
!= first_object
) {
1224 * free the absent page we just found
1229 * drop reference and lock on current object
1231 vm_object_paging_end(object
);
1232 vm_object_unlock(object
);
1235 * grab the original page we
1236 * 'soldered' in place and
1237 * retake lock on 'first_object'
1240 first_m
= VM_PAGE_NULL
;
1242 object
= first_object
;
1243 offset
= first_offset
;
1245 vm_object_lock(object
);
1248 * we're going to use the absent page we just found
1249 * so convert it to a 'busy' page
1254 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1257 * zero-fill the page and put it on
1258 * the correct paging queue
1260 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1264 if (must_be_resident
)
1265 vm_object_paging_end(object
);
1266 else if (object
!= first_object
) {
1267 vm_object_paging_end(object
);
1274 vm_page_lockspin_queues();
1275 vm_page_queues_remove(m
, FALSE
);
1276 vm_page_unlock_queues();
1279 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1282 offset
+object
->vo_shadow_offset
,0);
1284 offset
+= object
->vo_shadow_offset
;
1285 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1286 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1287 access_required
= VM_PROT_READ
;
1289 vm_object_lock(next_object
);
1290 vm_object_unlock(object
);
1291 object
= next_object
;
1292 vm_object_paging_begin(object
);
1295 * reset to default type of fault
1297 my_fault
= DBG_CACHE_HIT_FAULT
;
1303 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1304 && (fault_type
& VM_PROT_WRITE
)) {
1306 * This is a copy-on-write fault that will
1307 * cause us to revoke access to this page, but
1308 * this page is in the process of being cleaned
1309 * in a clustered pageout. We must wait until
1310 * the cleaning operation completes before
1311 * revoking access to the original page,
1312 * otherwise we might attempt to remove a
1316 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1319 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1323 * take an extra ref so that object won't die
1325 vm_object_reference_locked(object
);
1327 vm_fault_cleanup(object
, first_m
);
1329 counter(c_vm_fault_page_block_backoff_kernel
++);
1330 vm_object_lock(object
);
1331 assert(object
->ref_count
> 0);
1333 m
= vm_page_lookup(object
, offset
);
1335 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
1336 PAGE_ASSERT_WAIT(m
, interruptible
);
1338 vm_object_unlock(object
);
1339 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1340 vm_object_deallocate(object
);
1344 vm_object_unlock(object
);
1346 vm_object_deallocate(object
);
1347 thread_interrupt_level(interruptible_state
);
1349 return (VM_FAULT_RETRY
);
1352 if (type_of_fault
== NULL
&& (m
->vm_page_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) &&
1353 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1355 * If we were passed a non-NULL pointer for
1356 * "type_of_fault", than we came from
1357 * vm_fault... we'll let it deal with
1358 * this condition, since it
1359 * needs to see m->speculative to correctly
1360 * account the pageins, otherwise...
1361 * take it off the speculative queue, we'll
1362 * let the caller of vm_fault_page deal
1363 * with getting it onto the correct queue
1365 * If the caller specified in fault_info that
1366 * it wants a "stealth" fault, we also leave
1367 * the page in the speculative queue.
1369 vm_page_lockspin_queues();
1370 if (m
->vm_page_q_state
== VM_PAGE_ON_SPECULATIVE_Q
)
1371 vm_page_queues_remove(m
, FALSE
);
1372 vm_page_unlock_queues();
1374 assert(object
== VM_PAGE_OBJECT(m
));
1379 * the user needs access to a page that we
1380 * encrypted before paging it out.
1381 * Decrypt the page now.
1382 * Keep it busy to prevent anyone from
1383 * accessing it during the decryption.
1386 vm_page_decrypt(m
, 0);
1388 PAGE_WAKEUP_DONE(m
);
1391 * Retry from the top, in case
1392 * something changed while we were
1397 ASSERT_PAGE_DECRYPTED(m
);
1399 if (object
->code_signed
) {
1402 * We just paged in a page from a signed
1403 * memory object but we don't need to
1404 * validate it now. We'll validate it if
1405 * when it gets mapped into a user address
1406 * space for the first time or when the page
1407 * gets copied to another object as a result
1408 * of a copy-on-write.
1413 * We mark the page busy and leave it on
1414 * the pageout queues. If the pageout
1415 * deamon comes across it, then it will
1416 * remove the page from the queue, but not the object
1419 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1422 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1423 object
, offset
, m
, 0, 0);
1433 * we get here when there is no page present in the object at
1434 * the offset we're interested in... we'll allocate a page
1435 * at this point if the pager associated with
1436 * this object can provide the data or we're the top object...
1437 * object is locked; m == NULL
1439 if (must_be_resident
) {
1440 if (fault_type
== VM_PROT_NONE
&&
1441 object
== kernel_object
) {
1443 * We've been called from vm_fault_unwire()
1444 * while removing a map entry that was allocated
1445 * with KMA_KOBJECT and KMA_VAONLY. This page
1446 * is not present and there's nothing more to
1447 * do here (nothing to unwire).
1449 vm_fault_cleanup(object
, first_m
);
1450 thread_interrupt_level(interruptible_state
);
1452 return VM_FAULT_MEMORY_ERROR
;
1455 goto dont_look_for_page
;
1457 data_supply
= FALSE
;
1459 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
, external_state
) == TRUE
) && !data_supply
);
1462 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1464 if (!look_for_page
&& object
== first_object
&& !object
->phys_contiguous
) {
1466 * Allocate a new page for this object/offset pair as a placeholder
1468 m
= vm_page_grab_options(grab_options
);
1470 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1472 if (m
== VM_PAGE_NULL
) {
1474 vm_fault_cleanup(object
, first_m
);
1475 thread_interrupt_level(interruptible_state
);
1477 return (VM_FAULT_MEMORY_SHORTAGE
);
1480 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1481 vm_page_insert_internal(m
, object
, offset
, VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1483 vm_page_insert(m
, object
, offset
);
1486 if (look_for_page
) {
1491 * If the memory manager is not ready, we
1492 * cannot make requests.
1494 if (!object
->pager_ready
) {
1496 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1498 if (m
!= VM_PAGE_NULL
)
1502 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1503 object
, offset
, 0, 0, 0);
1506 * take an extra ref so object won't die
1508 vm_object_reference_locked(object
);
1509 vm_fault_cleanup(object
, first_m
);
1510 counter(c_vm_fault_page_block_backoff_kernel
++);
1512 vm_object_lock(object
);
1513 assert(object
->ref_count
> 0);
1515 if (!object
->pager_ready
) {
1516 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1518 vm_object_unlock(object
);
1519 if (wait_result
== THREAD_WAITING
)
1520 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1521 vm_object_deallocate(object
);
1525 vm_object_unlock(object
);
1526 vm_object_deallocate(object
);
1527 thread_interrupt_level(interruptible_state
);
1529 return (VM_FAULT_RETRY
);
1532 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1534 * If there are too many outstanding page
1535 * requests pending on this external object, we
1536 * wait for them to be resolved now.
1539 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1541 if (m
!= VM_PAGE_NULL
)
1544 * take an extra ref so object won't die
1546 vm_object_reference_locked(object
);
1548 vm_fault_cleanup(object
, first_m
);
1550 counter(c_vm_fault_page_block_backoff_kernel
++);
1552 vm_object_lock(object
);
1553 assert(object
->ref_count
> 0);
1555 if (object
->paging_in_progress
>= vm_object_pagein_throttle
) {
1556 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS
, interruptible
);
1558 vm_object_unlock(object
);
1559 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1560 vm_object_deallocate(object
);
1564 vm_object_unlock(object
);
1565 vm_object_deallocate(object
);
1566 thread_interrupt_level(interruptible_state
);
1568 return (VM_FAULT_RETRY
);
1571 if (object
->internal
) {
1572 int compressed_count_delta
;
1574 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
1576 if (m
== VM_PAGE_NULL
) {
1578 * Allocate a new page for this object/offset pair as a placeholder
1580 m
= vm_page_grab_options(grab_options
);
1582 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1584 if (m
== VM_PAGE_NULL
) {
1586 vm_fault_cleanup(object
, first_m
);
1587 thread_interrupt_level(interruptible_state
);
1589 return (VM_FAULT_MEMORY_SHORTAGE
);
1593 if (fault_info
&& fault_info
->batch_pmap_op
== TRUE
) {
1594 vm_page_insert_internal(m
, object
, offset
, VM_KERN_MEMORY_NONE
, FALSE
, TRUE
, TRUE
, FALSE
, NULL
);
1596 vm_page_insert(m
, object
, offset
);
1602 pager
= object
->pager
;
1604 assert(object
->paging_in_progress
> 0);
1605 vm_object_unlock(object
);
1607 rc
= vm_compressor_pager_get(
1609 offset
+ object
->paging_offset
,
1610 VM_PAGE_GET_PHYS_PAGE(m
),
1613 &compressed_count_delta
);
1615 if (type_of_fault
== NULL
) {
1619 * we weren't called from vm_fault, so we
1620 * need to apply page creation throttling
1621 * do it before we re-acquire any locks
1623 if (my_fault_type
== DBG_COMPRESSOR_FAULT
) {
1624 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
1625 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 1, 0);
1626 delay(throttle_delay
);
1630 vm_object_lock(object
);
1631 assert(object
->paging_in_progress
> 0);
1633 vm_compressor_pager_count(
1635 compressed_count_delta
,
1636 FALSE
, /* shared_lock */
1643 if ((object
->wimg_bits
&
1645 VM_WIMG_USE_DEFAULT
) {
1647 * If the page is not cacheable,
1648 * we can't let its contents
1649 * linger in the data cache
1650 * after the decompression.
1652 pmap_sync_page_attributes_phys(
1653 VM_PAGE_GET_PHYS_PAGE(m
));
1655 m
->written_by_kernel
= TRUE
;
1659 * If the object is purgeable, its
1660 * owner's purgeable ledgers have been
1661 * updated in vm_page_insert() but the
1662 * page was also accounted for in a
1663 * "compressed purgeable" ledger, so
1666 if ((object
->purgable
!=
1667 VM_PURGABLE_DENY
) &&
1668 (object
->vo_purgeable_owner
!=
1671 * One less compressed
1674 vm_purgeable_compressed_update(
1680 case KERN_MEMORY_FAILURE
:
1685 case KERN_MEMORY_ERROR
:
1689 panic("vm_fault_page(): unexpected "
1691 "vm_compressor_pager_get()\n",
1694 PAGE_WAKEUP_DONE(m
);
1697 goto data_requested
;
1699 my_fault_type
= DBG_PAGEIN_FAULT
;
1701 if (m
!= VM_PAGE_NULL
) {
1707 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1711 * It's possible someone called vm_object_destroy while we weren't
1712 * holding the object lock. If that has happened, then bail out
1716 pager
= object
->pager
;
1718 if (pager
== MEMORY_OBJECT_NULL
) {
1719 vm_fault_cleanup(object
, first_m
);
1720 thread_interrupt_level(interruptible_state
);
1721 return VM_FAULT_MEMORY_ERROR
;
1725 * We have an absent page in place for the faulting offset,
1726 * so we can release the object lock.
1729 vm_object_unlock(object
);
1732 * If this object uses a copy_call strategy,
1733 * and we are interested in a copy of this object
1734 * (having gotten here only by following a
1735 * shadow chain), then tell the memory manager
1736 * via a flag added to the desired_access
1737 * parameter, so that it can detect a race
1738 * between our walking down the shadow chain
1739 * and its pushing pages up into a copy of
1740 * the object that it manages.
1742 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
)
1743 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1745 wants_copy_flag
= VM_PROT_NONE
;
1748 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1750 access_required
| wants_copy_flag
, 0);
1752 if (object
->copy
== first_object
) {
1754 * if we issue the memory_object_data_request in
1755 * this state, we are subject to a deadlock with
1756 * the underlying filesystem if it is trying to
1757 * shrink the file resulting in a push of pages
1758 * into the copy object... that push will stall
1759 * on the placeholder page, and if the pushing thread
1760 * is holding a lock that is required on the pagein
1761 * path (such as a truncate lock), we'll deadlock...
1762 * to avoid this potential deadlock, we throw away
1763 * our placeholder page before calling memory_object_data_request
1764 * and force this thread to retry the vm_fault_page after
1765 * we have issued the I/O. the second time through this path
1766 * we will find the page already in the cache (presumably still
1767 * busy waiting for the I/O to complete) and then complete
1768 * the fault w/o having to go through memory_object_data_request again
1770 assert(first_m
!= VM_PAGE_NULL
);
1771 assert(VM_PAGE_OBJECT(first_m
) == first_object
);
1773 vm_object_lock(first_object
);
1774 VM_PAGE_FREE(first_m
);
1775 vm_object_paging_end(first_object
);
1776 vm_object_unlock(first_object
);
1778 first_m
= VM_PAGE_NULL
;
1779 force_fault_retry
= TRUE
;
1781 vm_fault_page_forced_retry
++;
1784 if (data_already_requested
== TRUE
) {
1785 orig_behavior
= fault_info
->behavior
;
1786 orig_cluster_size
= fault_info
->cluster_size
;
1788 fault_info
->behavior
= VM_BEHAVIOR_RANDOM
;
1789 fault_info
->cluster_size
= PAGE_SIZE
;
1792 * Call the memory manager to retrieve the data.
1794 rc
= memory_object_data_request(
1796 offset
+ object
->paging_offset
,
1798 access_required
| wants_copy_flag
,
1799 (memory_object_fault_info_t
)fault_info
);
1801 if (data_already_requested
== TRUE
) {
1802 fault_info
->behavior
= orig_behavior
;
1803 fault_info
->cluster_size
= orig_cluster_size
;
1805 data_already_requested
= TRUE
;
1807 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
1809 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1811 vm_object_lock(object
);
1814 if (rc
!= KERN_SUCCESS
) {
1816 vm_fault_cleanup(object
, first_m
);
1817 thread_interrupt_level(interruptible_state
);
1819 return ((rc
== MACH_SEND_INTERRUPTED
) ?
1820 VM_FAULT_INTERRUPTED
:
1821 VM_FAULT_MEMORY_ERROR
);
1824 clock_usec_t tv_usec
;
1826 if (my_fault_type
== DBG_PAGEIN_FAULT
) {
1827 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1828 current_thread()->t_page_creation_time
= tv_sec
;
1829 current_thread()->t_page_creation_count
= 0;
1832 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_flags
& TH_SFLAG_ABORT
)) {
1834 vm_fault_cleanup(object
, first_m
);
1835 thread_interrupt_level(interruptible_state
);
1837 return (VM_FAULT_INTERRUPTED
);
1839 if (force_fault_retry
== TRUE
) {
1841 vm_fault_cleanup(object
, first_m
);
1842 thread_interrupt_level(interruptible_state
);
1844 return (VM_FAULT_RETRY
);
1846 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1848 * No page here means that the object we
1849 * initially looked up was "physically
1850 * contiguous" (i.e. device memory). However,
1851 * with Virtual VRAM, the object might not
1852 * be backed by that device memory anymore,
1853 * so we're done here only if the object is
1854 * still "phys_contiguous".
1855 * Otherwise, if the object is no longer
1856 * "phys_contiguous", we need to retry the
1857 * page fault against the object's new backing
1858 * store (different memory object).
1864 * potentially a pagein fault
1865 * if we make it through the state checks
1866 * above, than we'll count it as such
1868 my_fault
= my_fault_type
;
1871 * Retry with same object/offset, since new data may
1872 * be in a different page (i.e., m is meaningless at
1879 * We get here if the object has no pager, or an existence map
1880 * exists and indicates the page isn't present on the pager
1881 * or we're unwiring a page. If a pager exists, but there
1882 * is no existence map, then the m->absent case above handles
1883 * the ZF case when the pager can't provide the page
1886 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1888 if (object
== first_object
)
1891 assert(m
== VM_PAGE_NULL
);
1894 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1898 next_object
= object
->shadow
;
1900 if (next_object
== VM_OBJECT_NULL
) {
1902 * we've hit the bottom of the shadown chain,
1903 * fill the page in the top object with zeros.
1905 assert(!must_be_resident
);
1907 if (object
!= first_object
) {
1908 vm_object_paging_end(object
);
1909 vm_object_unlock(object
);
1911 object
= first_object
;
1912 offset
= first_offset
;
1913 vm_object_lock(object
);
1916 assert(VM_PAGE_OBJECT(m
) == object
);
1917 first_m
= VM_PAGE_NULL
;
1920 * check for any conditions that prevent
1921 * us from creating a new zero-fill page
1922 * vm_fault_check will do all of the
1923 * fault cleanup in the case of an error condition
1924 * including resetting the thread_interrupt_level
1926 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
, (type_of_fault
== NULL
) ? TRUE
: FALSE
);
1928 if (error
!= VM_FAULT_SUCCESS
)
1931 if (m
== VM_PAGE_NULL
) {
1932 m
= vm_page_grab_options(grab_options
);
1934 if (m
== VM_PAGE_NULL
) {
1935 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1936 thread_interrupt_level(interruptible_state
);
1938 return (VM_FAULT_MEMORY_SHORTAGE
);
1940 vm_page_insert(m
, object
, offset
);
1942 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1945 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1951 * Move on to the next object. Lock the next
1952 * object before unlocking the current one.
1954 if ((object
!= first_object
) || must_be_resident
)
1955 vm_object_paging_end(object
);
1957 offset
+= object
->vo_shadow_offset
;
1958 fault_info
->lo_offset
+= object
->vo_shadow_offset
;
1959 fault_info
->hi_offset
+= object
->vo_shadow_offset
;
1960 access_required
= VM_PROT_READ
;
1962 vm_object_lock(next_object
);
1963 vm_object_unlock(object
);
1965 object
= next_object
;
1966 vm_object_paging_begin(object
);
1971 * PAGE HAS BEEN FOUND.
1974 * busy, so that we can play with it;
1975 * not absent, so that nobody else will fill it;
1976 * possibly eligible for pageout;
1978 * The top-level page (first_m) is:
1979 * VM_PAGE_NULL if the page was found in the
1981 * busy, not absent, and ineligible for pageout.
1983 * The current object (object) is locked. A paging
1984 * reference is held for the current and top-level
1989 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1991 #if EXTRA_ASSERTIONS
1992 assert(m
->busy
&& !m
->absent
);
1993 assert((first_m
== VM_PAGE_NULL
) ||
1994 (first_m
->busy
&& !first_m
->absent
&&
1995 !first_m
->active
&& !first_m
->inactive
&& !first_m
->secluded
));
1996 #endif /* EXTRA_ASSERTIONS */
2000 * If we found a page, we must have decrypted it before we
2003 ASSERT_PAGE_DECRYPTED(m
);
2006 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
2008 first_object
, first_m
);
2011 * If the page is being written, but isn't
2012 * already owned by the top-level object,
2013 * we have to copy it into a new page owned
2014 * by the top-level object.
2016 if (object
!= first_object
) {
2019 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2021 if (fault_type
& VM_PROT_WRITE
) {
2025 * We only really need to copy if we
2028 assert(!must_be_resident
);
2031 * are we protecting the system from
2032 * backing store exhaustion. If so
2033 * sleep unless we are privileged.
2035 if (vm_backing_store_low
) {
2036 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
2039 vm_fault_cleanup(object
, first_m
);
2041 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
2043 thread_block(THREAD_CONTINUE_NULL
);
2044 thread_interrupt_level(interruptible_state
);
2046 return (VM_FAULT_RETRY
);
2050 * If we try to collapse first_object at this
2051 * point, we may deadlock when we try to get
2052 * the lock on an intermediate object (since we
2053 * have the bottom object locked). We can't
2054 * unlock the bottom object, because the page
2055 * we found may move (by collapse) if we do.
2057 * Instead, we first copy the page. Then, when
2058 * we have no more use for the bottom object,
2059 * we unlock it and try to collapse.
2061 * Note that we copy the page even if we didn't
2062 * need to... that's the breaks.
2066 * Allocate a page for the copy
2068 copy_m
= vm_page_grab_options(grab_options
);
2070 if (copy_m
== VM_PAGE_NULL
) {
2073 vm_fault_cleanup(object
, first_m
);
2074 thread_interrupt_level(interruptible_state
);
2076 return (VM_FAULT_MEMORY_SHORTAGE
);
2079 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
2083 vm_page_copy(m
, copy_m
);
2086 * If another map is truly sharing this
2087 * page with us, we have to flush all
2088 * uses of the original page, since we
2089 * can't distinguish those which want the
2090 * original from those which need the
2093 * XXXO If we know that only one map has
2094 * access to this page, then we could
2095 * avoid the pmap_disconnect() call.
2098 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2101 VM_PAGE_COUNT_AS_PAGEIN(m
);
2102 VM_PAGE_CONSUME_CLUSTERED(m
);
2104 assert(!m
->cleaning
);
2107 * We no longer need the old page or object.
2112 * This check helps with marking the object as having a sequential pattern
2113 * Normally we'll miss doing this below because this fault is about COW to
2114 * the first_object i.e. bring page in from disk, push to object above but
2115 * don't update the file object's sequential pattern.
2117 if (object
->internal
== FALSE
) {
2118 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2121 vm_object_paging_end(object
);
2122 vm_object_unlock(object
);
2124 my_fault
= DBG_COW_FAULT
;
2125 VM_STAT_INCR(cow_faults
);
2126 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
2127 current_task()->cow_faults
++;
2129 object
= first_object
;
2130 offset
= first_offset
;
2132 vm_object_lock(object
);
2134 * get rid of the place holder
2135 * page that we soldered in earlier
2137 VM_PAGE_FREE(first_m
);
2138 first_m
= VM_PAGE_NULL
;
2141 * and replace it with the
2142 * page we just copied into
2144 assert(copy_m
->busy
);
2145 vm_page_insert(copy_m
, object
, offset
);
2146 SET_PAGE_DIRTY(copy_m
, TRUE
);
2150 * Now that we've gotten the copy out of the
2151 * way, let's try to collapse the top object.
2152 * But we have to play ugly games with
2153 * paging_in_progress to do that...
2155 vm_object_paging_end(object
);
2156 vm_object_collapse(object
, offset
, TRUE
);
2157 vm_object_paging_begin(object
);
2160 *protection
&= (~VM_PROT_WRITE
);
2163 * Now check whether the page needs to be pushed into the
2164 * copy object. The use of asymmetric copy on write for
2165 * shared temporary objects means that we may do two copies to
2166 * satisfy the fault; one above to get the page from a
2167 * shadowed object, and one here to push it into the copy.
2169 try_failed_count
= 0;
2171 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
2172 vm_object_offset_t copy_offset
;
2176 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
2179 * If the page is being written, but hasn't been
2180 * copied to the copy-object, we have to copy it there.
2182 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2183 *protection
&= ~VM_PROT_WRITE
;
2188 * If the page was guaranteed to be resident,
2189 * we must have already performed the copy.
2191 if (must_be_resident
)
2195 * Try to get the lock on the copy_object.
2197 if (!vm_object_lock_try(copy_object
)) {
2199 vm_object_unlock(object
);
2202 mutex_pause(try_failed_count
); /* wait a bit */
2203 vm_object_lock(object
);
2207 try_failed_count
= 0;
2210 * Make another reference to the copy-object,
2211 * to keep it from disappearing during the
2214 vm_object_reference_locked(copy_object
);
2217 * Does the page exist in the copy?
2219 copy_offset
= first_offset
- copy_object
->vo_shadow_offset
;
2221 if (copy_object
->vo_size
<= copy_offset
)
2223 * Copy object doesn't cover this page -- do nothing.
2226 else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
2228 * Page currently exists in the copy object
2232 * If the page is being brought
2233 * in, wait for it and then retry.
2238 * take an extra ref so object won't die
2240 vm_object_reference_locked(copy_object
);
2241 vm_object_unlock(copy_object
);
2242 vm_fault_cleanup(object
, first_m
);
2243 counter(c_vm_fault_page_block_backoff_kernel
++);
2245 vm_object_lock(copy_object
);
2246 assert(copy_object
->ref_count
> 0);
2247 VM_OBJ_RES_DECR(copy_object
);
2248 vm_object_lock_assert_exclusive(copy_object
);
2249 copy_object
->ref_count
--;
2250 assert(copy_object
->ref_count
> 0);
2251 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
2254 * it's OK if the "copy_m" page is encrypted,
2255 * because we're not moving it nor handling its
2258 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
2259 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
2261 vm_object_unlock(copy_object
);
2262 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2263 vm_object_deallocate(copy_object
);
2267 vm_object_unlock(copy_object
);
2268 vm_object_deallocate(copy_object
);
2269 thread_interrupt_level(interruptible_state
);
2271 return (VM_FAULT_RETRY
);
2275 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
2277 * If PAGED_OUT is TRUE, then the page used to exist
2278 * in the copy-object, and has already been paged out.
2279 * We don't need to repeat this. If PAGED_OUT is
2280 * FALSE, then either we don't know (!pager_created,
2281 * for example) or it hasn't been paged out.
2282 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
2283 * We must copy the page to the copy object.
2286 if (vm_backing_store_low
) {
2288 * we are protecting the system from
2289 * backing store exhaustion. If so
2290 * sleep unless we are privileged.
2292 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
2293 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
2296 VM_OBJ_RES_DECR(copy_object
);
2297 vm_object_lock_assert_exclusive(copy_object
);
2298 copy_object
->ref_count
--;
2299 assert(copy_object
->ref_count
> 0);
2301 vm_object_unlock(copy_object
);
2302 vm_fault_cleanup(object
, first_m
);
2303 thread_block(THREAD_CONTINUE_NULL
);
2304 thread_interrupt_level(interruptible_state
);
2306 return (VM_FAULT_RETRY
);
2310 * Allocate a page for the copy
2312 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
2314 if (copy_m
== VM_PAGE_NULL
) {
2317 VM_OBJ_RES_DECR(copy_object
);
2318 vm_object_lock_assert_exclusive(copy_object
);
2319 copy_object
->ref_count
--;
2320 assert(copy_object
->ref_count
> 0);
2322 vm_object_unlock(copy_object
);
2323 vm_fault_cleanup(object
, first_m
);
2324 thread_interrupt_level(interruptible_state
);
2326 return (VM_FAULT_MEMORY_SHORTAGE
);
2329 * Must copy page into copy-object.
2331 vm_page_copy(m
, copy_m
);
2334 * If the old page was in use by any users
2335 * of the copy-object, it must be removed
2336 * from all pmaps. (We can't know which
2340 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2343 VM_PAGE_COUNT_AS_PAGEIN(m
);
2344 VM_PAGE_CONSUME_CLUSTERED(m
);
2347 * If there's a pager, then immediately
2348 * page out this page, using the "initialize"
2349 * option. Else, we use the copy.
2351 if ((!copy_object
->pager_ready
)
2352 || VM_COMPRESSOR_PAGER_STATE_GET(copy_object
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2355 vm_page_lockspin_queues();
2356 assert(!m
->cleaning
);
2357 vm_page_activate(copy_m
);
2358 vm_page_unlock_queues();
2360 SET_PAGE_DIRTY(copy_m
, TRUE
);
2361 PAGE_WAKEUP_DONE(copy_m
);
2365 assert(copy_m
->busy
== TRUE
);
2366 assert(!m
->cleaning
);
2369 * dirty is protected by the object lock
2371 SET_PAGE_DIRTY(copy_m
, TRUE
);
2374 * The page is already ready for pageout:
2375 * not on pageout queues and busy.
2376 * Unlock everything except the
2377 * copy_object itself.
2379 vm_object_unlock(object
);
2382 * Write the page to the copy-object,
2383 * flushing it from the kernel.
2385 vm_pageout_initialize_page(copy_m
);
2388 * Since the pageout may have
2389 * temporarily dropped the
2390 * copy_object's lock, we
2391 * check whether we'll have
2392 * to deallocate the hard way.
2394 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2395 vm_object_unlock(copy_object
);
2396 vm_object_deallocate(copy_object
);
2397 vm_object_lock(object
);
2402 * Pick back up the old object's
2403 * lock. [It is safe to do so,
2404 * since it must be deeper in the
2407 vm_object_lock(object
);
2411 * Because we're pushing a page upward
2412 * in the object tree, we must restart
2413 * any faults that are waiting here.
2414 * [Note that this is an expansion of
2415 * PAGE_WAKEUP that uses the THREAD_RESTART
2416 * wait result]. Can't turn off the page's
2417 * busy bit because we're not done with it.
2421 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2425 * The reference count on copy_object must be
2426 * at least 2: one for our extra reference,
2427 * and at least one from the outside world
2428 * (we checked that when we last locked
2431 vm_object_lock_assert_exclusive(copy_object
);
2432 copy_object
->ref_count
--;
2433 assert(copy_object
->ref_count
> 0);
2435 VM_OBJ_RES_DECR(copy_object
);
2436 vm_object_unlock(copy_object
);
2443 *top_page
= first_m
;
2446 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2447 object
, offset
, m
, first_m
, 0);
2449 if (m
!= VM_PAGE_NULL
) {
2450 assert(VM_PAGE_OBJECT(m
) == object
);
2452 retval
= VM_FAULT_SUCCESS
;
2454 if (my_fault
== DBG_PAGEIN_FAULT
) {
2456 VM_PAGE_COUNT_AS_PAGEIN(m
);
2458 if (object
->internal
)
2459 my_fault
= DBG_PAGEIND_FAULT
;
2461 my_fault
= DBG_PAGEINV_FAULT
;
2464 * evaluate access pattern and update state
2465 * vm_fault_deactivate_behind depends on the
2466 * state being up to date
2468 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2470 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2471 } else if (my_fault
== DBG_COMPRESSOR_FAULT
|| my_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
) {
2473 VM_STAT_INCR(decompressions
);
2476 *type_of_fault
= my_fault
;
2478 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2479 assert(first_m
== VM_PAGE_NULL
);
2480 assert(object
== first_object
);
2483 thread_interrupt_level(interruptible_state
);
2486 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2491 thread_interrupt_level(interruptible_state
);
2493 if (wait_result
== THREAD_INTERRUPTED
)
2494 return (VM_FAULT_INTERRUPTED
);
2495 return (VM_FAULT_RETRY
);
2504 * When soft faulting a page, we have to validate the page if:
2505 * 1. the page is being mapped in user space
2506 * 2. the page hasn't already been found to be "tainted"
2507 * 3. the page belongs to a code-signed object
2508 * 4. the page has not been validated yet or has been mapped for write.
2510 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page, page_obj) \
2511 ((pmap) != kernel_pmap /*1*/ && \
2512 !(page)->cs_tainted /*2*/ && \
2513 (page_obj)->code_signed /*3*/ && \
2514 (!(page)->cs_validated || (page)->wpmapped /*4*/))
2518 * page queue lock must NOT be held
2519 * m->object must be locked
2521 * NOTE: m->object could be locked "shared" only if we are called
2522 * from vm_fault() as part of a soft fault. If so, we must be
2523 * careful not to modify the VM object in any way that is not
2524 * legal under a shared lock...
2526 extern int panic_on_cs_killed
;
2527 extern int proc_selfpid(void);
2528 extern char *proc_name_address(void *p
);
2529 unsigned long cs_enter_tainted_rejected
= 0;
2530 unsigned long cs_enter_tainted_accepted
= 0;
2532 vm_fault_enter(vm_page_t m
,
2534 vm_map_offset_t vaddr
,
2536 vm_prot_t caller_prot
,
2538 boolean_t change_wiring
,
2540 boolean_t cs_bypass
,
2541 __unused
int user_tag
,
2543 boolean_t
*need_retry
,
2546 kern_return_t kr
, pe_result
;
2547 boolean_t previously_pmapped
= m
->pmapped
;
2548 boolean_t must_disconnect
= 0;
2549 boolean_t map_is_switched
, map_is_switch_protected
;
2550 int cs_enforcement_enabled
;
2551 vm_prot_t fault_type
;
2554 fault_type
= change_wiring
? VM_PROT_NONE
: caller_prot
;
2555 object
= VM_PAGE_OBJECT(m
);
2557 vm_object_lock_assert_held(object
);
2558 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2560 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
2561 assert(m
->fictitious
);
2562 return KERN_SUCCESS
;
2565 if (*type_of_fault
== DBG_ZERO_FILL_FAULT
) {
2567 vm_object_lock_assert_exclusive(object
);
2569 } else if ((fault_type
& VM_PROT_WRITE
) == 0 && !m
->wpmapped
) {
2571 * This is not a "write" fault, so we
2572 * might not have taken the object lock
2573 * exclusively and we might not be able
2574 * to update the "wpmapped" bit in
2576 * Let's just grant read access to
2577 * the page for now and we'll
2578 * soft-fault again if we need write
2581 prot
&= ~VM_PROT_WRITE
;
2583 if (m
->pmapped
== FALSE
) {
2586 if (*type_of_fault
== DBG_CACHE_HIT_FAULT
) {
2588 * found it in the cache, but this
2589 * is the first fault-in of the page (m->pmapped == FALSE)
2590 * so it must have come in as part of
2591 * a cluster... account 1 pagein against it
2593 if (object
->internal
)
2594 *type_of_fault
= DBG_PAGEIND_FAULT
;
2596 *type_of_fault
= DBG_PAGEINV_FAULT
;
2598 VM_PAGE_COUNT_AS_PAGEIN(m
);
2600 VM_PAGE_CONSUME_CLUSTERED(m
);
2604 if (*type_of_fault
!= DBG_COW_FAULT
) {
2605 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2607 if (pmap
== kernel_pmap
) {
2608 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2612 /* Validate code signature if necessary. */
2613 if (VM_FAULT_NEED_CS_VALIDATION(pmap
, m
, object
)) {
2614 vm_object_lock_assert_exclusive(object
);
2616 if (m
->cs_validated
) {
2617 vm_cs_revalidates
++;
2620 /* VM map is locked, so 1 ref will remain on VM object -
2621 * so no harm if vm_page_validate_cs drops the object lock */
2622 vm_page_validate_cs(m
);
2625 #define page_immutable(m,prot) ((m)->cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/)
2626 #define page_nx(m) ((m)->cs_nx)
2628 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2629 (pmap
== vm_map_pmap(current_thread()->map
)));
2630 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2632 /* If the map is switched, and is switch-protected, we must protect
2633 * some pages from being write-faulted: immutable pages because by
2634 * definition they may not be written, and executable pages because that
2635 * would provide a way to inject unsigned code.
2636 * If the page is immutable, we can simply return. However, we can't
2637 * immediately determine whether a page is executable anywhere. But,
2638 * we can disconnect it everywhere and remove the executable protection
2639 * from the current map. We do that below right before we do the
2642 cs_enforcement_enabled
= cs_enforcement(NULL
);
2644 if(cs_enforcement_enabled
&& map_is_switched
&&
2645 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2646 (prot
& VM_PROT_WRITE
))
2648 return KERN_CODESIGN_ERROR
;
2651 if (cs_enforcement_enabled
&& page_nx(m
) && (prot
& VM_PROT_EXECUTE
)) {
2653 printf("page marked to be NX, not letting it be mapped EXEC\n");
2654 return KERN_CODESIGN_ERROR
;
2657 if (cs_enforcement_enabled
&&
2659 (prot
& VM_PROT_EXECUTE
) &&
2660 !(caller_prot
& VM_PROT_EXECUTE
)) {
2663 * This page has not been validated and will not be
2664 * allowed to be mapped for "execute".
2665 * But the caller did not request "execute" access for this
2666 * fault, so we should not raise a code-signing violation
2667 * (and possibly kill the process) below.
2668 * Instead, let's just remove the "execute" access request.
2670 * This can happen on devices with a 4K page size if a 16K
2671 * page contains a mix of signed&executable and
2672 * unsigned&non-executable 4K pages, making the whole 16K
2673 * mapping "executable".
2675 prot
&= ~VM_PROT_EXECUTE
;
2678 /* A page could be tainted, or pose a risk of being tainted later.
2679 * Check whether the receiving process wants it, and make it feel
2680 * the consequences (that hapens in cs_invalid_page()).
2681 * For CS Enforcement, two other conditions will
2682 * cause that page to be tainted as well:
2683 * - pmapping an unsigned page executable - this means unsigned code;
2684 * - writeable mapping of a validated page - the content of that page
2685 * can be changed without the kernel noticing, therefore unsigned
2686 * code can be created
2690 (cs_enforcement_enabled
&&
2691 (/* The page is unsigned and wants to be executable */
2692 (!m
->cs_validated
&& (prot
& VM_PROT_EXECUTE
)) ||
2693 /* The page should be immutable, but is in danger of being modified
2694 * This is the case where we want policy from the code directory -
2695 * is the page immutable or not? For now we have to assume that
2696 * code pages will be immutable, data pages not.
2697 * We'll assume a page is a code page if it has a code directory
2698 * and we fault for execution.
2699 * That is good enough since if we faulted the code page for
2700 * writing in another map before, it is wpmapped; if we fault
2701 * it for writing in this map later it will also be faulted for executing
2702 * at the same time; and if we fault for writing in another map
2703 * later, we will disconnect it from this pmap so we'll notice
2706 (page_immutable(m
, prot
) && ((prot
& VM_PROT_WRITE
) || m
->wpmapped
))
2710 /* We will have a tainted page. Have to handle the special case
2711 * of a switched map now. If the map is not switched, standard
2712 * procedure applies - call cs_invalid_page().
2713 * If the map is switched, the real owner is invalid already.
2714 * There is no point in invalidating the switching process since
2715 * it will not be executing from the map. So we don't call
2716 * cs_invalid_page() in that case. */
2717 boolean_t reject_page
, cs_killed
;
2718 if(map_is_switched
) {
2719 assert(pmap
==vm_map_pmap(current_thread()->map
));
2720 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2721 reject_page
= FALSE
;
2724 printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s slid: %s prot: 0x%x\n",
2725 object
->code_signed
? "yes" : "no",
2726 m
->cs_validated
? "yes" : "no",
2727 m
->cs_tainted
? "yes" : "no",
2728 m
->wpmapped
? "yes" : "no",
2729 m
->slid
? "yes" : "no",
2731 reject_page
= cs_invalid_page((addr64_t
) vaddr
, &cs_killed
);
2735 /* reject the invalid page: abort the page fault */
2737 const char *procname
;
2739 vm_object_t file_object
, shadow
;
2740 vm_object_offset_t file_offset
;
2741 char *pathname
, *filename
;
2742 vm_size_t pathname_len
, filename_len
;
2743 boolean_t truncated_path
;
2744 #define __PATH_MAX 1024
2745 struct timespec mtime
, cs_mtime
;
2747 os_reason_t codesigning_exit_reason
= OS_REASON_NULL
;
2749 kr
= KERN_CODESIGN_ERROR
;
2750 cs_enter_tainted_rejected
++;
2752 /* get process name and pid */
2754 task
= current_task();
2755 pid
= proc_selfpid();
2756 if (task
->bsd_info
!= NULL
)
2757 procname
= proc_name_address(task
->bsd_info
);
2759 /* get file's VM object */
2760 file_object
= object
;
2761 file_offset
= m
->offset
;
2762 for (shadow
= file_object
->shadow
,
2764 shadow
!= VM_OBJECT_NULL
;
2765 shadow
= file_object
->shadow
,
2767 vm_object_lock_shared(shadow
);
2768 if (file_object
!= object
) {
2769 vm_object_unlock(file_object
);
2771 file_offset
+= file_object
->vo_shadow_offset
;
2772 file_object
= shadow
;
2777 cs_mtime
.tv_sec
= 0;
2778 cs_mtime
.tv_nsec
= 0;
2780 /* get file's pathname and/or filename */
2785 truncated_path
= FALSE
;
2786 /* no pager -> no file -> no pathname, use "<nil>" in that case */
2787 if (file_object
->pager
!= NULL
) {
2788 pathname
= (char *)kalloc(__PATH_MAX
* 2);
2791 pathname_len
= __PATH_MAX
;
2792 filename
= pathname
+ pathname_len
;
2793 filename_len
= __PATH_MAX
;
2795 vnode_pager_get_object_name(file_object
->pager
,
2802 /* safety first... */
2803 pathname
[__PATH_MAX
-1] = '\0';
2804 filename
[__PATH_MAX
-1] = '\0';
2806 vnode_pager_get_object_mtime(file_object
->pager
,
2810 printf("CODE SIGNING: process %d[%s]: "
2811 "rejecting invalid page at address 0x%llx "
2812 "from offset 0x%llx in file \"%s%s%s\" "
2813 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2814 "(signed:%d validated:%d tainted:%d nx:%d "
2815 "wpmapped:%d slid:%d dirty:%d depth:%d)\n",
2816 pid
, procname
, (addr64_t
) vaddr
,
2818 (pathname
? pathname
: "<nil>"),
2819 (truncated_path
? "/.../" : ""),
2820 (truncated_path
? filename
: ""),
2821 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2822 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2823 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2826 mtime
.tv_sec
, mtime
.tv_nsec
,
2827 object
->code_signed
,
2837 * We currently only generate an exit reason if cs_invalid_page directly killed a process. If cs_invalid_page
2838 * did not kill the process (more the case on desktop), vm_fault_enter will not satisfy the fault and whether the
2839 * process dies is dependent on whether there is a signal handler registered for SIGSEGV and how that handler
2840 * will deal with the segmentation fault.
2843 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
2844 pid
, OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_INVALID_PAGE
, 0, 0);
2846 codesigning_exit_reason
= os_reason_create(OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_INVALID_PAGE
);
2847 if (codesigning_exit_reason
== NULL
) {
2848 printf("vm_fault_enter: failed to allocate codesigning exit reason\n");
2850 mach_vm_address_t data_addr
= 0;
2851 struct codesigning_exit_reason_info
*ceri
= NULL
;
2852 uint32_t reason_buffer_size_estimate
= kcdata_estimate_required_buffer_size(1, sizeof(*ceri
));
2854 if (os_reason_alloc_buffer_noblock(codesigning_exit_reason
, reason_buffer_size_estimate
)) {
2855 printf("vm_fault_enter: failed to allocate buffer for codesigning exit reason\n");
2857 if (KERN_SUCCESS
== kcdata_get_memory_addr(&codesigning_exit_reason
->osr_kcd_descriptor
,
2858 EXIT_REASON_CODESIGNING_INFO
, sizeof(*ceri
), &data_addr
)) {
2859 ceri
= (struct codesigning_exit_reason_info
*)data_addr
;
2860 static_assert(__PATH_MAX
== sizeof(ceri
->ceri_pathname
));
2862 ceri
->ceri_virt_addr
= vaddr
;
2863 ceri
->ceri_file_offset
= file_offset
;
2865 strncpy((char *)&ceri
->ceri_pathname
, pathname
, sizeof(ceri
->ceri_pathname
));
2867 ceri
->ceri_pathname
[0] = '\0';
2869 strncpy((char *)&ceri
->ceri_filename
, filename
, sizeof(ceri
->ceri_filename
));
2871 ceri
->ceri_filename
[0] = '\0';
2872 ceri
->ceri_path_truncated
= (truncated_path
);
2873 ceri
->ceri_codesig_modtime_secs
= cs_mtime
.tv_sec
;
2874 ceri
->ceri_codesig_modtime_nsecs
= cs_mtime
.tv_nsec
;
2875 ceri
->ceri_page_modtime_secs
= mtime
.tv_sec
;
2876 ceri
->ceri_page_modtime_nsecs
= mtime
.tv_nsec
;
2877 ceri
->ceri_object_codesigned
= (object
->code_signed
);
2878 ceri
->ceri_page_codesig_validated
= (m
->cs_validated
);
2879 ceri
->ceri_page_codesig_tainted
= (m
->cs_tainted
);
2880 ceri
->ceri_page_codesig_nx
= (m
->cs_nx
);
2881 ceri
->ceri_page_wpmapped
= (m
->wpmapped
);
2882 ceri
->ceri_page_slid
= (m
->slid
);
2883 ceri
->ceri_page_dirty
= (m
->dirty
);
2884 ceri
->ceri_page_shadow_depth
= shadow_depth
;
2886 #if DEBUG || DEVELOPMENT
2887 panic("vm_fault_enter: failed to allocate kcdata for codesigning exit reason");
2889 printf("vm_fault_enter: failed to allocate kcdata for codesigning exit reason\n");
2890 #endif /* DEBUG || DEVELOPMENT */
2891 /* Free the buffer */
2892 os_reason_alloc_buffer_noblock(codesigning_exit_reason
, 0);
2897 set_thread_exit_reason(current_thread(), codesigning_exit_reason
, FALSE
);
2899 if (panic_on_cs_killed
&&
2900 object
->object_slid
) {
2901 panic("CODE SIGNING: process %d[%s]: "
2902 "rejecting invalid page at address 0x%llx "
2903 "from offset 0x%llx in file \"%s%s%s\" "
2904 "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) "
2905 "(signed:%d validated:%d tainted:%d nx:%d"
2906 "wpmapped:%d slid:%d dirty:%d depth:%d)\n",
2907 pid
, procname
, (addr64_t
) vaddr
,
2909 (pathname
? pathname
: "<nil>"),
2910 (truncated_path
? "/.../" : ""),
2911 (truncated_path
? filename
: ""),
2912 cs_mtime
.tv_sec
, cs_mtime
.tv_nsec
,
2913 ((cs_mtime
.tv_sec
== mtime
.tv_sec
&&
2914 cs_mtime
.tv_nsec
== mtime
.tv_nsec
)
2917 mtime
.tv_sec
, mtime
.tv_nsec
,
2918 object
->code_signed
,
2928 if (file_object
!= object
) {
2929 vm_object_unlock(file_object
);
2931 if (pathname_len
!= 0) {
2932 kfree(pathname
, __PATH_MAX
* 2);
2937 /* proceed with the invalid page */
2939 if (!m
->cs_validated
&&
2940 !object
->code_signed
) {
2942 * This page has not been (fully) validated but
2943 * does not belong to a code-signed object
2944 * so it should not be forcefully considered
2946 * We're just concerned about it here because
2947 * we've been asked to "execute" it but that
2948 * does not mean that it should cause other
2950 * This happens when a debugger sets a
2951 * breakpoint and we then execute code in
2952 * that page. Marking the page as "tainted"
2953 * would cause any inspection tool ("leaks",
2954 * "vmmap", "CrashReporter", ...) to get killed
2955 * due to code-signing violation on that page,
2956 * even though they're just reading it and not
2957 * executing from it.
2961 * Page might have been tainted before or not;
2962 * now it definitively is. If the page wasn't
2963 * tainted, we must disconnect it from all
2964 * pmaps later, to force existing mappings
2965 * through that code path for re-consideration
2966 * of the validity of that page.
2968 must_disconnect
= !m
->cs_tainted
;
2969 m
->cs_tainted
= TRUE
;
2971 cs_enter_tainted_accepted
++;
2973 if (kr
!= KERN_SUCCESS
) {
2975 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2976 "*** INVALID PAGE ***\n",
2980 if (cs_enforcement_panic
) {
2981 panic("CODESIGNING: panicking on invalid page\n");
2987 /* proceed with the valid page */
2991 boolean_t page_queues_locked
= FALSE
;
2992 #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \
2994 if (! page_queues_locked) { \
2995 page_queues_locked = TRUE; \
2996 vm_page_lockspin_queues(); \
2999 #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \
3001 if (page_queues_locked) { \
3002 page_queues_locked = FALSE; \
3003 vm_page_unlock_queues(); \
3008 * Hold queues lock to manipulate
3009 * the page queues. Change wiring
3012 assert((m
->vm_page_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) || object
!= compressor_object
);
3014 #if CONFIG_BACKGROUND_QUEUE
3015 vm_page_update_background_state(m
);
3017 if (m
->vm_page_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
3019 * Compressor pages are neither wired
3020 * nor pageable and should never change.
3022 assert(object
== compressor_object
);
3023 } else if (change_wiring
) {
3024 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3027 if (kr
== KERN_SUCCESS
) {
3028 vm_page_wire(m
, VM_PROT_MEMORY_TAG(caller_prot
), TRUE
);
3031 vm_page_unwire(m
, TRUE
);
3033 /* we keep the page queues lock, if we need it later */
3036 if (object
->internal
== TRUE
) {
3038 * don't allow anonymous pages on
3039 * the speculative queues
3043 if (kr
!= KERN_SUCCESS
) {
3044 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3045 vm_page_deactivate(m
);
3046 /* we keep the page queues lock, if we need it later */
3047 } else if (((m
->vm_page_q_state
== VM_PAGE_NOT_ON_Q
) ||
3048 (m
->vm_page_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) ||
3049 (m
->vm_page_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) ||
3050 ((m
->vm_page_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && no_cache
)) &&
3051 !VM_PAGE_WIRED(m
)) {
3053 if (vm_page_local_q
&&
3054 (*type_of_fault
== DBG_COW_FAULT
||
3055 *type_of_fault
== DBG_ZERO_FILL_FAULT
) ) {
3059 assert(m
->vm_page_q_state
== VM_PAGE_NOT_ON_Q
);
3061 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3062 vm_object_lock_assert_exclusive(object
);
3065 * we got a local queue to stuff this
3067 * its safe to manipulate local and
3068 * local_id at this point since we're
3069 * behind an exclusive object lock and
3070 * the page is not on any global queue.
3072 * we'll use the current cpu number to
3073 * select the queue note that we don't
3074 * need to disable preemption... we're
3075 * going to be behind the local queue's
3076 * lock to do the real work
3080 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
3082 VPL_LOCK(&lq
->vpl_lock
);
3084 vm_page_check_pageable_safe(m
);
3085 vm_page_queue_enter(&lq
->vpl_queue
, m
,
3087 m
->vm_page_q_state
= VM_PAGE_ON_ACTIVE_LOCAL_Q
;
3091 if (object
->internal
)
3092 lq
->vpl_internal_count
++;
3094 lq
->vpl_external_count
++;
3096 VPL_UNLOCK(&lq
->vpl_lock
);
3098 if (lq
->vpl_count
> vm_page_local_q_soft_limit
)
3101 * we're beyond the soft limit
3102 * for the local queue
3103 * vm_page_reactivate_local will
3104 * 'try' to take the global page
3105 * queue lock... if it can't
3106 * that's ok... we'll let the
3107 * queue continue to grow up
3108 * to the hard limit... at that
3109 * point we'll wait for the
3110 * lock... once we've got the
3111 * lock, we'll transfer all of
3112 * the pages from the local
3113 * queue to the global active
3116 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
3120 __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED();
3123 * test again now that we hold the
3126 if (!VM_PAGE_WIRED(m
)) {
3127 if (m
->vm_page_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
) {
3128 vm_page_queues_remove(m
, FALSE
);
3130 vm_pageout_cleaned_reactivated
++;
3131 vm_pageout_cleaned_fault_reactivated
++;
3134 if ( !VM_PAGE_ACTIVE_OR_INACTIVE(m
) ||
3137 * If this is a no_cache mapping
3138 * and the page has never been
3139 * mapped before or was
3140 * previously a no_cache page,
3141 * then we want to leave pages
3142 * in the speculative state so
3143 * that they can be readily
3144 * recycled if free memory runs
3145 * low. Otherwise the page is
3146 * activated as normal.
3150 (!previously_pmapped
||
3154 if (m
->vm_page_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
)
3155 vm_page_speculate(m
, FALSE
);
3157 } else if ( !VM_PAGE_ACTIVE_OR_INACTIVE(m
)) {
3158 vm_page_activate(m
);
3162 /* we keep the page queues lock, if we need it later */
3166 /* we're done with the page queues lock, if we ever took it */
3167 __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED();
3170 /* If we have a KERN_SUCCESS from the previous checks, we either have
3171 * a good page, or a tainted page that has been accepted by the process.
3172 * In both cases the page will be entered into the pmap.
3173 * If the page is writeable, we need to disconnect it from other pmaps
3174 * now so those processes can take note.
3176 if (kr
== KERN_SUCCESS
) {
3178 * NOTE: we may only hold the vm_object lock SHARED
3179 * at this point, so we need the phys_page lock to
3180 * properly serialize updating the pmapped and
3183 if ((prot
& VM_PROT_EXECUTE
) && !m
->xpmapped
) {
3184 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3186 pmap_lock_phys_page(phys_page
);
3188 * go ahead and take the opportunity
3189 * to set 'pmapped' here so that we don't
3190 * need to grab this lock a 2nd time
3199 pmap_unlock_phys_page(phys_page
);
3201 if (!object
->internal
)
3202 OSAddAtomic(1, &vm_page_xpmapped_external_count
);
3204 if (object
->internal
&&
3205 object
->pager
!= NULL
) {
3207 * This page could have been
3208 * uncompressed by the
3209 * compressor pager and its
3210 * contents might be only in
3212 * Since it's being mapped for
3213 * "execute" for the fist time,
3214 * make sure the icache is in
3217 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3218 pmap_sync_page_data_phys(phys_page
);
3221 pmap_unlock_phys_page(phys_page
);
3223 if (m
->pmapped
== FALSE
) {
3224 ppnum_t phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
3226 pmap_lock_phys_page(phys_page
);
3228 pmap_unlock_phys_page(phys_page
);
3231 if (vm_page_is_slideable(m
)) {
3232 boolean_t was_busy
= m
->busy
;
3234 vm_object_lock_assert_exclusive(object
);
3237 kr
= vm_page_slide(m
, 0);
3240 PAGE_WAKEUP_DONE(m
);
3242 if (kr
!= KERN_SUCCESS
) {
3244 * This page has not been slid correctly,
3245 * do not do the pmap_enter() !
3246 * Let vm_fault_enter() return the error
3247 * so the caller can fail the fault.
3249 goto after_the_pmap_enter
;
3253 if (fault_type
& VM_PROT_WRITE
) {
3255 if (m
->wpmapped
== FALSE
) {
3256 vm_object_lock_assert_exclusive(object
);
3257 if (!object
->internal
&& object
->pager
) {
3258 task_update_logical_writes(current_task(), PAGE_SIZE
, TASK_WRITE_DEFERRED
, vnode_pager_lookup_vnode(object
->pager
));
3262 if (must_disconnect
) {
3264 * We can only get here
3265 * because of the CSE logic
3267 assert(cs_enforcement_enabled
);
3268 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
3270 * If we are faulting for a write, we can clear
3271 * the execute bit - that will ensure the page is
3272 * checked again before being executable, which
3273 * protects against a map switch.
3274 * This only happens the first time the page
3275 * gets tainted, so we won't get stuck here
3276 * to make an already writeable page executable.
3279 prot
&= ~VM_PROT_EXECUTE
;
3283 assert(VM_PAGE_OBJECT(m
) == object
);
3285 /* Prevent a deadlock by not
3286 * holding the object lock if we need to wait for a page in
3287 * pmap_enter() - <rdar://problem/7138958> */
3288 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
, 0,
3290 pmap_options
| PMAP_OPTIONS_NOWAIT
,
3293 if(pe_result
== KERN_RESOURCE_SHORTAGE
) {
3297 * this will be non-null in the case where we hold the lock
3298 * on the top-object in this chain... we can't just drop
3299 * the lock on the object we're inserting the page into
3300 * and recall the PMAP_ENTER since we can still cause
3301 * a deadlock if one of the critical paths tries to
3302 * acquire the lock on the top-object and we're blocked
3303 * in PMAP_ENTER waiting for memory... our only recourse
3304 * is to deal with it at a higher level where we can
3308 vm_pmap_enter_retried
++;
3309 goto after_the_pmap_enter
;
3311 /* The nonblocking version of pmap_enter did not succeed.
3312 * and we don't need to drop other locks and retry
3313 * at the level above us, so
3314 * use the blocking version instead. Requires marking
3315 * the page busy and unlocking the object */
3316 boolean_t was_busy
= m
->busy
;
3318 vm_object_lock_assert_exclusive(object
);
3321 vm_object_unlock(object
);
3323 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, fault_type
,
3325 pmap_options
, pe_result
);
3327 assert(VM_PAGE_OBJECT(m
) == object
);
3329 /* Take the object lock again. */
3330 vm_object_lock(object
);
3332 /* If the page was busy, someone else will wake it up.
3333 * Otherwise, we have to do it now. */
3336 PAGE_WAKEUP_DONE(m
);
3338 vm_pmap_enter_blocked
++;
3342 after_the_pmap_enter
:
3347 vm_pre_fault(vm_map_offset_t vaddr
)
3349 if (pmap_find_phys(current_map()->pmap
, vaddr
) == 0) {
3351 vm_fault(current_map(), /* map */
3353 VM_PROT_READ
, /* fault_type */
3354 FALSE
, /* change_wiring */
3355 THREAD_UNINT
, /* interruptible */
3356 NULL
, /* caller_pmap */
3357 0 /* caller_pmap_addr */);
3365 * Handle page faults, including pseudo-faults
3366 * used to change the wiring status of pages.
3368 * Explicit continuations have been removed.
3370 * vm_fault and vm_fault_page save mucho state
3371 * in the moral equivalent of a closure. The state
3372 * structure is allocated when first entering vm_fault
3373 * and deallocated when leaving vm_fault.
3376 extern int _map_enter_debug
;
3377 extern uint64_t get_current_unique_pid(void);
3379 unsigned long vm_fault_collapse_total
= 0;
3380 unsigned long vm_fault_collapse_skipped
= 0;
3386 vm_map_offset_t vaddr
,
3387 vm_prot_t fault_type
,
3388 boolean_t change_wiring
,
3391 vm_map_offset_t caller_pmap_addr
)
3393 return vm_fault_internal(map
, vaddr
, fault_type
, change_wiring
,
3394 interruptible
, caller_pmap
, caller_pmap_addr
,
3402 vm_map_offset_t vaddr
,
3403 vm_prot_t caller_prot
,
3404 boolean_t change_wiring
,
3407 vm_map_offset_t caller_pmap_addr
,
3408 ppnum_t
*physpage_p
)
3410 vm_map_version_t version
; /* Map version for verificiation */
3411 boolean_t wired
; /* Should mapping be wired down? */
3412 vm_object_t object
; /* Top-level object */
3413 vm_object_offset_t offset
; /* Top-level offset */
3414 vm_prot_t prot
; /* Protection for mapping */
3415 vm_object_t old_copy_object
; /* Saved copy object */
3416 vm_page_t result_page
; /* Result of vm_fault_page */
3417 vm_page_t top_page
; /* Placeholder page */
3420 vm_page_t m
; /* Fast access to result_page */
3421 kern_return_t error_code
;
3422 vm_object_t cur_object
;
3423 vm_object_t m_object
= NULL
;
3424 vm_object_offset_t cur_offset
;
3426 vm_object_t new_object
;
3429 boolean_t interruptible_state
;
3430 vm_map_t real_map
= map
;
3431 vm_map_t original_map
= map
;
3432 vm_prot_t fault_type
;
3433 vm_prot_t original_fault_type
;
3434 struct vm_object_fault_info fault_info
;
3435 boolean_t need_collapse
= FALSE
;
3436 boolean_t need_retry
= FALSE
;
3437 boolean_t
*need_retry_ptr
= NULL
;
3438 int object_lock_type
= 0;
3439 int cur_object_lock_type
;
3440 vm_object_t top_object
= VM_OBJECT_NULL
;
3442 int compressed_count_delta
;
3444 vm_map_offset_t trace_vaddr
;
3445 vm_map_offset_t trace_real_vaddr
;
3446 #if DEVELOPMENT || DEBUG
3447 vm_map_offset_t real_vaddr
;
3450 #endif /* DEVELOPMENT || DEBUG */
3451 trace_real_vaddr
= vaddr
;
3452 vaddr
= vm_map_trunc_page(vaddr
, PAGE_MASK
);
3454 if (map
== kernel_map
) {
3455 trace_vaddr
= VM_KERNEL_UNSLIDE_OR_PERM(vaddr
);
3456 trace_real_vaddr
= VM_KERNEL_UNSLIDE_OR_PERM(trace_real_vaddr
);
3458 trace_vaddr
= vaddr
;
3461 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3462 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
3463 ((uint64_t)trace_vaddr
>> 32),
3465 (map
== kernel_map
),
3469 if (get_preemption_level() != 0) {
3470 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3471 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3472 ((uint64_t)trace_vaddr
>> 32),
3478 return (KERN_FAILURE
);
3481 interruptible_state
= thread_interrupt_level(interruptible
);
3483 fault_type
= (change_wiring
? VM_PROT_NONE
: caller_prot
);
3485 VM_STAT_INCR(faults
);
3486 current_task()->faults
++;
3487 original_fault_type
= fault_type
;
3489 if (fault_type
& VM_PROT_WRITE
)
3490 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3492 object_lock_type
= OBJECT_LOCK_SHARED
;
3494 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
3498 * assume we will hit a page in the cache
3499 * otherwise, explicitly override with
3500 * the real fault type once we determine it
3502 type_of_fault
= DBG_CACHE_HIT_FAULT
;
3505 * Find the backing store object and offset into
3506 * it to begin the search.
3508 fault_type
= original_fault_type
;
3510 vm_map_lock_read(map
);
3512 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
3513 object_lock_type
, &version
,
3514 &object
, &offset
, &prot
, &wired
,
3519 if (kr
!= KERN_SUCCESS
) {
3520 vm_map_unlock_read(map
);
3523 pmap
= real_map
->pmap
;
3524 fault_info
.interruptible
= interruptible
;
3525 fault_info
.stealth
= FALSE
;
3526 fault_info
.io_sync
= FALSE
;
3527 fault_info
.mark_zf_absent
= FALSE
;
3528 fault_info
.batch_pmap_op
= FALSE
;
3531 * If the page is wired, we must fault for the current protection
3532 * value, to avoid further faults.
3535 fault_type
= prot
| VM_PROT_WRITE
;
3537 * since we're treating this fault as a 'write'
3538 * we must hold the top object lock exclusively
3540 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3542 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3544 if (vm_object_lock_upgrade(object
) == FALSE
) {
3546 * couldn't upgrade, so explictly
3547 * take the lock exclusively
3549 vm_object_lock(object
);
3554 #if VM_FAULT_CLASSIFY
3556 * Temporary data gathering code
3558 vm_fault_classify(object
, offset
, fault_type
);
3561 * Fast fault code. The basic idea is to do as much as
3562 * possible while holding the map lock and object locks.
3563 * Busy pages are not used until the object lock has to
3564 * be dropped to do something (copy, zero fill, pmap enter).
3565 * Similarly, paging references aren't acquired until that
3566 * point, and object references aren't used.
3568 * If we can figure out what to do
3569 * (zero fill, copy on write, pmap enter) while holding
3570 * the locks, then it gets done. Otherwise, we give up,
3571 * and use the original fault path (which doesn't hold
3572 * the map lock, and relies on busy pages).
3573 * The give up cases include:
3574 * - Have to talk to pager.
3575 * - Page is busy, absent or in error.
3576 * - Pager has locked out desired access.
3577 * - Fault needs to be restarted.
3578 * - Have to push page into copy object.
3580 * The code is an infinite loop that moves one level down
3581 * the shadow chain each time. cur_object and cur_offset
3582 * refer to the current object being examined. object and offset
3583 * are the original object from the map. The loop is at the
3584 * top level if and only if object and cur_object are the same.
3586 * Invariants: Map lock is held throughout. Lock is held on
3587 * original object and cur_object (if different) when
3588 * continuing or exiting loop.
3594 * If this page is to be inserted in a copy delay object
3595 * for writing, and if the object has a copy, then the
3596 * copy delay strategy is implemented in the slow fault page.
3598 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
3599 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
))
3600 goto handle_copy_delay
;
3602 cur_object
= object
;
3603 cur_offset
= offset
;
3606 #if CONFIG_SECLUDED_MEMORY
3607 if (object
->can_grab_secluded
) {
3608 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
3610 #endif /* CONFIG_SECLUDED_MEMORY */
3613 if (!cur_object
->pager_created
&&
3614 cur_object
->phys_contiguous
) /* superpage */
3617 if (cur_object
->blocked_access
) {
3619 * Access to this VM object has been blocked.
3620 * Let the slow path handle it.
3625 m
= vm_page_lookup(cur_object
, cur_offset
);
3628 if (m
!= VM_PAGE_NULL
) {
3629 m_object
= cur_object
;
3632 wait_result_t result
;
3635 * in order to do the PAGE_ASSERT_WAIT, we must
3636 * have object that 'm' belongs to locked exclusively
3638 if (object
!= cur_object
) {
3640 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3642 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3644 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3646 * couldn't upgrade so go do a full retry
3647 * immediately since we can no longer be
3648 * certain about cur_object (since we
3649 * don't hold a reference on it)...
3650 * first drop the top object lock
3652 vm_object_unlock(object
);
3654 vm_map_unlock_read(map
);
3655 if (real_map
!= map
)
3656 vm_map_unlock(real_map
);
3661 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3663 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3665 if (vm_object_lock_upgrade(object
) == FALSE
) {
3667 * couldn't upgrade, so explictly take the lock
3668 * exclusively and go relookup the page since we
3669 * will have dropped the object lock and
3670 * a different thread could have inserted
3671 * a page at this offset
3672 * no need for a full retry since we're
3673 * at the top level of the object chain
3675 vm_object_lock(object
);
3680 if ((m
->vm_page_q_state
== VM_PAGE_ON_PAGEOUT_Q
) && m_object
->internal
) {
3682 * m->busy == TRUE and the object is locked exclusively
3683 * if m->pageout_queue == TRUE after we acquire the
3684 * queues lock, we are guaranteed that it is stable on
3685 * the pageout queue and therefore reclaimable
3687 * NOTE: this is only true for the internal pageout queue
3688 * in the compressor world
3690 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
3692 vm_page_lock_queues();
3694 if (m
->vm_page_q_state
== VM_PAGE_ON_PAGEOUT_Q
) {
3695 vm_pageout_throttle_up(m
);
3696 vm_page_unlock_queues();
3698 PAGE_WAKEUP_DONE(m
);
3699 goto reclaimed_from_pageout
;
3701 vm_page_unlock_queues();
3703 if (object
!= cur_object
)
3704 vm_object_unlock(object
);
3706 vm_map_unlock_read(map
);
3707 if (real_map
!= map
)
3708 vm_map_unlock(real_map
);
3710 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
3712 vm_object_unlock(cur_object
);
3714 if (result
== THREAD_WAITING
) {
3715 result
= thread_block(THREAD_CONTINUE_NULL
);
3717 counter(c_vm_fault_page_block_busy_kernel
++);
3719 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
3725 reclaimed_from_pageout
:
3727 if (object
!= cur_object
) {
3728 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3729 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3731 vm_object_unlock(object
);
3732 vm_object_unlock(cur_object
);
3734 vm_map_unlock_read(map
);
3735 if (real_map
!= map
)
3736 vm_map_unlock(real_map
);
3741 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3743 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3745 if (vm_object_lock_upgrade(object
) == FALSE
) {
3747 * couldn't upgrade, so explictly take the lock
3748 * exclusively and go relookup the page since we
3749 * will have dropped the object lock and
3750 * a different thread could have inserted
3751 * a page at this offset
3752 * no need for a full retry since we're
3753 * at the top level of the object chain
3755 vm_object_lock(object
);
3760 vm_pageout_steal_laundry(m
, FALSE
);
3763 if (VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
3765 * Guard page: let the slow path deal with it
3769 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
)) {
3771 * Unusual case... let the slow path deal with it
3775 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object
)) {
3776 if (object
!= cur_object
)
3777 vm_object_unlock(object
);
3778 vm_map_unlock_read(map
);
3779 if (real_map
!= map
)
3780 vm_map_unlock(real_map
);
3781 vm_object_unlock(cur_object
);
3782 kr
= KERN_MEMORY_ERROR
;
3789 * We've soft-faulted (because it's not in the page
3790 * table) on an encrypted page.
3791 * Keep the page "busy" so that no one messes with
3792 * it during the decryption.
3793 * Release the extra locks we're holding, keep only
3794 * the page's VM object lock.
3796 * in order to set 'busy' on 'm', we must
3797 * have object that 'm' belongs to locked exclusively
3799 if (object
!= cur_object
) {
3800 vm_object_unlock(object
);
3802 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3804 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3806 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
3808 * couldn't upgrade so go do a full retry
3809 * immediately since we've already dropped
3810 * the top object lock associated with this page
3811 * and the current one got dropped due to the
3812 * failed upgrade... the state is no longer valid
3814 vm_map_unlock_read(map
);
3815 if (real_map
!= map
)
3816 vm_map_unlock(real_map
);
3821 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3823 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3825 if (vm_object_lock_upgrade(object
) == FALSE
) {
3827 * couldn't upgrade, so explictly take the lock
3828 * exclusively and go relookup the page since we
3829 * will have dropped the object lock and
3830 * a different thread could have inserted
3831 * a page at this offset
3832 * no need for a full retry since we're
3833 * at the top level of the object chain
3835 vm_object_lock(object
);
3842 vm_map_unlock_read(map
);
3843 if (real_map
!= map
)
3844 vm_map_unlock(real_map
);
3846 vm_page_decrypt(m
, 0);
3849 PAGE_WAKEUP_DONE(m
);
3851 vm_object_unlock(cur_object
);
3853 * Retry from the top, in case anything
3854 * changed while we were decrypting...
3858 ASSERT_PAGE_DECRYPTED(m
);
3860 if(vm_page_is_slideable(m
)) {
3862 * We might need to slide this page, and so,
3863 * we want to hold the VM object exclusively.
3865 if (object
!= cur_object
) {
3866 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3867 vm_object_unlock(object
);
3868 vm_object_unlock(cur_object
);
3870 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3872 vm_map_unlock_read(map
);
3873 if (real_map
!= map
)
3874 vm_map_unlock(real_map
);
3878 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3880 vm_object_unlock(object
);
3881 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3882 vm_map_unlock_read(map
);
3886 assert(m_object
== VM_PAGE_OBJECT(m
));
3888 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
, m_object
) ||
3889 (physpage_p
!= NULL
&& (prot
& VM_PROT_WRITE
))) {
3890 upgrade_for_validation
:
3892 * We might need to validate this page
3893 * against its code signature, so we
3894 * want to hold the VM object exclusively.
3896 if (object
!= cur_object
) {
3897 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
3898 vm_object_unlock(object
);
3899 vm_object_unlock(cur_object
);
3901 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3903 vm_map_unlock_read(map
);
3904 if (real_map
!= map
)
3905 vm_map_unlock(real_map
);
3910 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3912 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3914 if (vm_object_lock_upgrade(object
) == FALSE
) {
3916 * couldn't upgrade, so explictly take the lock
3917 * exclusively and go relookup the page since we
3918 * will have dropped the object lock and
3919 * a different thread could have inserted
3920 * a page at this offset
3921 * no need for a full retry since we're
3922 * at the top level of the object chain
3924 vm_object_lock(object
);
3931 * Two cases of map in faults:
3932 * - At top level w/o copy object.
3933 * - Read fault anywhere.
3934 * --> must disallow write.
3937 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
3942 if ((fault_type
& VM_PROT_WRITE
) == 0) {
3944 prot
&= ~VM_PROT_WRITE
;
3946 if (object
!= cur_object
) {
3948 * We still need to hold the top object
3949 * lock here to prevent a race between
3950 * a read fault (taking only "shared"
3951 * locks) and a write fault (taking
3952 * an "exclusive" lock on the top
3954 * Otherwise, as soon as we release the
3955 * top lock, the write fault could
3956 * proceed and actually complete before
3957 * the read fault, and the copied page's
3958 * translation could then be overwritten
3959 * by the read fault's translation for
3960 * the original page.
3962 * Let's just record what the top object
3963 * is and we'll release it later.
3965 top_object
= object
;
3968 * switch to the object that has the new page
3970 object
= cur_object
;
3971 object_lock_type
= cur_object_lock_type
;
3974 assert(m_object
== VM_PAGE_OBJECT(m
));
3977 * prepare for the pmap_enter...
3978 * object and map are both locked
3979 * m contains valid data
3980 * object == m->object
3981 * cur_object == NULL or it's been unlocked
3982 * no paging references on either object or cur_object
3984 if (top_object
!= VM_OBJECT_NULL
|| object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
)
3985 need_retry_ptr
= &need_retry
;
3987 need_retry_ptr
= NULL
;
3990 kr
= vm_fault_enter(m
,
3997 fault_info
.no_cache
,
3998 fault_info
.cs_bypass
,
3999 fault_info
.user_tag
,
4000 fault_info
.pmap_options
,
4004 kr
= vm_fault_enter(m
,
4011 fault_info
.no_cache
,
4012 fault_info
.cs_bypass
,
4013 fault_info
.user_tag
,
4014 fault_info
.pmap_options
,
4018 #if DEVELOPMENT || DEBUG
4022 if (m_object
->internal
)
4023 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
4024 else if (m_object
->object_slid
)
4025 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
4027 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
4029 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, event_code
, trace_real_vaddr
, (fault_info
.user_tag
<< 16) | (caller_prot
<< 8) | type_of_fault
, m
->offset
, get_current_unique_pid(), 0);
4031 DTRACE_VM6(real_fault
, vm_map_offset_t
, real_vaddr
, vm_map_offset_t
, m
->offset
, int, event_code
, int, caller_prot
, int, type_of_fault
, int, fault_info
.user_tag
);
4034 if (kr
== KERN_SUCCESS
&&
4035 physpage_p
!= NULL
) {
4036 /* for vm_map_wire_and_extract() */
4037 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
4038 if (prot
& VM_PROT_WRITE
) {
4039 vm_object_lock_assert_exclusive(m_object
);
4044 if (top_object
!= VM_OBJECT_NULL
) {
4046 * It's safe to drop the top object
4047 * now that we've done our
4048 * vm_fault_enter(). Any other fault
4049 * in progress for that virtual
4050 * address will either find our page
4051 * and translation or put in a new page
4054 vm_object_unlock(top_object
);
4055 top_object
= VM_OBJECT_NULL
;
4058 if (need_collapse
== TRUE
)
4059 vm_object_collapse(object
, offset
, TRUE
);
4061 if (need_retry
== FALSE
&&
4062 (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
)) {
4064 * evaluate access pattern and update state
4065 * vm_fault_deactivate_behind depends on the
4066 * state being up to date
4068 vm_fault_is_sequential(object
, cur_offset
, fault_info
.behavior
);
4070 vm_fault_deactivate_behind(object
, cur_offset
, fault_info
.behavior
);
4073 * That's it, clean up and return.
4076 PAGE_WAKEUP_DONE(m
);
4078 vm_object_unlock(object
);
4080 vm_map_unlock_read(map
);
4081 if (real_map
!= map
)
4082 vm_map_unlock(real_map
);
4084 if (need_retry
== TRUE
) {
4086 * vm_fault_enter couldn't complete the PMAP_ENTER...
4087 * at this point we don't hold any locks so it's safe
4088 * to ask the pmap layer to expand the page table to
4089 * accommodate this mapping... once expanded, we'll
4090 * re-drive the fault which should result in vm_fault_enter
4091 * being able to successfully enter the mapping this time around
4093 (void)pmap_enter_options(
4094 pmap
, vaddr
, 0, 0, 0, 0, 0,
4095 PMAP_OPTIONS_NOENTER
, NULL
);
4103 * COPY ON WRITE FAULT
4105 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
4108 * If objects match, then
4109 * object->copy must not be NULL (else control
4110 * would be in previous code block), and we
4111 * have a potential push into the copy object
4112 * with which we can't cope with here.
4114 if (cur_object
== object
) {
4116 * must take the slow path to
4117 * deal with the copy push
4123 * This is now a shadow based copy on write
4124 * fault -- it requires a copy up the shadow
4127 assert(m_object
== VM_PAGE_OBJECT(m
));
4129 if ((cur_object_lock_type
== OBJECT_LOCK_SHARED
) &&
4130 VM_FAULT_NEED_CS_VALIDATION(NULL
, m
, m_object
)) {
4131 goto upgrade_for_validation
;
4135 * Allocate a page in the original top level
4136 * object. Give up if allocate fails. Also
4137 * need to remember current page, as it's the
4138 * source of the copy.
4140 * at this point we hold locks on both
4141 * object and cur_object... no need to take
4142 * paging refs or mark pages BUSY since
4143 * we don't drop either object lock until
4144 * the page has been copied and inserted
4147 m
= vm_page_grab_options(grab_options
);
4150 if (m
== VM_PAGE_NULL
) {
4152 * no free page currently available...
4153 * must take the slow path
4158 * Now do the copy. Mark the source page busy...
4160 * NOTE: This code holds the map lock across
4163 vm_page_copy(cur_m
, m
);
4164 vm_page_insert(m
, object
, offset
);
4166 SET_PAGE_DIRTY(m
, FALSE
);
4169 * Now cope with the source page and object
4171 if (object
->ref_count
> 1 && cur_m
->pmapped
)
4172 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m
));
4174 if (cur_m
->clustered
) {
4175 VM_PAGE_COUNT_AS_PAGEIN(cur_m
);
4176 VM_PAGE_CONSUME_CLUSTERED(cur_m
);
4177 vm_fault_is_sequential(cur_object
, cur_offset
, fault_info
.behavior
);
4179 need_collapse
= TRUE
;
4181 if (!cur_object
->internal
&&
4182 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
4184 * The object from which we've just
4185 * copied a page is most probably backed
4186 * by a vnode. We don't want to waste too
4187 * much time trying to collapse the VM objects
4188 * and create a bottleneck when several tasks
4189 * map the same file.
4191 if (cur_object
->copy
== object
) {
4193 * Shared mapping or no COW yet.
4194 * We can never collapse a copy
4195 * object into its backing object.
4197 need_collapse
= FALSE
;
4198 } else if (cur_object
->copy
== object
->shadow
&&
4199 object
->shadow
->resident_page_count
== 0) {
4201 * Shared mapping after a COW occurred.
4203 need_collapse
= FALSE
;
4206 vm_object_unlock(cur_object
);
4208 if (need_collapse
== FALSE
)
4209 vm_fault_collapse_skipped
++;
4210 vm_fault_collapse_total
++;
4212 type_of_fault
= DBG_COW_FAULT
;
4213 VM_STAT_INCR(cow_faults
);
4214 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
4215 current_task()->cow_faults
++;
4221 * No page at cur_object, cur_offset... m == NULL
4223 if (cur_object
->pager_created
) {
4224 int compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
4226 if (MUST_ASK_PAGER(cur_object
, cur_offset
, compressor_external_state
) == TRUE
) {
4228 int c_flags
= C_DONT_BLOCK
;
4229 boolean_t insert_cur_object
= FALSE
;
4232 * May have to talk to a pager...
4233 * if so, take the slow path by
4234 * doing a 'break' from the while (TRUE) loop
4236 * external_state will only be set to VM_EXTERNAL_STATE_EXISTS
4237 * if the compressor is active and the page exists there
4239 if (compressor_external_state
!= VM_EXTERNAL_STATE_EXISTS
)
4242 if (map
== kernel_map
|| real_map
== kernel_map
) {
4244 * can't call into the compressor with the kernel_map
4245 * lock held, since the compressor may try to operate
4246 * on the kernel map in order to return an empty c_segment
4250 if (object
!= cur_object
) {
4251 if (fault_type
& VM_PROT_WRITE
)
4254 insert_cur_object
= TRUE
;
4256 if (insert_cur_object
== TRUE
) {
4258 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
4260 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4262 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
4264 * couldn't upgrade so go do a full retry
4265 * immediately since we can no longer be
4266 * certain about cur_object (since we
4267 * don't hold a reference on it)...
4268 * first drop the top object lock
4270 vm_object_unlock(object
);
4272 vm_map_unlock_read(map
);
4273 if (real_map
!= map
)
4274 vm_map_unlock(real_map
);
4279 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4281 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4283 if (object
!= cur_object
) {
4285 * we can't go for the upgrade on the top
4286 * lock since the upgrade may block waiting
4287 * for readers to drain... since we hold
4288 * cur_object locked at this point, waiting
4289 * for the readers to drain would represent
4290 * a lock order inversion since the lock order
4291 * for objects is the reference order in the
4294 vm_object_unlock(object
);
4295 vm_object_unlock(cur_object
);
4297 vm_map_unlock_read(map
);
4298 if (real_map
!= map
)
4299 vm_map_unlock(real_map
);
4303 if (vm_object_lock_upgrade(object
) == FALSE
) {
4305 * couldn't upgrade, so explictly take the lock
4306 * exclusively and go relookup the page since we
4307 * will have dropped the object lock and
4308 * a different thread could have inserted
4309 * a page at this offset
4310 * no need for a full retry since we're
4311 * at the top level of the object chain
4313 vm_object_lock(object
);
4318 m
= vm_page_grab_options(grab_options
);
4321 if (m
== VM_PAGE_NULL
) {
4323 * no free page currently available...
4324 * must take the slow path
4330 * The object is and remains locked
4331 * so no need to take a
4332 * "paging_in_progress" reference.
4334 boolean_t shared_lock
;
4335 if ((object
== cur_object
&&
4336 object_lock_type
== OBJECT_LOCK_EXCLUSIVE
) ||
4337 (object
!= cur_object
&&
4338 cur_object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)) {
4339 shared_lock
= FALSE
;
4344 kr
= vm_compressor_pager_get(
4347 cur_object
->paging_offset
),
4348 VM_PAGE_GET_PHYS_PAGE(m
),
4351 &compressed_count_delta
);
4353 vm_compressor_pager_count(
4355 compressed_count_delta
,
4359 if (kr
!= KERN_SUCCESS
) {
4360 vm_page_release(m
, FALSE
);
4367 * If the object is purgeable, its
4368 * owner's purgeable ledgers will be
4369 * updated in vm_page_insert() but the
4370 * page was also accounted for in a
4371 * "compressed purgeable" ledger, so
4374 if (object
!= cur_object
&&
4375 !insert_cur_object
) {
4377 * We're not going to insert
4378 * the decompressed page into
4379 * the object it came from.
4381 * We're dealing with a
4382 * copy-on-write fault on
4384 * We're going to decompress
4385 * the page directly into the
4386 * target "object" while
4387 * keepin the compressed
4388 * page for "cur_object", so
4389 * no ledger update in that
4392 } else if ((cur_object
->purgable
==
4393 VM_PURGABLE_DENY
) ||
4394 (cur_object
->vo_purgeable_owner
==
4397 * "cur_object" is not purgeable
4398 * or is not owned, so no
4399 * purgeable ledgers to update.
4403 * One less compressed
4404 * purgeable page for
4405 * cur_object's owner.
4407 vm_purgeable_compressed_update(
4412 if (insert_cur_object
) {
4413 vm_page_insert(m
, cur_object
, cur_offset
);
4414 m_object
= cur_object
;
4416 vm_page_insert(m
, object
, offset
);
4420 if ((m_object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_USE_DEFAULT
) {
4422 * If the page is not cacheable,
4423 * we can't let its contents
4424 * linger in the data cache
4425 * after the decompression.
4427 pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m
));
4430 type_of_fault
= my_fault_type
;
4432 VM_STAT_INCR(decompressions
);
4434 if (cur_object
!= object
) {
4435 if (insert_cur_object
) {
4436 top_object
= object
;
4438 * switch to the object that has the new page
4440 object
= cur_object
;
4441 object_lock_type
= cur_object_lock_type
;
4443 vm_object_unlock(cur_object
);
4444 cur_object
= object
;
4450 * existence map present and indicates
4451 * that the pager doesn't have this page
4454 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
4456 * Zero fill fault. Page gets
4457 * inserted into the original object.
4459 if (cur_object
->shadow_severed
||
4460 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
) ||
4461 cur_object
== compressor_object
||
4462 cur_object
== kernel_object
||
4463 cur_object
== vm_submap_object
) {
4464 if (object
!= cur_object
)
4465 vm_object_unlock(cur_object
);
4466 vm_object_unlock(object
);
4468 vm_map_unlock_read(map
);
4469 if (real_map
!= map
)
4470 vm_map_unlock(real_map
);
4472 kr
= KERN_MEMORY_ERROR
;
4475 if (vm_backing_store_low
) {
4477 * we are protecting the system from
4478 * backing store exhaustion...
4479 * must take the slow path if we're
4482 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
))
4485 if (cur_object
!= object
) {
4486 vm_object_unlock(cur_object
);
4488 cur_object
= object
;
4490 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4492 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4494 if (vm_object_lock_upgrade(object
) == FALSE
) {
4496 * couldn't upgrade so do a full retry on the fault
4497 * since we dropped the object lock which
4498 * could allow another thread to insert
4499 * a page at this offset
4501 vm_map_unlock_read(map
);
4502 if (real_map
!= map
)
4503 vm_map_unlock(real_map
);
4508 m
= vm_page_alloc(object
, offset
);
4511 if (m
== VM_PAGE_NULL
) {
4513 * no free page currently available...
4514 * must take the slow path
4521 * Now zero fill page...
4522 * the page is probably going to
4523 * be written soon, so don't bother
4524 * to clear the modified bit
4526 * NOTE: This code holds the map
4527 * lock across the zero fill.
4529 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
4534 * On to the next level in the shadow chain
4536 cur_offset
+= cur_object
->vo_shadow_offset
;
4537 new_object
= cur_object
->shadow
;
4540 * take the new_object's lock with the indicated state
4542 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
)
4543 vm_object_lock_shared(new_object
);
4545 vm_object_lock(new_object
);
4547 if (cur_object
!= object
)
4548 vm_object_unlock(cur_object
);
4550 cur_object
= new_object
;
4556 * Cleanup from fast fault failure. Drop any object
4557 * lock other than original and drop map lock.
4559 if (object
!= cur_object
)
4560 vm_object_unlock(cur_object
);
4563 * must own the object lock exclusively at this point
4565 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
4566 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4568 if (vm_object_lock_upgrade(object
) == FALSE
) {
4570 * couldn't upgrade, so explictly
4571 * take the lock exclusively
4572 * no need to retry the fault at this
4573 * point since "vm_fault_page" will
4574 * completely re-evaluate the state
4576 vm_object_lock(object
);
4581 vm_map_unlock_read(map
);
4582 if (real_map
!= map
)
4583 vm_map_unlock(real_map
);
4585 assert(object
!= compressor_object
);
4586 assert(object
!= kernel_object
);
4587 assert(object
!= vm_submap_object
);
4590 * Make a reference to this object to
4591 * prevent its disposal while we are messing with
4592 * it. Once we have the reference, the map is free
4593 * to be diddled. Since objects reference their
4594 * shadows (and copies), they will stay around as well.
4596 vm_object_reference_locked(object
);
4597 vm_object_paging_begin(object
);
4599 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
4603 result_page
= VM_PAGE_NULL
;
4604 kr
= vm_fault_page(object
, offset
, fault_type
,
4605 (change_wiring
&& !wired
),
4606 FALSE
, /* page not looked up */
4607 &prot
, &result_page
, &top_page
,
4609 &error_code
, map
->no_zero_fill
,
4610 FALSE
, &fault_info
);
4613 * if kr != VM_FAULT_SUCCESS, then the paging reference
4614 * has been dropped and the object unlocked... the ref_count
4617 * if kr == VM_FAULT_SUCCESS, then the paging reference
4618 * is still held along with the ref_count on the original object
4620 * the object is returned locked with a paging reference
4622 * if top_page != NULL, then it's BUSY and the
4623 * object it belongs to has a paging reference
4624 * but is returned unlocked
4626 if (kr
!= VM_FAULT_SUCCESS
&&
4627 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
4629 * we didn't succeed, lose the object reference immediately.
4631 vm_object_deallocate(object
);
4634 * See why we failed, and take corrective action.
4637 case VM_FAULT_MEMORY_SHORTAGE
:
4638 if (vm_page_wait((change_wiring
) ?
4645 case VM_FAULT_INTERRUPTED
:
4648 case VM_FAULT_RETRY
:
4650 case VM_FAULT_MEMORY_ERROR
:
4654 kr
= KERN_MEMORY_ERROR
;
4657 panic("vm_fault: unexpected error 0x%x from "
4658 "vm_fault_page()\n", kr
);
4664 if (m
!= VM_PAGE_NULL
) {
4665 m_object
= VM_PAGE_OBJECT(m
);
4666 assert((change_wiring
&& !wired
) ?
4667 (top_page
== VM_PAGE_NULL
) :
4668 ((top_page
== VM_PAGE_NULL
) == (m_object
== object
)));
4672 * What to do with the resulting page from vm_fault_page
4673 * if it doesn't get entered into the physical map:
4675 #define RELEASE_PAGE(m) \
4677 PAGE_WAKEUP_DONE(m); \
4678 if ( !VM_PAGE_PAGEABLE(m)) { \
4679 vm_page_lockspin_queues(); \
4680 if ( !VM_PAGE_PAGEABLE(m)) \
4681 vm_page_activate(m); \
4682 vm_page_unlock_queues(); \
4687 * We must verify that the maps have not changed
4688 * since our last lookup.
4690 if (m
!= VM_PAGE_NULL
) {
4691 old_copy_object
= m_object
->copy
;
4692 vm_object_unlock(m_object
);
4694 old_copy_object
= VM_OBJECT_NULL
;
4695 vm_object_unlock(object
);
4699 * no object locks are held at this point
4701 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
4702 vm_object_t retry_object
;
4703 vm_object_offset_t retry_offset
;
4704 vm_prot_t retry_prot
;
4707 * To avoid trying to write_lock the map while another
4708 * thread has it read_locked (in vm_map_pageable), we
4709 * do not try for write permission. If the page is
4710 * still writable, we will get write permission. If it
4711 * is not, or has been marked needs_copy, we enter the
4712 * mapping without write permission, and will merely
4713 * take another fault.
4716 vm_map_lock_read(map
);
4718 kr
= vm_map_lookup_locked(&map
, vaddr
,
4719 fault_type
& ~VM_PROT_WRITE
,
4720 OBJECT_LOCK_EXCLUSIVE
, &version
,
4721 &retry_object
, &retry_offset
, &retry_prot
,
4725 pmap
= real_map
->pmap
;
4727 if (kr
!= KERN_SUCCESS
) {
4728 vm_map_unlock_read(map
);
4730 if (m
!= VM_PAGE_NULL
) {
4731 assert(VM_PAGE_OBJECT(m
) == m_object
);
4734 * retake the lock so that
4735 * we can drop the paging reference
4736 * in vm_fault_cleanup and do the
4737 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4739 vm_object_lock(m_object
);
4743 vm_fault_cleanup(m_object
, top_page
);
4746 * retake the lock so that
4747 * we can drop the paging reference
4748 * in vm_fault_cleanup
4750 vm_object_lock(object
);
4752 vm_fault_cleanup(object
, top_page
);
4754 vm_object_deallocate(object
);
4758 vm_object_unlock(retry_object
);
4760 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
4762 vm_map_unlock_read(map
);
4763 if (real_map
!= map
)
4764 vm_map_unlock(real_map
);
4766 if (m
!= VM_PAGE_NULL
) {
4767 assert(VM_PAGE_OBJECT(m
) == m_object
);
4770 * retake the lock so that
4771 * we can drop the paging reference
4772 * in vm_fault_cleanup and do the
4773 * PAGE_WAKEUP_DONE in RELEASE_PAGE
4775 vm_object_lock(m_object
);
4779 vm_fault_cleanup(m_object
, top_page
);
4782 * retake the lock so that
4783 * we can drop the paging reference
4784 * in vm_fault_cleanup
4786 vm_object_lock(object
);
4788 vm_fault_cleanup(object
, top_page
);
4790 vm_object_deallocate(object
);
4795 * Check whether the protection has changed or the object
4796 * has been copied while we left the map unlocked.
4800 if (m
!= VM_PAGE_NULL
) {
4801 vm_object_lock(m_object
);
4803 if (m_object
->copy
!= old_copy_object
) {
4805 * The copy object changed while the top-level object
4806 * was unlocked, so take away write permission.
4808 prot
&= ~VM_PROT_WRITE
;
4811 vm_object_lock(object
);
4814 * If we want to wire down this page, but no longer have
4815 * adequate permissions, we must start all over.
4817 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
4819 vm_map_verify_done(map
, &version
);
4820 if (real_map
!= map
)
4821 vm_map_unlock(real_map
);
4823 if (m
!= VM_PAGE_NULL
) {
4824 assert(VM_PAGE_OBJECT(m
) == m_object
);
4828 vm_fault_cleanup(m_object
, top_page
);
4830 vm_fault_cleanup(object
, top_page
);
4832 vm_object_deallocate(object
);
4836 if (m
!= VM_PAGE_NULL
) {
4838 * Put this page into the physical map.
4839 * We had to do the unlock above because pmap_enter
4840 * may cause other faults. The page may be on
4841 * the pageout queues. If the pageout daemon comes
4842 * across the page, it will remove it from the queues.
4845 kr
= vm_fault_enter(m
,
4852 fault_info
.no_cache
,
4853 fault_info
.cs_bypass
,
4854 fault_info
.user_tag
,
4855 fault_info
.pmap_options
,
4859 kr
= vm_fault_enter(m
,
4866 fault_info
.no_cache
,
4867 fault_info
.cs_bypass
,
4868 fault_info
.user_tag
,
4869 fault_info
.pmap_options
,
4873 assert(VM_PAGE_OBJECT(m
) == m_object
);
4875 #if DEVELOPMENT || DEBUG
4879 if (m_object
->internal
)
4880 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_INTERNAL
));
4881 else if (m_object
->object_slid
)
4882 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_SHAREDCACHE
));
4884 event_code
= (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_REAL_FAULT_ADDR_EXTERNAL
));
4886 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, event_code
, trace_real_vaddr
, (fault_info
.user_tag
<< 16) | (caller_prot
<< 8) | type_of_fault
, m
->offset
, get_current_unique_pid(), 0);
4888 DTRACE_VM6(real_fault
, vm_map_offset_t
, real_vaddr
, vm_map_offset_t
, m
->offset
, int, event_code
, int, caller_prot
, int, type_of_fault
, int, fault_info
.user_tag
);
4891 if (kr
!= KERN_SUCCESS
) {
4892 /* abort this page fault */
4893 vm_map_verify_done(map
, &version
);
4894 if (real_map
!= map
)
4895 vm_map_unlock(real_map
);
4896 PAGE_WAKEUP_DONE(m
);
4897 vm_fault_cleanup(m_object
, top_page
);
4898 vm_object_deallocate(object
);
4901 if (physpage_p
!= NULL
) {
4902 /* for vm_map_wire_and_extract() */
4903 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
4904 if (prot
& VM_PROT_WRITE
) {
4905 vm_object_lock_assert_exclusive(m_object
);
4911 vm_map_entry_t entry
;
4912 vm_map_offset_t laddr
;
4913 vm_map_offset_t ldelta
, hdelta
;
4916 * do a pmap block mapping from the physical address
4921 /* While we do not worry about execution protection in */
4922 /* general, certian pages may have instruction execution */
4923 /* disallowed. We will check here, and if not allowed */
4924 /* to execute, we return with a protection failure. */
4926 if ((fault_type
& VM_PROT_EXECUTE
) &&
4927 (!pmap_eligible_for_execute((ppnum_t
)(object
->vo_shadow_offset
>> 12)))) {
4929 vm_map_verify_done(map
, &version
);
4931 if (real_map
!= map
)
4932 vm_map_unlock(real_map
);
4934 vm_fault_cleanup(object
, top_page
);
4935 vm_object_deallocate(object
);
4937 kr
= KERN_PROTECTION_FAILURE
;
4942 if (real_map
!= map
)
4943 vm_map_unlock(real_map
);
4945 if (original_map
!= map
) {
4946 vm_map_unlock_read(map
);
4947 vm_map_lock_read(original_map
);
4953 hdelta
= 0xFFFFF000;
4954 ldelta
= 0xFFFFF000;
4956 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
4957 if (ldelta
> (laddr
- entry
->vme_start
))
4958 ldelta
= laddr
- entry
->vme_start
;
4959 if (hdelta
> (entry
->vme_end
- laddr
))
4960 hdelta
= entry
->vme_end
- laddr
;
4961 if (entry
->is_sub_map
) {
4963 laddr
= ((laddr
- entry
->vme_start
)
4964 + VME_OFFSET(entry
));
4965 vm_map_lock_read(VME_SUBMAP(entry
));
4967 if (map
!= real_map
)
4968 vm_map_unlock_read(map
);
4969 if (entry
->use_pmap
) {
4970 vm_map_unlock_read(real_map
);
4971 real_map
= VME_SUBMAP(entry
);
4973 map
= VME_SUBMAP(entry
);
4980 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
4981 (VME_OBJECT(entry
) != NULL
) &&
4982 (VME_OBJECT(entry
) == object
)) {
4985 if (!object
->pager_created
&&
4986 object
->phys_contiguous
&&
4987 VME_OFFSET(entry
) == 0 &&
4988 (entry
->vme_end
- entry
->vme_start
== object
->vo_size
) &&
4989 VM_MAP_PAGE_ALIGNED(entry
->vme_start
, (object
->vo_size
-1))) {
4990 superpage
= VM_MEM_SUPERPAGE
;
4995 if (superpage
&& physpage_p
) {
4996 /* for vm_map_wire_and_extract() */
4997 *physpage_p
= (ppnum_t
)
4998 ((((vm_map_offset_t
)
4999 object
->vo_shadow_offset
)
5001 + (laddr
- entry
->vme_start
))
5007 * Set up a block mapped area
5009 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5010 pmap_map_block(caller_pmap
,
5011 (addr64_t
)(caller_pmap_addr
- ldelta
),
5012 (ppnum_t
)((((vm_map_offset_t
) (VME_OBJECT(entry
)->vo_shadow_offset
)) +
5013 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5014 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5015 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5018 * Set up a block mapped area
5020 assert((uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
) == ((ldelta
+ hdelta
) >> PAGE_SHIFT
));
5021 pmap_map_block(real_map
->pmap
,
5022 (addr64_t
)(vaddr
- ldelta
),
5023 (ppnum_t
)((((vm_map_offset_t
)(VME_OBJECT(entry
)->vo_shadow_offset
)) +
5024 VME_OFFSET(entry
) + (laddr
- entry
->vme_start
) - ldelta
) >> PAGE_SHIFT
),
5025 (uint32_t)((ldelta
+ hdelta
) >> PAGE_SHIFT
), prot
,
5026 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
5032 * Unlock everything, and return
5034 vm_map_verify_done(map
, &version
);
5035 if (real_map
!= map
)
5036 vm_map_unlock(real_map
);
5038 if (m
!= VM_PAGE_NULL
) {
5039 assert(VM_PAGE_OBJECT(m
) == m_object
);
5041 PAGE_WAKEUP_DONE(m
);
5043 vm_fault_cleanup(m_object
, top_page
);
5045 vm_fault_cleanup(object
, top_page
);
5047 vm_object_deallocate(object
);
5053 thread_interrupt_level(interruptible_state
);
5056 * Only I/O throttle on faults which cause a pagein/swapin.
5058 if ((type_of_fault
== DBG_PAGEIND_FAULT
) || (type_of_fault
== DBG_PAGEINV_FAULT
) || (type_of_fault
== DBG_COMPRESSOR_SWAPIN_FAULT
)) {
5059 throttle_lowpri_io(1);
5061 if (kr
== KERN_SUCCESS
&& type_of_fault
!= DBG_CACHE_HIT_FAULT
&& type_of_fault
!= DBG_GUARD_FAULT
) {
5063 if ((throttle_delay
= vm_page_throttled(TRUE
))) {
5065 if (vm_debug_events
) {
5066 if (type_of_fault
== DBG_COMPRESSOR_FAULT
)
5067 VM_DEBUG_EVENT(vmf_compressordelay
, VMF_COMPRESSORDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5068 else if (type_of_fault
== DBG_COW_FAULT
)
5069 VM_DEBUG_EVENT(vmf_cowdelay
, VMF_COWDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5071 VM_DEBUG_EVENT(vmf_zfdelay
, VMF_ZFDELAY
, DBG_FUNC_NONE
, throttle_delay
, 0, 0, 0);
5073 delay(throttle_delay
);
5077 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5078 (MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
5079 ((uint64_t)trace_vaddr
>> 32),
5091 * Wire down a range of virtual addresses in a map.
5096 vm_map_entry_t entry
,
5099 vm_map_offset_t pmap_addr
,
5100 ppnum_t
*physpage_p
)
5103 vm_map_offset_t end_addr
= entry
->vme_end
;
5106 assert(entry
->in_transition
);
5108 if ((VME_OBJECT(entry
) != NULL
) &&
5109 !entry
->is_sub_map
&&
5110 VME_OBJECT(entry
)->phys_contiguous
) {
5111 return KERN_SUCCESS
;
5115 * Inform the physical mapping system that the
5116 * range of addresses may not fault, so that
5117 * page tables and such can be locked down as well.
5120 pmap_pageable(pmap
, pmap_addr
,
5121 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
5124 * We simulate a fault to get the page and enter it
5125 * in the physical map.
5128 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
5129 rc
= vm_fault_wire_fast(map
, va
, prot
, entry
, pmap
,
5130 pmap_addr
+ (va
- entry
->vme_start
),
5132 if (rc
!= KERN_SUCCESS
) {
5133 rc
= vm_fault_internal(map
, va
, prot
, TRUE
,
5134 ((pmap
== kernel_pmap
)
5136 : THREAD_ABORTSAFE
),
5139 (va
- entry
->vme_start
)),
5141 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
5144 if (rc
!= KERN_SUCCESS
) {
5145 struct vm_map_entry tmp_entry
= *entry
;
5147 /* unwire wired pages */
5148 tmp_entry
.vme_end
= va
;
5149 vm_fault_unwire(map
,
5150 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
5155 return KERN_SUCCESS
;
5161 * Unwire a range of virtual addresses in a map.
5166 vm_map_entry_t entry
,
5167 boolean_t deallocate
,
5169 vm_map_offset_t pmap_addr
)
5172 vm_map_offset_t end_addr
= entry
->vme_end
;
5174 struct vm_object_fault_info fault_info
;
5176 object
= (entry
->is_sub_map
) ? VM_OBJECT_NULL
: VME_OBJECT(entry
);
5179 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
5180 * do anything since such memory is wired by default. So we don't have
5181 * anything to undo here.
5184 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
)
5187 fault_info
.interruptible
= THREAD_UNINT
;
5188 fault_info
.behavior
= entry
->behavior
;
5189 fault_info
.user_tag
= VME_ALIAS(entry
);
5190 fault_info
.pmap_options
= 0;
5191 if (entry
->iokit_acct
||
5192 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
5193 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
5195 fault_info
.lo_offset
= VME_OFFSET(entry
);
5196 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + VME_OFFSET(entry
);
5197 fault_info
.no_cache
= entry
->no_cache
;
5198 fault_info
.stealth
= TRUE
;
5199 fault_info
.io_sync
= FALSE
;
5200 fault_info
.cs_bypass
= FALSE
;
5201 fault_info
.mark_zf_absent
= FALSE
;
5202 fault_info
.batch_pmap_op
= FALSE
;
5205 * Since the pages are wired down, we must be able to
5206 * get their mappings from the physical map system.
5209 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
5211 if (object
== VM_OBJECT_NULL
) {
5213 pmap_change_wiring(pmap
,
5214 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5216 (void) vm_fault(map
, va
, VM_PROT_NONE
,
5217 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
5220 vm_page_t result_page
;
5222 vm_object_t result_object
;
5223 vm_fault_return_t result
;
5225 if (end_addr
- va
> (vm_size_t
) -1) {
5226 /* 32-bit overflow */
5227 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5229 fault_info
.cluster_size
= (vm_size_t
) (end_addr
- va
);
5230 assert(fault_info
.cluster_size
== end_addr
- va
);
5234 prot
= VM_PROT_NONE
;
5236 vm_object_lock(object
);
5237 vm_object_paging_begin(object
);
5239 "vm_fault_unwire -> vm_fault_page\n",
5241 result_page
= VM_PAGE_NULL
;
5242 result
= vm_fault_page(
5244 (VME_OFFSET(entry
) +
5245 (va
- entry
->vme_start
)),
5247 FALSE
, /* page not looked up */
5248 &prot
, &result_page
, &top_page
,
5250 NULL
, map
->no_zero_fill
,
5251 FALSE
, &fault_info
);
5252 } while (result
== VM_FAULT_RETRY
);
5255 * If this was a mapping to a file on a device that has been forcibly
5256 * unmounted, then we won't get a page back from vm_fault_page(). Just
5257 * move on to the next one in case the remaining pages are mapped from
5258 * different objects. During a forced unmount, the object is terminated
5259 * so the alive flag will be false if this happens. A forced unmount will
5260 * will occur when an external disk is unplugged before the user does an
5261 * eject, so we don't want to panic in that situation.
5264 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
)
5267 if (result
== VM_FAULT_MEMORY_ERROR
&&
5268 object
== kernel_object
) {
5270 * This must have been allocated with
5271 * KMA_KOBJECT and KMA_VAONLY and there's
5272 * no physical page at this offset.
5273 * We're done (no page to free).
5279 if (result
!= VM_FAULT_SUCCESS
)
5280 panic("vm_fault_unwire: failure");
5282 result_object
= VM_PAGE_OBJECT(result_page
);
5285 assert(VM_PAGE_GET_PHYS_PAGE(result_page
) !=
5286 vm_page_fictitious_addr
);
5287 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page
));
5288 VM_PAGE_FREE(result_page
);
5290 if ((pmap
) && (VM_PAGE_GET_PHYS_PAGE(result_page
) != vm_page_guard_addr
))
5291 pmap_change_wiring(pmap
,
5292 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
5295 if (VM_PAGE_WIRED(result_page
)) {
5296 vm_page_lockspin_queues();
5297 vm_page_unwire(result_page
, TRUE
);
5298 vm_page_unlock_queues();
5300 if(entry
->zero_wired_pages
) {
5301 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page
));
5302 entry
->zero_wired_pages
= FALSE
;
5305 PAGE_WAKEUP_DONE(result_page
);
5307 vm_fault_cleanup(result_object
, top_page
);
5312 * Inform the physical mapping system that the range
5313 * of addresses may fault, so that page tables and
5314 * such may be unwired themselves.
5317 pmap_pageable(pmap
, pmap_addr
,
5318 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
5323 * vm_fault_wire_fast:
5325 * Handle common case of a wire down page fault at the given address.
5326 * If successful, the page is inserted into the associated physical map.
5327 * The map entry is passed in to avoid the overhead of a map lookup.
5329 * NOTE: the given address should be truncated to the
5330 * proper page address.
5332 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
5333 * a standard error specifying why the fault is fatal is returned.
5335 * The map in question must be referenced, and remains so.
5336 * Caller has a read lock on the map.
5338 * This is a stripped version of vm_fault() for wiring pages. Anything
5339 * other than the common case will return KERN_FAILURE, and the caller
5340 * is expected to call vm_fault().
5342 static kern_return_t
5344 __unused vm_map_t map
,
5346 vm_prot_t caller_prot
,
5347 vm_map_entry_t entry
,
5349 vm_map_offset_t pmap_addr
,
5350 ppnum_t
*physpage_p
)
5353 vm_object_offset_t offset
;
5356 thread_t thread
= current_thread();
5360 VM_STAT_INCR(faults
);
5362 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
)
5363 thread
->task
->faults
++;
5370 #define RELEASE_PAGE(m) { \
5371 PAGE_WAKEUP_DONE(m); \
5372 vm_page_lockspin_queues(); \
5373 vm_page_unwire(m, TRUE); \
5374 vm_page_unlock_queues(); \
5378 #undef UNLOCK_THINGS
5379 #define UNLOCK_THINGS { \
5380 vm_object_paging_end(object); \
5381 vm_object_unlock(object); \
5384 #undef UNLOCK_AND_DEALLOCATE
5385 #define UNLOCK_AND_DEALLOCATE { \
5387 vm_object_deallocate(object); \
5390 * Give up and have caller do things the hard way.
5394 UNLOCK_AND_DEALLOCATE; \
5395 return(KERN_FAILURE); \
5400 * If this entry is not directly to a vm_object, bail out.
5402 if (entry
->is_sub_map
) {
5403 assert(physpage_p
== NULL
);
5404 return(KERN_FAILURE
);
5408 * Find the backing store object and offset into it.
5411 object
= VME_OBJECT(entry
);
5412 offset
= (va
- entry
->vme_start
) + VME_OFFSET(entry
);
5413 prot
= entry
->protection
;
5416 * Make a reference to this object to prevent its
5417 * disposal while we are messing with it.
5420 vm_object_lock(object
);
5421 vm_object_reference_locked(object
);
5422 vm_object_paging_begin(object
);
5425 * INVARIANTS (through entire routine):
5427 * 1) At all times, we must either have the object
5428 * lock or a busy page in some object to prevent
5429 * some other thread from trying to bring in
5432 * 2) Once we have a busy page, we must remove it from
5433 * the pageout queues, so that the pageout daemon
5434 * will not grab it away.
5439 * Look for page in top-level object. If it's not there or
5440 * there's something going on, give up.
5441 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
5442 * decrypt the page before wiring it down.
5444 m
= vm_page_lookup(object
, offset
);
5445 if ((m
== VM_PAGE_NULL
) || (m
->busy
) || (m
->encrypted
) ||
5446 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
5450 ASSERT_PAGE_DECRYPTED(m
);
5452 if (m
->fictitious
&&
5453 VM_PAGE_GET_PHYS_PAGE(m
) == vm_page_guard_addr
) {
5455 * Guard pages are fictitious pages and are never
5456 * entered into a pmap, so let's say it's been wired...
5463 * Wire the page down now. All bail outs beyond this
5464 * point must unwire the page.
5467 vm_page_lockspin_queues();
5468 vm_page_wire(m
, VM_PROT_MEMORY_TAG(caller_prot
), TRUE
);
5469 vm_page_unlock_queues();
5472 * Mark page busy for other threads.
5479 * Give up if the page is being written and there's a copy object
5481 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
5487 * Put this page into the physical map.
5489 type_of_fault
= DBG_CACHE_HIT_FAULT
;
5490 kr
= vm_fault_enter(m
,
5500 ((entry
->iokit_acct
||
5501 (!entry
->is_sub_map
&& !entry
->use_pmap
))
5502 ? PMAP_OPTIONS_ALT_ACCT
5506 if (kr
!= KERN_SUCCESS
) {
5513 * Unlock everything, and return
5517 /* for vm_map_wire_and_extract() */
5518 if (kr
== KERN_SUCCESS
) {
5519 assert(object
== VM_PAGE_OBJECT(m
));
5520 *physpage_p
= VM_PAGE_GET_PHYS_PAGE(m
);
5521 if (prot
& VM_PROT_WRITE
) {
5522 vm_object_lock_assert_exclusive(object
);
5530 PAGE_WAKEUP_DONE(m
);
5531 UNLOCK_AND_DEALLOCATE
;
5538 * Routine: vm_fault_copy_cleanup
5540 * Release a page used by vm_fault_copy.
5544 vm_fault_copy_cleanup(
5548 vm_object_t object
= VM_PAGE_OBJECT(page
);
5550 vm_object_lock(object
);
5551 PAGE_WAKEUP_DONE(page
);
5552 if ( !VM_PAGE_PAGEABLE(page
)) {
5553 vm_page_lockspin_queues();
5554 if ( !VM_PAGE_PAGEABLE(page
)) {
5555 vm_page_activate(page
);
5557 vm_page_unlock_queues();
5559 vm_fault_cleanup(object
, top_page
);
5563 vm_fault_copy_dst_cleanup(
5568 if (page
!= VM_PAGE_NULL
) {
5569 object
= VM_PAGE_OBJECT(page
);
5570 vm_object_lock(object
);
5571 vm_page_lockspin_queues();
5572 vm_page_unwire(page
, TRUE
);
5573 vm_page_unlock_queues();
5574 vm_object_paging_end(object
);
5575 vm_object_unlock(object
);
5580 * Routine: vm_fault_copy
5583 * Copy pages from one virtual memory object to another --
5584 * neither the source nor destination pages need be resident.
5586 * Before actually copying a page, the version associated with
5587 * the destination address map wil be verified.
5589 * In/out conditions:
5590 * The caller must hold a reference, but not a lock, to
5591 * each of the source and destination objects and to the
5595 * Returns KERN_SUCCESS if no errors were encountered in
5596 * reading or writing the data. Returns KERN_INTERRUPTED if
5597 * the operation was interrupted (only possible if the
5598 * "interruptible" argument is asserted). Other return values
5599 * indicate a permanent error in copying the data.
5601 * The actual amount of data copied will be returned in the
5602 * "copy_size" argument. In the event that the destination map
5603 * verification failed, this amount may be less than the amount
5608 vm_object_t src_object
,
5609 vm_object_offset_t src_offset
,
5610 vm_map_size_t
*copy_size
, /* INOUT */
5611 vm_object_t dst_object
,
5612 vm_object_offset_t dst_offset
,
5614 vm_map_version_t
*dst_version
,
5617 vm_page_t result_page
;
5620 vm_page_t src_top_page
;
5624 vm_page_t dst_top_page
;
5627 vm_map_size_t amount_left
;
5628 vm_object_t old_copy_object
;
5629 vm_object_t result_page_object
= NULL
;
5630 kern_return_t error
= 0;
5631 vm_fault_return_t result
;
5633 vm_map_size_t part_size
;
5634 struct vm_object_fault_info fault_info_src
;
5635 struct vm_object_fault_info fault_info_dst
;
5638 * In order not to confuse the clustered pageins, align
5639 * the different offsets on a page boundary.
5644 *copy_size -= amount_left; \
5648 amount_left
= *copy_size
;
5650 fault_info_src
.interruptible
= interruptible
;
5651 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5652 fault_info_src
.user_tag
= 0;
5653 fault_info_src
.pmap_options
= 0;
5654 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
5655 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
5656 fault_info_src
.no_cache
= FALSE
;
5657 fault_info_src
.stealth
= TRUE
;
5658 fault_info_src
.io_sync
= FALSE
;
5659 fault_info_src
.cs_bypass
= FALSE
;
5660 fault_info_src
.mark_zf_absent
= FALSE
;
5661 fault_info_src
.batch_pmap_op
= FALSE
;
5663 fault_info_dst
.interruptible
= interruptible
;
5664 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5665 fault_info_dst
.user_tag
= 0;
5666 fault_info_dst
.pmap_options
= 0;
5667 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
5668 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
5669 fault_info_dst
.no_cache
= FALSE
;
5670 fault_info_dst
.stealth
= TRUE
;
5671 fault_info_dst
.io_sync
= FALSE
;
5672 fault_info_dst
.cs_bypass
= FALSE
;
5673 fault_info_dst
.mark_zf_absent
= FALSE
;
5674 fault_info_dst
.batch_pmap_op
= FALSE
;
5676 do { /* while (amount_left > 0) */
5678 * There may be a deadlock if both source and destination
5679 * pages are the same. To avoid this deadlock, the copy must
5680 * start by getting the destination page in order to apply
5681 * COW semantics if any.
5684 RetryDestinationFault
: ;
5686 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
5688 vm_object_lock(dst_object
);
5689 vm_object_paging_begin(dst_object
);
5691 if (amount_left
> (vm_size_t
) -1) {
5692 /* 32-bit overflow */
5693 fault_info_dst
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5695 fault_info_dst
.cluster_size
= (vm_size_t
) amount_left
;
5696 assert(fault_info_dst
.cluster_size
== amount_left
);
5699 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
5700 dst_page
= VM_PAGE_NULL
;
5701 result
= vm_fault_page(dst_object
,
5702 vm_object_trunc_page(dst_offset
),
5703 VM_PROT_WRITE
|VM_PROT_READ
,
5705 FALSE
, /* page not looked up */
5706 &dst_prot
, &dst_page
, &dst_top_page
,
5709 dst_map
->no_zero_fill
,
5710 FALSE
, &fault_info_dst
);
5712 case VM_FAULT_SUCCESS
:
5714 case VM_FAULT_RETRY
:
5715 goto RetryDestinationFault
;
5716 case VM_FAULT_MEMORY_SHORTAGE
:
5717 if (vm_page_wait(interruptible
))
5718 goto RetryDestinationFault
;
5720 case VM_FAULT_INTERRUPTED
:
5721 RETURN(MACH_SEND_INTERRUPTED
);
5722 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5723 /* success but no VM page: fail the copy */
5724 vm_object_paging_end(dst_object
);
5725 vm_object_unlock(dst_object
);
5727 case VM_FAULT_MEMORY_ERROR
:
5731 return(KERN_MEMORY_ERROR
);
5733 panic("vm_fault_copy: unexpected error 0x%x from "
5734 "vm_fault_page()\n", result
);
5736 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
5738 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
5739 old_copy_object
= dst_object
->copy
;
5742 * There exists the possiblity that the source and
5743 * destination page are the same. But we can't
5744 * easily determine that now. If they are the
5745 * same, the call to vm_fault_page() for the
5746 * destination page will deadlock. To prevent this we
5747 * wire the page so we can drop busy without having
5748 * the page daemon steal the page. We clean up the
5749 * top page but keep the paging reference on the object
5750 * holding the dest page so it doesn't go away.
5753 vm_page_lockspin_queues();
5754 vm_page_wire(dst_page
, VM_KERN_MEMORY_OSFMK
, TRUE
);
5755 vm_page_unlock_queues();
5756 PAGE_WAKEUP_DONE(dst_page
);
5757 vm_object_unlock(dst_object
);
5759 if (dst_top_page
!= VM_PAGE_NULL
) {
5760 vm_object_lock(dst_object
);
5761 VM_PAGE_FREE(dst_top_page
);
5762 vm_object_paging_end(dst_object
);
5763 vm_object_unlock(dst_object
);
5768 if (src_object
== VM_OBJECT_NULL
) {
5770 * No source object. We will just
5771 * zero-fill the page in dst_object.
5773 src_page
= VM_PAGE_NULL
;
5774 result_page
= VM_PAGE_NULL
;
5776 vm_object_lock(src_object
);
5777 src_page
= vm_page_lookup(src_object
,
5778 vm_object_trunc_page(src_offset
));
5779 if (src_page
== dst_page
) {
5780 src_prot
= dst_prot
;
5781 result_page
= VM_PAGE_NULL
;
5783 src_prot
= VM_PROT_READ
;
5784 vm_object_paging_begin(src_object
);
5786 if (amount_left
> (vm_size_t
) -1) {
5787 /* 32-bit overflow */
5788 fault_info_src
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
5790 fault_info_src
.cluster_size
= (vm_size_t
) amount_left
;
5791 assert(fault_info_src
.cluster_size
== amount_left
);
5795 "vm_fault_copy(2) -> vm_fault_page\n",
5797 result_page
= VM_PAGE_NULL
;
5798 result
= vm_fault_page(
5800 vm_object_trunc_page(src_offset
),
5801 VM_PROT_READ
, FALSE
,
5802 FALSE
, /* page not looked up */
5804 &result_page
, &src_top_page
,
5805 (int *)0, &error
, FALSE
,
5806 FALSE
, &fault_info_src
);
5809 case VM_FAULT_SUCCESS
:
5811 case VM_FAULT_RETRY
:
5812 goto RetrySourceFault
;
5813 case VM_FAULT_MEMORY_SHORTAGE
:
5814 if (vm_page_wait(interruptible
))
5815 goto RetrySourceFault
;
5817 case VM_FAULT_INTERRUPTED
:
5818 vm_fault_copy_dst_cleanup(dst_page
);
5819 RETURN(MACH_SEND_INTERRUPTED
);
5820 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5821 /* success but no VM page: fail */
5822 vm_object_paging_end(src_object
);
5823 vm_object_unlock(src_object
);
5825 case VM_FAULT_MEMORY_ERROR
:
5826 vm_fault_copy_dst_cleanup(dst_page
);
5830 return(KERN_MEMORY_ERROR
);
5832 panic("vm_fault_copy(2): unexpected "
5834 "vm_fault_page()\n", result
);
5837 result_page_object
= VM_PAGE_OBJECT(result_page
);
5838 assert((src_top_page
== VM_PAGE_NULL
) ==
5839 (result_page_object
== src_object
));
5841 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
5842 vm_object_unlock(result_page_object
);
5845 if (!vm_map_verify(dst_map
, dst_version
)) {
5846 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5847 vm_fault_copy_cleanup(result_page
, src_top_page
);
5848 vm_fault_copy_dst_cleanup(dst_page
);
5851 assert(dst_object
== VM_PAGE_OBJECT(dst_page
));
5853 vm_object_lock(dst_object
);
5855 if (dst_object
->copy
!= old_copy_object
) {
5856 vm_object_unlock(dst_object
);
5857 vm_map_verify_done(dst_map
, dst_version
);
5858 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5859 vm_fault_copy_cleanup(result_page
, src_top_page
);
5860 vm_fault_copy_dst_cleanup(dst_page
);
5863 vm_object_unlock(dst_object
);
5866 * Copy the page, and note that it is dirty
5870 if (!page_aligned(src_offset
) ||
5871 !page_aligned(dst_offset
) ||
5872 !page_aligned(amount_left
)) {
5874 vm_object_offset_t src_po
,
5877 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
5878 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
5880 if (dst_po
> src_po
) {
5881 part_size
= PAGE_SIZE
- dst_po
;
5883 part_size
= PAGE_SIZE
- src_po
;
5885 if (part_size
> (amount_left
)){
5886 part_size
= amount_left
;
5889 if (result_page
== VM_PAGE_NULL
) {
5890 assert((vm_offset_t
) dst_po
== dst_po
);
5891 assert((vm_size_t
) part_size
== part_size
);
5892 vm_page_part_zero_fill(dst_page
,
5893 (vm_offset_t
) dst_po
,
5894 (vm_size_t
) part_size
);
5896 assert((vm_offset_t
) src_po
== src_po
);
5897 assert((vm_offset_t
) dst_po
== dst_po
);
5898 assert((vm_size_t
) part_size
== part_size
);
5899 vm_page_part_copy(result_page
,
5900 (vm_offset_t
) src_po
,
5902 (vm_offset_t
) dst_po
,
5903 (vm_size_t
)part_size
);
5904 if(!dst_page
->dirty
){
5905 vm_object_lock(dst_object
);
5906 SET_PAGE_DIRTY(dst_page
, TRUE
);
5907 vm_object_unlock(dst_object
);
5912 part_size
= PAGE_SIZE
;
5914 if (result_page
== VM_PAGE_NULL
)
5915 vm_page_zero_fill(dst_page
);
5917 vm_object_lock(result_page_object
);
5918 vm_page_copy(result_page
, dst_page
);
5919 vm_object_unlock(result_page_object
);
5921 if(!dst_page
->dirty
){
5922 vm_object_lock(dst_object
);
5923 SET_PAGE_DIRTY(dst_page
, TRUE
);
5924 vm_object_unlock(dst_object
);
5931 * Unlock everything, and return
5934 vm_map_verify_done(dst_map
, dst_version
);
5936 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
5937 vm_fault_copy_cleanup(result_page
, src_top_page
);
5938 vm_fault_copy_dst_cleanup(dst_page
);
5940 amount_left
-= part_size
;
5941 src_offset
+= part_size
;
5942 dst_offset
+= part_size
;
5943 } while (amount_left
> 0);
5945 RETURN(KERN_SUCCESS
);
5951 #if VM_FAULT_CLASSIFY
5953 * Temporary statistics gathering support.
5957 * Statistics arrays:
5959 #define VM_FAULT_TYPES_MAX 5
5960 #define VM_FAULT_LEVEL_MAX 8
5962 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
5964 #define VM_FAULT_TYPE_ZERO_FILL 0
5965 #define VM_FAULT_TYPE_MAP_IN 1
5966 #define VM_FAULT_TYPE_PAGER 2
5967 #define VM_FAULT_TYPE_COPY 3
5968 #define VM_FAULT_TYPE_OTHER 4
5972 vm_fault_classify(vm_object_t object
,
5973 vm_object_offset_t offset
,
5974 vm_prot_t fault_type
)
5976 int type
, level
= 0;
5980 m
= vm_page_lookup(object
, offset
);
5981 if (m
!= VM_PAGE_NULL
) {
5982 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
) {
5983 type
= VM_FAULT_TYPE_OTHER
;
5986 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
5987 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
5988 type
= VM_FAULT_TYPE_MAP_IN
;
5991 type
= VM_FAULT_TYPE_COPY
;
5995 if (object
->pager_created
) {
5996 type
= VM_FAULT_TYPE_PAGER
;
5999 if (object
->shadow
== VM_OBJECT_NULL
) {
6000 type
= VM_FAULT_TYPE_ZERO_FILL
;
6004 offset
+= object
->vo_shadow_offset
;
6005 object
= object
->shadow
;
6011 if (level
> VM_FAULT_LEVEL_MAX
)
6012 level
= VM_FAULT_LEVEL_MAX
;
6014 vm_fault_stats
[type
][level
] += 1;
6019 /* cleanup routine to call from debugger */
6022 vm_fault_classify_init(void)
6026 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
6027 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
6028 vm_fault_stats
[type
][level
] = 0;
6034 #endif /* VM_FAULT_CLASSIFY */
6037 kdp_lightweight_fault(vm_map_t map
, vm_offset_t cur_target_addr
)
6039 vm_map_entry_t entry
;
6041 vm_offset_t object_offset
;
6043 int compressor_external_state
, compressed_count_delta
;
6044 int compressor_flags
= (C_DONT_BLOCK
| C_KEEP
| C_KDP
);
6045 int my_fault_type
= VM_PROT_READ
;
6049 panic("kdp_lightweight_fault called from outside of debugger context");
6052 assert(map
!= VM_MAP_NULL
);
6054 assert((cur_target_addr
& PAGE_MASK
) == 0);
6055 if ((cur_target_addr
& PAGE_MASK
) != 0) {
6059 if (kdp_lck_rw_lock_is_acquired_exclusive(&map
->lock
)) {
6063 if (!vm_map_lookup_entry(map
, cur_target_addr
, &entry
)) {
6067 if (entry
->is_sub_map
) {
6071 object
= VME_OBJECT(entry
);
6072 if (object
== VM_OBJECT_NULL
) {
6076 object_offset
= cur_target_addr
- entry
->vme_start
+ VME_OFFSET(entry
);
6079 if (kdp_lck_rw_lock_is_acquired_exclusive(&object
->Lock
)) {
6083 if (object
->pager_created
&& (object
->paging_in_progress
||
6084 object
->activity_in_progress
)) {
6088 m
= kdp_vm_page_lookup(object
, object_offset
);
6090 if (m
!= VM_PAGE_NULL
) {
6092 if ((object
->wimg_bits
& VM_WIMG_MASK
) != VM_WIMG_DEFAULT
) {
6096 if (m
->laundry
|| m
->busy
|| m
->free_when_done
|| m
->absent
|| m
->error
|| m
->cleaning
||
6097 m
->overwriting
|| m
->restart
|| m
->unusual
) {
6101 assert(!m
->private);
6106 assert(!m
->fictitious
);
6107 if (m
->fictitious
) {
6111 assert(!m
->encrypted
);
6116 assert(!m
->encrypted_cleaning
);
6117 if (m
->encrypted_cleaning
) {
6121 assert(m
->vm_page_q_state
!= VM_PAGE_USED_BY_COMPRESSOR
);
6122 if (m
->vm_page_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
6126 return ptoa(VM_PAGE_GET_PHYS_PAGE(m
));
6129 compressor_external_state
= VM_EXTERNAL_STATE_UNKNOWN
;
6131 if (object
->pager_created
&& MUST_ASK_PAGER(object
, object_offset
, compressor_external_state
)) {
6132 if (compressor_external_state
== VM_EXTERNAL_STATE_EXISTS
) {
6133 kr
= vm_compressor_pager_get(object
->pager
, (object_offset
+ object
->paging_offset
),
6134 kdp_compressor_decompressed_page_ppnum
, &my_fault_type
,
6135 compressor_flags
, &compressed_count_delta
);
6136 if (kr
== KERN_SUCCESS
) {
6137 return kdp_compressor_decompressed_page_paddr
;
6144 if (object
->shadow
== VM_OBJECT_NULL
) {
6148 object_offset
+= object
->vo_shadow_offset
;
6149 object
= object
->shadow
;
6155 vm_page_validate_cs_mapped(
6160 vm_object_offset_t offset
;
6161 memory_object_t pager
;
6162 struct vnode
*vnode
;
6163 boolean_t validated
;
6167 object
= VM_PAGE_OBJECT(page
);
6168 vm_object_lock_assert_exclusive(object
);
6170 if (page
->wpmapped
&& !page
->cs_tainted
) {
6172 * This page was mapped for "write" access sometime in the
6173 * past and could still be modifiable in the future.
6174 * Consider it tainted.
6175 * [ If the page was already found to be "tainted", no
6176 * need to re-validate. ]
6178 page
->cs_validated
= TRUE
;
6179 page
->cs_tainted
= TRUE
;
6181 printf("CODESIGNING: vm_page_validate_cs: "
6182 "page %p obj %p off 0x%llx "
6184 page
, object
, page
->offset
);
6186 vm_cs_validated_dirtied
++;
6189 if (page
->cs_validated
|| page
->cs_tainted
) {
6195 assert(object
->code_signed
);
6196 offset
= page
->offset
;
6198 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
6200 * The object is terminating and we don't have its pager
6201 * so we can't validate the data...
6206 * Since we get here to validate a page that was brought in by
6207 * the pager, we know that this pager is all setup and ready
6210 assert(!object
->internal
);
6211 assert(object
->pager
!= NULL
);
6212 assert(object
->pager_ready
);
6214 pager
= object
->pager
;
6215 assert(object
->paging_in_progress
);
6216 vnode
= vnode_pager_lookup_vnode(pager
);
6218 /* verify the SHA1 hash for this page */
6220 validated
= cs_validate_range(vnode
,
6222 (object
->paging_offset
+
6224 (const void *)((const char *)kaddr
),
6228 if (tainted
& CS_VALIDATE_TAINTED
) {
6229 page
->cs_tainted
= TRUE
;
6231 if (tainted
& CS_VALIDATE_NX
) {
6236 page
->cs_validated
= TRUE
;
6241 vm_page_validate_cs(
6245 vm_object_offset_t offset
;
6246 vm_map_offset_t koffset
;
6247 vm_map_size_t ksize
;
6250 boolean_t busy_page
;
6251 boolean_t need_unmap
;
6253 object
= VM_PAGE_OBJECT(page
);
6254 vm_object_lock_assert_held(object
);
6256 if (page
->wpmapped
&& !page
->cs_tainted
) {
6257 vm_object_lock_assert_exclusive(object
);
6260 * This page was mapped for "write" access sometime in the
6261 * past and could still be modifiable in the future.
6262 * Consider it tainted.
6263 * [ If the page was already found to be "tainted", no
6264 * need to re-validate. ]
6266 page
->cs_validated
= TRUE
;
6267 page
->cs_tainted
= TRUE
;
6269 printf("CODESIGNING: vm_page_validate_cs: "
6270 "page %p obj %p off 0x%llx "
6272 page
, object
, page
->offset
);
6274 vm_cs_validated_dirtied
++;
6277 if (page
->cs_validated
|| page
->cs_tainted
) {
6282 panic("vm_page_validate_cs(%p): page is slid\n", page
);
6284 assert(!page
->slid
);
6286 #if CHECK_CS_VALIDATION_BITMAP
6287 if ( vnode_pager_cs_check_validation_bitmap( object
->pager
, trunc_page(page
->offset
+ object
->paging_offset
), CS_BITMAP_CHECK
) == KERN_SUCCESS
) {
6288 page
->cs_validated
= TRUE
;
6289 page
->cs_tainted
= FALSE
;
6290 vm_cs_bitmap_validated
++;
6294 vm_object_lock_assert_exclusive(object
);
6296 assert(object
->code_signed
);
6297 offset
= page
->offset
;
6299 busy_page
= page
->busy
;
6301 /* keep page busy while we map (and unlock) the VM object */
6306 * Take a paging reference on the VM object
6307 * to protect it from collapse or bypass,
6308 * and keep it from disappearing too.
6310 vm_object_paging_begin(object
);
6312 /* map the page in the kernel address space */
6313 ksize
= PAGE_SIZE_64
;
6316 kr
= vm_paging_map_object(page
,
6320 FALSE
, /* can't unlock object ! */
6324 if (kr
!= KERN_SUCCESS
) {
6325 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr
);
6327 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
6329 /* validate the mapped page */
6330 vm_page_validate_cs_mapped(page
, (const void *) kaddr
);
6332 #if CHECK_CS_VALIDATION_BITMAP
6333 if ( page
->cs_validated
== TRUE
&& page
->cs_tainted
== FALSE
) {
6334 vnode_pager_cs_check_validation_bitmap( object
->pager
, trunc_page( offset
+ object
->paging_offset
), CS_BITMAP_SET
);
6338 assert(object
== VM_PAGE_OBJECT(page
));
6339 vm_object_lock_assert_exclusive(object
);
6342 PAGE_WAKEUP_DONE(page
);
6345 /* unmap the map from the kernel address space */
6346 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
6351 vm_object_paging_end(object
);
6355 vm_page_validate_cs_mapped_chunk(
6358 vm_offset_t chunk_offset
,
6359 vm_size_t chunk_size
,
6360 boolean_t
*validated_p
,
6361 unsigned *tainted_p
)
6364 vm_object_offset_t offset
, offset_in_page
;
6365 memory_object_t pager
;
6366 struct vnode
*vnode
;
6367 boolean_t validated
;
6370 *validated_p
= FALSE
;
6374 object
= VM_PAGE_OBJECT(page
);
6375 vm_object_lock_assert_exclusive(object
);
6377 assert(object
->code_signed
);
6378 offset
= page
->offset
;
6380 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
6382 * The object is terminating and we don't have its pager
6383 * so we can't validate the data...
6388 * Since we get here to validate a page that was brought in by
6389 * the pager, we know that this pager is all setup and ready
6392 assert(!object
->internal
);
6393 assert(object
->pager
!= NULL
);
6394 assert(object
->pager_ready
);
6396 pager
= object
->pager
;
6397 assert(object
->paging_in_progress
);
6398 vnode
= vnode_pager_lookup_vnode(pager
);
6400 /* verify the signature for this chunk */
6401 offset_in_page
= chunk_offset
;
6402 assert(offset_in_page
< PAGE_SIZE
);
6405 validated
= cs_validate_range(vnode
,
6407 (object
->paging_offset
+
6410 (const void *)((const char *)kaddr
6415 *validated_p
= TRUE
;
6418 *tainted_p
= tainted
;