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>
68 #include <libkern/OSAtomic.h>
70 #include <mach/mach_types.h>
71 #include <mach/kern_return.h>
72 #include <mach/message.h> /* for error codes */
73 #include <mach/vm_param.h>
74 #include <mach/vm_behavior.h>
75 #include <mach/memory_object.h>
76 /* For memory_object_data_{request,unlock} */
79 #include <kern/kern_types.h>
80 #include <kern/host_statistics.h>
81 #include <kern/counters.h>
82 #include <kern/task.h>
83 #include <kern/thread.h>
84 #include <kern/sched_prim.h>
85 #include <kern/host.h>
87 #include <kern/mach_param.h>
88 #include <kern/macro_help.h>
89 #include <kern/zalloc.h>
90 #include <kern/misc_protos.h>
92 #include <ppc/proc_reg.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 */
106 #include <sys/kdebug.h>
108 #define VM_FAULT_CLASSIFY 0
110 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
112 int vm_object_pagein_throttle
= 16;
115 * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which
116 * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts
117 * of memory if they're buggy and can run the system completely out of swap space. If this happens, we
118 * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps
119 * keep the UI active so that the user has a chance to kill the offending task before the system
122 * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied
123 * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold
124 * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a
125 * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again.
128 boolean_t
thread_is_io_throttled(void);
130 uint64_t vm_hard_throttle_threshold
;
132 extern unsigned int dp_pages_free
, dp_pages_reserve
;
134 #define NEED_TO_HARD_THROTTLE_THIS_TASK() (((dp_pages_free + dp_pages_reserve < 2000) && \
135 (get_task_resident_size(current_task()) > vm_hard_throttle_threshold) && \
136 (current_task() != kernel_task) && IP_VALID(memory_manager_default)) || \
137 (vm_page_free_count < vm_page_throttle_limit && thread_is_io_throttled() && \
138 (get_task_resident_size(current_task()) > vm_hard_throttle_threshold)))
141 #define HARD_THROTTLE_DELAY 10000 /* 10000 us == 10 ms */
147 extern struct db_watchpoint
*db_watchpoint_list
;
148 #endif /* MACH_KDB */
150 boolean_t
current_thread_aborted(void);
152 /* Forward declarations of internal routines. */
153 extern kern_return_t
vm_fault_wire_fast(
156 vm_map_entry_t entry
,
158 vm_map_offset_t pmap_addr
);
160 extern void vm_fault_continue(void);
162 extern void vm_fault_copy_cleanup(
166 extern void vm_fault_copy_dst_cleanup(
169 #if VM_FAULT_CLASSIFY
170 extern void vm_fault_classify(vm_object_t object
,
171 vm_object_offset_t offset
,
172 vm_prot_t fault_type
);
174 extern void vm_fault_classify_init(void);
177 unsigned long vm_pmap_enter_blocked
= 0;
179 unsigned long vm_cs_validates
= 0;
180 unsigned long vm_cs_revalidates
= 0;
181 unsigned long vm_cs_query_modified
= 0;
182 unsigned long vm_cs_validated_dirtied
= 0;
183 #if CONFIG_ENFORCE_SIGNED_CODE
184 int cs_enforcement_disable
=0;
186 static const int cs_enforcement_disable
=1;
190 * Routine: vm_fault_init
192 * Initialize our private data structures.
198 #if CONFIG_ENFORCE_SIGNED_CODE
199 PE_parse_boot_argn("cs_enforcement_disable", &cs_enforcement_disable
,
200 sizeof (cs_enforcement_disable
));
202 PE_parse_boot_argn("cs_debug", &cs_debug
, sizeof (cs_debug
));
206 * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is
207 * computed as a percentage of available memory, and the percentage used is scaled inversely with
208 * the amount of memory. The pertange runs between 10% and 35%. We use 35% for small memory systems
209 * and reduce the value down to 10% for very large memory configurations. This helps give us a
210 * definition of a memory hog that makes more sense relative to the amount of ram in the machine.
211 * The formula here simply uses the number of gigabytes of ram to adjust the percentage.
214 vm_hard_throttle_threshold
= sane_size
* (35 - MIN((int)(sane_size
/ (1024*1024*1024)), 25)) / 100;
218 * Routine: vm_fault_cleanup
220 * Clean up the result of vm_fault_page.
222 * The paging reference for "object" is released.
223 * "object" is unlocked.
224 * If "top_page" is not null, "top_page" is
225 * freed and the paging reference for the object
226 * containing it is released.
229 * "object" must be locked.
233 register vm_object_t object
,
234 register vm_page_t top_page
)
236 vm_object_paging_end(object
);
237 vm_object_unlock(object
);
239 if (top_page
!= VM_PAGE_NULL
) {
240 object
= top_page
->object
;
242 vm_object_lock(object
);
243 VM_PAGE_FREE(top_page
);
244 vm_object_paging_end(object
);
245 vm_object_unlock(object
);
249 #if MACH_CLUSTER_STATS
250 #define MAXCLUSTERPAGES 16
252 unsigned long pages_in_cluster
;
253 unsigned long pages_at_higher_offsets
;
254 unsigned long pages_at_lower_offsets
;
255 } cluster_stats_in
[MAXCLUSTERPAGES
];
256 #define CLUSTER_STAT(clause) clause
257 #define CLUSTER_STAT_HIGHER(x) \
258 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
259 #define CLUSTER_STAT_LOWER(x) \
260 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
261 #define CLUSTER_STAT_CLUSTER(x) \
262 ((cluster_stats_in[(x)].pages_in_cluster)++)
263 #else /* MACH_CLUSTER_STATS */
264 #define CLUSTER_STAT(clause)
265 #endif /* MACH_CLUSTER_STATS */
267 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
270 boolean_t vm_page_deactivate_behind
= TRUE
;
272 * default sizes given VM_BEHAVIOR_DEFAULT reference behavior
274 #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128
275 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */
276 /* we use it to size an array on the stack */
278 int vm_default_behind
= VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW
;
280 #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024)
283 * vm_page_is_sequential
285 * Determine if sequential access is in progress
286 * in accordance with the behavior specified.
287 * Update state to indicate current access pattern.
289 * object must have at least the shared lock held
293 vm_fault_is_sequential(
295 vm_object_offset_t offset
,
296 vm_behavior_t behavior
)
298 vm_object_offset_t last_alloc
;
302 last_alloc
= object
->last_alloc
;
303 sequential
= object
->sequential
;
304 orig_sequential
= sequential
;
307 case VM_BEHAVIOR_RANDOM
:
309 * reset indicator of sequential behavior
314 case VM_BEHAVIOR_SEQUENTIAL
:
315 if (offset
&& last_alloc
== offset
- PAGE_SIZE_64
) {
317 * advance indicator of sequential behavior
319 if (sequential
< MAX_SEQUENTIAL_RUN
)
320 sequential
+= PAGE_SIZE
;
323 * reset indicator of sequential behavior
329 case VM_BEHAVIOR_RSEQNTL
:
330 if (last_alloc
&& last_alloc
== offset
+ PAGE_SIZE_64
) {
332 * advance indicator of sequential behavior
334 if (sequential
> -MAX_SEQUENTIAL_RUN
)
335 sequential
-= PAGE_SIZE
;
338 * reset indicator of sequential behavior
344 case VM_BEHAVIOR_DEFAULT
:
346 if (offset
&& last_alloc
== (offset
- PAGE_SIZE_64
)) {
348 * advance indicator of sequential behavior
352 if (sequential
< MAX_SEQUENTIAL_RUN
)
353 sequential
+= PAGE_SIZE
;
355 } else if (last_alloc
&& last_alloc
== (offset
+ PAGE_SIZE_64
)) {
357 * advance indicator of sequential behavior
361 if (sequential
> -MAX_SEQUENTIAL_RUN
)
362 sequential
-= PAGE_SIZE
;
365 * reset indicator of sequential behavior
371 if (sequential
!= orig_sequential
) {
372 if (!OSCompareAndSwap(orig_sequential
, sequential
, (UInt32
*)&object
->sequential
)) {
374 * if someone else has already updated object->sequential
375 * don't bother trying to update it or object->last_alloc
381 * I'd like to do this with a OSCompareAndSwap64, but that
382 * doesn't exist for PPC... however, it shouldn't matter
383 * that much... last_alloc is maintained so that we can determine
384 * if a sequential access pattern is taking place... if only
385 * one thread is banging on this object, no problem with the unprotected
386 * update... if 2 or more threads are banging away, we run the risk of
387 * someone seeing a mangled update... however, in the face of multiple
388 * accesses, no sequential access pattern can develop anyway, so we
389 * haven't lost any real info.
391 object
->last_alloc
= offset
;
395 int vm_page_deactivate_behind_count
= 0;
398 * vm_page_deactivate_behind
400 * Determine if sequential access is in progress
401 * in accordance with the behavior specified. If
402 * so, compute a potential page to deactivate and
405 * object must be locked.
407 * return TRUE if we actually deactivate a page
411 vm_fault_deactivate_behind(
413 vm_object_offset_t offset
,
414 vm_behavior_t behavior
)
417 int pages_in_run
= 0;
418 int max_pages_in_run
= 0;
420 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
421 vm_object_offset_t run_offset
= 0;
422 vm_object_offset_t pg_offset
= 0;
424 vm_page_t page_run
[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
];
428 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_fault_deactivate_behind
); /* (TEST/DEBUG) */
431 if (object
== kernel_object
|| vm_page_deactivate_behind
== FALSE
) {
433 * Do not deactivate pages from the kernel object: they
434 * are not intended to become pageable.
435 * or we've disabled the deactivate behind mechanism
439 if ((sequential_run
= object
->sequential
)) {
440 if (sequential_run
< 0) {
441 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
442 sequential_run
= 0 - sequential_run
;
444 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
448 case VM_BEHAVIOR_RANDOM
:
450 case VM_BEHAVIOR_SEQUENTIAL
:
451 if (sequential_run
>= (int)PAGE_SIZE
) {
452 run_offset
= 0 - PAGE_SIZE_64
;
453 max_pages_in_run
= 1;
456 case VM_BEHAVIOR_RSEQNTL
:
457 if (sequential_run
>= (int)PAGE_SIZE
) {
458 run_offset
= PAGE_SIZE_64
;
459 max_pages_in_run
= 1;
462 case VM_BEHAVIOR_DEFAULT
:
464 { vm_object_offset_t behind
= vm_default_behind
* PAGE_SIZE_64
;
467 * determine if the run of sequential accesss has been
468 * long enough on an object with default access behavior
469 * to consider it for deactivation
471 if ((uint64_t)sequential_run
>= behind
&& (sequential_run
% (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
* PAGE_SIZE
)) == 0) {
473 * the comparisons between offset and behind are done
474 * in this kind of odd fashion in order to prevent wrap around
477 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
478 if (offset
>= behind
) {
479 run_offset
= 0 - behind
;
480 pg_offset
= PAGE_SIZE_64
;
481 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
484 if (offset
< -behind
) {
486 pg_offset
= 0 - PAGE_SIZE_64
;
487 max_pages_in_run
= VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER
;
494 for (n
= 0; n
< max_pages_in_run
; n
++) {
495 m
= vm_page_lookup(object
, offset
+ run_offset
+ (n
* pg_offset
));
497 if (m
&& !m
->busy
&& !m
->no_cache
&& !m
->throttled
&& !m
->fictitious
&& !m
->absent
) {
498 page_run
[pages_in_run
++] = m
;
499 pmap_clear_reference(m
->phys_page
);
503 vm_page_lockspin_queues();
505 for (n
= 0; n
< pages_in_run
; n
++) {
509 vm_page_deactivate_internal(m
, FALSE
);
511 vm_page_deactivate_behind_count
++;
513 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
516 vm_page_unlock_queues();
525 vm_page_throttled(void)
527 clock_sec_t elapsed_sec
;
529 clock_usec_t tv_usec
;
531 thread_t thread
= current_thread();
533 if (thread
->options
& TH_OPT_VMPRIV
)
536 thread
->t_page_creation_count
++;
538 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
541 if (vm_page_free_count
< vm_page_throttle_limit
&&
542 thread
->t_page_creation_count
> vm_page_creation_throttle
) {
544 clock_get_system_microtime(&tv_sec
, &tv_usec
);
546 elapsed_sec
= tv_sec
- thread
->t_page_creation_time
;
548 if (elapsed_sec
<= 6 || (thread
->t_page_creation_count
/ elapsed_sec
) >= (vm_page_creation_throttle
/ 6)) {
550 if (elapsed_sec
>= 60) {
552 * we'll reset our stats to give a well behaved app
553 * that was unlucky enough to accumulate a bunch of pages
554 * over a long period of time a chance to get out of
555 * the throttled state... we reset the counter and timestamp
556 * so that if it stays under the rate limit for the next second
557 * it will be back in our good graces... if it exceeds it, it
558 * will remain in the throttled state
560 thread
->t_page_creation_time
= tv_sec
;
561 thread
->t_page_creation_count
= (vm_page_creation_throttle
/ 6) * 5;
563 ++vm_page_throttle_count
;
567 thread
->t_page_creation_time
= tv_sec
;
568 thread
->t_page_creation_count
= 0;
575 * check for various conditions that would
576 * prevent us from creating a ZF page...
577 * cleanup is based on being called from vm_fault_page
579 * object must be locked
580 * object == m->object
582 static vm_fault_return_t
583 vm_fault_check(vm_object_t object
, vm_page_t m
, vm_page_t first_m
, boolean_t interruptible_state
)
585 if (object
->shadow_severed
||
586 VM_OBJECT_PURGEABLE_FAULT_ERROR(object
)) {
589 * 1. the shadow chain was severed,
590 * 2. the purgeable object is volatile or empty and is marked
591 * to fault on access while volatile.
592 * Just have to return an error at this point
594 if (m
!= VM_PAGE_NULL
)
596 vm_fault_cleanup(object
, first_m
);
598 thread_interrupt_level(interruptible_state
);
600 return (VM_FAULT_MEMORY_ERROR
);
602 if (vm_backing_store_low
) {
604 * are we protecting the system from
605 * backing store exhaustion. If so
606 * sleep unless we are privileged.
608 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
610 if (m
!= VM_PAGE_NULL
)
612 vm_fault_cleanup(object
, first_m
);
614 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
616 thread_block(THREAD_CONTINUE_NULL
);
617 thread_interrupt_level(interruptible_state
);
619 return (VM_FAULT_RETRY
);
622 if (vm_page_throttled()) {
624 * we're throttling zero-fills...
625 * treat this as if we couldn't grab a page
627 if (m
!= VM_PAGE_NULL
)
629 vm_fault_cleanup(object
, first_m
);
631 if (NEED_TO_HARD_THROTTLE_THIS_TASK()) {
632 delay(HARD_THROTTLE_DELAY
);
634 if (current_thread_aborted()) {
635 thread_interrupt_level(interruptible_state
);
636 return VM_FAULT_INTERRUPTED
;
640 thread_interrupt_level(interruptible_state
);
642 return (VM_FAULT_MEMORY_SHORTAGE
);
644 return (VM_FAULT_SUCCESS
);
649 * do the work to zero fill a page and
650 * inject it into the correct paging queue
652 * m->object must be locked
653 * page queue lock must NOT be held
656 vm_fault_zero_page(vm_page_t m
, boolean_t no_zero_fill
)
658 int my_fault
= DBG_ZERO_FILL_FAULT
;
661 * This is is a zero-fill page fault...
663 * Checking the page lock is a waste of
664 * time; this page was absent, so
665 * it can't be page locked by a pager.
667 * we also consider it undefined
668 * with respect to instruction
669 * execution. i.e. it is the responsibility
670 * of higher layers to call for an instruction
671 * sync after changing the contents and before
672 * sending a program into this area. We
673 * choose this approach for performance
677 m
->cs_validated
= FALSE
;
678 m
->cs_tainted
= FALSE
;
680 if (no_zero_fill
== TRUE
)
681 my_fault
= DBG_NZF_PAGE_FAULT
;
683 vm_page_zero_fill(m
);
685 VM_STAT_INCR(zero_fill_count
);
686 DTRACE_VM2(zfod
, int, 1, (uint64_t *), NULL
);
689 assert(m
->object
!= kernel_object
);
690 //assert(m->pageq.next == NULL && m->pageq.prev == NULL);
692 if (!IP_VALID(memory_manager_default
) &&
693 (m
->object
->purgable
== VM_PURGABLE_DENY
||
694 m
->object
->purgable
== VM_PURGABLE_NONVOLATILE
||
695 m
->object
->purgable
== VM_PURGABLE_VOLATILE
)) {
696 vm_page_lockspin_queues();
698 queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
700 vm_page_throttled_count
++;
702 vm_page_unlock_queues();
704 if (current_thread()->t_page_creation_count
> vm_page_creation_throttle
) {
714 * Routine: vm_fault_page
716 * Find the resident page for the virtual memory
717 * specified by the given virtual memory object
719 * Additional arguments:
720 * The required permissions for the page is given
721 * in "fault_type". Desired permissions are included
723 * fault_info is passed along to determine pagein cluster
724 * limits... it contains the expected reference pattern,
725 * cluster size if available, etc...
727 * If the desired page is known to be resident (for
728 * example, because it was previously wired down), asserting
729 * the "unwiring" parameter will speed the search.
731 * If the operation can be interrupted (by thread_abort
732 * or thread_terminate), then the "interruptible"
733 * parameter should be asserted.
736 * The page containing the proper data is returned
740 * The source object must be locked and referenced,
741 * and must donate one paging reference. The reference
742 * is not affected. The paging reference and lock are
745 * If the call succeeds, the object in which "result_page"
746 * resides is left locked and holding a paging reference.
747 * If this is not the original object, a busy page in the
748 * original object is returned in "top_page", to prevent other
749 * callers from pursuing this same data, along with a paging
750 * reference for the original object. The "top_page" should
751 * be destroyed when this guarantee is no longer required.
752 * The "result_page" is also left busy. It is not removed
753 * from the pageout queues.
755 * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the
756 * fault succeeded but there's no VM page (i.e. the VM object
757 * does not actually hold VM pages, but device memory or
758 * large pages). The object is still locked and we still hold a
759 * paging_in_progress reference.
761 unsigned int vm_fault_page_blocked_access
= 0;
766 vm_object_t first_object
, /* Object to begin search */
767 vm_object_offset_t first_offset
, /* Offset into object */
768 vm_prot_t fault_type
, /* What access is requested */
769 boolean_t must_be_resident
,/* Must page be resident? */
770 /* Modifies in place: */
771 vm_prot_t
*protection
, /* Protection for mapping */
773 vm_page_t
*result_page
, /* Page found, if successful */
774 vm_page_t
*top_page
, /* Page in top object, if
775 * not result_page. */
776 int *type_of_fault
, /* if non-null, fill in with type of fault
777 * COW, zero-fill, etc... returned in trace point */
778 /* More arguments: */
779 kern_return_t
*error_code
, /* code if page is in error */
780 boolean_t no_zero_fill
, /* don't zero fill absent pages */
782 boolean_t data_supply
, /* treat as data_supply if
783 * it is a write fault and a full
784 * page is provided */
786 __unused boolean_t data_supply
,
788 vm_object_fault_info_t fault_info
)
792 vm_object_offset_t offset
;
794 vm_object_t next_object
;
795 vm_object_t copy_object
;
796 boolean_t look_for_page
;
797 vm_prot_t access_required
= fault_type
;
798 vm_prot_t wants_copy_flag
;
799 CLUSTER_STAT(int pages_at_higher_offsets
;)
800 CLUSTER_STAT(int pages_at_lower_offsets
;)
801 kern_return_t wait_result
;
802 boolean_t interruptible_state
;
803 vm_fault_return_t error
;
805 uint32_t try_failed_count
;
806 int interruptible
; /* how may fault be interrupted? */
807 memory_object_t pager
;
808 vm_fault_return_t retval
;
811 * MACH page map - an optional optimization where a bit map is maintained
812 * by the VM subsystem for internal objects to indicate which pages of
813 * the object currently reside on backing store. This existence map
814 * duplicates information maintained by the vnode pager. It is
815 * created at the time of the first pageout against the object, i.e.
816 * at the same time pager for the object is created. The optimization
817 * is designed to eliminate pager interaction overhead, if it is
818 * 'known' that the page does not exist on backing store.
820 * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is
821 * either marked as paged out in the existence map for the object or no
822 * existence map exists for the object. MUST_ASK_PAGER() is one of the
823 * criteria in the decision to invoke the pager. It is also used as one
824 * of the criteria to terminate the scan for adjacent pages in a clustered
825 * pagein operation. Note that MUST_ASK_PAGER() always evaluates to TRUE for
826 * permanent objects. Note also that if the pager for an internal object
827 * has not been created, the pager is not invoked regardless of the value
828 * of MUST_ASK_PAGER() and that clustered pagein scans are only done on an object
829 * for which a pager has been created.
831 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
832 * is marked as paged out in the existence map for the object. PAGED_OUT()
833 * PAGED_OUT() is used to determine if a page has already been pushed
834 * into a copy object in order to avoid a redundant page out operation.
837 #define MUST_ASK_PAGER(o, f) (vm_external_state_get((o)->existence_map, (f)) \
838 != VM_EXTERNAL_STATE_ABSENT)
839 #define PAGED_OUT(o, f) (vm_external_state_get((o)->existence_map, (f)) \
840 == VM_EXTERNAL_STATE_EXISTS)
842 #define MUST_ASK_PAGER(o, f) (TRUE)
843 #define PAGED_OUT(o, f) (FALSE)
849 #define RELEASE_PAGE(m) \
851 PAGE_WAKEUP_DONE(m); \
852 if (!m->active && !m->inactive && !m->throttled) { \
853 vm_page_lockspin_queues(); \
854 if (!m->active && !m->inactive && !m->throttled) \
855 vm_page_activate(m); \
856 vm_page_unlock_queues(); \
861 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
867 * If there are watchpoints set, then
868 * we don't want to give away write permission
869 * on a read fault. Make the task write fault,
870 * so that the watchpoint code notices the access.
872 if (db_watchpoint_list
) {
874 * If we aren't asking for write permission,
875 * then don't give it away. We're using write
876 * faults to set the dirty bit.
878 if (!(fault_type
& VM_PROT_WRITE
))
879 *protection
&= ~VM_PROT_WRITE
;
881 #endif /* MACH_KDB */
883 interruptible
= fault_info
->interruptible
;
884 interruptible_state
= thread_interrupt_level(interruptible
);
887 * INVARIANTS (through entire routine):
889 * 1) At all times, we must either have the object
890 * lock or a busy page in some object to prevent
891 * some other thread from trying to bring in
894 * Note that we cannot hold any locks during the
895 * pager access or when waiting for memory, so
896 * we use a busy page then.
898 * 2) To prevent another thread from racing us down the
899 * shadow chain and entering a new page in the top
900 * object before we do, we must keep a busy page in
901 * the top object while following the shadow chain.
903 * 3) We must increment paging_in_progress on any object
904 * for which we have a busy page before dropping
907 * 4) We leave busy pages on the pageout queues.
908 * If the pageout daemon comes across a busy page,
909 * it will remove the page from the pageout queues.
912 object
= first_object
;
913 offset
= first_offset
;
914 first_m
= VM_PAGE_NULL
;
915 access_required
= fault_type
;
919 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
920 object
, offset
, fault_type
, *protection
, 0);
923 * default type of fault
925 my_fault
= DBG_CACHE_HIT_FAULT
;
929 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
931 if (!object
->alive
) {
933 * object is no longer valid
934 * clean up and return error
936 vm_fault_cleanup(object
, first_m
);
937 thread_interrupt_level(interruptible_state
);
939 return (VM_FAULT_MEMORY_ERROR
);
942 if (!object
->pager_created
&& object
->phys_contiguous
) {
944 * A physically-contiguous object without a pager:
945 * must be a "large page" object. We do not deal
946 * with VM pages for this object.
949 goto phys_contig_object
;
952 if (object
->blocked_access
) {
954 * Access to this VM object has been blocked.
955 * Replace our "paging_in_progress" reference with
956 * a "activity_in_progress" reference and wait for
957 * access to be unblocked.
959 vm_object_activity_begin(object
);
960 vm_object_paging_end(object
);
961 while (object
->blocked_access
) {
962 vm_object_sleep(object
,
963 VM_OBJECT_EVENT_UNBLOCKED
,
966 vm_fault_page_blocked_access
++;
967 vm_object_paging_begin(object
);
968 vm_object_activity_end(object
);
972 * See whether the page at 'offset' is resident
974 m
= vm_page_lookup(object
, offset
);
976 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
978 if (m
!= VM_PAGE_NULL
) {
982 * The page is being brought in,
983 * wait for it and then retry.
985 * A possible optimization: if the page
986 * is known to be resident, we can ignore
987 * pages that are absent (regardless of
988 * whether they're busy).
991 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
993 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
995 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
998 counter(c_vm_fault_page_block_busy_kernel
++);
1000 if (wait_result
!= THREAD_AWAKENED
) {
1001 vm_fault_cleanup(object
, first_m
);
1002 thread_interrupt_level(interruptible_state
);
1004 if (wait_result
== THREAD_RESTART
)
1005 return (VM_FAULT_RETRY
);
1007 return (VM_FAULT_INTERRUPTED
);
1012 if (m
->phys_page
== vm_page_guard_addr
) {
1014 * Guard page: off limits !
1016 if (fault_type
== VM_PROT_NONE
) {
1018 * The fault is not requesting any
1019 * access to the guard page, so it must
1020 * be just to wire or unwire it.
1021 * Let's pretend it succeeded...
1025 assert(first_m
== VM_PAGE_NULL
);
1026 *top_page
= first_m
;
1028 *type_of_fault
= DBG_GUARD_FAULT
;
1029 return VM_FAULT_SUCCESS
;
1032 * The fault requests access to the
1033 * guard page: let's deny that !
1035 vm_fault_cleanup(object
, first_m
);
1036 thread_interrupt_level(interruptible_state
);
1037 return VM_FAULT_MEMORY_ERROR
;
1043 * The page is in error, give up now.
1046 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
1049 *error_code
= KERN_MEMORY_ERROR
;
1052 vm_fault_cleanup(object
, first_m
);
1053 thread_interrupt_level(interruptible_state
);
1055 return (VM_FAULT_MEMORY_ERROR
);
1059 * The pager wants us to restart
1060 * at the top of the chain,
1061 * typically because it has moved the
1062 * page to another pager, then do so.
1065 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1069 vm_fault_cleanup(object
, first_m
);
1070 thread_interrupt_level(interruptible_state
);
1072 return (VM_FAULT_RETRY
);
1076 * The page isn't busy, but is absent,
1077 * therefore it's deemed "unavailable".
1079 * Remove the non-existent page (unless it's
1080 * in the top object) and move on down to the
1081 * next object (if there is one).
1084 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
1086 next_object
= object
->shadow
;
1088 if (next_object
== VM_OBJECT_NULL
) {
1090 * Absent page at bottom of shadow
1091 * chain; zero fill the page we left
1092 * busy in the first object, and free
1095 assert(!must_be_resident
);
1098 * check for any conditions that prevent
1099 * us from creating a new zero-fill page
1100 * vm_fault_check will do all of the
1101 * fault cleanup in the case of an error condition
1102 * including resetting the thread_interrupt_level
1104 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
1106 if (error
!= VM_FAULT_SUCCESS
)
1110 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
1115 if (object
!= first_object
) {
1117 * free the absent page we just found
1122 * drop reference and lock on current object
1124 vm_object_paging_end(object
);
1125 vm_object_unlock(object
);
1128 * grab the original page we
1129 * 'soldered' in place and
1130 * retake lock on 'first_object'
1133 first_m
= VM_PAGE_NULL
;
1135 object
= first_object
;
1136 offset
= first_offset
;
1138 vm_object_lock(object
);
1141 * we're going to use the absent page we just found
1142 * so convert it to a 'busy' page
1148 * zero-fill the page and put it on
1149 * the correct paging queue
1151 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1153 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1157 if (must_be_resident
)
1158 vm_object_paging_end(object
);
1159 else if (object
!= first_object
) {
1160 vm_object_paging_end(object
);
1167 vm_page_lockspin_queues();
1168 VM_PAGE_QUEUES_REMOVE(m
);
1169 vm_page_unlock_queues();
1172 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1175 offset
+object
->shadow_offset
,0);
1177 offset
+= object
->shadow_offset
;
1178 fault_info
->lo_offset
+= object
->shadow_offset
;
1179 fault_info
->hi_offset
+= object
->shadow_offset
;
1180 access_required
= VM_PROT_READ
;
1182 vm_object_lock(next_object
);
1183 vm_object_unlock(object
);
1184 object
= next_object
;
1185 vm_object_paging_begin(object
);
1188 * reset to default type of fault
1190 my_fault
= DBG_CACHE_HIT_FAULT
;
1196 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1197 && (fault_type
& VM_PROT_WRITE
)) {
1199 * This is a copy-on-write fault that will
1200 * cause us to revoke access to this page, but
1201 * this page is in the process of being cleaned
1202 * in a clustered pageout. We must wait until
1203 * the cleaning operation completes before
1204 * revoking access to the original page,
1205 * otherwise we might attempt to remove a
1209 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1212 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1216 * take an extra ref so that object won't die
1218 vm_object_reference_locked(object
);
1220 vm_fault_cleanup(object
, first_m
);
1222 counter(c_vm_fault_page_block_backoff_kernel
++);
1223 vm_object_lock(object
);
1224 assert(object
->ref_count
> 0);
1226 m
= vm_page_lookup(object
, offset
);
1228 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
1229 PAGE_ASSERT_WAIT(m
, interruptible
);
1231 vm_object_unlock(object
);
1232 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1233 vm_object_deallocate(object
);
1237 vm_object_unlock(object
);
1239 vm_object_deallocate(object
);
1240 thread_interrupt_level(interruptible_state
);
1242 return (VM_FAULT_RETRY
);
1245 if (type_of_fault
== NULL
&& m
->speculative
&&
1246 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1248 * If we were passed a non-NULL pointer for
1249 * "type_of_fault", than we came from
1250 * vm_fault... we'll let it deal with
1251 * this condition, since it
1252 * needs to see m->speculative to correctly
1253 * account the pageins, otherwise...
1254 * take it off the speculative queue, we'll
1255 * let the caller of vm_fault_page deal
1256 * with getting it onto the correct queue
1258 * If the caller specified in fault_info that
1259 * it wants a "stealth" fault, we also leave
1260 * the page in the speculative queue.
1262 vm_page_lockspin_queues();
1263 VM_PAGE_QUEUES_REMOVE(m
);
1264 vm_page_unlock_queues();
1270 * the user needs access to a page that we
1271 * encrypted before paging it out.
1272 * Decrypt the page now.
1273 * Keep it busy to prevent anyone from
1274 * accessing it during the decryption.
1277 vm_page_decrypt(m
, 0);
1278 assert(object
== m
->object
);
1280 PAGE_WAKEUP_DONE(m
);
1283 * Retry from the top, in case
1284 * something changed while we were
1289 ASSERT_PAGE_DECRYPTED(m
);
1291 if (m
->object
->code_signed
) {
1294 * We just paged in a page from a signed
1295 * memory object but we don't need to
1296 * validate it now. We'll validate it if
1297 * when it gets mapped into a user address
1298 * space for the first time or when the page
1299 * gets copied to another object as a result
1300 * of a copy-on-write.
1305 * We mark the page busy and leave it on
1306 * the pageout queues. If the pageout
1307 * deamon comes across it, then it will
1308 * remove the page from the queue, but not the object
1311 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1314 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1315 object
, offset
, m
, 0, 0);
1325 * we get here when there is no page present in the object at
1326 * the offset we're interested in... we'll allocate a page
1327 * at this point if the pager associated with
1328 * this object can provide the data or we're the top object...
1329 * object is locked; m == NULL
1331 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
) == TRUE
) && !data_supply
);
1334 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1336 if ((look_for_page
|| (object
== first_object
)) && !must_be_resident
&& !object
->phys_contiguous
) {
1338 * Allocate a new page for this object/offset pair
1342 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1344 if (m
== VM_PAGE_NULL
) {
1346 vm_fault_cleanup(object
, first_m
);
1347 thread_interrupt_level(interruptible_state
);
1349 return (VM_FAULT_MEMORY_SHORTAGE
);
1351 vm_page_insert(m
, object
, offset
);
1353 if (look_for_page
&& !must_be_resident
) {
1357 * If the memory manager is not ready, we
1358 * cannot make requests.
1360 if (!object
->pager_ready
) {
1362 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1364 if (m
!= VM_PAGE_NULL
)
1368 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1369 object
, offset
, 0, 0, 0);
1372 * take an extra ref so object won't die
1374 vm_object_reference_locked(object
);
1375 vm_fault_cleanup(object
, first_m
);
1376 counter(c_vm_fault_page_block_backoff_kernel
++);
1378 vm_object_lock(object
);
1379 assert(object
->ref_count
> 0);
1381 if (!object
->pager_ready
) {
1382 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1384 vm_object_unlock(object
);
1385 if (wait_result
== THREAD_WAITING
)
1386 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1387 vm_object_deallocate(object
);
1391 vm_object_unlock(object
);
1392 vm_object_deallocate(object
);
1393 thread_interrupt_level(interruptible_state
);
1395 return (VM_FAULT_RETRY
);
1398 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1400 * If there are too many outstanding page
1401 * requests pending on this external object, we
1402 * wait for them to be resolved now.
1405 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1407 if (m
!= VM_PAGE_NULL
)
1410 * take an extra ref so object won't die
1412 vm_object_reference_locked(object
);
1414 vm_fault_cleanup(object
, first_m
);
1416 counter(c_vm_fault_page_block_backoff_kernel
++);
1418 vm_object_lock(object
);
1419 assert(object
->ref_count
> 0);
1421 if (object
->paging_in_progress
> vm_object_pagein_throttle
) {
1422 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
, interruptible
);
1424 vm_object_unlock(object
);
1425 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1426 vm_object_deallocate(object
);
1430 vm_object_unlock(object
);
1431 vm_object_deallocate(object
);
1432 thread_interrupt_level(interruptible_state
);
1434 return (VM_FAULT_RETRY
);
1437 if (m
!= VM_PAGE_NULL
) {
1439 * Indicate that the page is waiting for data
1440 * from the memory manager.
1442 m
->list_req_pending
= TRUE
;
1447 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1451 * It's possible someone called vm_object_destroy while we weren't
1452 * holding the object lock. If that has happened, then bail out
1456 pager
= object
->pager
;
1458 if (pager
== MEMORY_OBJECT_NULL
) {
1459 vm_fault_cleanup(object
, first_m
);
1460 thread_interrupt_level(interruptible_state
);
1461 return VM_FAULT_MEMORY_ERROR
;
1465 * We have an absent page in place for the faulting offset,
1466 * so we can release the object lock.
1469 vm_object_unlock(object
);
1472 * If this object uses a copy_call strategy,
1473 * and we are interested in a copy of this object
1474 * (having gotten here only by following a
1475 * shadow chain), then tell the memory manager
1476 * via a flag added to the desired_access
1477 * parameter, so that it can detect a race
1478 * between our walking down the shadow chain
1479 * and its pushing pages up into a copy of
1480 * the object that it manages.
1482 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
)
1483 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1485 wants_copy_flag
= VM_PROT_NONE
;
1488 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1490 access_required
| wants_copy_flag
, 0);
1493 * Call the memory manager to retrieve the data.
1495 rc
= memory_object_data_request(
1497 offset
+ object
->paging_offset
,
1499 access_required
| wants_copy_flag
,
1500 (memory_object_fault_info_t
)fault_info
);
1503 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1505 vm_object_lock(object
);
1507 if (rc
!= KERN_SUCCESS
) {
1509 vm_fault_cleanup(object
, first_m
);
1510 thread_interrupt_level(interruptible_state
);
1512 return ((rc
== MACH_SEND_INTERRUPTED
) ?
1513 VM_FAULT_INTERRUPTED
:
1514 VM_FAULT_MEMORY_ERROR
);
1517 clock_usec_t tv_usec
;
1519 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1520 current_thread()->t_page_creation_time
= tv_sec
;
1521 current_thread()->t_page_creation_count
= 0;
1523 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_mode
& TH_MODE_ABORT
)) {
1525 vm_fault_cleanup(object
, first_m
);
1526 thread_interrupt_level(interruptible_state
);
1528 return (VM_FAULT_INTERRUPTED
);
1530 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1532 * No page here means that the object we
1533 * initially looked up was "physically
1534 * contiguous" (i.e. device memory). However,
1535 * with Virtual VRAM, the object might not
1536 * be backed by that device memory anymore,
1537 * so we're done here only if the object is
1538 * still "phys_contiguous".
1539 * Otherwise, if the object is no longer
1540 * "phys_contiguous", we need to retry the
1541 * page fault against the object's new backing
1542 * store (different memory object).
1548 * potentially a pagein fault
1549 * if we make it through the state checks
1550 * above, than we'll count it as such
1552 my_fault
= DBG_PAGEIN_FAULT
;
1555 * Retry with same object/offset, since new data may
1556 * be in a different page (i.e., m is meaningless at
1563 * We get here if the object has no pager, or an existence map
1564 * exists and indicates the page isn't present on the pager
1565 * or we're unwiring a page. If a pager exists, but there
1566 * is no existence map, then the m->absent case above handles
1567 * the ZF case when the pager can't provide the page
1570 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1572 if (object
== first_object
)
1575 assert(m
== VM_PAGE_NULL
);
1578 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1582 next_object
= object
->shadow
;
1584 if (next_object
== VM_OBJECT_NULL
) {
1586 * we've hit the bottom of the shadown chain,
1587 * fill the page in the top object with zeros.
1589 assert(!must_be_resident
);
1591 if (object
!= first_object
) {
1592 vm_object_paging_end(object
);
1593 vm_object_unlock(object
);
1595 object
= first_object
;
1596 offset
= first_offset
;
1597 vm_object_lock(object
);
1600 assert(m
->object
== object
);
1601 first_m
= VM_PAGE_NULL
;
1604 * check for any conditions that prevent
1605 * us from creating a new zero-fill page
1606 * vm_fault_check will do all of the
1607 * fault cleanup in the case of an error condition
1608 * including resetting the thread_interrupt_level
1610 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
1612 if (error
!= VM_FAULT_SUCCESS
)
1615 if (m
== VM_PAGE_NULL
) {
1618 if (m
== VM_PAGE_NULL
) {
1619 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1620 thread_interrupt_level(interruptible_state
);
1622 return (VM_FAULT_MEMORY_SHORTAGE
);
1624 vm_page_insert(m
, object
, offset
);
1626 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1628 if (fault_info
->mark_zf_absent
&& no_zero_fill
== TRUE
)
1634 * Move on to the next object. Lock the next
1635 * object before unlocking the current one.
1637 if ((object
!= first_object
) || must_be_resident
)
1638 vm_object_paging_end(object
);
1640 offset
+= object
->shadow_offset
;
1641 fault_info
->lo_offset
+= object
->shadow_offset
;
1642 fault_info
->hi_offset
+= object
->shadow_offset
;
1643 access_required
= VM_PROT_READ
;
1645 vm_object_lock(next_object
);
1646 vm_object_unlock(object
);
1648 object
= next_object
;
1649 vm_object_paging_begin(object
);
1654 * PAGE HAS BEEN FOUND.
1657 * busy, so that we can play with it;
1658 * not absent, so that nobody else will fill it;
1659 * possibly eligible for pageout;
1661 * The top-level page (first_m) is:
1662 * VM_PAGE_NULL if the page was found in the
1664 * busy, not absent, and ineligible for pageout.
1666 * The current object (object) is locked. A paging
1667 * reference is held for the current and top-level
1672 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1674 #if EXTRA_ASSERTIONS
1675 assert(m
->busy
&& !m
->absent
);
1676 assert((first_m
== VM_PAGE_NULL
) ||
1677 (first_m
->busy
&& !first_m
->absent
&&
1678 !first_m
->active
&& !first_m
->inactive
));
1679 #endif /* EXTRA_ASSERTIONS */
1683 * If we found a page, we must have decrypted it before we
1686 ASSERT_PAGE_DECRYPTED(m
);
1689 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1691 first_object
, first_m
);
1694 * If the page is being written, but isn't
1695 * already owned by the top-level object,
1696 * we have to copy it into a new page owned
1697 * by the top-level object.
1699 if (object
!= first_object
) {
1702 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1704 if (fault_type
& VM_PROT_WRITE
) {
1708 * We only really need to copy if we
1711 assert(!must_be_resident
);
1714 * are we protecting the system from
1715 * backing store exhaustion. If so
1716 * sleep unless we are privileged.
1718 if (vm_backing_store_low
) {
1719 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1722 vm_fault_cleanup(object
, first_m
);
1724 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1726 thread_block(THREAD_CONTINUE_NULL
);
1727 thread_interrupt_level(interruptible_state
);
1729 return (VM_FAULT_RETRY
);
1733 * If we try to collapse first_object at this
1734 * point, we may deadlock when we try to get
1735 * the lock on an intermediate object (since we
1736 * have the bottom object locked). We can't
1737 * unlock the bottom object, because the page
1738 * we found may move (by collapse) if we do.
1740 * Instead, we first copy the page. Then, when
1741 * we have no more use for the bottom object,
1742 * we unlock it and try to collapse.
1744 * Note that we copy the page even if we didn't
1745 * need to... that's the breaks.
1749 * Allocate a page for the copy
1751 copy_m
= vm_page_grab();
1753 if (copy_m
== VM_PAGE_NULL
) {
1756 vm_fault_cleanup(object
, first_m
);
1757 thread_interrupt_level(interruptible_state
);
1759 return (VM_FAULT_MEMORY_SHORTAGE
);
1762 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
1766 vm_page_copy(m
, copy_m
);
1769 * If another map is truly sharing this
1770 * page with us, we have to flush all
1771 * uses of the original page, since we
1772 * can't distinguish those which want the
1773 * original from those which need the
1776 * XXXO If we know that only one map has
1777 * access to this page, then we could
1778 * avoid the pmap_disconnect() call.
1781 pmap_disconnect(m
->phys_page
);
1783 assert(!m
->cleaning
);
1786 * We no longer need the old page or object.
1788 PAGE_WAKEUP_DONE(m
);
1789 vm_object_paging_end(object
);
1790 vm_object_unlock(object
);
1792 my_fault
= DBG_COW_FAULT
;
1793 VM_STAT_INCR(cow_faults
);
1794 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
1795 current_task()->cow_faults
++;
1797 object
= first_object
;
1798 offset
= first_offset
;
1800 vm_object_lock(object
);
1802 * get rid of the place holder
1803 * page that we soldered in earlier
1805 VM_PAGE_FREE(first_m
);
1806 first_m
= VM_PAGE_NULL
;
1809 * and replace it with the
1810 * page we just copied into
1812 assert(copy_m
->busy
);
1813 vm_page_insert(copy_m
, object
, offset
);
1814 copy_m
->dirty
= TRUE
;
1818 * Now that we've gotten the copy out of the
1819 * way, let's try to collapse the top object.
1820 * But we have to play ugly games with
1821 * paging_in_progress to do that...
1823 vm_object_paging_end(object
);
1824 vm_object_collapse(object
, offset
, TRUE
);
1825 vm_object_paging_begin(object
);
1828 *protection
&= (~VM_PROT_WRITE
);
1831 * Now check whether the page needs to be pushed into the
1832 * copy object. The use of asymmetric copy on write for
1833 * shared temporary objects means that we may do two copies to
1834 * satisfy the fault; one above to get the page from a
1835 * shadowed object, and one here to push it into the copy.
1837 try_failed_count
= 0;
1839 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
1840 vm_object_offset_t copy_offset
;
1844 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1847 * If the page is being written, but hasn't been
1848 * copied to the copy-object, we have to copy it there.
1850 if ((fault_type
& VM_PROT_WRITE
) == 0) {
1851 *protection
&= ~VM_PROT_WRITE
;
1856 * If the page was guaranteed to be resident,
1857 * we must have already performed the copy.
1859 if (must_be_resident
)
1863 * Try to get the lock on the copy_object.
1865 if (!vm_object_lock_try(copy_object
)) {
1867 vm_object_unlock(object
);
1870 mutex_pause(try_failed_count
); /* wait a bit */
1871 vm_object_lock(object
);
1875 try_failed_count
= 0;
1878 * Make another reference to the copy-object,
1879 * to keep it from disappearing during the
1882 vm_object_reference_locked(copy_object
);
1885 * Does the page exist in the copy?
1887 copy_offset
= first_offset
- copy_object
->shadow_offset
;
1889 if (copy_object
->size
<= copy_offset
)
1891 * Copy object doesn't cover this page -- do nothing.
1894 else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
1896 * Page currently exists in the copy object
1900 * If the page is being brought
1901 * in, wait for it and then retry.
1906 * take an extra ref so object won't die
1908 vm_object_reference_locked(copy_object
);
1909 vm_object_unlock(copy_object
);
1910 vm_fault_cleanup(object
, first_m
);
1911 counter(c_vm_fault_page_block_backoff_kernel
++);
1913 vm_object_lock(copy_object
);
1914 assert(copy_object
->ref_count
> 0);
1915 VM_OBJ_RES_DECR(copy_object
);
1916 vm_object_lock_assert_exclusive(copy_object
);
1917 copy_object
->ref_count
--;
1918 assert(copy_object
->ref_count
> 0);
1919 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
1922 * it's OK if the "copy_m" page is encrypted,
1923 * because we're not moving it nor handling its
1926 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
1927 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
1929 vm_object_unlock(copy_object
);
1930 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1931 vm_object_deallocate(copy_object
);
1935 vm_object_unlock(copy_object
);
1936 vm_object_deallocate(copy_object
);
1937 thread_interrupt_level(interruptible_state
);
1939 return (VM_FAULT_RETRY
);
1943 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
1945 * If PAGED_OUT is TRUE, then the page used to exist
1946 * in the copy-object, and has already been paged out.
1947 * We don't need to repeat this. If PAGED_OUT is
1948 * FALSE, then either we don't know (!pager_created,
1949 * for example) or it hasn't been paged out.
1950 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
1951 * We must copy the page to the copy object.
1954 if (vm_backing_store_low
) {
1956 * we are protecting the system from
1957 * backing store exhaustion. If so
1958 * sleep unless we are privileged.
1960 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1961 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1964 VM_OBJ_RES_DECR(copy_object
);
1965 vm_object_lock_assert_exclusive(copy_object
);
1966 copy_object
->ref_count
--;
1967 assert(copy_object
->ref_count
> 0);
1969 vm_object_unlock(copy_object
);
1970 vm_fault_cleanup(object
, first_m
);
1971 thread_block(THREAD_CONTINUE_NULL
);
1972 thread_interrupt_level(interruptible_state
);
1974 return (VM_FAULT_RETRY
);
1978 * Allocate a page for the copy
1980 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
1982 if (copy_m
== VM_PAGE_NULL
) {
1985 VM_OBJ_RES_DECR(copy_object
);
1986 vm_object_lock_assert_exclusive(copy_object
);
1987 copy_object
->ref_count
--;
1988 assert(copy_object
->ref_count
> 0);
1990 vm_object_unlock(copy_object
);
1991 vm_fault_cleanup(object
, first_m
);
1992 thread_interrupt_level(interruptible_state
);
1994 return (VM_FAULT_MEMORY_SHORTAGE
);
1997 * Must copy page into copy-object.
1999 vm_page_copy(m
, copy_m
);
2002 * If the old page was in use by any users
2003 * of the copy-object, it must be removed
2004 * from all pmaps. (We can't know which
2008 pmap_disconnect(m
->phys_page
);
2011 * If there's a pager, then immediately
2012 * page out this page, using the "initialize"
2013 * option. Else, we use the copy.
2015 if ((!copy_object
->pager_created
)
2017 || vm_external_state_get(copy_object
->existence_map
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2021 vm_page_lockspin_queues();
2022 assert(!m
->cleaning
);
2023 vm_page_activate(copy_m
);
2024 vm_page_unlock_queues();
2026 copy_m
->dirty
= TRUE
;
2027 PAGE_WAKEUP_DONE(copy_m
);
2030 assert(copy_m
->busy
== TRUE
);
2031 assert(!m
->cleaning
);
2034 * dirty is protected by the object lock
2036 copy_m
->dirty
= TRUE
;
2039 * The page is already ready for pageout:
2040 * not on pageout queues and busy.
2041 * Unlock everything except the
2042 * copy_object itself.
2044 vm_object_unlock(object
);
2047 * Write the page to the copy-object,
2048 * flushing it from the kernel.
2050 vm_pageout_initialize_page(copy_m
);
2053 * Since the pageout may have
2054 * temporarily dropped the
2055 * copy_object's lock, we
2056 * check whether we'll have
2057 * to deallocate the hard way.
2059 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2060 vm_object_unlock(copy_object
);
2061 vm_object_deallocate(copy_object
);
2062 vm_object_lock(object
);
2067 * Pick back up the old object's
2068 * lock. [It is safe to do so,
2069 * since it must be deeper in the
2072 vm_object_lock(object
);
2075 * Because we're pushing a page upward
2076 * in the object tree, we must restart
2077 * any faults that are waiting here.
2078 * [Note that this is an expansion of
2079 * PAGE_WAKEUP that uses the THREAD_RESTART
2080 * wait result]. Can't turn off the page's
2081 * busy bit because we're not done with it.
2085 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2089 * The reference count on copy_object must be
2090 * at least 2: one for our extra reference,
2091 * and at least one from the outside world
2092 * (we checked that when we last locked
2095 vm_object_lock_assert_exclusive(copy_object
);
2096 copy_object
->ref_count
--;
2097 assert(copy_object
->ref_count
> 0);
2099 VM_OBJ_RES_DECR(copy_object
);
2100 vm_object_unlock(copy_object
);
2107 *top_page
= first_m
;
2110 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2111 object
, offset
, m
, first_m
, 0);
2113 if (m
!= VM_PAGE_NULL
) {
2114 retval
= VM_FAULT_SUCCESS
;
2115 if (my_fault
== DBG_PAGEIN_FAULT
) {
2117 VM_STAT_INCR(pageins
);
2118 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
2119 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
2120 current_task()->pageins
++;
2122 if (m
->object
->internal
) {
2123 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
2124 my_fault
= DBG_PAGEIND_FAULT
;
2126 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
2127 my_fault
= DBG_PAGEINV_FAULT
;
2131 * evaluate access pattern and update state
2132 * vm_fault_deactivate_behind depends on the
2133 * state being up to date
2135 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2137 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2140 *type_of_fault
= my_fault
;
2142 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2143 assert(first_m
== VM_PAGE_NULL
);
2144 assert(object
== first_object
);
2147 thread_interrupt_level(interruptible_state
);
2150 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2155 thread_interrupt_level(interruptible_state
);
2157 if (wait_result
== THREAD_INTERRUPTED
)
2158 return (VM_FAULT_INTERRUPTED
);
2159 return (VM_FAULT_RETRY
);
2168 * When soft faulting a page, we have to validate the page if:
2169 * 1. the page is being mapped in user space
2170 * 2. the page hasn't already been found to be "tainted"
2171 * 3. the page belongs to a code-signed object
2172 * 4. the page has not been validated yet or has been mapped for write.
2174 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page) \
2175 ((pmap) != kernel_pmap /*1*/ && \
2176 !(page)->cs_tainted /*2*/ && \
2177 (page)->object->code_signed /*3*/ && \
2178 (!(page)->cs_validated || (page)->wpmapped /*4*/))
2182 * page queue lock must NOT be held
2183 * m->object must be locked
2185 * NOTE: m->object could be locked "shared" only if we are called
2186 * from vm_fault() as part of a soft fault. If so, we must be
2187 * careful not to modify the VM object in any way that is not
2188 * legal under a shared lock...
2190 unsigned long cs_enter_tainted_rejected
= 0;
2191 unsigned long cs_enter_tainted_accepted
= 0;
2193 vm_fault_enter(vm_page_t m
,
2195 vm_map_offset_t vaddr
,
2198 boolean_t change_wiring
,
2202 unsigned int cache_attr
;
2203 kern_return_t kr
, pe_result
;
2204 boolean_t previously_pmapped
= m
->pmapped
;
2205 boolean_t must_disconnect
= 0;
2206 boolean_t map_is_switched
, map_is_switch_protected
;
2208 vm_object_lock_assert_held(m
->object
);
2210 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2213 if (m
->phys_page
== vm_page_guard_addr
) {
2214 assert(m
->fictitious
);
2215 return KERN_SUCCESS
;
2218 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
2220 if (m
->pmapped
== FALSE
) {
2222 * This is the first time this page is being
2223 * mapped in an address space (pmapped == FALSE).
2225 * Part of that page may still be in the data cache
2226 * and not flushed to memory. In case we end up
2227 * accessing that page via the instruction cache,
2228 * we need to ensure that the 2 caches are in sync.
2230 pmap_sync_page_data_phys(m
->phys_page
);
2232 if ((*type_of_fault
== DBG_CACHE_HIT_FAULT
) && m
->clustered
) {
2234 * found it in the cache, but this
2235 * is the first fault-in of the page (m->pmapped == FALSE)
2236 * so it must have come in as part of
2237 * a cluster... account 1 pagein against it
2239 VM_STAT_INCR(pageins
);
2240 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
2242 if (m
->object
->internal
) {
2243 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
2244 *type_of_fault
= DBG_PAGEIND_FAULT
;
2246 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
2247 *type_of_fault
= DBG_PAGEINV_FAULT
;
2250 current_task()->pageins
++;
2252 VM_PAGE_CONSUME_CLUSTERED(m
);
2254 } else if (cache_attr
!= VM_WIMG_DEFAULT
)
2255 pmap_sync_page_attributes_phys(m
->phys_page
);
2257 if (*type_of_fault
!= DBG_COW_FAULT
) {
2258 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2260 if (pmap
== kernel_pmap
) {
2261 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2265 /* Validate code signature if necessary. */
2266 if (VM_FAULT_NEED_CS_VALIDATION(pmap
, m
)) {
2267 vm_object_lock_assert_exclusive(m
->object
);
2269 if (m
->cs_validated
) {
2270 vm_cs_revalidates
++;
2273 /* VM map is locked, so 1 ref will remain on VM object -
2274 * so no harm if vm_page_validate_cs drops the object lock */
2275 vm_page_validate_cs(m
);
2278 #define page_immutable(m,prot) ((m)->cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/)
2280 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2281 (pmap
== vm_map_pmap(current_thread()->map
)));
2282 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2284 /* If the map is switched, and is switch-protected, we must protect
2285 * some pages from being write-faulted: immutable pages because by
2286 * definition they may not be written, and executable pages because that
2287 * would provide a way to inject unsigned code.
2288 * If the page is immutable, we can simply return. However, we can't
2289 * immediately determine whether a page is executable anywhere. But,
2290 * we can disconnect it everywhere and remove the executable protection
2291 * from the current map. We do that below right before we do the
2294 if(!cs_enforcement_disable
&& map_is_switched
&&
2295 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2296 (prot
& VM_PROT_WRITE
))
2298 return KERN_CODESIGN_ERROR
;
2301 /* A page could be tainted, or pose a risk of being tainted later.
2302 * Check whether the receiving process wants it, and make it feel
2303 * the consequences (that hapens in cs_invalid_page()).
2304 * For CS Enforcement, two other conditions will
2305 * cause that page to be tainted as well:
2306 * - pmapping an unsigned page executable - this means unsigned code;
2307 * - writeable mapping of a validated page - the content of that page
2308 * can be changed without the kernel noticing, therefore unsigned
2309 * code can be created
2311 if (m
->cs_tainted
||
2312 ( !cs_enforcement_disable
&&
2313 (/* The page is unsigned and wants to be executable */
2314 (!m
->cs_validated
&& (prot
& VM_PROT_EXECUTE
)) ||
2315 /* The page should be immutable, but is in danger of being modified
2316 * This is the case where we want policy from the code directory -
2317 * is the page immutable or not? For now we have to assume that
2318 * code pages will be immutable, data pages not.
2319 * We'll assume a page is a code page if it has a code directory
2320 * and we fault for execution.
2321 * That is good enough since if we faulted the code page for
2322 * writing in another map before, it is wpmapped; if we fault
2323 * it for writing in this map later it will also be faulted for executing
2324 * at the same time; and if we fault for writing in another map
2325 * later, we will disconnect it from this pmap so we'll notice
2328 (page_immutable(m
, prot
) && ((prot
& VM_PROT_WRITE
) || m
->wpmapped
))
2332 /* We will have a tainted page. Have to handle the special case
2333 * of a switched map now. If the map is not switched, standard
2334 * procedure applies - call cs_invalid_page().
2335 * If the map is switched, the real owner is invalid already.
2336 * There is no point in invalidating the switching process since
2337 * it will not be executing from the map. So we don't call
2338 * cs_invalid_page() in that case. */
2339 boolean_t reject_page
;
2340 if(map_is_switched
) {
2341 assert(pmap
==vm_map_pmap(current_thread()->map
));
2342 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2343 reject_page
= FALSE
;
2345 reject_page
= cs_invalid_page((addr64_t
) vaddr
);
2349 /* reject the tainted page: abort the page fault */
2350 kr
= KERN_CODESIGN_ERROR
;
2351 cs_enter_tainted_rejected
++;
2353 /* proceed with the tainted page */
2355 /* Page might have been tainted before or not; now it
2356 * definitively is. If the page wasn't tainted, we must
2357 * disconnect it from all pmaps later. */
2358 must_disconnect
= !m
->cs_tainted
;
2359 m
->cs_tainted
= TRUE
;
2360 cs_enter_tainted_accepted
++;
2362 if (cs_debug
|| kr
!= KERN_SUCCESS
) {
2363 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2364 "page %p obj %p off 0x%llx *** INVALID PAGE ***\n",
2365 (long long)vaddr
, m
, m
->object
, m
->offset
);
2369 /* proceed with the valid page */
2373 /* If we have a KERN_SUCCESS from the previous checks, we either have
2374 * a good page, or a tainted page that has been accepted by the process.
2375 * In both cases the page will be entered into the pmap.
2376 * If the page is writeable, we need to disconnect it from other pmaps
2377 * now so those processes can take note.
2379 if (kr
== KERN_SUCCESS
) {
2381 * NOTE: we may only hold the vm_object lock SHARED
2382 * at this point, but the update of pmapped is ok
2383 * since this is the ONLY bit updated behind the SHARED
2384 * lock... however, we need to figure out how to do an atomic
2385 * update on a bit field to make this less fragile... right
2386 * now I don't know how to coerce 'C' to give me the offset info
2387 * that's needed for an AtomicCompareAndSwap
2390 if (prot
& VM_PROT_WRITE
) {
2391 vm_object_lock_assert_exclusive(m
->object
);
2393 if(must_disconnect
) {
2394 /* We can only get here
2395 * because of the CSE logic */
2396 assert(cs_enforcement_disable
== FALSE
);
2397 pmap_disconnect(m
->phys_page
);
2398 /* If we are faulting for a write, we can clear
2399 * the execute bit - that will ensure the page is
2400 * checked again before being executable, which
2401 * protects against a map switch.
2402 * This only happens the first time the page
2403 * gets tainted, so we won't get stuck here
2404 * to make an already writeable page executable. */
2405 prot
&= ~VM_PROT_EXECUTE
;
2409 /* Prevent a deadlock by not
2410 * holding the object lock if we need to wait for a page in
2411 * pmap_enter() - <rdar://problem/7138958> */
2412 PMAP_ENTER_OPTIONS(pmap
, vaddr
, m
, prot
, cache_attr
,
2413 wired
, PMAP_OPTIONS_NOWAIT
, pe_result
);
2415 if(pe_result
== KERN_RESOURCE_SHORTAGE
) {
2416 /* The nonblocking version of pmap_enter did not succeed.
2417 * Use the blocking version instead. Requires marking
2418 * the page busy and unlocking the object */
2419 boolean_t was_busy
= m
->busy
;
2421 vm_object_unlock(m
->object
);
2423 PMAP_ENTER(pmap
, vaddr
, m
, prot
, cache_attr
, wired
);
2425 /* Take the object lock again. */
2426 vm_object_lock(m
->object
);
2428 /* If the page was busy, someone else will wake it up.
2429 * Otherwise, we have to do it now. */
2432 PAGE_WAKEUP_DONE(m
);
2434 vm_pmap_enter_blocked
++;
2439 * Hold queues lock to manipulate
2440 * the page queues. Change wiring
2443 if (change_wiring
) {
2444 vm_page_lockspin_queues();
2447 if (kr
== KERN_SUCCESS
) {
2451 vm_page_unwire(m
, TRUE
);
2453 vm_page_unlock_queues();
2456 if (kr
!= KERN_SUCCESS
) {
2457 vm_page_lockspin_queues();
2458 vm_page_deactivate(m
);
2459 vm_page_unlock_queues();
2461 if (((!m
->active
&& !m
->inactive
) || no_cache
) && !VM_PAGE_WIRED(m
) && !m
->throttled
) {
2463 if ( vm_page_local_q
&& !no_cache
&& (*type_of_fault
== DBG_COW_FAULT
|| *type_of_fault
== DBG_ZERO_FILL_FAULT
) ) {
2468 * we got a local queue to stuff this new page on...
2469 * its safe to manipulate local and local_id at this point
2470 * since we're behind an exclusive object lock and the
2471 * page is not on any global queue.
2473 * we'll use the current cpu number to select the queue
2474 * note that we don't need to disable preemption... we're
2475 * going to behind the local queue's lock to do the real
2480 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
2482 VPL_LOCK(&lq
->vpl_lock
);
2484 queue_enter(&lq
->vpl_queue
, m
, vm_page_t
, pageq
);
2489 VPL_UNLOCK(&lq
->vpl_lock
);
2491 if (lq
->vpl_count
> vm_page_local_q_soft_limit
) {
2493 * we're beyond the soft limit for the local queue
2494 * vm_page_reactivate_local will 'try' to take
2495 * the global page queue lock... if it can't that's
2496 * ok... we'll let the queue continue to grow up
2497 * to the hard limit... at that point we'll wait
2498 * for the lock... once we've got the lock, we'll
2499 * transfer all of the pages from the local queue
2500 * to the global active queue
2502 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
2507 vm_page_lockspin_queues();
2509 * test again now that we hold the page queue lock
2511 if (((!m
->active
&& !m
->inactive
) || no_cache
) && !VM_PAGE_WIRED(m
)) {
2514 * If this is a no_cache mapping and the page has never been
2515 * mapped before or was previously a no_cache page, then we
2516 * want to leave pages in the speculative state so that they
2517 * can be readily recycled if free memory runs low. Otherwise
2518 * the page is activated as normal.
2521 if (no_cache
&& (!previously_pmapped
|| m
->no_cache
)) {
2524 if (m
->active
|| m
->inactive
)
2525 VM_PAGE_QUEUES_REMOVE(m
);
2527 if (!m
->speculative
)
2528 vm_page_speculate(m
, TRUE
);
2530 } else if (!m
->active
&& !m
->inactive
)
2531 vm_page_activate(m
);
2535 vm_page_unlock_queues();
2546 * Handle page faults, including pseudo-faults
2547 * used to change the wiring status of pages.
2549 * Explicit continuations have been removed.
2551 * vm_fault and vm_fault_page save mucho state
2552 * in the moral equivalent of a closure. The state
2553 * structure is allocated when first entering vm_fault
2554 * and deallocated when leaving vm_fault.
2557 extern int _map_enter_debug
;
2559 unsigned long vm_fault_collapse_total
= 0;
2560 unsigned long vm_fault_collapse_skipped
= 0;
2565 vm_map_offset_t vaddr
,
2566 vm_prot_t fault_type
,
2567 boolean_t change_wiring
,
2570 vm_map_offset_t caller_pmap_addr
)
2572 vm_map_version_t version
; /* Map version for verificiation */
2573 boolean_t wired
; /* Should mapping be wired down? */
2574 vm_object_t object
; /* Top-level object */
2575 vm_object_offset_t offset
; /* Top-level offset */
2576 vm_prot_t prot
; /* Protection for mapping */
2577 vm_object_t old_copy_object
; /* Saved copy object */
2578 vm_page_t result_page
; /* Result of vm_fault_page */
2579 vm_page_t top_page
; /* Placeholder page */
2582 vm_page_t m
; /* Fast access to result_page */
2583 kern_return_t error_code
;
2584 vm_object_t cur_object
;
2585 vm_object_offset_t cur_offset
;
2587 vm_object_t new_object
;
2590 boolean_t interruptible_state
;
2591 vm_map_t real_map
= map
;
2592 vm_map_t original_map
= map
;
2593 vm_prot_t original_fault_type
;
2594 struct vm_object_fault_info fault_info
;
2595 boolean_t need_collapse
= FALSE
;
2596 int object_lock_type
= 0;
2597 int cur_object_lock_type
;
2598 vm_object_t top_object
= VM_OBJECT_NULL
;
2601 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
2602 (int)((uint64_t)vaddr
>> 32),
2608 if (get_preemption_level() != 0) {
2609 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
2610 (int)((uint64_t)vaddr
>> 32),
2616 return (KERN_FAILURE
);
2619 interruptible_state
= thread_interrupt_level(interruptible
);
2621 VM_STAT_INCR(faults
);
2622 current_task()->faults
++;
2623 original_fault_type
= fault_type
;
2625 if (fault_type
& VM_PROT_WRITE
)
2626 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2628 object_lock_type
= OBJECT_LOCK_SHARED
;
2630 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
2634 * assume we will hit a page in the cache
2635 * otherwise, explicitly override with
2636 * the real fault type once we determine it
2638 type_of_fault
= DBG_CACHE_HIT_FAULT
;
2641 * Find the backing store object and offset into
2642 * it to begin the search.
2644 fault_type
= original_fault_type
;
2646 vm_map_lock_read(map
);
2648 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
2649 object_lock_type
, &version
,
2650 &object
, &offset
, &prot
, &wired
,
2654 if (kr
!= KERN_SUCCESS
) {
2655 vm_map_unlock_read(map
);
2658 pmap
= real_map
->pmap
;
2659 fault_info
.interruptible
= interruptible
;
2660 fault_info
.stealth
= FALSE
;
2661 fault_info
.mark_zf_absent
= FALSE
;
2664 * If the page is wired, we must fault for the current protection
2665 * value, to avoid further faults.
2668 fault_type
= prot
| VM_PROT_WRITE
;
2670 * since we're treating this fault as a 'write'
2671 * we must hold the top object lock exclusively
2673 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2675 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2677 if (vm_object_lock_upgrade(object
) == FALSE
) {
2679 * couldn't upgrade, so explictly
2680 * take the lock exclusively
2682 vm_object_lock(object
);
2687 #if VM_FAULT_CLASSIFY
2689 * Temporary data gathering code
2691 vm_fault_classify(object
, offset
, fault_type
);
2694 * Fast fault code. The basic idea is to do as much as
2695 * possible while holding the map lock and object locks.
2696 * Busy pages are not used until the object lock has to
2697 * be dropped to do something (copy, zero fill, pmap enter).
2698 * Similarly, paging references aren't acquired until that
2699 * point, and object references aren't used.
2701 * If we can figure out what to do
2702 * (zero fill, copy on write, pmap enter) while holding
2703 * the locks, then it gets done. Otherwise, we give up,
2704 * and use the original fault path (which doesn't hold
2705 * the map lock, and relies on busy pages).
2706 * The give up cases include:
2707 * - Have to talk to pager.
2708 * - Page is busy, absent or in error.
2709 * - Pager has locked out desired access.
2710 * - Fault needs to be restarted.
2711 * - Have to push page into copy object.
2713 * The code is an infinite loop that moves one level down
2714 * the shadow chain each time. cur_object and cur_offset
2715 * refer to the current object being examined. object and offset
2716 * are the original object from the map. The loop is at the
2717 * top level if and only if object and cur_object are the same.
2719 * Invariants: Map lock is held throughout. Lock is held on
2720 * original object and cur_object (if different) when
2721 * continuing or exiting loop.
2727 * If this page is to be inserted in a copy delay object
2728 * for writing, and if the object has a copy, then the
2729 * copy delay strategy is implemented in the slow fault page.
2731 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
2732 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
))
2733 goto handle_copy_delay
;
2735 cur_object
= object
;
2736 cur_offset
= offset
;
2739 if (!cur_object
->pager_created
&&
2740 cur_object
->phys_contiguous
) /* superpage */
2743 if (cur_object
->blocked_access
) {
2745 * Access to this VM object has been blocked.
2746 * Let the slow path handle it.
2751 m
= vm_page_lookup(cur_object
, cur_offset
);
2753 if (m
!= VM_PAGE_NULL
) {
2755 wait_result_t result
;
2758 * in order to do the PAGE_ASSERT_WAIT, we must
2759 * have object that 'm' belongs to locked exclusively
2761 if (object
!= cur_object
) {
2762 vm_object_unlock(object
);
2764 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2766 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2768 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2770 * couldn't upgrade so go do a full retry
2771 * immediately since we've already dropped
2772 * the top object lock associated with this page
2773 * and the current one got dropped due to the
2774 * failed upgrade... the state is no longer valid
2776 vm_map_unlock_read(map
);
2777 if (real_map
!= map
)
2778 vm_map_unlock(real_map
);
2783 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2785 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2787 if (vm_object_lock_upgrade(object
) == FALSE
) {
2789 * couldn't upgrade, so explictly take the lock
2790 * exclusively and go relookup the page since we
2791 * will have dropped the object lock and
2792 * a different thread could have inserted
2793 * a page at this offset
2794 * no need for a full retry since we're
2795 * at the top level of the object chain
2797 vm_object_lock(object
);
2802 vm_map_unlock_read(map
);
2803 if (real_map
!= map
)
2804 vm_map_unlock(real_map
);
2806 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
2808 vm_object_unlock(cur_object
);
2810 if (result
== THREAD_WAITING
) {
2811 result
= thread_block(THREAD_CONTINUE_NULL
);
2813 counter(c_vm_fault_page_block_busy_kernel
++);
2815 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
2821 if (m
->phys_page
== vm_page_guard_addr
) {
2823 * Guard page: let the slow path deal with it
2827 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
)) {
2829 * Unusual case... let the slow path deal with it
2833 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m
->object
)) {
2834 if (object
!= cur_object
)
2835 vm_object_unlock(object
);
2836 vm_map_unlock_read(map
);
2837 if (real_map
!= map
)
2838 vm_map_unlock(real_map
);
2839 vm_object_unlock(cur_object
);
2840 kr
= KERN_MEMORY_ERROR
;
2847 * We've soft-faulted (because it's not in the page
2848 * table) on an encrypted page.
2849 * Keep the page "busy" so that no one messes with
2850 * it during the decryption.
2851 * Release the extra locks we're holding, keep only
2852 * the page's VM object lock.
2854 * in order to set 'busy' on 'm', we must
2855 * have object that 'm' belongs to locked exclusively
2857 if (object
!= cur_object
) {
2858 vm_object_unlock(object
);
2860 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2862 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2864 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2866 * couldn't upgrade so go do a full retry
2867 * immediately since we've already dropped
2868 * the top object lock associated with this page
2869 * and the current one got dropped due to the
2870 * failed upgrade... the state is no longer valid
2872 vm_map_unlock_read(map
);
2873 if (real_map
!= map
)
2874 vm_map_unlock(real_map
);
2879 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2881 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2883 if (vm_object_lock_upgrade(object
) == FALSE
) {
2885 * couldn't upgrade, so explictly take the lock
2886 * exclusively and go relookup the page since we
2887 * will have dropped the object lock and
2888 * a different thread could have inserted
2889 * a page at this offset
2890 * no need for a full retry since we're
2891 * at the top level of the object chain
2893 vm_object_lock(object
);
2900 vm_map_unlock_read(map
);
2901 if (real_map
!= map
)
2902 vm_map_unlock(real_map
);
2904 vm_page_decrypt(m
, 0);
2907 PAGE_WAKEUP_DONE(m
);
2909 vm_object_unlock(cur_object
);
2911 * Retry from the top, in case anything
2912 * changed while we were decrypting...
2916 ASSERT_PAGE_DECRYPTED(m
);
2918 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
)) {
2920 * We might need to validate this page
2921 * against its code signature, so we
2922 * want to hold the VM object exclusively.
2924 if (object
!= cur_object
) {
2925 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2926 vm_object_unlock(object
);
2927 vm_object_unlock(cur_object
);
2929 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2931 vm_map_unlock_read(map
);
2932 if (real_map
!= map
)
2933 vm_map_unlock(real_map
);
2938 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2940 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2942 if (vm_object_lock_upgrade(object
) == FALSE
) {
2944 * couldn't upgrade, so explictly take the lock
2945 * exclusively and go relookup the page since we
2946 * will have dropped the object lock and
2947 * a different thread could have inserted
2948 * a page at this offset
2949 * no need for a full retry since we're
2950 * at the top level of the object chain
2952 vm_object_lock(object
);
2959 * Two cases of map in faults:
2960 * - At top level w/o copy object.
2961 * - Read fault anywhere.
2962 * --> must disallow write.
2965 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
2966 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2968 * This is not a "write" fault, so we
2969 * might not have taken the object lock
2970 * exclusively and we might not be able
2971 * to update the "wpmapped" bit in
2973 * Let's just grant read access to
2974 * the page for now and we'll
2975 * soft-fault again if we need write
2978 prot
&= ~VM_PROT_WRITE
;
2983 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2985 prot
&= ~VM_PROT_WRITE
;
2987 if (object
!= cur_object
) {
2989 * We still need to hold the top object
2990 * lock here to prevent a race between
2991 * a read fault (taking only "shared"
2992 * locks) and a write fault (taking
2993 * an "exclusive" lock on the top
2995 * Otherwise, as soon as we release the
2996 * top lock, the write fault could
2997 * proceed and actually complete before
2998 * the read fault, and the copied page's
2999 * translation could then be overwritten
3000 * by the read fault's translation for
3001 * the original page.
3003 * Let's just record what the top object
3004 * is and we'll release it later.
3006 top_object
= object
;
3009 * switch to the object that has the new page
3011 object
= cur_object
;
3012 object_lock_type
= cur_object_lock_type
;
3016 * prepare for the pmap_enter...
3017 * object and map are both locked
3018 * m contains valid data
3019 * object == m->object
3020 * cur_object == NULL or it's been unlocked
3021 * no paging references on either object or cur_object
3024 if (db_watchpoint_list
&& (fault_type
& VM_PROT_WRITE
) == 0)
3025 prot
&= ~VM_PROT_WRITE
;
3028 kr
= vm_fault_enter(m
,
3034 fault_info
.no_cache
,
3037 kr
= vm_fault_enter(m
,
3043 fault_info
.no_cache
,
3047 if (top_object
!= VM_OBJECT_NULL
) {
3049 * It's safe to drop the top object
3050 * now that we've done our
3051 * vm_fault_enter(). Any other fault
3052 * in progress for that virtual
3053 * address will either find our page
3054 * and translation or put in a new page
3057 vm_object_unlock(top_object
);
3058 top_object
= VM_OBJECT_NULL
;
3061 if (need_collapse
== TRUE
)
3062 vm_object_collapse(object
, offset
, TRUE
);
3064 if (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
) {
3066 * evaluate access pattern and update state
3067 * vm_fault_deactivate_behind depends on the
3068 * state being up to date
3070 vm_fault_is_sequential(object
, cur_offset
, fault_info
.behavior
);
3072 vm_fault_deactivate_behind(object
, cur_offset
, fault_info
.behavior
);
3075 * That's it, clean up and return.
3078 PAGE_WAKEUP_DONE(m
);
3080 vm_object_unlock(object
);
3082 vm_map_unlock_read(map
);
3083 if (real_map
!= map
)
3084 vm_map_unlock(real_map
);
3089 * COPY ON WRITE FAULT
3091 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
3093 if (vm_page_throttled()) {
3095 * drop all of our locks...
3096 * wait until the free queue is
3097 * pumped back up and then
3100 if (object
!= cur_object
)
3101 vm_object_unlock(cur_object
);
3102 vm_object_unlock(object
);
3103 vm_map_unlock_read(map
);
3104 if (real_map
!= map
)
3105 vm_map_unlock(real_map
);
3107 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
3108 delay(HARD_THROTTLE_DELAY
);
3110 if (!current_thread_aborted() && vm_page_wait((change_wiring
) ?
3118 * If objects match, then
3119 * object->copy must not be NULL (else control
3120 * would be in previous code block), and we
3121 * have a potential push into the copy object
3122 * with which we can't cope with here.
3124 if (cur_object
== object
) {
3126 * must take the slow path to
3127 * deal with the copy push
3132 * This is now a shadow based copy on write
3133 * fault -- it requires a copy up the shadow
3136 * Allocate a page in the original top level
3137 * object. Give up if allocate fails. Also
3138 * need to remember current page, as it's the
3139 * source of the copy.
3141 * at this point we hold locks on both
3142 * object and cur_object... no need to take
3143 * paging refs or mark pages BUSY since
3144 * we don't drop either object lock until
3145 * the page has been copied and inserted
3150 if (m
== VM_PAGE_NULL
) {
3152 * no free page currently available...
3153 * must take the slow path
3158 * Now do the copy. Mark the source page busy...
3160 * NOTE: This code holds the map lock across
3163 vm_page_copy(cur_m
, m
);
3164 vm_page_insert(m
, object
, offset
);
3168 * Now cope with the source page and object
3170 if (object
->ref_count
> 1 && cur_m
->pmapped
)
3171 pmap_disconnect(cur_m
->phys_page
);
3173 need_collapse
= TRUE
;
3175 if (!cur_object
->internal
&&
3176 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3178 * The object from which we've just
3179 * copied a page is most probably backed
3180 * by a vnode. We don't want to waste too
3181 * much time trying to collapse the VM objects
3182 * and create a bottleneck when several tasks
3183 * map the same file.
3185 if (cur_object
->copy
== object
) {
3187 * Shared mapping or no COW yet.
3188 * We can never collapse a copy
3189 * object into its backing object.
3191 need_collapse
= FALSE
;
3192 } else if (cur_object
->copy
== object
->shadow
&&
3193 object
->shadow
->resident_page_count
== 0) {
3195 * Shared mapping after a COW occurred.
3197 need_collapse
= FALSE
;
3200 vm_object_unlock(cur_object
);
3202 if (need_collapse
== FALSE
)
3203 vm_fault_collapse_skipped
++;
3204 vm_fault_collapse_total
++;
3206 type_of_fault
= DBG_COW_FAULT
;
3207 VM_STAT_INCR(cow_faults
);
3208 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
3209 current_task()->cow_faults
++;
3215 * No page at cur_object, cur_offset... m == NULL
3217 if (cur_object
->pager_created
) {
3218 if (MUST_ASK_PAGER(cur_object
, cur_offset
) == TRUE
) {
3220 * May have to talk to a pager...
3221 * take the slow path.
3226 * existence map present and indicates
3227 * that the pager doesn't have this page
3230 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
3232 * Zero fill fault. Page gets
3233 * inserted into the original object.
3235 if (cur_object
->shadow_severed
||
3236 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
))
3238 if (object
!= cur_object
)
3239 vm_object_unlock(cur_object
);
3240 vm_object_unlock(object
);
3242 vm_map_unlock_read(map
);
3243 if (real_map
!= map
)
3244 vm_map_unlock(real_map
);
3246 kr
= KERN_MEMORY_ERROR
;
3249 if (vm_page_throttled()) {
3251 * drop all of our locks...
3252 * wait until the free queue is
3253 * pumped back up and then
3256 if (object
!= cur_object
)
3257 vm_object_unlock(cur_object
);
3258 vm_object_unlock(object
);
3259 vm_map_unlock_read(map
);
3260 if (real_map
!= map
)
3261 vm_map_unlock(real_map
);
3263 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
3264 delay(HARD_THROTTLE_DELAY
);
3266 if (!current_thread_aborted() && vm_page_wait((change_wiring
) ?
3273 if (vm_backing_store_low
) {
3275 * we are protecting the system from
3276 * backing store exhaustion...
3277 * must take the slow path if we're
3280 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
))
3283 if (cur_object
!= object
) {
3284 vm_object_unlock(cur_object
);
3286 cur_object
= object
;
3288 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3290 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3292 if (vm_object_lock_upgrade(object
) == FALSE
) {
3294 * couldn't upgrade so do a full retry on the fault
3295 * since we dropped the object lock which
3296 * could allow another thread to insert
3297 * a page at this offset
3299 vm_map_unlock_read(map
);
3300 if (real_map
!= map
)
3301 vm_map_unlock(real_map
);
3306 m
= vm_page_alloc(object
, offset
);
3308 if (m
== VM_PAGE_NULL
) {
3310 * no free page currently available...
3311 * must take the slow path
3317 * Now zero fill page...
3318 * the page is probably going to
3319 * be written soon, so don't bother
3320 * to clear the modified bit
3322 * NOTE: This code holds the map
3323 * lock across the zero fill.
3325 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
3330 * On to the next level in the shadow chain
3332 cur_offset
+= cur_object
->shadow_offset
;
3333 new_object
= cur_object
->shadow
;
3336 * take the new_object's lock with the indicated state
3338 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
)
3339 vm_object_lock_shared(new_object
);
3341 vm_object_lock(new_object
);
3343 if (cur_object
!= object
)
3344 vm_object_unlock(cur_object
);
3346 cur_object
= new_object
;
3352 * Cleanup from fast fault failure. Drop any object
3353 * lock other than original and drop map lock.
3355 if (object
!= cur_object
)
3356 vm_object_unlock(cur_object
);
3359 * must own the object lock exclusively at this point
3361 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3362 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3364 if (vm_object_lock_upgrade(object
) == FALSE
) {
3366 * couldn't upgrade, so explictly
3367 * take the lock exclusively
3368 * no need to retry the fault at this
3369 * point since "vm_fault_page" will
3370 * completely re-evaluate the state
3372 vm_object_lock(object
);
3377 vm_map_unlock_read(map
);
3378 if (real_map
!= map
)
3379 vm_map_unlock(real_map
);
3382 * Make a reference to this object to
3383 * prevent its disposal while we are messing with
3384 * it. Once we have the reference, the map is free
3385 * to be diddled. Since objects reference their
3386 * shadows (and copies), they will stay around as well.
3388 vm_object_reference_locked(object
);
3389 vm_object_paging_begin(object
);
3391 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
3395 kr
= vm_fault_page(object
, offset
, fault_type
,
3396 (change_wiring
&& !wired
),
3397 &prot
, &result_page
, &top_page
,
3399 &error_code
, map
->no_zero_fill
,
3400 FALSE
, &fault_info
);
3403 * if kr != VM_FAULT_SUCCESS, then the paging reference
3404 * has been dropped and the object unlocked... the ref_count
3407 * if kr == VM_FAULT_SUCCESS, then the paging reference
3408 * is still held along with the ref_count on the original object
3410 * the object is returned locked with a paging reference
3412 * if top_page != NULL, then it's BUSY and the
3413 * object it belongs to has a paging reference
3414 * but is returned unlocked
3416 if (kr
!= VM_FAULT_SUCCESS
&&
3417 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
3419 * we didn't succeed, lose the object reference immediately.
3421 vm_object_deallocate(object
);
3424 * See why we failed, and take corrective action.
3427 case VM_FAULT_MEMORY_SHORTAGE
:
3428 if (vm_page_wait((change_wiring
) ?
3435 case VM_FAULT_INTERRUPTED
:
3438 case VM_FAULT_RETRY
:
3440 case VM_FAULT_MEMORY_ERROR
:
3444 kr
= KERN_MEMORY_ERROR
;
3447 panic("vm_fault: unexpected error 0x%x from "
3448 "vm_fault_page()\n", kr
);
3453 if (m
!= VM_PAGE_NULL
) {
3454 assert((change_wiring
&& !wired
) ?
3455 (top_page
== VM_PAGE_NULL
) :
3456 ((top_page
== VM_PAGE_NULL
) == (m
->object
== object
)));
3460 * What to do with the resulting page from vm_fault_page
3461 * if it doesn't get entered into the physical map:
3463 #define RELEASE_PAGE(m) \
3465 PAGE_WAKEUP_DONE(m); \
3466 if (!m->active && !m->inactive && !m->throttled) { \
3467 vm_page_lockspin_queues(); \
3468 if (!m->active && !m->inactive && !m->throttled) \
3469 vm_page_activate(m); \
3470 vm_page_unlock_queues(); \
3475 * We must verify that the maps have not changed
3476 * since our last lookup.
3478 if (m
!= VM_PAGE_NULL
) {
3479 old_copy_object
= m
->object
->copy
;
3480 vm_object_unlock(m
->object
);
3482 old_copy_object
= VM_OBJECT_NULL
;
3483 vm_object_unlock(object
);
3487 * no object locks are held at this point
3489 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
3490 vm_object_t retry_object
;
3491 vm_object_offset_t retry_offset
;
3492 vm_prot_t retry_prot
;
3495 * To avoid trying to write_lock the map while another
3496 * thread has it read_locked (in vm_map_pageable), we
3497 * do not try for write permission. If the page is
3498 * still writable, we will get write permission. If it
3499 * is not, or has been marked needs_copy, we enter the
3500 * mapping without write permission, and will merely
3501 * take another fault.
3504 vm_map_lock_read(map
);
3506 kr
= vm_map_lookup_locked(&map
, vaddr
,
3507 fault_type
& ~VM_PROT_WRITE
,
3508 OBJECT_LOCK_EXCLUSIVE
, &version
,
3509 &retry_object
, &retry_offset
, &retry_prot
,
3513 pmap
= real_map
->pmap
;
3515 if (kr
!= KERN_SUCCESS
) {
3516 vm_map_unlock_read(map
);
3518 if (m
!= VM_PAGE_NULL
) {
3520 * retake the lock so that
3521 * we can drop the paging reference
3522 * in vm_fault_cleanup and do the
3523 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3525 vm_object_lock(m
->object
);
3529 vm_fault_cleanup(m
->object
, top_page
);
3532 * retake the lock so that
3533 * we can drop the paging reference
3534 * in vm_fault_cleanup
3536 vm_object_lock(object
);
3538 vm_fault_cleanup(object
, top_page
);
3540 vm_object_deallocate(object
);
3544 vm_object_unlock(retry_object
);
3546 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
3548 vm_map_unlock_read(map
);
3549 if (real_map
!= map
)
3550 vm_map_unlock(real_map
);
3552 if (m
!= VM_PAGE_NULL
) {
3554 * retake the lock so that
3555 * we can drop the paging reference
3556 * in vm_fault_cleanup and do the
3557 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3559 vm_object_lock(m
->object
);
3563 vm_fault_cleanup(m
->object
, top_page
);
3566 * retake the lock so that
3567 * we can drop the paging reference
3568 * in vm_fault_cleanup
3570 vm_object_lock(object
);
3572 vm_fault_cleanup(object
, top_page
);
3574 vm_object_deallocate(object
);
3579 * Check whether the protection has changed or the object
3580 * has been copied while we left the map unlocked.
3584 if (m
!= VM_PAGE_NULL
) {
3585 vm_object_lock(m
->object
);
3587 if (m
->object
->copy
!= old_copy_object
) {
3589 * The copy object changed while the top-level object
3590 * was unlocked, so take away write permission.
3592 prot
&= ~VM_PROT_WRITE
;
3595 vm_object_lock(object
);
3598 * If we want to wire down this page, but no longer have
3599 * adequate permissions, we must start all over.
3601 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
3603 vm_map_verify_done(map
, &version
);
3604 if (real_map
!= map
)
3605 vm_map_unlock(real_map
);
3607 if (m
!= VM_PAGE_NULL
) {
3610 vm_fault_cleanup(m
->object
, top_page
);
3612 vm_fault_cleanup(object
, top_page
);
3614 vm_object_deallocate(object
);
3618 if (m
!= VM_PAGE_NULL
) {
3620 * Put this page into the physical map.
3621 * We had to do the unlock above because pmap_enter
3622 * may cause other faults. The page may be on
3623 * the pageout queues. If the pageout daemon comes
3624 * across the page, it will remove it from the queues.
3627 kr
= vm_fault_enter(m
,
3633 fault_info
.no_cache
,
3636 kr
= vm_fault_enter(m
,
3642 fault_info
.no_cache
,
3645 if (kr
!= KERN_SUCCESS
) {
3646 /* abort this page fault */
3647 vm_map_verify_done(map
, &version
);
3648 if (real_map
!= map
)
3649 vm_map_unlock(real_map
);
3650 PAGE_WAKEUP_DONE(m
);
3651 vm_fault_cleanup(m
->object
, top_page
);
3652 vm_object_deallocate(object
);
3657 vm_map_entry_t entry
;
3658 vm_map_offset_t laddr
;
3659 vm_map_offset_t ldelta
, hdelta
;
3662 * do a pmap block mapping from the physical address
3667 /* While we do not worry about execution protection in */
3668 /* general, certian pages may have instruction execution */
3669 /* disallowed. We will check here, and if not allowed */
3670 /* to execute, we return with a protection failure. */
3672 if ((fault_type
& VM_PROT_EXECUTE
) &&
3673 (!pmap_eligible_for_execute((ppnum_t
)(object
->shadow_offset
>> 12)))) {
3675 vm_map_verify_done(map
, &version
);
3677 if (real_map
!= map
)
3678 vm_map_unlock(real_map
);
3680 vm_fault_cleanup(object
, top_page
);
3681 vm_object_deallocate(object
);
3683 kr
= KERN_PROTECTION_FAILURE
;
3688 if (real_map
!= map
)
3689 vm_map_unlock(real_map
);
3691 if (original_map
!= map
) {
3692 vm_map_unlock_read(map
);
3693 vm_map_lock_read(original_map
);
3699 hdelta
= 0xFFFFF000;
3700 ldelta
= 0xFFFFF000;
3702 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
3703 if (ldelta
> (laddr
- entry
->vme_start
))
3704 ldelta
= laddr
- entry
->vme_start
;
3705 if (hdelta
> (entry
->vme_end
- laddr
))
3706 hdelta
= entry
->vme_end
- laddr
;
3707 if (entry
->is_sub_map
) {
3709 laddr
= (laddr
- entry
->vme_start
)
3711 vm_map_lock_read(entry
->object
.sub_map
);
3713 if (map
!= real_map
)
3714 vm_map_unlock_read(map
);
3715 if (entry
->use_pmap
) {
3716 vm_map_unlock_read(real_map
);
3717 real_map
= entry
->object
.sub_map
;
3719 map
= entry
->object
.sub_map
;
3726 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
3727 (entry
->object
.vm_object
!= NULL
) &&
3728 (entry
->object
.vm_object
== object
)) {
3730 int superpage
= (!object
->pager_created
&& object
->phys_contiguous
)? VM_MEM_SUPERPAGE
: 0;
3733 * Set up a block mapped area
3735 assert((uint32_t)((ldelta
+ hdelta
) >> 12) == ((ldelta
+ hdelta
) >> 12));
3736 pmap_map_block(caller_pmap
,
3737 (addr64_t
)(caller_pmap_addr
- ldelta
),
3738 (ppnum_t
)((((vm_map_offset_t
) (entry
->object
.vm_object
->shadow_offset
)) +
3739 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12),
3740 (uint32_t)((ldelta
+ hdelta
) >> 12), prot
,
3741 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
3744 * Set up a block mapped area
3746 assert((uint32_t)((ldelta
+ hdelta
) >> 12) == ((ldelta
+ hdelta
) >> 12));
3747 pmap_map_block(real_map
->pmap
,
3748 (addr64_t
)(vaddr
- ldelta
),
3749 (ppnum_t
)((((vm_map_offset_t
)(entry
->object
.vm_object
->shadow_offset
)) +
3750 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12),
3751 (uint32_t)((ldelta
+ hdelta
) >> 12), prot
,
3752 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
3758 * Unlock everything, and return
3760 vm_map_verify_done(map
, &version
);
3761 if (real_map
!= map
)
3762 vm_map_unlock(real_map
);
3764 if (m
!= VM_PAGE_NULL
) {
3765 PAGE_WAKEUP_DONE(m
);
3767 vm_fault_cleanup(m
->object
, top_page
);
3769 vm_fault_cleanup(object
, top_page
);
3771 vm_object_deallocate(object
);
3777 thread_interrupt_level(interruptible_state
);
3779 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3780 (int)((uint64_t)vaddr
>> 32),
3792 * Wire down a range of virtual addresses in a map.
3797 vm_map_entry_t entry
,
3799 vm_map_offset_t pmap_addr
)
3802 register vm_map_offset_t va
;
3803 register vm_map_offset_t end_addr
= entry
->vme_end
;
3804 register kern_return_t rc
;
3806 assert(entry
->in_transition
);
3808 if ((entry
->object
.vm_object
!= NULL
) &&
3809 !entry
->is_sub_map
&&
3810 entry
->object
.vm_object
->phys_contiguous
) {
3811 return KERN_SUCCESS
;
3815 * Inform the physical mapping system that the
3816 * range of addresses may not fault, so that
3817 * page tables and such can be locked down as well.
3820 pmap_pageable(pmap
, pmap_addr
,
3821 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
3824 * We simulate a fault to get the page and enter it
3825 * in the physical map.
3828 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3829 if ((rc
= vm_fault_wire_fast(
3830 map
, va
, entry
, pmap
,
3831 pmap_addr
+ (va
- entry
->vme_start
)
3832 )) != KERN_SUCCESS
) {
3833 rc
= vm_fault(map
, va
, VM_PROT_NONE
, TRUE
,
3834 (pmap
== kernel_pmap
) ?
3835 THREAD_UNINT
: THREAD_ABORTSAFE
,
3836 pmap
, pmap_addr
+ (va
- entry
->vme_start
));
3837 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
3840 if (rc
!= KERN_SUCCESS
) {
3841 struct vm_map_entry tmp_entry
= *entry
;
3843 /* unwire wired pages */
3844 tmp_entry
.vme_end
= va
;
3845 vm_fault_unwire(map
,
3846 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
3851 return KERN_SUCCESS
;
3857 * Unwire a range of virtual addresses in a map.
3862 vm_map_entry_t entry
,
3863 boolean_t deallocate
,
3865 vm_map_offset_t pmap_addr
)
3867 register vm_map_offset_t va
;
3868 register vm_map_offset_t end_addr
= entry
->vme_end
;
3870 struct vm_object_fault_info fault_info
;
3872 object
= (entry
->is_sub_map
)
3873 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
3876 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
3877 * do anything since such memory is wired by default. So we don't have
3878 * anything to undo here.
3881 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
)
3884 fault_info
.interruptible
= THREAD_UNINT
;
3885 fault_info
.behavior
= entry
->behavior
;
3886 fault_info
.user_tag
= entry
->alias
;
3887 fault_info
.lo_offset
= entry
->offset
;
3888 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
3889 fault_info
.no_cache
= entry
->no_cache
;
3890 fault_info
.stealth
= TRUE
;
3891 fault_info
.mark_zf_absent
= FALSE
;
3894 * Since the pages are wired down, we must be able to
3895 * get their mappings from the physical map system.
3898 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3900 if (object
== VM_OBJECT_NULL
) {
3902 pmap_change_wiring(pmap
,
3903 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3905 (void) vm_fault(map
, va
, VM_PROT_NONE
,
3906 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
3909 vm_page_t result_page
;
3911 vm_object_t result_object
;
3912 vm_fault_return_t result
;
3914 if (end_addr
- va
> (vm_size_t
) -1) {
3915 /* 32-bit overflow */
3916 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
3918 fault_info
.cluster_size
= (vm_size_t
) (end_addr
- va
);
3919 assert(fault_info
.cluster_size
== end_addr
- va
);
3923 prot
= VM_PROT_NONE
;
3925 vm_object_lock(object
);
3926 vm_object_paging_begin(object
);
3928 "vm_fault_unwire -> vm_fault_page\n",
3930 result
= vm_fault_page(
3932 entry
->offset
+ (va
- entry
->vme_start
),
3934 &prot
, &result_page
, &top_page
,
3936 NULL
, map
->no_zero_fill
,
3937 FALSE
, &fault_info
);
3938 } while (result
== VM_FAULT_RETRY
);
3941 * If this was a mapping to a file on a device that has been forcibly
3942 * unmounted, then we won't get a page back from vm_fault_page(). Just
3943 * move on to the next one in case the remaining pages are mapped from
3944 * different objects. During a forced unmount, the object is terminated
3945 * so the alive flag will be false if this happens. A forced unmount will
3946 * will occur when an external disk is unplugged before the user does an
3947 * eject, so we don't want to panic in that situation.
3950 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
)
3953 if (result
!= VM_FAULT_SUCCESS
)
3954 panic("vm_fault_unwire: failure");
3956 result_object
= result_page
->object
;
3958 if ((pmap
) && (result_page
->phys_page
!= vm_page_guard_addr
)) {
3959 pmap_change_wiring(pmap
,
3960 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3963 assert(result_page
->phys_page
!=
3964 vm_page_fictitious_addr
);
3965 pmap_disconnect(result_page
->phys_page
);
3966 VM_PAGE_FREE(result_page
);
3968 if (VM_PAGE_WIRED(result_page
)) {
3969 vm_page_lockspin_queues();
3970 vm_page_unwire(result_page
, TRUE
);
3971 vm_page_unlock_queues();
3973 if(entry
->zero_wired_pages
) {
3974 pmap_zero_page(result_page
->phys_page
);
3975 entry
->zero_wired_pages
= FALSE
;
3978 PAGE_WAKEUP_DONE(result_page
);
3980 vm_fault_cleanup(result_object
, top_page
);
3985 * Inform the physical mapping system that the range
3986 * of addresses may fault, so that page tables and
3987 * such may be unwired themselves.
3990 pmap_pageable(pmap
, pmap_addr
,
3991 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
3996 * vm_fault_wire_fast:
3998 * Handle common case of a wire down page fault at the given address.
3999 * If successful, the page is inserted into the associated physical map.
4000 * The map entry is passed in to avoid the overhead of a map lookup.
4002 * NOTE: the given address should be truncated to the
4003 * proper page address.
4005 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
4006 * a standard error specifying why the fault is fatal is returned.
4008 * The map in question must be referenced, and remains so.
4009 * Caller has a read lock on the map.
4011 * This is a stripped version of vm_fault() for wiring pages. Anything
4012 * other than the common case will return KERN_FAILURE, and the caller
4013 * is expected to call vm_fault().
4017 __unused vm_map_t map
,
4019 vm_map_entry_t entry
,
4021 vm_map_offset_t pmap_addr
)
4024 vm_object_offset_t offset
;
4025 register vm_page_t m
;
4027 thread_t thread
= current_thread();
4031 VM_STAT_INCR(faults
);
4033 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
)
4034 thread
->task
->faults
++;
4041 #define RELEASE_PAGE(m) { \
4042 PAGE_WAKEUP_DONE(m); \
4043 vm_page_lockspin_queues(); \
4044 vm_page_unwire(m, TRUE); \
4045 vm_page_unlock_queues(); \
4049 #undef UNLOCK_THINGS
4050 #define UNLOCK_THINGS { \
4051 vm_object_paging_end(object); \
4052 vm_object_unlock(object); \
4055 #undef UNLOCK_AND_DEALLOCATE
4056 #define UNLOCK_AND_DEALLOCATE { \
4058 vm_object_deallocate(object); \
4061 * Give up and have caller do things the hard way.
4065 UNLOCK_AND_DEALLOCATE; \
4066 return(KERN_FAILURE); \
4071 * If this entry is not directly to a vm_object, bail out.
4073 if (entry
->is_sub_map
)
4074 return(KERN_FAILURE
);
4077 * Find the backing store object and offset into it.
4080 object
= entry
->object
.vm_object
;
4081 offset
= (va
- entry
->vme_start
) + entry
->offset
;
4082 prot
= entry
->protection
;
4085 * Make a reference to this object to prevent its
4086 * disposal while we are messing with it.
4089 vm_object_lock(object
);
4090 vm_object_reference_locked(object
);
4091 vm_object_paging_begin(object
);
4094 * INVARIANTS (through entire routine):
4096 * 1) At all times, we must either have the object
4097 * lock or a busy page in some object to prevent
4098 * some other thread from trying to bring in
4101 * 2) Once we have a busy page, we must remove it from
4102 * the pageout queues, so that the pageout daemon
4103 * will not grab it away.
4108 * Look for page in top-level object. If it's not there or
4109 * there's something going on, give up.
4110 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
4111 * decrypt the page before wiring it down.
4113 m
= vm_page_lookup(object
, offset
);
4114 if ((m
== VM_PAGE_NULL
) || (m
->busy
) || (m
->encrypted
) ||
4115 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
4119 ASSERT_PAGE_DECRYPTED(m
);
4121 if (m
->fictitious
&&
4122 m
->phys_page
== vm_page_guard_addr
) {
4124 * Guard pages are fictitious pages and are never
4125 * entered into a pmap, so let's say it's been wired...
4132 * Wire the page down now. All bail outs beyond this
4133 * point must unwire the page.
4136 vm_page_lockspin_queues();
4138 vm_page_unlock_queues();
4141 * Mark page busy for other threads.
4148 * Give up if the page is being written and there's a copy object
4150 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
4156 * Put this page into the physical map.
4158 type_of_fault
= DBG_CACHE_HIT_FAULT
;
4159 kr
= vm_fault_enter(m
,
4170 * Unlock everything, and return
4173 PAGE_WAKEUP_DONE(m
);
4174 UNLOCK_AND_DEALLOCATE
;
4181 * Routine: vm_fault_copy_cleanup
4183 * Release a page used by vm_fault_copy.
4187 vm_fault_copy_cleanup(
4191 vm_object_t object
= page
->object
;
4193 vm_object_lock(object
);
4194 PAGE_WAKEUP_DONE(page
);
4195 if (!page
->active
&& !page
->inactive
&& !page
->throttled
) {
4196 vm_page_lockspin_queues();
4197 if (!page
->active
&& !page
->inactive
&& !page
->throttled
)
4198 vm_page_activate(page
);
4199 vm_page_unlock_queues();
4201 vm_fault_cleanup(object
, top_page
);
4205 vm_fault_copy_dst_cleanup(
4210 if (page
!= VM_PAGE_NULL
) {
4211 object
= page
->object
;
4212 vm_object_lock(object
);
4213 vm_page_lockspin_queues();
4214 vm_page_unwire(page
, TRUE
);
4215 vm_page_unlock_queues();
4216 vm_object_paging_end(object
);
4217 vm_object_unlock(object
);
4222 * Routine: vm_fault_copy
4225 * Copy pages from one virtual memory object to another --
4226 * neither the source nor destination pages need be resident.
4228 * Before actually copying a page, the version associated with
4229 * the destination address map wil be verified.
4231 * In/out conditions:
4232 * The caller must hold a reference, but not a lock, to
4233 * each of the source and destination objects and to the
4237 * Returns KERN_SUCCESS if no errors were encountered in
4238 * reading or writing the data. Returns KERN_INTERRUPTED if
4239 * the operation was interrupted (only possible if the
4240 * "interruptible" argument is asserted). Other return values
4241 * indicate a permanent error in copying the data.
4243 * The actual amount of data copied will be returned in the
4244 * "copy_size" argument. In the event that the destination map
4245 * verification failed, this amount may be less than the amount
4250 vm_object_t src_object
,
4251 vm_object_offset_t src_offset
,
4252 vm_map_size_t
*copy_size
, /* INOUT */
4253 vm_object_t dst_object
,
4254 vm_object_offset_t dst_offset
,
4256 vm_map_version_t
*dst_version
,
4259 vm_page_t result_page
;
4262 vm_page_t src_top_page
;
4266 vm_page_t dst_top_page
;
4269 vm_map_size_t amount_left
;
4270 vm_object_t old_copy_object
;
4271 kern_return_t error
= 0;
4272 vm_fault_return_t result
;
4274 vm_map_size_t part_size
;
4275 struct vm_object_fault_info fault_info_src
;
4276 struct vm_object_fault_info fault_info_dst
;
4279 * In order not to confuse the clustered pageins, align
4280 * the different offsets on a page boundary.
4285 *copy_size -= amount_left; \
4289 amount_left
= *copy_size
;
4291 fault_info_src
.interruptible
= interruptible
;
4292 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
4293 fault_info_src
.user_tag
= 0;
4294 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
4295 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
4296 fault_info_src
.no_cache
= FALSE
;
4297 fault_info_src
.stealth
= TRUE
;
4298 fault_info_src
.mark_zf_absent
= FALSE
;
4300 fault_info_dst
.interruptible
= interruptible
;
4301 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
4302 fault_info_dst
.user_tag
= 0;
4303 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
4304 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
4305 fault_info_dst
.no_cache
= FALSE
;
4306 fault_info_dst
.stealth
= TRUE
;
4307 fault_info_dst
.mark_zf_absent
= FALSE
;
4309 do { /* while (amount_left > 0) */
4311 * There may be a deadlock if both source and destination
4312 * pages are the same. To avoid this deadlock, the copy must
4313 * start by getting the destination page in order to apply
4314 * COW semantics if any.
4317 RetryDestinationFault
: ;
4319 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
4321 vm_object_lock(dst_object
);
4322 vm_object_paging_begin(dst_object
);
4324 if (amount_left
> (vm_size_t
) -1) {
4325 /* 32-bit overflow */
4326 fault_info_dst
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
4328 fault_info_dst
.cluster_size
= (vm_size_t
) amount_left
;
4329 assert(fault_info_dst
.cluster_size
== amount_left
);
4332 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
4333 result
= vm_fault_page(dst_object
,
4334 vm_object_trunc_page(dst_offset
),
4335 VM_PROT_WRITE
|VM_PROT_READ
,
4337 &dst_prot
, &dst_page
, &dst_top_page
,
4340 dst_map
->no_zero_fill
,
4341 FALSE
, &fault_info_dst
);
4343 case VM_FAULT_SUCCESS
:
4345 case VM_FAULT_RETRY
:
4346 goto RetryDestinationFault
;
4347 case VM_FAULT_MEMORY_SHORTAGE
:
4348 if (vm_page_wait(interruptible
))
4349 goto RetryDestinationFault
;
4351 case VM_FAULT_INTERRUPTED
:
4352 RETURN(MACH_SEND_INTERRUPTED
);
4353 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
4354 /* success but no VM page: fail the copy */
4355 vm_object_paging_end(dst_object
);
4356 vm_object_unlock(dst_object
);
4358 case VM_FAULT_MEMORY_ERROR
:
4362 return(KERN_MEMORY_ERROR
);
4364 panic("vm_fault_copy: unexpected error 0x%x from "
4365 "vm_fault_page()\n", result
);
4367 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
4369 old_copy_object
= dst_page
->object
->copy
;
4372 * There exists the possiblity that the source and
4373 * destination page are the same. But we can't
4374 * easily determine that now. If they are the
4375 * same, the call to vm_fault_page() for the
4376 * destination page will deadlock. To prevent this we
4377 * wire the page so we can drop busy without having
4378 * the page daemon steal the page. We clean up the
4379 * top page but keep the paging reference on the object
4380 * holding the dest page so it doesn't go away.
4383 vm_page_lockspin_queues();
4384 vm_page_wire(dst_page
);
4385 vm_page_unlock_queues();
4386 PAGE_WAKEUP_DONE(dst_page
);
4387 vm_object_unlock(dst_page
->object
);
4389 if (dst_top_page
!= VM_PAGE_NULL
) {
4390 vm_object_lock(dst_object
);
4391 VM_PAGE_FREE(dst_top_page
);
4392 vm_object_paging_end(dst_object
);
4393 vm_object_unlock(dst_object
);
4398 if (src_object
== VM_OBJECT_NULL
) {
4400 * No source object. We will just
4401 * zero-fill the page in dst_object.
4403 src_page
= VM_PAGE_NULL
;
4404 result_page
= VM_PAGE_NULL
;
4406 vm_object_lock(src_object
);
4407 src_page
= vm_page_lookup(src_object
,
4408 vm_object_trunc_page(src_offset
));
4409 if (src_page
== dst_page
) {
4410 src_prot
= dst_prot
;
4411 result_page
= VM_PAGE_NULL
;
4413 src_prot
= VM_PROT_READ
;
4414 vm_object_paging_begin(src_object
);
4416 if (amount_left
> (vm_size_t
) -1) {
4417 /* 32-bit overflow */
4418 fault_info_src
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
4420 fault_info_src
.cluster_size
= (vm_size_t
) amount_left
;
4421 assert(fault_info_src
.cluster_size
== amount_left
);
4425 "vm_fault_copy(2) -> vm_fault_page\n",
4427 result
= vm_fault_page(
4429 vm_object_trunc_page(src_offset
),
4430 VM_PROT_READ
, FALSE
,
4432 &result_page
, &src_top_page
,
4433 (int *)0, &error
, FALSE
,
4434 FALSE
, &fault_info_src
);
4437 case VM_FAULT_SUCCESS
:
4439 case VM_FAULT_RETRY
:
4440 goto RetrySourceFault
;
4441 case VM_FAULT_MEMORY_SHORTAGE
:
4442 if (vm_page_wait(interruptible
))
4443 goto RetrySourceFault
;
4445 case VM_FAULT_INTERRUPTED
:
4446 vm_fault_copy_dst_cleanup(dst_page
);
4447 RETURN(MACH_SEND_INTERRUPTED
);
4448 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
4449 /* success but no VM page: fail */
4450 vm_object_paging_end(src_object
);
4451 vm_object_unlock(src_object
);
4453 case VM_FAULT_MEMORY_ERROR
:
4454 vm_fault_copy_dst_cleanup(dst_page
);
4458 return(KERN_MEMORY_ERROR
);
4460 panic("vm_fault_copy(2): unexpected "
4462 "vm_fault_page()\n", result
);
4466 assert((src_top_page
== VM_PAGE_NULL
) ==
4467 (result_page
->object
== src_object
));
4469 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
4470 vm_object_unlock(result_page
->object
);
4473 if (!vm_map_verify(dst_map
, dst_version
)) {
4474 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4475 vm_fault_copy_cleanup(result_page
, src_top_page
);
4476 vm_fault_copy_dst_cleanup(dst_page
);
4480 vm_object_lock(dst_page
->object
);
4482 if (dst_page
->object
->copy
!= old_copy_object
) {
4483 vm_object_unlock(dst_page
->object
);
4484 vm_map_verify_done(dst_map
, dst_version
);
4485 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4486 vm_fault_copy_cleanup(result_page
, src_top_page
);
4487 vm_fault_copy_dst_cleanup(dst_page
);
4490 vm_object_unlock(dst_page
->object
);
4493 * Copy the page, and note that it is dirty
4497 if (!page_aligned(src_offset
) ||
4498 !page_aligned(dst_offset
) ||
4499 !page_aligned(amount_left
)) {
4501 vm_object_offset_t src_po
,
4504 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
4505 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
4507 if (dst_po
> src_po
) {
4508 part_size
= PAGE_SIZE
- dst_po
;
4510 part_size
= PAGE_SIZE
- src_po
;
4512 if (part_size
> (amount_left
)){
4513 part_size
= amount_left
;
4516 if (result_page
== VM_PAGE_NULL
) {
4517 assert((vm_offset_t
) dst_po
== dst_po
);
4518 assert((vm_size_t
) part_size
== part_size
);
4519 vm_page_part_zero_fill(dst_page
,
4520 (vm_offset_t
) dst_po
,
4521 (vm_size_t
) part_size
);
4523 assert((vm_offset_t
) src_po
== src_po
);
4524 assert((vm_offset_t
) dst_po
== dst_po
);
4525 assert((vm_size_t
) part_size
== part_size
);
4526 vm_page_part_copy(result_page
,
4527 (vm_offset_t
) src_po
,
4529 (vm_offset_t
) dst_po
,
4530 (vm_size_t
)part_size
);
4531 if(!dst_page
->dirty
){
4532 vm_object_lock(dst_object
);
4533 dst_page
->dirty
= TRUE
;
4534 vm_object_unlock(dst_page
->object
);
4539 part_size
= PAGE_SIZE
;
4541 if (result_page
== VM_PAGE_NULL
)
4542 vm_page_zero_fill(dst_page
);
4544 vm_page_copy(result_page
, dst_page
);
4545 if(!dst_page
->dirty
){
4546 vm_object_lock(dst_object
);
4547 dst_page
->dirty
= TRUE
;
4548 vm_object_unlock(dst_page
->object
);
4555 * Unlock everything, and return
4558 vm_map_verify_done(dst_map
, dst_version
);
4560 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4561 vm_fault_copy_cleanup(result_page
, src_top_page
);
4562 vm_fault_copy_dst_cleanup(dst_page
);
4564 amount_left
-= part_size
;
4565 src_offset
+= part_size
;
4566 dst_offset
+= part_size
;
4567 } while (amount_left
> 0);
4569 RETURN(KERN_SUCCESS
);
4575 #if VM_FAULT_CLASSIFY
4577 * Temporary statistics gathering support.
4581 * Statistics arrays:
4583 #define VM_FAULT_TYPES_MAX 5
4584 #define VM_FAULT_LEVEL_MAX 8
4586 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
4588 #define VM_FAULT_TYPE_ZERO_FILL 0
4589 #define VM_FAULT_TYPE_MAP_IN 1
4590 #define VM_FAULT_TYPE_PAGER 2
4591 #define VM_FAULT_TYPE_COPY 3
4592 #define VM_FAULT_TYPE_OTHER 4
4596 vm_fault_classify(vm_object_t object
,
4597 vm_object_offset_t offset
,
4598 vm_prot_t fault_type
)
4600 int type
, level
= 0;
4604 m
= vm_page_lookup(object
, offset
);
4605 if (m
!= VM_PAGE_NULL
) {
4606 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
) {
4607 type
= VM_FAULT_TYPE_OTHER
;
4610 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
4611 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
4612 type
= VM_FAULT_TYPE_MAP_IN
;
4615 type
= VM_FAULT_TYPE_COPY
;
4619 if (object
->pager_created
) {
4620 type
= VM_FAULT_TYPE_PAGER
;
4623 if (object
->shadow
== VM_OBJECT_NULL
) {
4624 type
= VM_FAULT_TYPE_ZERO_FILL
;
4628 offset
+= object
->shadow_offset
;
4629 object
= object
->shadow
;
4635 if (level
> VM_FAULT_LEVEL_MAX
)
4636 level
= VM_FAULT_LEVEL_MAX
;
4638 vm_fault_stats
[type
][level
] += 1;
4643 /* cleanup routine to call from debugger */
4646 vm_fault_classify_init(void)
4650 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
4651 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
4652 vm_fault_stats
[type
][level
] = 0;
4658 #endif /* VM_FAULT_CLASSIFY */
4661 extern int cs_validation
;
4664 vm_page_validate_cs_mapped(
4669 vm_object_offset_t offset
;
4671 memory_object_t pager
;
4673 boolean_t validated
, tainted
;
4676 vm_object_lock_assert_exclusive(page
->object
);
4678 if (!cs_validation
) {
4682 if (page
->wpmapped
&& !page
->cs_tainted
) {
4684 * This page was mapped for "write" access sometime in the
4685 * past and could still be modifiable in the future.
4686 * Consider it tainted.
4687 * [ If the page was already found to be "tainted", no
4688 * need to re-validate. ]
4690 page
->cs_validated
= TRUE
;
4691 page
->cs_tainted
= TRUE
;
4693 printf("CODESIGNING: vm_page_validate_cs: "
4694 "page %p obj %p off 0x%llx "
4696 page
, page
->object
, page
->offset
);
4698 vm_cs_validated_dirtied
++;
4701 if (page
->cs_validated
) {
4707 object
= page
->object
;
4708 assert(object
->code_signed
);
4709 offset
= page
->offset
;
4711 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
4713 * The object is terminating and we don't have its pager
4714 * so we can't validate the data...
4719 * Since we get here to validate a page that was brought in by
4720 * the pager, we know that this pager is all setup and ready
4723 assert(!object
->internal
);
4724 assert(object
->pager
!= NULL
);
4725 assert(object
->pager_ready
);
4727 pager
= object
->pager
;
4728 assert(object
->paging_in_progress
);
4729 kr
= vnode_pager_get_object_cs_blobs(pager
, &blobs
);
4730 if (kr
!= KERN_SUCCESS
) {
4734 /* verify the SHA1 hash for this page */
4735 validated
= cs_validate_page(blobs
,
4736 offset
+ object
->paging_offset
,
4737 (const void *)kaddr
,
4740 page
->cs_validated
= validated
;
4742 page
->cs_tainted
= tainted
;
4747 vm_page_validate_cs(
4751 vm_object_offset_t offset
;
4752 vm_map_offset_t koffset
;
4753 vm_map_size_t ksize
;
4756 boolean_t busy_page
;
4758 vm_object_lock_assert_held(page
->object
);
4760 if (!cs_validation
) {
4764 if (page
->wpmapped
&& !page
->cs_tainted
) {
4765 vm_object_lock_assert_exclusive(page
->object
);
4768 * This page was mapped for "write" access sometime in the
4769 * past and could still be modifiable in the future.
4770 * Consider it tainted.
4771 * [ If the page was already found to be "tainted", no
4772 * need to re-validate. ]
4774 page
->cs_validated
= TRUE
;
4775 page
->cs_tainted
= TRUE
;
4777 printf("CODESIGNING: vm_page_validate_cs: "
4778 "page %p obj %p off 0x%llx "
4780 page
, page
->object
, page
->offset
);
4782 vm_cs_validated_dirtied
++;
4785 if (page
->cs_validated
) {
4789 vm_object_lock_assert_exclusive(page
->object
);
4791 object
= page
->object
;
4792 assert(object
->code_signed
);
4793 offset
= page
->offset
;
4795 busy_page
= page
->busy
;
4797 /* keep page busy while we map (and unlock) the VM object */
4802 * Take a paging reference on the VM object
4803 * to protect it from collapse or bypass,
4804 * and keep it from disappearing too.
4806 vm_object_paging_begin(object
);
4808 /* map the page in the kernel address space */
4810 ksize
= PAGE_SIZE_64
;
4811 kr
= vm_paging_map_object(&koffset
,
4817 FALSE
); /* can't unlock object ! */
4818 if (kr
!= KERN_SUCCESS
) {
4819 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr
);
4821 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
4823 /* validate the mapped page */
4824 vm_page_validate_cs_mapped(page
, (const void *) kaddr
);
4827 assert(object
== page
->object
);
4828 vm_object_lock_assert_exclusive(object
);
4831 PAGE_WAKEUP_DONE(page
);
4834 /* unmap the map from the kernel address space */
4835 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
4840 vm_object_paging_end(object
);