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);
178 unsigned long vm_cs_validates
= 0;
179 unsigned long vm_cs_revalidates
= 0;
180 unsigned long vm_cs_query_modified
= 0;
181 unsigned long vm_cs_validated_dirtied
= 0;
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
);
1155 if (must_be_resident
)
1156 vm_object_paging_end(object
);
1157 else if (object
!= first_object
) {
1158 vm_object_paging_end(object
);
1165 vm_page_lockspin_queues();
1166 VM_PAGE_QUEUES_REMOVE(m
);
1167 vm_page_unlock_queues();
1170 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
1173 offset
+object
->shadow_offset
,0);
1175 offset
+= object
->shadow_offset
;
1176 fault_info
->lo_offset
+= object
->shadow_offset
;
1177 fault_info
->hi_offset
+= object
->shadow_offset
;
1178 access_required
= VM_PROT_READ
;
1180 vm_object_lock(next_object
);
1181 vm_object_unlock(object
);
1182 object
= next_object
;
1183 vm_object_paging_begin(object
);
1186 * reset to default type of fault
1188 my_fault
= DBG_CACHE_HIT_FAULT
;
1194 && ((object
!= first_object
) || (object
->copy
!= VM_OBJECT_NULL
))
1195 && (fault_type
& VM_PROT_WRITE
)) {
1197 * This is a copy-on-write fault that will
1198 * cause us to revoke access to this page, but
1199 * this page is in the process of being cleaned
1200 * in a clustered pageout. We must wait until
1201 * the cleaning operation completes before
1202 * revoking access to the original page,
1203 * otherwise we might attempt to remove a
1207 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
1210 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
1214 * take an extra ref so that object won't die
1216 vm_object_reference_locked(object
);
1218 vm_fault_cleanup(object
, first_m
);
1220 counter(c_vm_fault_page_block_backoff_kernel
++);
1221 vm_object_lock(object
);
1222 assert(object
->ref_count
> 0);
1224 m
= vm_page_lookup(object
, offset
);
1226 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
1227 PAGE_ASSERT_WAIT(m
, interruptible
);
1229 vm_object_unlock(object
);
1230 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1231 vm_object_deallocate(object
);
1235 vm_object_unlock(object
);
1237 vm_object_deallocate(object
);
1238 thread_interrupt_level(interruptible_state
);
1240 return (VM_FAULT_RETRY
);
1243 if (type_of_fault
== NULL
&& m
->speculative
&&
1244 !(fault_info
!= NULL
&& fault_info
->stealth
)) {
1246 * If we were passed a non-NULL pointer for
1247 * "type_of_fault", than we came from
1248 * vm_fault... we'll let it deal with
1249 * this condition, since it
1250 * needs to see m->speculative to correctly
1251 * account the pageins, otherwise...
1252 * take it off the speculative queue, we'll
1253 * let the caller of vm_fault_page deal
1254 * with getting it onto the correct queue
1256 * If the caller specified in fault_info that
1257 * it wants a "stealth" fault, we also leave
1258 * the page in the speculative queue.
1260 vm_page_lockspin_queues();
1261 VM_PAGE_QUEUES_REMOVE(m
);
1262 vm_page_unlock_queues();
1268 * the user needs access to a page that we
1269 * encrypted before paging it out.
1270 * Decrypt the page now.
1271 * Keep it busy to prevent anyone from
1272 * accessing it during the decryption.
1275 vm_page_decrypt(m
, 0);
1276 assert(object
== m
->object
);
1278 PAGE_WAKEUP_DONE(m
);
1281 * Retry from the top, in case
1282 * something changed while we were
1287 ASSERT_PAGE_DECRYPTED(m
);
1289 if (m
->object
->code_signed
) {
1292 * We just paged in a page from a signed
1293 * memory object but we don't need to
1294 * validate it now. We'll validate it if
1295 * when it gets mapped into a user address
1296 * space for the first time or when the page
1297 * gets copied to another object as a result
1298 * of a copy-on-write.
1303 * We mark the page busy and leave it on
1304 * the pageout queues. If the pageout
1305 * deamon comes across it, then it will
1306 * remove the page from the queue, but not the object
1309 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1312 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
1313 object
, offset
, m
, 0, 0);
1323 * we get here when there is no page present in the object at
1324 * the offset we're interested in... we'll allocate a page
1325 * at this point if the pager associated with
1326 * this object can provide the data or we're the top object...
1327 * object is locked; m == NULL
1329 look_for_page
= (object
->pager_created
&& (MUST_ASK_PAGER(object
, offset
) == TRUE
) && !data_supply
);
1332 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
1334 if ((look_for_page
|| (object
== first_object
)) && !must_be_resident
&& !object
->phys_contiguous
) {
1336 * Allocate a new page for this object/offset pair
1340 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
1342 if (m
== VM_PAGE_NULL
) {
1344 vm_fault_cleanup(object
, first_m
);
1345 thread_interrupt_level(interruptible_state
);
1347 return (VM_FAULT_MEMORY_SHORTAGE
);
1349 vm_page_insert(m
, object
, offset
);
1351 if (look_for_page
&& !must_be_resident
) {
1355 * If the memory manager is not ready, we
1356 * cannot make requests.
1358 if (!object
->pager_ready
) {
1360 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
1362 if (m
!= VM_PAGE_NULL
)
1366 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
1367 object
, offset
, 0, 0, 0);
1370 * take an extra ref so object won't die
1372 vm_object_reference_locked(object
);
1373 vm_fault_cleanup(object
, first_m
);
1374 counter(c_vm_fault_page_block_backoff_kernel
++);
1376 vm_object_lock(object
);
1377 assert(object
->ref_count
> 0);
1379 if (!object
->pager_ready
) {
1380 wait_result
= vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGER_READY
, interruptible
);
1382 vm_object_unlock(object
);
1383 if (wait_result
== THREAD_WAITING
)
1384 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1385 vm_object_deallocate(object
);
1389 vm_object_unlock(object
);
1390 vm_object_deallocate(object
);
1391 thread_interrupt_level(interruptible_state
);
1393 return (VM_FAULT_RETRY
);
1396 if (!object
->internal
&& !object
->phys_contiguous
&& object
->paging_in_progress
> vm_object_pagein_throttle
) {
1398 * If there are too many outstanding page
1399 * requests pending on this external object, we
1400 * wait for them to be resolved now.
1403 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1405 if (m
!= VM_PAGE_NULL
)
1408 * take an extra ref so object won't die
1410 vm_object_reference_locked(object
);
1412 vm_fault_cleanup(object
, first_m
);
1414 counter(c_vm_fault_page_block_backoff_kernel
++);
1416 vm_object_lock(object
);
1417 assert(object
->ref_count
> 0);
1419 if (object
->paging_in_progress
> vm_object_pagein_throttle
) {
1420 vm_object_assert_wait(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
, interruptible
);
1422 vm_object_unlock(object
);
1423 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1424 vm_object_deallocate(object
);
1428 vm_object_unlock(object
);
1429 vm_object_deallocate(object
);
1430 thread_interrupt_level(interruptible_state
);
1432 return (VM_FAULT_RETRY
);
1435 if (m
!= VM_PAGE_NULL
) {
1437 * Indicate that the page is waiting for data
1438 * from the memory manager.
1440 m
->list_req_pending
= TRUE
;
1445 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1449 * It's possible someone called vm_object_destroy while we weren't
1450 * holding the object lock. If that has happened, then bail out
1454 pager
= object
->pager
;
1456 if (pager
== MEMORY_OBJECT_NULL
) {
1457 vm_fault_cleanup(object
, first_m
);
1458 thread_interrupt_level(interruptible_state
);
1459 return VM_FAULT_MEMORY_ERROR
;
1463 * We have an absent page in place for the faulting offset,
1464 * so we can release the object lock.
1467 vm_object_unlock(object
);
1470 * If this object uses a copy_call strategy,
1471 * and we are interested in a copy of this object
1472 * (having gotten here only by following a
1473 * shadow chain), then tell the memory manager
1474 * via a flag added to the desired_access
1475 * parameter, so that it can detect a race
1476 * between our walking down the shadow chain
1477 * and its pushing pages up into a copy of
1478 * the object that it manages.
1480 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&& object
!= first_object
)
1481 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1483 wants_copy_flag
= VM_PROT_NONE
;
1486 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1488 access_required
| wants_copy_flag
, 0);
1491 * Call the memory manager to retrieve the data.
1493 rc
= memory_object_data_request(
1495 offset
+ object
->paging_offset
,
1497 access_required
| wants_copy_flag
,
1498 (memory_object_fault_info_t
)fault_info
);
1501 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1503 vm_object_lock(object
);
1505 if (rc
!= KERN_SUCCESS
) {
1507 vm_fault_cleanup(object
, first_m
);
1508 thread_interrupt_level(interruptible_state
);
1510 return ((rc
== MACH_SEND_INTERRUPTED
) ?
1511 VM_FAULT_INTERRUPTED
:
1512 VM_FAULT_MEMORY_ERROR
);
1515 clock_usec_t tv_usec
;
1517 clock_get_system_microtime(&tv_sec
, &tv_usec
);
1518 current_thread()->t_page_creation_time
= tv_sec
;
1519 current_thread()->t_page_creation_count
= 0;
1521 if ((interruptible
!= THREAD_UNINT
) && (current_thread()->sched_mode
& TH_MODE_ABORT
)) {
1523 vm_fault_cleanup(object
, first_m
);
1524 thread_interrupt_level(interruptible_state
);
1526 return (VM_FAULT_INTERRUPTED
);
1528 if (m
== VM_PAGE_NULL
&& object
->phys_contiguous
) {
1530 * No page here means that the object we
1531 * initially looked up was "physically
1532 * contiguous" (i.e. device memory). However,
1533 * with Virtual VRAM, the object might not
1534 * be backed by that device memory anymore,
1535 * so we're done here only if the object is
1536 * still "phys_contiguous".
1537 * Otherwise, if the object is no longer
1538 * "phys_contiguous", we need to retry the
1539 * page fault against the object's new backing
1540 * store (different memory object).
1546 * potentially a pagein fault
1547 * if we make it through the state checks
1548 * above, than we'll count it as such
1550 my_fault
= DBG_PAGEIN_FAULT
;
1553 * Retry with same object/offset, since new data may
1554 * be in a different page (i.e., m is meaningless at
1561 * We get here if the object has no pager, or an existence map
1562 * exists and indicates the page isn't present on the pager
1563 * or we're unwiring a page. If a pager exists, but there
1564 * is no existence map, then the m->absent case above handles
1565 * the ZF case when the pager can't provide the page
1568 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1570 if (object
== first_object
)
1573 assert(m
== VM_PAGE_NULL
);
1576 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1580 next_object
= object
->shadow
;
1582 if (next_object
== VM_OBJECT_NULL
) {
1584 * we've hit the bottom of the shadown chain,
1585 * fill the page in the top object with zeros.
1587 assert(!must_be_resident
);
1589 if (object
!= first_object
) {
1590 vm_object_paging_end(object
);
1591 vm_object_unlock(object
);
1593 object
= first_object
;
1594 offset
= first_offset
;
1595 vm_object_lock(object
);
1598 assert(m
->object
== object
);
1599 first_m
= VM_PAGE_NULL
;
1602 * check for any conditions that prevent
1603 * us from creating a new zero-fill page
1604 * vm_fault_check will do all of the
1605 * fault cleanup in the case of an error condition
1606 * including resetting the thread_interrupt_level
1608 error
= vm_fault_check(object
, m
, first_m
, interruptible_state
);
1610 if (error
!= VM_FAULT_SUCCESS
)
1613 if (m
== VM_PAGE_NULL
) {
1616 if (m
== VM_PAGE_NULL
) {
1617 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1618 thread_interrupt_level(interruptible_state
);
1620 return (VM_FAULT_MEMORY_SHORTAGE
);
1622 vm_page_insert(m
, object
, offset
);
1624 my_fault
= vm_fault_zero_page(m
, no_zero_fill
);
1630 * Move on to the next object. Lock the next
1631 * object before unlocking the current one.
1633 if ((object
!= first_object
) || must_be_resident
)
1634 vm_object_paging_end(object
);
1636 offset
+= object
->shadow_offset
;
1637 fault_info
->lo_offset
+= object
->shadow_offset
;
1638 fault_info
->hi_offset
+= object
->shadow_offset
;
1639 access_required
= VM_PROT_READ
;
1641 vm_object_lock(next_object
);
1642 vm_object_unlock(object
);
1644 object
= next_object
;
1645 vm_object_paging_begin(object
);
1650 * PAGE HAS BEEN FOUND.
1653 * busy, so that we can play with it;
1654 * not absent, so that nobody else will fill it;
1655 * possibly eligible for pageout;
1657 * The top-level page (first_m) is:
1658 * VM_PAGE_NULL if the page was found in the
1660 * busy, not absent, and ineligible for pageout.
1662 * The current object (object) is locked. A paging
1663 * reference is held for the current and top-level
1668 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1670 #if EXTRA_ASSERTIONS
1671 assert(m
->busy
&& !m
->absent
);
1672 assert((first_m
== VM_PAGE_NULL
) ||
1673 (first_m
->busy
&& !first_m
->absent
&&
1674 !first_m
->active
&& !first_m
->inactive
));
1675 #endif /* EXTRA_ASSERTIONS */
1679 * If we found a page, we must have decrypted it before we
1682 ASSERT_PAGE_DECRYPTED(m
);
1685 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1687 first_object
, first_m
);
1690 * If the page is being written, but isn't
1691 * already owned by the top-level object,
1692 * we have to copy it into a new page owned
1693 * by the top-level object.
1695 if (object
!= first_object
) {
1698 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1700 if (fault_type
& VM_PROT_WRITE
) {
1704 * We only really need to copy if we
1707 assert(!must_be_resident
);
1710 * are we protecting the system from
1711 * backing store exhaustion. If so
1712 * sleep unless we are privileged.
1714 if (vm_backing_store_low
) {
1715 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1718 vm_fault_cleanup(object
, first_m
);
1720 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1722 thread_block(THREAD_CONTINUE_NULL
);
1723 thread_interrupt_level(interruptible_state
);
1725 return (VM_FAULT_RETRY
);
1729 * If we try to collapse first_object at this
1730 * point, we may deadlock when we try to get
1731 * the lock on an intermediate object (since we
1732 * have the bottom object locked). We can't
1733 * unlock the bottom object, because the page
1734 * we found may move (by collapse) if we do.
1736 * Instead, we first copy the page. Then, when
1737 * we have no more use for the bottom object,
1738 * we unlock it and try to collapse.
1740 * Note that we copy the page even if we didn't
1741 * need to... that's the breaks.
1745 * Allocate a page for the copy
1747 copy_m
= vm_page_grab();
1749 if (copy_m
== VM_PAGE_NULL
) {
1752 vm_fault_cleanup(object
, first_m
);
1753 thread_interrupt_level(interruptible_state
);
1755 return (VM_FAULT_MEMORY_SHORTAGE
);
1758 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
1762 vm_page_copy(m
, copy_m
);
1765 * If another map is truly sharing this
1766 * page with us, we have to flush all
1767 * uses of the original page, since we
1768 * can't distinguish those which want the
1769 * original from those which need the
1772 * XXXO If we know that only one map has
1773 * access to this page, then we could
1774 * avoid the pmap_disconnect() call.
1777 pmap_disconnect(m
->phys_page
);
1779 assert(!m
->cleaning
);
1782 * We no longer need the old page or object.
1784 PAGE_WAKEUP_DONE(m
);
1785 vm_object_paging_end(object
);
1786 vm_object_unlock(object
);
1788 my_fault
= DBG_COW_FAULT
;
1789 VM_STAT_INCR(cow_faults
);
1790 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
1791 current_task()->cow_faults
++;
1793 object
= first_object
;
1794 offset
= first_offset
;
1796 vm_object_lock(object
);
1798 * get rid of the place holder
1799 * page that we soldered in earlier
1801 VM_PAGE_FREE(first_m
);
1802 first_m
= VM_PAGE_NULL
;
1805 * and replace it with the
1806 * page we just copied into
1808 assert(copy_m
->busy
);
1809 vm_page_insert(copy_m
, object
, offset
);
1810 copy_m
->dirty
= TRUE
;
1814 * Now that we've gotten the copy out of the
1815 * way, let's try to collapse the top object.
1816 * But we have to play ugly games with
1817 * paging_in_progress to do that...
1819 vm_object_paging_end(object
);
1820 vm_object_collapse(object
, offset
, TRUE
);
1821 vm_object_paging_begin(object
);
1824 *protection
&= (~VM_PROT_WRITE
);
1827 * Now check whether the page needs to be pushed into the
1828 * copy object. The use of asymmetric copy on write for
1829 * shared temporary objects means that we may do two copies to
1830 * satisfy the fault; one above to get the page from a
1831 * shadowed object, and one here to push it into the copy.
1833 try_failed_count
= 0;
1835 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
) {
1836 vm_object_offset_t copy_offset
;
1840 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1843 * If the page is being written, but hasn't been
1844 * copied to the copy-object, we have to copy it there.
1846 if ((fault_type
& VM_PROT_WRITE
) == 0) {
1847 *protection
&= ~VM_PROT_WRITE
;
1852 * If the page was guaranteed to be resident,
1853 * we must have already performed the copy.
1855 if (must_be_resident
)
1859 * Try to get the lock on the copy_object.
1861 if (!vm_object_lock_try(copy_object
)) {
1863 vm_object_unlock(object
);
1866 mutex_pause(try_failed_count
); /* wait a bit */
1867 vm_object_lock(object
);
1871 try_failed_count
= 0;
1874 * Make another reference to the copy-object,
1875 * to keep it from disappearing during the
1878 vm_object_reference_locked(copy_object
);
1881 * Does the page exist in the copy?
1883 copy_offset
= first_offset
- copy_object
->shadow_offset
;
1885 if (copy_object
->size
<= copy_offset
)
1887 * Copy object doesn't cover this page -- do nothing.
1890 else if ((copy_m
= vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
1892 * Page currently exists in the copy object
1896 * If the page is being brought
1897 * in, wait for it and then retry.
1902 * take an extra ref so object won't die
1904 vm_object_reference_locked(copy_object
);
1905 vm_object_unlock(copy_object
);
1906 vm_fault_cleanup(object
, first_m
);
1907 counter(c_vm_fault_page_block_backoff_kernel
++);
1909 vm_object_lock(copy_object
);
1910 assert(copy_object
->ref_count
> 0);
1911 VM_OBJ_RES_DECR(copy_object
);
1912 vm_object_lock_assert_exclusive(copy_object
);
1913 copy_object
->ref_count
--;
1914 assert(copy_object
->ref_count
> 0);
1915 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
1918 * it's OK if the "copy_m" page is encrypted,
1919 * because we're not moving it nor handling its
1922 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
1923 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
1925 vm_object_unlock(copy_object
);
1926 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1927 vm_object_deallocate(copy_object
);
1931 vm_object_unlock(copy_object
);
1932 vm_object_deallocate(copy_object
);
1933 thread_interrupt_level(interruptible_state
);
1935 return (VM_FAULT_RETRY
);
1939 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
1941 * If PAGED_OUT is TRUE, then the page used to exist
1942 * in the copy-object, and has already been paged out.
1943 * We don't need to repeat this. If PAGED_OUT is
1944 * FALSE, then either we don't know (!pager_created,
1945 * for example) or it hasn't been paged out.
1946 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
1947 * We must copy the page to the copy object.
1950 if (vm_backing_store_low
) {
1952 * we are protecting the system from
1953 * backing store exhaustion. If so
1954 * sleep unless we are privileged.
1956 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
)) {
1957 assert_wait((event_t
)&vm_backing_store_low
, THREAD_UNINT
);
1960 VM_OBJ_RES_DECR(copy_object
);
1961 vm_object_lock_assert_exclusive(copy_object
);
1962 copy_object
->ref_count
--;
1963 assert(copy_object
->ref_count
> 0);
1965 vm_object_unlock(copy_object
);
1966 vm_fault_cleanup(object
, first_m
);
1967 thread_block(THREAD_CONTINUE_NULL
);
1968 thread_interrupt_level(interruptible_state
);
1970 return (VM_FAULT_RETRY
);
1974 * Allocate a page for the copy
1976 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
1978 if (copy_m
== VM_PAGE_NULL
) {
1981 VM_OBJ_RES_DECR(copy_object
);
1982 vm_object_lock_assert_exclusive(copy_object
);
1983 copy_object
->ref_count
--;
1984 assert(copy_object
->ref_count
> 0);
1986 vm_object_unlock(copy_object
);
1987 vm_fault_cleanup(object
, first_m
);
1988 thread_interrupt_level(interruptible_state
);
1990 return (VM_FAULT_MEMORY_SHORTAGE
);
1993 * Must copy page into copy-object.
1995 vm_page_copy(m
, copy_m
);
1998 * If the old page was in use by any users
1999 * of the copy-object, it must be removed
2000 * from all pmaps. (We can't know which
2004 pmap_disconnect(m
->phys_page
);
2007 * If there's a pager, then immediately
2008 * page out this page, using the "initialize"
2009 * option. Else, we use the copy.
2011 if ((!copy_object
->pager_created
)
2013 || vm_external_state_get(copy_object
->existence_map
, copy_offset
) == VM_EXTERNAL_STATE_ABSENT
2017 vm_page_lockspin_queues();
2018 assert(!m
->cleaning
);
2019 vm_page_activate(copy_m
);
2020 vm_page_unlock_queues();
2022 copy_m
->dirty
= TRUE
;
2023 PAGE_WAKEUP_DONE(copy_m
);
2026 assert(copy_m
->busy
== TRUE
);
2027 assert(!m
->cleaning
);
2030 * dirty is protected by the object lock
2032 copy_m
->dirty
= TRUE
;
2035 * The page is already ready for pageout:
2036 * not on pageout queues and busy.
2037 * Unlock everything except the
2038 * copy_object itself.
2040 vm_object_unlock(object
);
2043 * Write the page to the copy-object,
2044 * flushing it from the kernel.
2046 vm_pageout_initialize_page(copy_m
);
2049 * Since the pageout may have
2050 * temporarily dropped the
2051 * copy_object's lock, we
2052 * check whether we'll have
2053 * to deallocate the hard way.
2055 if ((copy_object
->shadow
!= object
) || (copy_object
->ref_count
== 1)) {
2056 vm_object_unlock(copy_object
);
2057 vm_object_deallocate(copy_object
);
2058 vm_object_lock(object
);
2063 * Pick back up the old object's
2064 * lock. [It is safe to do so,
2065 * since it must be deeper in the
2068 vm_object_lock(object
);
2071 * Because we're pushing a page upward
2072 * in the object tree, we must restart
2073 * any faults that are waiting here.
2074 * [Note that this is an expansion of
2075 * PAGE_WAKEUP that uses the THREAD_RESTART
2076 * wait result]. Can't turn off the page's
2077 * busy bit because we're not done with it.
2081 thread_wakeup_with_result((event_t
) m
, THREAD_RESTART
);
2085 * The reference count on copy_object must be
2086 * at least 2: one for our extra reference,
2087 * and at least one from the outside world
2088 * (we checked that when we last locked
2091 vm_object_lock_assert_exclusive(copy_object
);
2092 copy_object
->ref_count
--;
2093 assert(copy_object
->ref_count
> 0);
2095 VM_OBJ_RES_DECR(copy_object
);
2096 vm_object_unlock(copy_object
);
2103 *top_page
= first_m
;
2106 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
2107 object
, offset
, m
, first_m
, 0);
2109 if (m
!= VM_PAGE_NULL
) {
2110 retval
= VM_FAULT_SUCCESS
;
2111 if (my_fault
== DBG_PAGEIN_FAULT
) {
2113 VM_STAT_INCR(pageins
);
2114 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
2115 DTRACE_VM2(maj_fault
, int, 1, (uint64_t *), NULL
);
2116 current_task()->pageins
++;
2118 if (m
->object
->internal
) {
2119 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
2120 my_fault
= DBG_PAGEIND_FAULT
;
2122 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
2123 my_fault
= DBG_PAGEINV_FAULT
;
2127 * evaluate access pattern and update state
2128 * vm_fault_deactivate_behind depends on the
2129 * state being up to date
2131 vm_fault_is_sequential(object
, offset
, fault_info
->behavior
);
2133 vm_fault_deactivate_behind(object
, offset
, fault_info
->behavior
);
2136 *type_of_fault
= my_fault
;
2138 retval
= VM_FAULT_SUCCESS_NO_VM_PAGE
;
2139 assert(first_m
== VM_PAGE_NULL
);
2140 assert(object
== first_object
);
2143 thread_interrupt_level(interruptible_state
);
2146 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
2151 thread_interrupt_level(interruptible_state
);
2153 if (wait_result
== THREAD_INTERRUPTED
)
2154 return (VM_FAULT_INTERRUPTED
);
2155 return (VM_FAULT_RETRY
);
2164 * When soft faulting a page, we have to validate the page if:
2165 * 1. the page is being mapped in user space
2166 * 2. the page hasn't already been found to be "tainted"
2167 * 3. the page belongs to a code-signed object
2168 * 4. the page has not been validated yet or has been mapped for write.
2170 #define VM_FAULT_NEED_CS_VALIDATION(pmap, page) \
2171 ((pmap) != kernel_pmap /*1*/ && \
2172 !(page)->cs_tainted /*2*/ && \
2173 (page)->object->code_signed /*3*/ && \
2174 (!(page)->cs_validated || (page)->wpmapped /*4*/))
2178 * page queue lock must NOT be held
2179 * m->object must be locked
2181 * NOTE: m->object could be locked "shared" only if we are called
2182 * from vm_fault() as part of a soft fault. If so, we must be
2183 * careful not to modify the VM object in any way that is not
2184 * legal under a shared lock...
2186 unsigned long cs_enter_tainted_rejected
= 0;
2187 unsigned long cs_enter_tainted_accepted
= 0;
2189 vm_fault_enter(vm_page_t m
,
2191 vm_map_offset_t vaddr
,
2194 boolean_t change_wiring
,
2198 unsigned int cache_attr
;
2200 boolean_t previously_pmapped
= m
->pmapped
;
2201 boolean_t must_disconnect
= 0;
2202 boolean_t map_is_switched
, map_is_switch_protected
;
2204 vm_object_lock_assert_held(m
->object
);
2206 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2209 if (m
->phys_page
== vm_page_guard_addr
) {
2210 assert(m
->fictitious
);
2211 return KERN_SUCCESS
;
2214 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
2216 if (m
->pmapped
== FALSE
) {
2218 * This is the first time this page is being
2219 * mapped in an address space (pmapped == FALSE).
2221 * Part of that page may still be in the data cache
2222 * and not flushed to memory. In case we end up
2223 * accessing that page via the instruction cache,
2224 * we need to ensure that the 2 caches are in sync.
2226 pmap_sync_page_data_phys(m
->phys_page
);
2228 if ((*type_of_fault
== DBG_CACHE_HIT_FAULT
) && m
->clustered
) {
2230 * found it in the cache, but this
2231 * is the first fault-in of the page (m->pmapped == FALSE)
2232 * so it must have come in as part of
2233 * a cluster... account 1 pagein against it
2235 VM_STAT_INCR(pageins
);
2236 DTRACE_VM2(pgin
, int, 1, (uint64_t *), NULL
);
2238 if (m
->object
->internal
) {
2239 DTRACE_VM2(anonpgin
, int, 1, (uint64_t *), NULL
);
2240 *type_of_fault
= DBG_PAGEIND_FAULT
;
2242 DTRACE_VM2(fspgin
, int, 1, (uint64_t *), NULL
);
2243 *type_of_fault
= DBG_PAGEINV_FAULT
;
2246 current_task()->pageins
++;
2248 VM_PAGE_CONSUME_CLUSTERED(m
);
2250 } else if (cache_attr
!= VM_WIMG_DEFAULT
)
2251 pmap_sync_page_attributes_phys(m
->phys_page
);
2253 if (*type_of_fault
!= DBG_COW_FAULT
) {
2254 DTRACE_VM2(as_fault
, int, 1, (uint64_t *), NULL
);
2256 if (pmap
== kernel_pmap
) {
2257 DTRACE_VM2(kernel_asflt
, int, 1, (uint64_t *), NULL
);
2261 /* Validate code signature if necessary. */
2262 if (VM_FAULT_NEED_CS_VALIDATION(pmap
, m
)) {
2263 vm_object_lock_assert_exclusive(m
->object
);
2265 if (m
->cs_validated
) {
2266 vm_cs_revalidates
++;
2269 /* VM map is locked, so 1 ref will remain on VM object -
2270 * so no harm if vm_page_validate_cs drops the object lock */
2271 vm_page_validate_cs(m
);
2274 #define page_immutable(m,prot) ((m)->cs_validated /*&& ((prot) & VM_PROT_EXECUTE)*/)
2276 map_is_switched
= ((pmap
!= vm_map_pmap(current_task()->map
)) &&
2277 (pmap
== vm_map_pmap(current_thread()->map
)));
2278 map_is_switch_protected
= current_thread()->map
->switch_protect
;
2280 /* If the map is switched, and is switch-protected, we must protect
2281 * some pages from being write-faulted: immutable pages because by
2282 * definition they may not be written, and executable pages because that
2283 * would provide a way to inject unsigned code.
2284 * If the page is immutable, we can simply return. However, we can't
2285 * immediately determine whether a page is executable anywhere. But,
2286 * we can disconnect it everywhere and remove the executable protection
2287 * from the current map. We do that below right before we do the
2290 if(!cs_enforcement_disable
&& map_is_switched
&&
2291 map_is_switch_protected
&& page_immutable(m
, prot
) &&
2292 (prot
& VM_PROT_WRITE
))
2294 return KERN_CODESIGN_ERROR
;
2297 /* A page could be tainted, or pose a risk of being tainted later.
2298 * Check whether the receiving process wants it, and make it feel
2299 * the consequences (that hapens in cs_invalid_page()).
2300 * For CS Enforcement, two other conditions will
2301 * cause that page to be tainted as well:
2302 * - pmapping an unsigned page executable - this means unsigned code;
2303 * - writeable mapping of a validated page - the content of that page
2304 * can be changed without the kernel noticing, therefore unsigned
2305 * code can be created
2307 if (m
->cs_tainted
||
2308 ( !cs_enforcement_disable
&&
2309 (/* The page is unsigned and wants to be executable */
2310 (!m
->cs_validated
&& (prot
& VM_PROT_EXECUTE
)) ||
2311 /* The page should be immutable, but is in danger of being modified
2312 * This is the case where we want policy from the code directory -
2313 * is the page immutable or not? For now we have to assume that
2314 * code pages will be immutable, data pages not.
2315 * We'll assume a page is a code page if it has a code directory
2316 * and we fault for execution.
2317 * That is good enough since if we faulted the code page for
2318 * writing in another map before, it is wpmapped; if we fault
2319 * it for writing in this map later it will also be faulted for executing
2320 * at the same time; and if we fault for writing in another map
2321 * later, we will disconnect it from this pmap so we'll notice
2324 (page_immutable(m
, prot
) && ((prot
& VM_PROT_WRITE
) || m
->wpmapped
))
2328 /* We will have a tainted page. Have to handle the special case
2329 * of a switched map now. If the map is not switched, standard
2330 * procedure applies - call cs_invalid_page().
2331 * If the map is switched, the real owner is invalid already.
2332 * There is no point in invalidating the switching process since
2333 * it will not be executing from the map. So we don't call
2334 * cs_invalid_page() in that case. */
2335 boolean_t reject_page
;
2336 if(map_is_switched
) {
2337 assert(pmap
==vm_map_pmap(current_thread()->map
));
2338 assert(!(prot
& VM_PROT_WRITE
) || (map_is_switch_protected
== FALSE
));
2339 reject_page
= FALSE
;
2341 reject_page
= cs_invalid_page((addr64_t
) vaddr
);
2345 /* reject the tainted page: abort the page fault */
2346 kr
= KERN_CODESIGN_ERROR
;
2347 cs_enter_tainted_rejected
++;
2349 /* proceed with the tainted page */
2351 /* Page might have been tainted before or not; now it
2352 * definitively is. If the page wasn't tainted, we must
2353 * disconnect it from all pmaps later. */
2354 must_disconnect
= !m
->cs_tainted
;
2355 m
->cs_tainted
= TRUE
;
2356 cs_enter_tainted_accepted
++;
2358 if (cs_debug
|| kr
!= KERN_SUCCESS
) {
2359 printf("CODESIGNING: vm_fault_enter(0x%llx): "
2360 "page %p obj %p off 0x%llx *** INVALID PAGE ***\n",
2361 (long long)vaddr
, m
, m
->object
, m
->offset
);
2365 /* proceed with the valid page */
2369 /* If we have a KERN_SUCCESS from the previous checks, we either have
2370 * a good page, or a tainted page that has been accepted by the process.
2371 * In both cases the page will be entered into the pmap.
2372 * If the page is writeable, we need to disconnect it from other pmaps
2373 * now so those processes can take note.
2375 if (kr
== KERN_SUCCESS
) {
2377 * NOTE: we may only hold the vm_object lock SHARED
2378 * at this point, but the update of pmapped is ok
2379 * since this is the ONLY bit updated behind the SHARED
2380 * lock... however, we need to figure out how to do an atomic
2381 * update on a bit field to make this less fragile... right
2382 * now I don't know how to coerce 'C' to give me the offset info
2383 * that's needed for an AtomicCompareAndSwap
2386 if (prot
& VM_PROT_WRITE
) {
2387 vm_object_lock_assert_exclusive(m
->object
);
2389 if(must_disconnect
) {
2390 /* We can only get here
2391 * because of the CSE logic */
2392 assert(cs_enforcement_disable
== FALSE
);
2393 pmap_disconnect(m
->phys_page
);
2394 /* If we are faulting for a write, we can clear
2395 * the execute bit - that will ensure the page is
2396 * checked again before being executable, which
2397 * protects against a map switch.
2398 * This only happens the first time the page
2399 * gets tainted, so we won't get stuck here
2400 * to make an already writeable page executable. */
2401 prot
&= ~VM_PROT_EXECUTE
;
2404 PMAP_ENTER(pmap
, vaddr
, m
, prot
, cache_attr
, wired
);
2408 * Hold queues lock to manipulate
2409 * the page queues. Change wiring
2412 if (change_wiring
) {
2413 vm_page_lockspin_queues();
2416 if (kr
== KERN_SUCCESS
) {
2422 vm_page_unlock_queues();
2425 if (kr
!= KERN_SUCCESS
) {
2426 vm_page_lockspin_queues();
2427 vm_page_deactivate(m
);
2428 vm_page_unlock_queues();
2430 if (((!m
->active
&& !m
->inactive
) || no_cache
) && !VM_PAGE_WIRED(m
) && !m
->throttled
) {
2432 if ( vm_page_local_q
&& !no_cache
&& (*type_of_fault
== DBG_COW_FAULT
|| *type_of_fault
== DBG_ZERO_FILL_FAULT
) ) {
2437 * we got a local queue to stuff this new page on...
2438 * its safe to manipulate local and local_id at this point
2439 * since we're behind an exclusive object lock and the
2440 * page is not on any global queue.
2442 * we'll use the current cpu number to select the queue
2443 * note that we don't need to disable preemption... we're
2444 * going to behind the local queue's lock to do the real
2449 lq
= &vm_page_local_q
[lid
].vpl_un
.vpl
;
2451 VPL_LOCK(&lq
->vpl_lock
);
2453 queue_enter(&lq
->vpl_queue
, m
, vm_page_t
, pageq
);
2458 VPL_UNLOCK(&lq
->vpl_lock
);
2460 if (lq
->vpl_count
> vm_page_local_q_soft_limit
) {
2462 * we're beyond the soft limit for the local queue
2463 * vm_page_reactivate_local will 'try' to take
2464 * the global page queue lock... if it can't that's
2465 * ok... we'll let the queue continue to grow up
2466 * to the hard limit... at that point we'll wait
2467 * for the lock... once we've got the lock, we'll
2468 * transfer all of the pages from the local queue
2469 * to the global active queue
2471 vm_page_reactivate_local(lid
, FALSE
, FALSE
);
2476 vm_page_lockspin_queues();
2478 * test again now that we hold the page queue lock
2480 if (((!m
->active
&& !m
->inactive
) || no_cache
) && !VM_PAGE_WIRED(m
)) {
2483 * If this is a no_cache mapping and the page has never been
2484 * mapped before or was previously a no_cache page, then we
2485 * want to leave pages in the speculative state so that they
2486 * can be readily recycled if free memory runs low. Otherwise
2487 * the page is activated as normal.
2490 if (no_cache
&& (!previously_pmapped
|| m
->no_cache
)) {
2493 if (m
->active
|| m
->inactive
)
2494 VM_PAGE_QUEUES_REMOVE(m
);
2496 if (!m
->speculative
)
2497 vm_page_speculate(m
, TRUE
);
2499 } else if (!m
->active
&& !m
->inactive
)
2500 vm_page_activate(m
);
2504 vm_page_unlock_queues();
2515 * Handle page faults, including pseudo-faults
2516 * used to change the wiring status of pages.
2518 * Explicit continuations have been removed.
2520 * vm_fault and vm_fault_page save mucho state
2521 * in the moral equivalent of a closure. The state
2522 * structure is allocated when first entering vm_fault
2523 * and deallocated when leaving vm_fault.
2526 extern int _map_enter_debug
;
2528 unsigned long vm_fault_collapse_total
= 0;
2529 unsigned long vm_fault_collapse_skipped
= 0;
2534 vm_map_offset_t vaddr
,
2535 vm_prot_t fault_type
,
2536 boolean_t change_wiring
,
2539 vm_map_offset_t caller_pmap_addr
)
2541 vm_map_version_t version
; /* Map version for verificiation */
2542 boolean_t wired
; /* Should mapping be wired down? */
2543 vm_object_t object
; /* Top-level object */
2544 vm_object_offset_t offset
; /* Top-level offset */
2545 vm_prot_t prot
; /* Protection for mapping */
2546 vm_object_t old_copy_object
; /* Saved copy object */
2547 vm_page_t result_page
; /* Result of vm_fault_page */
2548 vm_page_t top_page
; /* Placeholder page */
2551 vm_page_t m
; /* Fast access to result_page */
2552 kern_return_t error_code
;
2553 vm_object_t cur_object
;
2554 vm_object_offset_t cur_offset
;
2556 vm_object_t new_object
;
2559 boolean_t interruptible_state
;
2560 vm_map_t real_map
= map
;
2561 vm_map_t original_map
= map
;
2562 vm_prot_t original_fault_type
;
2563 struct vm_object_fault_info fault_info
;
2564 boolean_t need_collapse
= FALSE
;
2565 int object_lock_type
= 0;
2566 int cur_object_lock_type
;
2567 vm_object_t top_object
= VM_OBJECT_NULL
;
2570 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_START
,
2571 (int)((uint64_t)vaddr
>> 32),
2577 if (get_preemption_level() != 0) {
2578 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
2579 (int)((uint64_t)vaddr
>> 32),
2585 return (KERN_FAILURE
);
2588 interruptible_state
= thread_interrupt_level(interruptible
);
2590 VM_STAT_INCR(faults
);
2591 current_task()->faults
++;
2592 original_fault_type
= fault_type
;
2594 if (fault_type
& VM_PROT_WRITE
)
2595 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2597 object_lock_type
= OBJECT_LOCK_SHARED
;
2599 cur_object_lock_type
= OBJECT_LOCK_SHARED
;
2603 * assume we will hit a page in the cache
2604 * otherwise, explicitly override with
2605 * the real fault type once we determine it
2607 type_of_fault
= DBG_CACHE_HIT_FAULT
;
2610 * Find the backing store object and offset into
2611 * it to begin the search.
2613 fault_type
= original_fault_type
;
2615 vm_map_lock_read(map
);
2617 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
,
2618 object_lock_type
, &version
,
2619 &object
, &offset
, &prot
, &wired
,
2623 if (kr
!= KERN_SUCCESS
) {
2624 vm_map_unlock_read(map
);
2627 pmap
= real_map
->pmap
;
2628 fault_info
.interruptible
= interruptible
;
2629 fault_info
.stealth
= FALSE
;
2632 * If the page is wired, we must fault for the current protection
2633 * value, to avoid further faults.
2636 fault_type
= prot
| VM_PROT_WRITE
;
2638 * since we're treating this fault as a 'write'
2639 * we must hold the top object lock exclusively
2641 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2643 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2645 if (vm_object_lock_upgrade(object
) == FALSE
) {
2647 * couldn't upgrade, so explictly
2648 * take the lock exclusively
2650 vm_object_lock(object
);
2655 #if VM_FAULT_CLASSIFY
2657 * Temporary data gathering code
2659 vm_fault_classify(object
, offset
, fault_type
);
2662 * Fast fault code. The basic idea is to do as much as
2663 * possible while holding the map lock and object locks.
2664 * Busy pages are not used until the object lock has to
2665 * be dropped to do something (copy, zero fill, pmap enter).
2666 * Similarly, paging references aren't acquired until that
2667 * point, and object references aren't used.
2669 * If we can figure out what to do
2670 * (zero fill, copy on write, pmap enter) while holding
2671 * the locks, then it gets done. Otherwise, we give up,
2672 * and use the original fault path (which doesn't hold
2673 * the map lock, and relies on busy pages).
2674 * The give up cases include:
2675 * - Have to talk to pager.
2676 * - Page is busy, absent or in error.
2677 * - Pager has locked out desired access.
2678 * - Fault needs to be restarted.
2679 * - Have to push page into copy object.
2681 * The code is an infinite loop that moves one level down
2682 * the shadow chain each time. cur_object and cur_offset
2683 * refer to the current object being examined. object and offset
2684 * are the original object from the map. The loop is at the
2685 * top level if and only if object and cur_object are the same.
2687 * Invariants: Map lock is held throughout. Lock is held on
2688 * original object and cur_object (if different) when
2689 * continuing or exiting loop.
2695 * If this page is to be inserted in a copy delay object
2696 * for writing, and if the object has a copy, then the
2697 * copy delay strategy is implemented in the slow fault page.
2699 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
&&
2700 object
->copy
!= VM_OBJECT_NULL
&& (fault_type
& VM_PROT_WRITE
))
2701 goto handle_copy_delay
;
2703 cur_object
= object
;
2704 cur_offset
= offset
;
2707 if (!cur_object
->pager_created
&&
2708 cur_object
->phys_contiguous
) /* superpage */
2711 if (cur_object
->blocked_access
) {
2713 * Access to this VM object has been blocked.
2714 * Let the slow path handle it.
2719 m
= vm_page_lookup(cur_object
, cur_offset
);
2721 if (m
!= VM_PAGE_NULL
) {
2723 wait_result_t result
;
2726 * in order to do the PAGE_ASSERT_WAIT, we must
2727 * have object that 'm' belongs to locked exclusively
2729 if (object
!= cur_object
) {
2730 vm_object_unlock(object
);
2732 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2734 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2736 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2738 * couldn't upgrade so go do a full retry
2739 * immediately since we've already dropped
2740 * the top object lock associated with this page
2741 * and the current one got dropped due to the
2742 * failed upgrade... the state is no longer valid
2744 vm_map_unlock_read(map
);
2745 if (real_map
!= map
)
2746 vm_map_unlock(real_map
);
2751 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2753 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2755 if (vm_object_lock_upgrade(object
) == FALSE
) {
2757 * couldn't upgrade, so explictly take the lock
2758 * exclusively and go relookup the page since we
2759 * will have dropped the object lock and
2760 * a different thread could have inserted
2761 * a page at this offset
2762 * no need for a full retry since we're
2763 * at the top level of the object chain
2765 vm_object_lock(object
);
2770 vm_map_unlock_read(map
);
2771 if (real_map
!= map
)
2772 vm_map_unlock(real_map
);
2774 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
2776 vm_object_unlock(cur_object
);
2778 if (result
== THREAD_WAITING
) {
2779 result
= thread_block(THREAD_CONTINUE_NULL
);
2781 counter(c_vm_fault_page_block_busy_kernel
++);
2783 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
2789 if (m
->phys_page
== vm_page_guard_addr
) {
2791 * Guard page: let the slow path deal with it
2795 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
)) {
2797 * Unusual case... let the slow path deal with it
2801 if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m
->object
)) {
2802 if (object
!= cur_object
)
2803 vm_object_unlock(object
);
2804 vm_map_unlock_read(map
);
2805 if (real_map
!= map
)
2806 vm_map_unlock(real_map
);
2807 vm_object_unlock(cur_object
);
2808 kr
= KERN_MEMORY_ERROR
;
2815 * We've soft-faulted (because it's not in the page
2816 * table) on an encrypted page.
2817 * Keep the page "busy" so that no one messes with
2818 * it during the decryption.
2819 * Release the extra locks we're holding, keep only
2820 * the page's VM object lock.
2822 * in order to set 'busy' on 'm', we must
2823 * have object that 'm' belongs to locked exclusively
2825 if (object
!= cur_object
) {
2826 vm_object_unlock(object
);
2828 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2830 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2832 if (vm_object_lock_upgrade(cur_object
) == FALSE
) {
2834 * couldn't upgrade so go do a full retry
2835 * immediately since we've already dropped
2836 * the top object lock associated with this page
2837 * and the current one got dropped due to the
2838 * failed upgrade... the state is no longer valid
2840 vm_map_unlock_read(map
);
2841 if (real_map
!= map
)
2842 vm_map_unlock(real_map
);
2847 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2849 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2851 if (vm_object_lock_upgrade(object
) == FALSE
) {
2853 * couldn't upgrade, so explictly take the lock
2854 * exclusively and go relookup the page since we
2855 * will have dropped the object lock and
2856 * a different thread could have inserted
2857 * a page at this offset
2858 * no need for a full retry since we're
2859 * at the top level of the object chain
2861 vm_object_lock(object
);
2868 vm_map_unlock_read(map
);
2869 if (real_map
!= map
)
2870 vm_map_unlock(real_map
);
2872 vm_page_decrypt(m
, 0);
2875 PAGE_WAKEUP_DONE(m
);
2877 vm_object_unlock(cur_object
);
2879 * Retry from the top, in case anything
2880 * changed while we were decrypting...
2884 ASSERT_PAGE_DECRYPTED(m
);
2886 if (VM_FAULT_NEED_CS_VALIDATION(map
->pmap
, m
)) {
2888 * We might need to validate this page
2889 * against its code signature, so we
2890 * want to hold the VM object exclusively.
2892 if (object
!= cur_object
) {
2893 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
) {
2894 vm_object_unlock(object
);
2895 vm_object_unlock(cur_object
);
2897 cur_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2899 vm_map_unlock_read(map
);
2900 if (real_map
!= map
)
2901 vm_map_unlock(real_map
);
2906 } else if (object_lock_type
== OBJECT_LOCK_SHARED
) {
2908 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
2910 if (vm_object_lock_upgrade(object
) == FALSE
) {
2912 * couldn't upgrade, so explictly take the lock
2913 * exclusively and go relookup the page since we
2914 * will have dropped the object lock and
2915 * a different thread could have inserted
2916 * a page at this offset
2917 * no need for a full retry since we're
2918 * at the top level of the object chain
2920 vm_object_lock(object
);
2927 * Two cases of map in faults:
2928 * - At top level w/o copy object.
2929 * - Read fault anywhere.
2930 * --> must disallow write.
2933 if (object
== cur_object
&& object
->copy
== VM_OBJECT_NULL
) {
2934 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2936 * This is not a "write" fault, so we
2937 * might not have taken the object lock
2938 * exclusively and we might not be able
2939 * to update the "wpmapped" bit in
2941 * Let's just grant read access to
2942 * the page for now and we'll
2943 * soft-fault again if we need write
2946 prot
&= ~VM_PROT_WRITE
;
2951 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2953 prot
&= ~VM_PROT_WRITE
;
2955 if (object
!= cur_object
) {
2957 * We still need to hold the top object
2958 * lock here to prevent a race between
2959 * a read fault (taking only "shared"
2960 * locks) and a write fault (taking
2961 * an "exclusive" lock on the top
2963 * Otherwise, as soon as we release the
2964 * top lock, the write fault could
2965 * proceed and actually complete before
2966 * the read fault, and the copied page's
2967 * translation could then be overwritten
2968 * by the read fault's translation for
2969 * the original page.
2971 * Let's just record what the top object
2972 * is and we'll release it later.
2974 top_object
= object
;
2977 * switch to the object that has the new page
2979 object
= cur_object
;
2980 object_lock_type
= cur_object_lock_type
;
2984 * prepare for the pmap_enter...
2985 * object and map are both locked
2986 * m contains valid data
2987 * object == m->object
2988 * cur_object == NULL or it's been unlocked
2989 * no paging references on either object or cur_object
2992 if (db_watchpoint_list
&& (fault_type
& VM_PROT_WRITE
) == 0)
2993 prot
&= ~VM_PROT_WRITE
;
2996 kr
= vm_fault_enter(m
,
3002 fault_info
.no_cache
,
3005 kr
= vm_fault_enter(m
,
3011 fault_info
.no_cache
,
3015 if (top_object
!= VM_OBJECT_NULL
) {
3017 * It's safe to drop the top object
3018 * now that we've done our
3019 * vm_fault_enter(). Any other fault
3020 * in progress for that virtual
3021 * address will either find our page
3022 * and translation or put in a new page
3025 vm_object_unlock(top_object
);
3026 top_object
= VM_OBJECT_NULL
;
3029 if (need_collapse
== TRUE
)
3030 vm_object_collapse(object
, offset
, TRUE
);
3032 if (type_of_fault
== DBG_PAGEIND_FAULT
|| type_of_fault
== DBG_PAGEINV_FAULT
|| type_of_fault
== DBG_CACHE_HIT_FAULT
) {
3034 * evaluate access pattern and update state
3035 * vm_fault_deactivate_behind depends on the
3036 * state being up to date
3038 vm_fault_is_sequential(object
, cur_offset
, fault_info
.behavior
);
3040 vm_fault_deactivate_behind(object
, cur_offset
, fault_info
.behavior
);
3043 * That's it, clean up and return.
3046 PAGE_WAKEUP_DONE(m
);
3048 vm_object_unlock(object
);
3050 vm_map_unlock_read(map
);
3051 if (real_map
!= map
)
3052 vm_map_unlock(real_map
);
3057 * COPY ON WRITE FAULT
3059 assert(object_lock_type
== OBJECT_LOCK_EXCLUSIVE
);
3061 if (vm_page_throttled()) {
3063 * drop all of our locks...
3064 * wait until the free queue is
3065 * pumped back up and then
3068 if (object
!= cur_object
)
3069 vm_object_unlock(cur_object
);
3070 vm_object_unlock(object
);
3071 vm_map_unlock_read(map
);
3072 if (real_map
!= map
)
3073 vm_map_unlock(real_map
);
3075 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
3076 delay(HARD_THROTTLE_DELAY
);
3078 if (!current_thread_aborted() && vm_page_wait((change_wiring
) ?
3086 * If objects match, then
3087 * object->copy must not be NULL (else control
3088 * would be in previous code block), and we
3089 * have a potential push into the copy object
3090 * with which we can't cope with here.
3092 if (cur_object
== object
) {
3094 * must take the slow path to
3095 * deal with the copy push
3100 * This is now a shadow based copy on write
3101 * fault -- it requires a copy up the shadow
3104 * Allocate a page in the original top level
3105 * object. Give up if allocate fails. Also
3106 * need to remember current page, as it's the
3107 * source of the copy.
3109 * at this point we hold locks on both
3110 * object and cur_object... no need to take
3111 * paging refs or mark pages BUSY since
3112 * we don't drop either object lock until
3113 * the page has been copied and inserted
3118 if (m
== VM_PAGE_NULL
) {
3120 * no free page currently available...
3121 * must take the slow path
3126 * Now do the copy. Mark the source page busy...
3128 * NOTE: This code holds the map lock across
3131 vm_page_copy(cur_m
, m
);
3132 vm_page_insert(m
, object
, offset
);
3136 * Now cope with the source page and object
3138 if (object
->ref_count
> 1 && cur_m
->pmapped
)
3139 pmap_disconnect(cur_m
->phys_page
);
3141 need_collapse
= TRUE
;
3143 if (!cur_object
->internal
&&
3144 cur_object
->copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3146 * The object from which we've just
3147 * copied a page is most probably backed
3148 * by a vnode. We don't want to waste too
3149 * much time trying to collapse the VM objects
3150 * and create a bottleneck when several tasks
3151 * map the same file.
3153 if (cur_object
->copy
== object
) {
3155 * Shared mapping or no COW yet.
3156 * We can never collapse a copy
3157 * object into its backing object.
3159 need_collapse
= FALSE
;
3160 } else if (cur_object
->copy
== object
->shadow
&&
3161 object
->shadow
->resident_page_count
== 0) {
3163 * Shared mapping after a COW occurred.
3165 need_collapse
= FALSE
;
3168 vm_object_unlock(cur_object
);
3170 if (need_collapse
== FALSE
)
3171 vm_fault_collapse_skipped
++;
3172 vm_fault_collapse_total
++;
3174 type_of_fault
= DBG_COW_FAULT
;
3175 VM_STAT_INCR(cow_faults
);
3176 DTRACE_VM2(cow_fault
, int, 1, (uint64_t *), NULL
);
3177 current_task()->cow_faults
++;
3183 * No page at cur_object, cur_offset... m == NULL
3185 if (cur_object
->pager_created
) {
3186 if (MUST_ASK_PAGER(cur_object
, cur_offset
) == TRUE
) {
3188 * May have to talk to a pager...
3189 * take the slow path.
3194 * existence map present and indicates
3195 * that the pager doesn't have this page
3198 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
3200 * Zero fill fault. Page gets
3201 * inserted into the original object.
3203 if (cur_object
->shadow_severed
||
3204 VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object
))
3206 if (object
!= cur_object
)
3207 vm_object_unlock(cur_object
);
3208 vm_object_unlock(object
);
3210 vm_map_unlock_read(map
);
3211 if (real_map
!= map
)
3212 vm_map_unlock(real_map
);
3214 kr
= KERN_MEMORY_ERROR
;
3217 if (vm_page_throttled()) {
3219 * drop all of our locks...
3220 * wait until the free queue is
3221 * pumped back up and then
3224 if (object
!= cur_object
)
3225 vm_object_unlock(cur_object
);
3226 vm_object_unlock(object
);
3227 vm_map_unlock_read(map
);
3228 if (real_map
!= map
)
3229 vm_map_unlock(real_map
);
3231 if (NEED_TO_HARD_THROTTLE_THIS_TASK())
3232 delay(HARD_THROTTLE_DELAY
);
3234 if (!current_thread_aborted() && vm_page_wait((change_wiring
) ?
3241 if (vm_backing_store_low
) {
3243 * we are protecting the system from
3244 * backing store exhaustion...
3245 * must take the slow path if we're
3248 if (!(current_task()->priv_flags
& VM_BACKING_STORE_PRIV
))
3251 if (cur_object
!= object
) {
3252 vm_object_unlock(cur_object
);
3254 cur_object
= object
;
3256 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3258 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3260 if (vm_object_lock_upgrade(object
) == FALSE
) {
3262 * couldn't upgrade so do a full retry on the fault
3263 * since we dropped the object lock which
3264 * could allow another thread to insert
3265 * a page at this offset
3267 vm_map_unlock_read(map
);
3268 if (real_map
!= map
)
3269 vm_map_unlock(real_map
);
3274 m
= vm_page_alloc(object
, offset
);
3276 if (m
== VM_PAGE_NULL
) {
3278 * no free page currently available...
3279 * must take the slow path
3285 * Now zero fill page...
3286 * the page is probably going to
3287 * be written soon, so don't bother
3288 * to clear the modified bit
3290 * NOTE: This code holds the map
3291 * lock across the zero fill.
3293 type_of_fault
= vm_fault_zero_page(m
, map
->no_zero_fill
);
3298 * On to the next level in the shadow chain
3300 cur_offset
+= cur_object
->shadow_offset
;
3301 new_object
= cur_object
->shadow
;
3304 * take the new_object's lock with the indicated state
3306 if (cur_object_lock_type
== OBJECT_LOCK_SHARED
)
3307 vm_object_lock_shared(new_object
);
3309 vm_object_lock(new_object
);
3311 if (cur_object
!= object
)
3312 vm_object_unlock(cur_object
);
3314 cur_object
= new_object
;
3320 * Cleanup from fast fault failure. Drop any object
3321 * lock other than original and drop map lock.
3323 if (object
!= cur_object
)
3324 vm_object_unlock(cur_object
);
3327 * must own the object lock exclusively at this point
3329 if (object_lock_type
== OBJECT_LOCK_SHARED
) {
3330 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
3332 if (vm_object_lock_upgrade(object
) == FALSE
) {
3334 * couldn't upgrade, so explictly
3335 * take the lock exclusively
3336 * no need to retry the fault at this
3337 * point since "vm_fault_page" will
3338 * completely re-evaluate the state
3340 vm_object_lock(object
);
3345 vm_map_unlock_read(map
);
3346 if (real_map
!= map
)
3347 vm_map_unlock(real_map
);
3350 * Make a reference to this object to
3351 * prevent its disposal while we are messing with
3352 * it. Once we have the reference, the map is free
3353 * to be diddled. Since objects reference their
3354 * shadows (and copies), they will stay around as well.
3356 vm_object_reference_locked(object
);
3357 vm_object_paging_begin(object
);
3359 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
3363 kr
= vm_fault_page(object
, offset
, fault_type
,
3364 (change_wiring
&& !wired
),
3365 &prot
, &result_page
, &top_page
,
3367 &error_code
, map
->no_zero_fill
,
3368 FALSE
, &fault_info
);
3371 * if kr != VM_FAULT_SUCCESS, then the paging reference
3372 * has been dropped and the object unlocked... the ref_count
3375 * if kr == VM_FAULT_SUCCESS, then the paging reference
3376 * is still held along with the ref_count on the original object
3378 * the object is returned locked with a paging reference
3380 * if top_page != NULL, then it's BUSY and the
3381 * object it belongs to has a paging reference
3382 * but is returned unlocked
3384 if (kr
!= VM_FAULT_SUCCESS
&&
3385 kr
!= VM_FAULT_SUCCESS_NO_VM_PAGE
) {
3387 * we didn't succeed, lose the object reference immediately.
3389 vm_object_deallocate(object
);
3392 * See why we failed, and take corrective action.
3395 case VM_FAULT_MEMORY_SHORTAGE
:
3396 if (vm_page_wait((change_wiring
) ?
3403 case VM_FAULT_INTERRUPTED
:
3406 case VM_FAULT_RETRY
:
3408 case VM_FAULT_MEMORY_ERROR
:
3412 kr
= KERN_MEMORY_ERROR
;
3415 panic("vm_fault: unexpected error 0x%x from "
3416 "vm_fault_page()\n", kr
);
3421 if (m
!= VM_PAGE_NULL
) {
3422 assert((change_wiring
&& !wired
) ?
3423 (top_page
== VM_PAGE_NULL
) :
3424 ((top_page
== VM_PAGE_NULL
) == (m
->object
== object
)));
3428 * What to do with the resulting page from vm_fault_page
3429 * if it doesn't get entered into the physical map:
3431 #define RELEASE_PAGE(m) \
3433 PAGE_WAKEUP_DONE(m); \
3434 if (!m->active && !m->inactive && !m->throttled) { \
3435 vm_page_lockspin_queues(); \
3436 if (!m->active && !m->inactive && !m->throttled) \
3437 vm_page_activate(m); \
3438 vm_page_unlock_queues(); \
3443 * We must verify that the maps have not changed
3444 * since our last lookup.
3446 if (m
!= VM_PAGE_NULL
) {
3447 old_copy_object
= m
->object
->copy
;
3448 vm_object_unlock(m
->object
);
3450 old_copy_object
= VM_OBJECT_NULL
;
3451 vm_object_unlock(object
);
3455 * no object locks are held at this point
3457 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
3458 vm_object_t retry_object
;
3459 vm_object_offset_t retry_offset
;
3460 vm_prot_t retry_prot
;
3463 * To avoid trying to write_lock the map while another
3464 * thread has it read_locked (in vm_map_pageable), we
3465 * do not try for write permission. If the page is
3466 * still writable, we will get write permission. If it
3467 * is not, or has been marked needs_copy, we enter the
3468 * mapping without write permission, and will merely
3469 * take another fault.
3472 vm_map_lock_read(map
);
3474 kr
= vm_map_lookup_locked(&map
, vaddr
,
3475 fault_type
& ~VM_PROT_WRITE
,
3476 OBJECT_LOCK_EXCLUSIVE
, &version
,
3477 &retry_object
, &retry_offset
, &retry_prot
,
3481 pmap
= real_map
->pmap
;
3483 if (kr
!= KERN_SUCCESS
) {
3484 vm_map_unlock_read(map
);
3486 if (m
!= VM_PAGE_NULL
) {
3488 * retake the lock so that
3489 * we can drop the paging reference
3490 * in vm_fault_cleanup and do the
3491 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3493 vm_object_lock(m
->object
);
3497 vm_fault_cleanup(m
->object
, top_page
);
3500 * retake the lock so that
3501 * we can drop the paging reference
3502 * in vm_fault_cleanup
3504 vm_object_lock(object
);
3506 vm_fault_cleanup(object
, top_page
);
3508 vm_object_deallocate(object
);
3512 vm_object_unlock(retry_object
);
3514 if ((retry_object
!= object
) || (retry_offset
!= offset
)) {
3516 vm_map_unlock_read(map
);
3517 if (real_map
!= map
)
3518 vm_map_unlock(real_map
);
3520 if (m
!= VM_PAGE_NULL
) {
3522 * retake the lock so that
3523 * we can drop the paging reference
3524 * in vm_fault_cleanup and do the
3525 * PAGE_WAKEUP_DONE in RELEASE_PAGE
3527 vm_object_lock(m
->object
);
3531 vm_fault_cleanup(m
->object
, top_page
);
3534 * retake the lock so that
3535 * we can drop the paging reference
3536 * in vm_fault_cleanup
3538 vm_object_lock(object
);
3540 vm_fault_cleanup(object
, top_page
);
3542 vm_object_deallocate(object
);
3547 * Check whether the protection has changed or the object
3548 * has been copied while we left the map unlocked.
3552 if (m
!= VM_PAGE_NULL
) {
3553 vm_object_lock(m
->object
);
3555 if (m
->object
->copy
!= old_copy_object
) {
3557 * The copy object changed while the top-level object
3558 * was unlocked, so take away write permission.
3560 prot
&= ~VM_PROT_WRITE
;
3563 vm_object_lock(object
);
3566 * If we want to wire down this page, but no longer have
3567 * adequate permissions, we must start all over.
3569 if (wired
&& (fault_type
!= (prot
| VM_PROT_WRITE
))) {
3571 vm_map_verify_done(map
, &version
);
3572 if (real_map
!= map
)
3573 vm_map_unlock(real_map
);
3575 if (m
!= VM_PAGE_NULL
) {
3578 vm_fault_cleanup(m
->object
, top_page
);
3580 vm_fault_cleanup(object
, top_page
);
3582 vm_object_deallocate(object
);
3586 if (m
!= VM_PAGE_NULL
) {
3588 * Put this page into the physical map.
3589 * We had to do the unlock above because pmap_enter
3590 * may cause other faults. The page may be on
3591 * the pageout queues. If the pageout daemon comes
3592 * across the page, it will remove it from the queues.
3595 kr
= vm_fault_enter(m
,
3601 fault_info
.no_cache
,
3604 kr
= vm_fault_enter(m
,
3610 fault_info
.no_cache
,
3613 if (kr
!= KERN_SUCCESS
) {
3614 /* abort this page fault */
3615 vm_map_verify_done(map
, &version
);
3616 if (real_map
!= map
)
3617 vm_map_unlock(real_map
);
3618 PAGE_WAKEUP_DONE(m
);
3619 vm_fault_cleanup(m
->object
, top_page
);
3620 vm_object_deallocate(object
);
3625 vm_map_entry_t entry
;
3626 vm_map_offset_t laddr
;
3627 vm_map_offset_t ldelta
, hdelta
;
3630 * do a pmap block mapping from the physical address
3635 /* While we do not worry about execution protection in */
3636 /* general, certian pages may have instruction execution */
3637 /* disallowed. We will check here, and if not allowed */
3638 /* to execute, we return with a protection failure. */
3640 if ((fault_type
& VM_PROT_EXECUTE
) &&
3641 (!pmap_eligible_for_execute((ppnum_t
)(object
->shadow_offset
>> 12)))) {
3643 vm_map_verify_done(map
, &version
);
3645 if (real_map
!= map
)
3646 vm_map_unlock(real_map
);
3648 vm_fault_cleanup(object
, top_page
);
3649 vm_object_deallocate(object
);
3651 kr
= KERN_PROTECTION_FAILURE
;
3656 if (real_map
!= map
)
3657 vm_map_unlock(real_map
);
3659 if (original_map
!= map
) {
3660 vm_map_unlock_read(map
);
3661 vm_map_lock_read(original_map
);
3667 hdelta
= 0xFFFFF000;
3668 ldelta
= 0xFFFFF000;
3670 while (vm_map_lookup_entry(map
, laddr
, &entry
)) {
3671 if (ldelta
> (laddr
- entry
->vme_start
))
3672 ldelta
= laddr
- entry
->vme_start
;
3673 if (hdelta
> (entry
->vme_end
- laddr
))
3674 hdelta
= entry
->vme_end
- laddr
;
3675 if (entry
->is_sub_map
) {
3677 laddr
= (laddr
- entry
->vme_start
)
3679 vm_map_lock_read(entry
->object
.sub_map
);
3681 if (map
!= real_map
)
3682 vm_map_unlock_read(map
);
3683 if (entry
->use_pmap
) {
3684 vm_map_unlock_read(real_map
);
3685 real_map
= entry
->object
.sub_map
;
3687 map
= entry
->object
.sub_map
;
3694 if (vm_map_lookup_entry(map
, laddr
, &entry
) &&
3695 (entry
->object
.vm_object
!= NULL
) &&
3696 (entry
->object
.vm_object
== object
)) {
3698 int superpage
= (!object
->pager_created
&& object
->phys_contiguous
)? VM_MEM_SUPERPAGE
: 0;
3701 * Set up a block mapped area
3703 assert((uint32_t)((ldelta
+ hdelta
) >> 12) == ((ldelta
+ hdelta
) >> 12));
3704 pmap_map_block(caller_pmap
,
3705 (addr64_t
)(caller_pmap_addr
- ldelta
),
3706 (ppnum_t
)((((vm_map_offset_t
) (entry
->object
.vm_object
->shadow_offset
)) +
3707 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12),
3708 (uint32_t)((ldelta
+ hdelta
) >> 12), prot
,
3709 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
3712 * Set up a block mapped area
3714 assert((uint32_t)((ldelta
+ hdelta
) >> 12) == ((ldelta
+ hdelta
) >> 12));
3715 pmap_map_block(real_map
->pmap
,
3716 (addr64_t
)(vaddr
- ldelta
),
3717 (ppnum_t
)((((vm_map_offset_t
)(entry
->object
.vm_object
->shadow_offset
)) +
3718 entry
->offset
+ (laddr
- entry
->vme_start
) - ldelta
) >> 12),
3719 (uint32_t)((ldelta
+ hdelta
) >> 12), prot
,
3720 (VM_WIMG_MASK
& (int)object
->wimg_bits
) | superpage
, 0);
3726 * Unlock everything, and return
3728 vm_map_verify_done(map
, &version
);
3729 if (real_map
!= map
)
3730 vm_map_unlock(real_map
);
3732 if (m
!= VM_PAGE_NULL
) {
3733 PAGE_WAKEUP_DONE(m
);
3735 vm_fault_cleanup(m
->object
, top_page
);
3737 vm_fault_cleanup(object
, top_page
);
3739 vm_object_deallocate(object
);
3745 thread_interrupt_level(interruptible_state
);
3747 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 2)) | DBG_FUNC_END
,
3748 (int)((uint64_t)vaddr
>> 32),
3760 * Wire down a range of virtual addresses in a map.
3765 vm_map_entry_t entry
,
3767 vm_map_offset_t pmap_addr
)
3770 register vm_map_offset_t va
;
3771 register vm_map_offset_t end_addr
= entry
->vme_end
;
3772 register kern_return_t rc
;
3774 assert(entry
->in_transition
);
3776 if ((entry
->object
.vm_object
!= NULL
) &&
3777 !entry
->is_sub_map
&&
3778 entry
->object
.vm_object
->phys_contiguous
) {
3779 return KERN_SUCCESS
;
3783 * Inform the physical mapping system that the
3784 * range of addresses may not fault, so that
3785 * page tables and such can be locked down as well.
3788 pmap_pageable(pmap
, pmap_addr
,
3789 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
3792 * We simulate a fault to get the page and enter it
3793 * in the physical map.
3796 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3797 if ((rc
= vm_fault_wire_fast(
3798 map
, va
, entry
, pmap
,
3799 pmap_addr
+ (va
- entry
->vme_start
)
3800 )) != KERN_SUCCESS
) {
3801 rc
= vm_fault(map
, va
, VM_PROT_NONE
, TRUE
,
3802 (pmap
== kernel_pmap
) ?
3803 THREAD_UNINT
: THREAD_ABORTSAFE
,
3804 pmap
, pmap_addr
+ (va
- entry
->vme_start
));
3805 DTRACE_VM2(softlock
, int, 1, (uint64_t *), NULL
);
3808 if (rc
!= KERN_SUCCESS
) {
3809 struct vm_map_entry tmp_entry
= *entry
;
3811 /* unwire wired pages */
3812 tmp_entry
.vme_end
= va
;
3813 vm_fault_unwire(map
,
3814 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
3819 return KERN_SUCCESS
;
3825 * Unwire a range of virtual addresses in a map.
3830 vm_map_entry_t entry
,
3831 boolean_t deallocate
,
3833 vm_map_offset_t pmap_addr
)
3835 register vm_map_offset_t va
;
3836 register vm_map_offset_t end_addr
= entry
->vme_end
;
3838 struct vm_object_fault_info fault_info
;
3840 object
= (entry
->is_sub_map
)
3841 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
3844 * If it's marked phys_contiguous, then vm_fault_wire() didn't actually
3845 * do anything since such memory is wired by default. So we don't have
3846 * anything to undo here.
3849 if (object
!= VM_OBJECT_NULL
&& object
->phys_contiguous
)
3852 fault_info
.interruptible
= THREAD_UNINT
;
3853 fault_info
.behavior
= entry
->behavior
;
3854 fault_info
.user_tag
= entry
->alias
;
3855 fault_info
.lo_offset
= entry
->offset
;
3856 fault_info
.hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
3857 fault_info
.no_cache
= entry
->no_cache
;
3858 fault_info
.stealth
= TRUE
;
3861 * Since the pages are wired down, we must be able to
3862 * get their mappings from the physical map system.
3865 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3867 if (object
== VM_OBJECT_NULL
) {
3869 pmap_change_wiring(pmap
,
3870 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3872 (void) vm_fault(map
, va
, VM_PROT_NONE
,
3873 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
3876 vm_page_t result_page
;
3878 vm_object_t result_object
;
3879 vm_fault_return_t result
;
3881 if (end_addr
- va
> (vm_size_t
) -1) {
3882 /* 32-bit overflow */
3883 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
3885 fault_info
.cluster_size
= (vm_size_t
) (end_addr
- va
);
3886 assert(fault_info
.cluster_size
== end_addr
- va
);
3890 prot
= VM_PROT_NONE
;
3892 vm_object_lock(object
);
3893 vm_object_paging_begin(object
);
3895 "vm_fault_unwire -> vm_fault_page\n",
3897 result
= vm_fault_page(
3899 entry
->offset
+ (va
- entry
->vme_start
),
3901 &prot
, &result_page
, &top_page
,
3903 NULL
, map
->no_zero_fill
,
3904 FALSE
, &fault_info
);
3905 } while (result
== VM_FAULT_RETRY
);
3908 * If this was a mapping to a file on a device that has been forcibly
3909 * unmounted, then we won't get a page back from vm_fault_page(). Just
3910 * move on to the next one in case the remaining pages are mapped from
3911 * different objects. During a forced unmount, the object is terminated
3912 * so the alive flag will be false if this happens. A forced unmount will
3913 * will occur when an external disk is unplugged before the user does an
3914 * eject, so we don't want to panic in that situation.
3917 if (result
== VM_FAULT_MEMORY_ERROR
&& !object
->alive
)
3920 if (result
!= VM_FAULT_SUCCESS
)
3921 panic("vm_fault_unwire: failure");
3923 result_object
= result_page
->object
;
3925 if ((pmap
) && (result_page
->phys_page
!= vm_page_guard_addr
)) {
3926 pmap_change_wiring(pmap
,
3927 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3930 assert(result_page
->phys_page
!=
3931 vm_page_fictitious_addr
);
3932 pmap_disconnect(result_page
->phys_page
);
3933 VM_PAGE_FREE(result_page
);
3935 if (VM_PAGE_WIRED(result_page
)) {
3936 vm_page_lockspin_queues();
3937 vm_page_unwire(result_page
);
3938 vm_page_unlock_queues();
3940 if(entry
->zero_wired_pages
) {
3941 pmap_zero_page(result_page
->phys_page
);
3942 entry
->zero_wired_pages
= FALSE
;
3945 PAGE_WAKEUP_DONE(result_page
);
3947 vm_fault_cleanup(result_object
, top_page
);
3952 * Inform the physical mapping system that the range
3953 * of addresses may fault, so that page tables and
3954 * such may be unwired themselves.
3957 pmap_pageable(pmap
, pmap_addr
,
3958 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
3963 * vm_fault_wire_fast:
3965 * Handle common case of a wire down page fault at the given address.
3966 * If successful, the page is inserted into the associated physical map.
3967 * The map entry is passed in to avoid the overhead of a map lookup.
3969 * NOTE: the given address should be truncated to the
3970 * proper page address.
3972 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
3973 * a standard error specifying why the fault is fatal is returned.
3975 * The map in question must be referenced, and remains so.
3976 * Caller has a read lock on the map.
3978 * This is a stripped version of vm_fault() for wiring pages. Anything
3979 * other than the common case will return KERN_FAILURE, and the caller
3980 * is expected to call vm_fault().
3984 __unused vm_map_t map
,
3986 vm_map_entry_t entry
,
3988 vm_map_offset_t pmap_addr
)
3991 vm_object_offset_t offset
;
3992 register vm_page_t m
;
3994 thread_t thread
= current_thread();
3998 VM_STAT_INCR(faults
);
4000 if (thread
!= THREAD_NULL
&& thread
->task
!= TASK_NULL
)
4001 thread
->task
->faults
++;
4008 #define RELEASE_PAGE(m) { \
4009 PAGE_WAKEUP_DONE(m); \
4010 vm_page_lockspin_queues(); \
4011 vm_page_unwire(m); \
4012 vm_page_unlock_queues(); \
4016 #undef UNLOCK_THINGS
4017 #define UNLOCK_THINGS { \
4018 vm_object_paging_end(object); \
4019 vm_object_unlock(object); \
4022 #undef UNLOCK_AND_DEALLOCATE
4023 #define UNLOCK_AND_DEALLOCATE { \
4025 vm_object_deallocate(object); \
4028 * Give up and have caller do things the hard way.
4032 UNLOCK_AND_DEALLOCATE; \
4033 return(KERN_FAILURE); \
4038 * If this entry is not directly to a vm_object, bail out.
4040 if (entry
->is_sub_map
)
4041 return(KERN_FAILURE
);
4044 * Find the backing store object and offset into it.
4047 object
= entry
->object
.vm_object
;
4048 offset
= (va
- entry
->vme_start
) + entry
->offset
;
4049 prot
= entry
->protection
;
4052 * Make a reference to this object to prevent its
4053 * disposal while we are messing with it.
4056 vm_object_lock(object
);
4057 vm_object_reference_locked(object
);
4058 vm_object_paging_begin(object
);
4061 * INVARIANTS (through entire routine):
4063 * 1) At all times, we must either have the object
4064 * lock or a busy page in some object to prevent
4065 * some other thread from trying to bring in
4068 * 2) Once we have a busy page, we must remove it from
4069 * the pageout queues, so that the pageout daemon
4070 * will not grab it away.
4075 * Look for page in top-level object. If it's not there or
4076 * there's something going on, give up.
4077 * ENCRYPTED SWAP: use the slow fault path, since we'll need to
4078 * decrypt the page before wiring it down.
4080 m
= vm_page_lookup(object
, offset
);
4081 if ((m
== VM_PAGE_NULL
) || (m
->busy
) || (m
->encrypted
) ||
4082 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
4086 ASSERT_PAGE_DECRYPTED(m
);
4088 if (m
->fictitious
&&
4089 m
->phys_page
== vm_page_guard_addr
) {
4091 * Guard pages are fictitious pages and are never
4092 * entered into a pmap, so let's say it's been wired...
4099 * Wire the page down now. All bail outs beyond this
4100 * point must unwire the page.
4103 vm_page_lockspin_queues();
4105 vm_page_unlock_queues();
4108 * Mark page busy for other threads.
4115 * Give up if the page is being written and there's a copy object
4117 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
4123 * Put this page into the physical map.
4125 type_of_fault
= DBG_CACHE_HIT_FAULT
;
4126 kr
= vm_fault_enter(m
,
4137 * Unlock everything, and return
4140 PAGE_WAKEUP_DONE(m
);
4141 UNLOCK_AND_DEALLOCATE
;
4148 * Routine: vm_fault_copy_cleanup
4150 * Release a page used by vm_fault_copy.
4154 vm_fault_copy_cleanup(
4158 vm_object_t object
= page
->object
;
4160 vm_object_lock(object
);
4161 PAGE_WAKEUP_DONE(page
);
4162 if (!page
->active
&& !page
->inactive
&& !page
->throttled
) {
4163 vm_page_lockspin_queues();
4164 if (!page
->active
&& !page
->inactive
&& !page
->throttled
)
4165 vm_page_activate(page
);
4166 vm_page_unlock_queues();
4168 vm_fault_cleanup(object
, top_page
);
4172 vm_fault_copy_dst_cleanup(
4177 if (page
!= VM_PAGE_NULL
) {
4178 object
= page
->object
;
4179 vm_object_lock(object
);
4180 vm_page_lockspin_queues();
4181 vm_page_unwire(page
);
4182 vm_page_unlock_queues();
4183 vm_object_paging_end(object
);
4184 vm_object_unlock(object
);
4189 * Routine: vm_fault_copy
4192 * Copy pages from one virtual memory object to another --
4193 * neither the source nor destination pages need be resident.
4195 * Before actually copying a page, the version associated with
4196 * the destination address map wil be verified.
4198 * In/out conditions:
4199 * The caller must hold a reference, but not a lock, to
4200 * each of the source and destination objects and to the
4204 * Returns KERN_SUCCESS if no errors were encountered in
4205 * reading or writing the data. Returns KERN_INTERRUPTED if
4206 * the operation was interrupted (only possible if the
4207 * "interruptible" argument is asserted). Other return values
4208 * indicate a permanent error in copying the data.
4210 * The actual amount of data copied will be returned in the
4211 * "copy_size" argument. In the event that the destination map
4212 * verification failed, this amount may be less than the amount
4217 vm_object_t src_object
,
4218 vm_object_offset_t src_offset
,
4219 vm_map_size_t
*copy_size
, /* INOUT */
4220 vm_object_t dst_object
,
4221 vm_object_offset_t dst_offset
,
4223 vm_map_version_t
*dst_version
,
4226 vm_page_t result_page
;
4229 vm_page_t src_top_page
;
4233 vm_page_t dst_top_page
;
4236 vm_map_size_t amount_left
;
4237 vm_object_t old_copy_object
;
4238 kern_return_t error
= 0;
4239 vm_fault_return_t result
;
4241 vm_map_size_t part_size
;
4242 struct vm_object_fault_info fault_info_src
;
4243 struct vm_object_fault_info fault_info_dst
;
4246 * In order not to confuse the clustered pageins, align
4247 * the different offsets on a page boundary.
4252 *copy_size -= amount_left; \
4256 amount_left
= *copy_size
;
4258 fault_info_src
.interruptible
= interruptible
;
4259 fault_info_src
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
4260 fault_info_src
.user_tag
= 0;
4261 fault_info_src
.lo_offset
= vm_object_trunc_page(src_offset
);
4262 fault_info_src
.hi_offset
= fault_info_src
.lo_offset
+ amount_left
;
4263 fault_info_src
.no_cache
= FALSE
;
4264 fault_info_src
.stealth
= TRUE
;
4266 fault_info_dst
.interruptible
= interruptible
;
4267 fault_info_dst
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
4268 fault_info_dst
.user_tag
= 0;
4269 fault_info_dst
.lo_offset
= vm_object_trunc_page(dst_offset
);
4270 fault_info_dst
.hi_offset
= fault_info_dst
.lo_offset
+ amount_left
;
4271 fault_info_dst
.no_cache
= FALSE
;
4272 fault_info_dst
.stealth
= TRUE
;
4274 do { /* while (amount_left > 0) */
4276 * There may be a deadlock if both source and destination
4277 * pages are the same. To avoid this deadlock, the copy must
4278 * start by getting the destination page in order to apply
4279 * COW semantics if any.
4282 RetryDestinationFault
: ;
4284 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
4286 vm_object_lock(dst_object
);
4287 vm_object_paging_begin(dst_object
);
4289 if (amount_left
> (vm_size_t
) -1) {
4290 /* 32-bit overflow */
4291 fault_info_dst
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
4293 fault_info_dst
.cluster_size
= (vm_size_t
) amount_left
;
4294 assert(fault_info_dst
.cluster_size
== amount_left
);
4297 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
4298 result
= vm_fault_page(dst_object
,
4299 vm_object_trunc_page(dst_offset
),
4300 VM_PROT_WRITE
|VM_PROT_READ
,
4302 &dst_prot
, &dst_page
, &dst_top_page
,
4305 dst_map
->no_zero_fill
,
4306 FALSE
, &fault_info_dst
);
4308 case VM_FAULT_SUCCESS
:
4310 case VM_FAULT_RETRY
:
4311 goto RetryDestinationFault
;
4312 case VM_FAULT_MEMORY_SHORTAGE
:
4313 if (vm_page_wait(interruptible
))
4314 goto RetryDestinationFault
;
4316 case VM_FAULT_INTERRUPTED
:
4317 RETURN(MACH_SEND_INTERRUPTED
);
4318 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
4319 /* success but no VM page: fail the copy */
4320 vm_object_paging_end(dst_object
);
4321 vm_object_unlock(dst_object
);
4323 case VM_FAULT_MEMORY_ERROR
:
4327 return(KERN_MEMORY_ERROR
);
4329 panic("vm_fault_copy: unexpected error 0x%x from "
4330 "vm_fault_page()\n", result
);
4332 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
4334 old_copy_object
= dst_page
->object
->copy
;
4337 * There exists the possiblity that the source and
4338 * destination page are the same. But we can't
4339 * easily determine that now. If they are the
4340 * same, the call to vm_fault_page() for the
4341 * destination page will deadlock. To prevent this we
4342 * wire the page so we can drop busy without having
4343 * the page daemon steal the page. We clean up the
4344 * top page but keep the paging reference on the object
4345 * holding the dest page so it doesn't go away.
4348 vm_page_lockspin_queues();
4349 vm_page_wire(dst_page
);
4350 vm_page_unlock_queues();
4351 PAGE_WAKEUP_DONE(dst_page
);
4352 vm_object_unlock(dst_page
->object
);
4354 if (dst_top_page
!= VM_PAGE_NULL
) {
4355 vm_object_lock(dst_object
);
4356 VM_PAGE_FREE(dst_top_page
);
4357 vm_object_paging_end(dst_object
);
4358 vm_object_unlock(dst_object
);
4363 if (src_object
== VM_OBJECT_NULL
) {
4365 * No source object. We will just
4366 * zero-fill the page in dst_object.
4368 src_page
= VM_PAGE_NULL
;
4369 result_page
= VM_PAGE_NULL
;
4371 vm_object_lock(src_object
);
4372 src_page
= vm_page_lookup(src_object
,
4373 vm_object_trunc_page(src_offset
));
4374 if (src_page
== dst_page
) {
4375 src_prot
= dst_prot
;
4376 result_page
= VM_PAGE_NULL
;
4378 src_prot
= VM_PROT_READ
;
4379 vm_object_paging_begin(src_object
);
4381 if (amount_left
> (vm_size_t
) -1) {
4382 /* 32-bit overflow */
4383 fault_info_src
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
4385 fault_info_src
.cluster_size
= (vm_size_t
) amount_left
;
4386 assert(fault_info_src
.cluster_size
== amount_left
);
4390 "vm_fault_copy(2) -> vm_fault_page\n",
4392 result
= vm_fault_page(
4394 vm_object_trunc_page(src_offset
),
4395 VM_PROT_READ
, FALSE
,
4397 &result_page
, &src_top_page
,
4398 (int *)0, &error
, FALSE
,
4399 FALSE
, &fault_info_src
);
4402 case VM_FAULT_SUCCESS
:
4404 case VM_FAULT_RETRY
:
4405 goto RetrySourceFault
;
4406 case VM_FAULT_MEMORY_SHORTAGE
:
4407 if (vm_page_wait(interruptible
))
4408 goto RetrySourceFault
;
4410 case VM_FAULT_INTERRUPTED
:
4411 vm_fault_copy_dst_cleanup(dst_page
);
4412 RETURN(MACH_SEND_INTERRUPTED
);
4413 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
4414 /* success but no VM page: fail */
4415 vm_object_paging_end(src_object
);
4416 vm_object_unlock(src_object
);
4418 case VM_FAULT_MEMORY_ERROR
:
4419 vm_fault_copy_dst_cleanup(dst_page
);
4423 return(KERN_MEMORY_ERROR
);
4425 panic("vm_fault_copy(2): unexpected "
4427 "vm_fault_page()\n", result
);
4431 assert((src_top_page
== VM_PAGE_NULL
) ==
4432 (result_page
->object
== src_object
));
4434 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
4435 vm_object_unlock(result_page
->object
);
4438 if (!vm_map_verify(dst_map
, dst_version
)) {
4439 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4440 vm_fault_copy_cleanup(result_page
, src_top_page
);
4441 vm_fault_copy_dst_cleanup(dst_page
);
4445 vm_object_lock(dst_page
->object
);
4447 if (dst_page
->object
->copy
!= old_copy_object
) {
4448 vm_object_unlock(dst_page
->object
);
4449 vm_map_verify_done(dst_map
, dst_version
);
4450 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4451 vm_fault_copy_cleanup(result_page
, src_top_page
);
4452 vm_fault_copy_dst_cleanup(dst_page
);
4455 vm_object_unlock(dst_page
->object
);
4458 * Copy the page, and note that it is dirty
4462 if (!page_aligned(src_offset
) ||
4463 !page_aligned(dst_offset
) ||
4464 !page_aligned(amount_left
)) {
4466 vm_object_offset_t src_po
,
4469 src_po
= src_offset
- vm_object_trunc_page(src_offset
);
4470 dst_po
= dst_offset
- vm_object_trunc_page(dst_offset
);
4472 if (dst_po
> src_po
) {
4473 part_size
= PAGE_SIZE
- dst_po
;
4475 part_size
= PAGE_SIZE
- src_po
;
4477 if (part_size
> (amount_left
)){
4478 part_size
= amount_left
;
4481 if (result_page
== VM_PAGE_NULL
) {
4482 assert((vm_offset_t
) dst_po
== dst_po
);
4483 assert((vm_size_t
) part_size
== part_size
);
4484 vm_page_part_zero_fill(dst_page
,
4485 (vm_offset_t
) dst_po
,
4486 (vm_size_t
) part_size
);
4488 assert((vm_offset_t
) src_po
== src_po
);
4489 assert((vm_offset_t
) dst_po
== dst_po
);
4490 assert((vm_size_t
) part_size
== part_size
);
4491 vm_page_part_copy(result_page
,
4492 (vm_offset_t
) src_po
,
4494 (vm_offset_t
) dst_po
,
4495 (vm_size_t
)part_size
);
4496 if(!dst_page
->dirty
){
4497 vm_object_lock(dst_object
);
4498 dst_page
->dirty
= TRUE
;
4499 vm_object_unlock(dst_page
->object
);
4504 part_size
= PAGE_SIZE
;
4506 if (result_page
== VM_PAGE_NULL
)
4507 vm_page_zero_fill(dst_page
);
4509 vm_page_copy(result_page
, dst_page
);
4510 if(!dst_page
->dirty
){
4511 vm_object_lock(dst_object
);
4512 dst_page
->dirty
= TRUE
;
4513 vm_object_unlock(dst_page
->object
);
4520 * Unlock everything, and return
4523 vm_map_verify_done(dst_map
, dst_version
);
4525 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
4526 vm_fault_copy_cleanup(result_page
, src_top_page
);
4527 vm_fault_copy_dst_cleanup(dst_page
);
4529 amount_left
-= part_size
;
4530 src_offset
+= part_size
;
4531 dst_offset
+= part_size
;
4532 } while (amount_left
> 0);
4534 RETURN(KERN_SUCCESS
);
4540 #if VM_FAULT_CLASSIFY
4542 * Temporary statistics gathering support.
4546 * Statistics arrays:
4548 #define VM_FAULT_TYPES_MAX 5
4549 #define VM_FAULT_LEVEL_MAX 8
4551 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
4553 #define VM_FAULT_TYPE_ZERO_FILL 0
4554 #define VM_FAULT_TYPE_MAP_IN 1
4555 #define VM_FAULT_TYPE_PAGER 2
4556 #define VM_FAULT_TYPE_COPY 3
4557 #define VM_FAULT_TYPE_OTHER 4
4561 vm_fault_classify(vm_object_t object
,
4562 vm_object_offset_t offset
,
4563 vm_prot_t fault_type
)
4565 int type
, level
= 0;
4569 m
= vm_page_lookup(object
, offset
);
4570 if (m
!= VM_PAGE_NULL
) {
4571 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
) {
4572 type
= VM_FAULT_TYPE_OTHER
;
4575 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
4576 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
4577 type
= VM_FAULT_TYPE_MAP_IN
;
4580 type
= VM_FAULT_TYPE_COPY
;
4584 if (object
->pager_created
) {
4585 type
= VM_FAULT_TYPE_PAGER
;
4588 if (object
->shadow
== VM_OBJECT_NULL
) {
4589 type
= VM_FAULT_TYPE_ZERO_FILL
;
4593 offset
+= object
->shadow_offset
;
4594 object
= object
->shadow
;
4600 if (level
> VM_FAULT_LEVEL_MAX
)
4601 level
= VM_FAULT_LEVEL_MAX
;
4603 vm_fault_stats
[type
][level
] += 1;
4608 /* cleanup routine to call from debugger */
4611 vm_fault_classify_init(void)
4615 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
4616 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
4617 vm_fault_stats
[type
][level
] = 0;
4623 #endif /* VM_FAULT_CLASSIFY */
4626 extern int cs_validation
;
4629 vm_page_validate_cs_mapped(
4634 vm_object_offset_t offset
;
4636 memory_object_t pager
;
4638 boolean_t validated
, tainted
;
4641 vm_object_lock_assert_exclusive(page
->object
);
4643 if (!cs_validation
) {
4647 if (page
->wpmapped
&& !page
->cs_tainted
) {
4649 * This page was mapped for "write" access sometime in the
4650 * past and could still be modifiable in the future.
4651 * Consider it tainted.
4652 * [ If the page was already found to be "tainted", no
4653 * need to re-validate. ]
4655 page
->cs_validated
= TRUE
;
4656 page
->cs_tainted
= TRUE
;
4658 printf("CODESIGNING: vm_page_validate_cs: "
4659 "page %p obj %p off 0x%llx "
4661 page
, page
->object
, page
->offset
);
4663 vm_cs_validated_dirtied
++;
4666 if (page
->cs_validated
) {
4672 object
= page
->object
;
4673 assert(object
->code_signed
);
4674 offset
= page
->offset
;
4676 if (!object
->alive
|| object
->terminating
|| object
->pager
== NULL
) {
4678 * The object is terminating and we don't have its pager
4679 * so we can't validate the data...
4684 * Since we get here to validate a page that was brought in by
4685 * the pager, we know that this pager is all setup and ready
4688 assert(!object
->internal
);
4689 assert(object
->pager
!= NULL
);
4690 assert(object
->pager_ready
);
4692 pager
= object
->pager
;
4693 assert(object
->paging_in_progress
);
4694 kr
= vnode_pager_get_object_cs_blobs(pager
, &blobs
);
4695 if (kr
!= KERN_SUCCESS
) {
4699 /* verify the SHA1 hash for this page */
4700 validated
= cs_validate_page(blobs
,
4701 offset
+ object
->paging_offset
,
4702 (const void *)kaddr
,
4705 page
->cs_validated
= validated
;
4707 page
->cs_tainted
= tainted
;
4712 vm_page_validate_cs(
4716 vm_object_offset_t offset
;
4717 vm_map_offset_t koffset
;
4718 vm_map_size_t ksize
;
4721 boolean_t busy_page
;
4723 vm_object_lock_assert_held(page
->object
);
4725 if (!cs_validation
) {
4729 if (page
->wpmapped
&& !page
->cs_tainted
) {
4730 vm_object_lock_assert_exclusive(page
->object
);
4733 * This page was mapped for "write" access sometime in the
4734 * past and could still be modifiable in the future.
4735 * Consider it tainted.
4736 * [ If the page was already found to be "tainted", no
4737 * need to re-validate. ]
4739 page
->cs_validated
= TRUE
;
4740 page
->cs_tainted
= TRUE
;
4742 printf("CODESIGNING: vm_page_validate_cs: "
4743 "page %p obj %p off 0x%llx "
4745 page
, page
->object
, page
->offset
);
4747 vm_cs_validated_dirtied
++;
4750 if (page
->cs_validated
) {
4754 vm_object_lock_assert_exclusive(page
->object
);
4756 object
= page
->object
;
4757 assert(object
->code_signed
);
4758 offset
= page
->offset
;
4760 busy_page
= page
->busy
;
4762 /* keep page busy while we map (and unlock) the VM object */
4767 * Take a paging reference on the VM object
4768 * to protect it from collapse or bypass,
4769 * and keep it from disappearing too.
4771 vm_object_paging_begin(object
);
4773 /* map the page in the kernel address space */
4775 ksize
= PAGE_SIZE_64
;
4776 kr
= vm_paging_map_object(&koffset
,
4782 FALSE
); /* can't unlock object ! */
4783 if (kr
!= KERN_SUCCESS
) {
4784 panic("vm_page_validate_cs: could not map page: 0x%x\n", kr
);
4786 kaddr
= CAST_DOWN(vm_offset_t
, koffset
);
4788 /* validate the mapped page */
4789 vm_page_validate_cs_mapped(page
, (const void *) kaddr
);
4792 assert(object
== page
->object
);
4793 vm_object_lock_assert_exclusive(object
);
4796 PAGE_WAKEUP_DONE(page
);
4799 /* unmap the map from the kernel address space */
4800 vm_paging_unmap_object(object
, koffset
, koffset
+ ksize
);
4805 vm_object_paging_end(object
);