3 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
9 * This file contains Original Code and/or Modifications of Original Code
10 * as defined in and that are subject to the Apple Public Source License
11 * Version 2.0 (the 'License'). You may not use this file except in
12 * compliance with the License. Please obtain a copy of the License at
13 * http://www.opensource.apple.com/apsl/ and read it before using this
16 * The Original Code and all software distributed under the License are
17 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
18 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
19 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
21 * Please see the License for the specific language governing rights and
22 * limitations under the License.
24 * @APPLE_LICENSE_HEADER_END@
30 * Mach Operating System
31 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
32 * All Rights Reserved.
34 * Permission to use, copy, modify and distribute this software and its
35 * documentation is hereby granted, provided that both the copyright
36 * notice and this permission notice appear in all copies of the
37 * software, derivative works or modified versions, and any portions
38 * thereof, and that both notices appear in supporting documentation.
40 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
41 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
42 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
44 * Carnegie Mellon requests users of this software to return to
46 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
47 * School of Computer Science
48 * Carnegie Mellon University
49 * Pittsburgh PA 15213-3890
51 * any improvements or extensions that they make and grant Carnegie Mellon
52 * the rights to redistribute these changes.
58 * Author: Avadis Tevanian, Jr., Michael Wayne Young
60 * Page fault handling module.
63 /* remove after component interface available */
64 extern int vnode_pager_workaround
;
65 extern int device_pager_workaround
;
68 #include <mach_cluster_stats.h>
69 #include <mach_pagemap.h>
72 #include <vm/vm_fault.h>
73 #include <mach/kern_return.h>
74 #include <mach/message.h> /* for error codes */
75 #include <kern/host_statistics.h>
76 #include <kern/counters.h>
77 #include <kern/task.h>
78 #include <kern/thread.h>
79 #include <kern/sched_prim.h>
80 #include <kern/host.h>
82 #include <ppc/proc_reg.h>
83 #include <ppc/pmap_internals.h>
84 #include <vm/task_working_set.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
89 #include <vm/vm_pageout.h>
90 #include <mach/vm_param.h>
91 #include <mach/vm_behavior.h>
92 #include <mach/memory_object.h>
93 /* For memory_object_data_{request,unlock} */
94 #include <kern/mach_param.h>
95 #include <kern/macro_help.h>
96 #include <kern/zalloc.h>
97 #include <kern/misc_protos.h>
99 #include <sys/kdebug.h>
101 #define VM_FAULT_CLASSIFY 0
102 #define VM_FAULT_STATIC_CONFIG 1
104 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
106 int vm_object_absent_max
= 50;
108 int vm_fault_debug
= 0;
109 boolean_t vm_page_deactivate_behind
= TRUE
;
112 #if !VM_FAULT_STATIC_CONFIG
113 boolean_t vm_fault_dirty_handling
= FALSE
;
114 boolean_t vm_fault_interruptible
= FALSE
;
115 boolean_t software_reference_bits
= TRUE
;
119 extern struct db_watchpoint
*db_watchpoint_list
;
120 #endif /* MACH_KDB */
122 /* Forward declarations of internal routines. */
123 extern kern_return_t
vm_fault_wire_fast(
126 vm_map_entry_t entry
,
128 vm_offset_t pmap_addr
);
130 extern void vm_fault_continue(void);
132 extern void vm_fault_copy_cleanup(
136 extern void vm_fault_copy_dst_cleanup(
139 #if VM_FAULT_CLASSIFY
140 extern void vm_fault_classify(vm_object_t object
,
141 vm_object_offset_t offset
,
142 vm_prot_t fault_type
);
144 extern void vm_fault_classify_init(void);
148 * Routine: vm_fault_init
150 * Initialize our private data structures.
158 * Routine: vm_fault_cleanup
160 * Clean up the result of vm_fault_page.
162 * The paging reference for "object" is released.
163 * "object" is unlocked.
164 * If "top_page" is not null, "top_page" is
165 * freed and the paging reference for the object
166 * containing it is released.
169 * "object" must be locked.
173 register vm_object_t object
,
174 register vm_page_t top_page
)
176 vm_object_paging_end(object
);
177 vm_object_unlock(object
);
179 if (top_page
!= VM_PAGE_NULL
) {
180 object
= top_page
->object
;
181 vm_object_lock(object
);
182 VM_PAGE_FREE(top_page
);
183 vm_object_paging_end(object
);
184 vm_object_unlock(object
);
188 #if MACH_CLUSTER_STATS
189 #define MAXCLUSTERPAGES 16
191 unsigned long pages_in_cluster
;
192 unsigned long pages_at_higher_offsets
;
193 unsigned long pages_at_lower_offsets
;
194 } cluster_stats_in
[MAXCLUSTERPAGES
];
195 #define CLUSTER_STAT(clause) clause
196 #define CLUSTER_STAT_HIGHER(x) \
197 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
198 #define CLUSTER_STAT_LOWER(x) \
199 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
200 #define CLUSTER_STAT_CLUSTER(x) \
201 ((cluster_stats_in[(x)].pages_in_cluster)++)
202 #else /* MACH_CLUSTER_STATS */
203 #define CLUSTER_STAT(clause)
204 #endif /* MACH_CLUSTER_STATS */
206 /* XXX - temporary */
207 boolean_t vm_allow_clustered_pagein
= FALSE
;
208 int vm_pagein_cluster_used
= 0;
211 * Prepage default sizes given VM_BEHAVIOR_DEFAULT reference behavior
213 int vm_default_ahead
= 1; /* Number of pages to prepage ahead */
214 int vm_default_behind
= 0; /* Number of pages to prepage behind */
216 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
219 * Routine: vm_fault_page
221 * Find the resident page for the virtual memory
222 * specified by the given virtual memory object
224 * Additional arguments:
225 * The required permissions for the page is given
226 * in "fault_type". Desired permissions are included
227 * in "protection". The minimum and maximum valid offsets
228 * within the object for the relevant map entry are
229 * passed in "lo_offset" and "hi_offset" respectively and
230 * the expected page reference pattern is passed in "behavior".
231 * These three parameters are used to determine pagein cluster
234 * If the desired page is known to be resident (for
235 * example, because it was previously wired down), asserting
236 * the "unwiring" parameter will speed the search.
238 * If the operation can be interrupted (by thread_abort
239 * or thread_terminate), then the "interruptible"
240 * parameter should be asserted.
243 * The page containing the proper data is returned
247 * The source object must be locked and referenced,
248 * and must donate one paging reference. The reference
249 * is not affected. The paging reference and lock are
252 * If the call succeeds, the object in which "result_page"
253 * resides is left locked and holding a paging reference.
254 * If this is not the original object, a busy page in the
255 * original object is returned in "top_page", to prevent other
256 * callers from pursuing this same data, along with a paging
257 * reference for the original object. The "top_page" should
258 * be destroyed when this guarantee is no longer required.
259 * The "result_page" is also left busy. It is not removed
260 * from the pageout queues.
266 vm_object_t first_object
, /* Object to begin search */
267 vm_object_offset_t first_offset
, /* Offset into object */
268 vm_prot_t fault_type
, /* What access is requested */
269 boolean_t must_be_resident
,/* Must page be resident? */
270 int interruptible
, /* how may fault be interrupted? */
271 vm_object_offset_t lo_offset
, /* Map entry start */
272 vm_object_offset_t hi_offset
, /* Map entry end */
273 vm_behavior_t behavior
, /* Page reference behavior */
274 /* Modifies in place: */
275 vm_prot_t
*protection
, /* Protection for mapping */
277 vm_page_t
*result_page
, /* Page found, if successful */
278 vm_page_t
*top_page
, /* Page in top object, if
279 * not result_page. */
280 int *type_of_fault
, /* if non-null, fill in with type of fault
281 * COW, zero-fill, etc... returned in trace point */
282 /* More arguments: */
283 kern_return_t
*error_code
, /* code if page is in error */
284 boolean_t no_zero_fill
, /* don't zero fill absent pages */
285 boolean_t data_supply
, /* treat as data_supply if
286 * it is a write fault and a full
287 * page is provided */
296 vm_object_offset_t offset
;
298 vm_object_t next_object
;
299 vm_object_t copy_object
;
300 boolean_t look_for_page
;
301 vm_prot_t access_required
= fault_type
;
302 vm_prot_t wants_copy_flag
;
303 vm_size_t cluster_size
, length
;
304 vm_object_offset_t cluster_offset
;
305 vm_object_offset_t cluster_start
, cluster_end
, paging_offset
;
306 vm_object_offset_t align_offset
;
307 CLUSTER_STAT(int pages_at_higher_offsets
;)
308 CLUSTER_STAT(int pages_at_lower_offsets
;)
309 kern_return_t wait_result
;
310 boolean_t interruptible_state
;
311 boolean_t bumped_pagein
= FALSE
;
316 * MACH page map - an optional optimization where a bit map is maintained
317 * by the VM subsystem for internal objects to indicate which pages of
318 * the object currently reside on backing store. This existence map
319 * duplicates information maintained by the vnode pager. It is
320 * created at the time of the first pageout against the object, i.e.
321 * at the same time pager for the object is created. The optimization
322 * is designed to eliminate pager interaction overhead, if it is
323 * 'known' that the page does not exist on backing store.
325 * LOOK_FOR() evaluates to TRUE if the page specified by object/offset is
326 * either marked as paged out in the existence map for the object or no
327 * existence map exists for the object. LOOK_FOR() is one of the
328 * criteria in the decision to invoke the pager. It is also used as one
329 * of the criteria to terminate the scan for adjacent pages in a clustered
330 * pagein operation. Note that LOOK_FOR() always evaluates to TRUE for
331 * permanent objects. Note also that if the pager for an internal object
332 * has not been created, the pager is not invoked regardless of the value
333 * of LOOK_FOR() and that clustered pagein scans are only done on an object
334 * for which a pager has been created.
336 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
337 * is marked as paged out in the existence map for the object. PAGED_OUT()
338 * PAGED_OUT() is used to determine if a page has already been pushed
339 * into a copy object in order to avoid a redundant page out operation.
341 #define LOOK_FOR(o, f) (vm_external_state_get((o)->existence_map, (f)) \
342 != VM_EXTERNAL_STATE_ABSENT)
343 #define PAGED_OUT(o, f) (vm_external_state_get((o)->existence_map, (f)) \
344 == VM_EXTERNAL_STATE_EXISTS)
345 #else /* MACH_PAGEMAP */
347 * If the MACH page map optimization is not enabled,
348 * LOOK_FOR() always evaluates to TRUE. The pager will always be
349 * invoked to resolve missing pages in an object, assuming the pager
350 * has been created for the object. In a clustered page operation, the
351 * absence of a page on backing backing store cannot be used to terminate
352 * a scan for adjacent pages since that information is available only in
353 * the pager. Hence pages that may not be paged out are potentially
354 * included in a clustered request. The vnode pager is coded to deal
355 * with any combination of absent/present pages in a clustered
356 * pagein request. PAGED_OUT() always evaluates to FALSE, i.e. the pager
357 * will always be invoked to push a dirty page into a copy object assuming
358 * a pager has been created. If the page has already been pushed, the
359 * pager will ingore the new request.
361 #define LOOK_FOR(o, f) TRUE
362 #define PAGED_OUT(o, f) FALSE
363 #endif /* MACH_PAGEMAP */
368 #define PREPARE_RELEASE_PAGE(m) \
370 vm_page_lock_queues(); \
373 #define DO_RELEASE_PAGE(m) \
375 PAGE_WAKEUP_DONE(m); \
376 if (!m->active && !m->inactive) \
377 vm_page_activate(m); \
378 vm_page_unlock_queues(); \
381 #define RELEASE_PAGE(m) \
383 PREPARE_RELEASE_PAGE(m); \
384 DO_RELEASE_PAGE(m); \
388 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
393 #if !VM_FAULT_STATIC_CONFIG
394 if (vm_fault_dirty_handling
397 * If there are watchpoints set, then
398 * we don't want to give away write permission
399 * on a read fault. Make the task write fault,
400 * so that the watchpoint code notices the access.
402 || db_watchpoint_list
403 #endif /* MACH_KDB */
406 * If we aren't asking for write permission,
407 * then don't give it away. We're using write
408 * faults to set the dirty bit.
410 if (!(fault_type
& VM_PROT_WRITE
))
411 *protection
&= ~VM_PROT_WRITE
;
414 if (!vm_fault_interruptible
)
415 interruptible
= THREAD_UNINT
;
416 #else /* STATIC_CONFIG */
419 * If there are watchpoints set, then
420 * we don't want to give away write permission
421 * on a read fault. Make the task write fault,
422 * so that the watchpoint code notices the access.
424 if (db_watchpoint_list
) {
426 * If we aren't asking for write permission,
427 * then don't give it away. We're using write
428 * faults to set the dirty bit.
430 if (!(fault_type
& VM_PROT_WRITE
))
431 *protection
&= ~VM_PROT_WRITE
;
434 #endif /* MACH_KDB */
435 #endif /* STATIC_CONFIG */
437 interruptible_state
= thread_interrupt_level(interruptible
);
440 * INVARIANTS (through entire routine):
442 * 1) At all times, we must either have the object
443 * lock or a busy page in some object to prevent
444 * some other thread from trying to bring in
447 * Note that we cannot hold any locks during the
448 * pager access or when waiting for memory, so
449 * we use a busy page then.
451 * Note also that we aren't as concerned about more than
452 * one thread attempting to memory_object_data_unlock
453 * the same page at once, so we don't hold the page
454 * as busy then, but do record the highest unlock
455 * value so far. [Unlock requests may also be delivered
458 * 2) To prevent another thread from racing us down the
459 * shadow chain and entering a new page in the top
460 * object before we do, we must keep a busy page in
461 * the top object while following the shadow chain.
463 * 3) We must increment paging_in_progress on any object
464 * for which we have a busy page
466 * 4) We leave busy pages on the pageout queues.
467 * If the pageout daemon comes across a busy page,
468 * it will remove the page from the pageout queues.
472 * Search for the page at object/offset.
475 object
= first_object
;
476 offset
= first_offset
;
477 first_m
= VM_PAGE_NULL
;
478 access_required
= fault_type
;
481 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
482 (integer_t
)object
, offset
, fault_type
, *protection
, 0);
485 * See whether this page is resident
490 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
492 if (!object
->alive
) {
493 vm_fault_cleanup(object
, first_m
);
494 thread_interrupt_level(interruptible_state
);
495 return(VM_FAULT_MEMORY_ERROR
);
497 m
= vm_page_lookup(object
, offset
);
499 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
501 if (m
!= VM_PAGE_NULL
) {
503 * If the page was pre-paged as part of a
504 * cluster, record the fact.
507 vm_pagein_cluster_used
++;
508 m
->clustered
= FALSE
;
512 * If the page is being brought in,
513 * wait for it and then retry.
515 * A possible optimization: if the page
516 * is known to be resident, we can ignore
517 * pages that are absent (regardless of
518 * whether they're busy).
523 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
525 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
527 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
528 (integer_t
)object
, offset
,
530 counter(c_vm_fault_page_block_busy_kernel
++);
532 if (wait_result
!= THREAD_AWAKENED
) {
533 vm_fault_cleanup(object
, first_m
);
534 thread_interrupt_level(interruptible_state
);
535 if (wait_result
== THREAD_RESTART
)
537 return(VM_FAULT_RETRY
);
541 return(VM_FAULT_INTERRUPTED
);
548 * If the page is in error, give up now.
553 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
556 *error_code
= m
->page_error
;
558 vm_fault_cleanup(object
, first_m
);
559 thread_interrupt_level(interruptible_state
);
560 return(VM_FAULT_MEMORY_ERROR
);
564 * If the pager wants us to restart
565 * at the top of the chain,
566 * typically because it has moved the
567 * page to another pager, then do so.
572 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
575 vm_fault_cleanup(object
, first_m
);
576 thread_interrupt_level(interruptible_state
);
577 return(VM_FAULT_RETRY
);
581 * If the page isn't busy, but is absent,
582 * then it was deemed "unavailable".
587 * Remove the non-existent page (unless it's
588 * in the top object) and move on down to the
589 * next object (if there is one).
592 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
595 next_object
= object
->shadow
;
596 if (next_object
== VM_OBJECT_NULL
) {
599 assert(!must_be_resident
);
601 if (object
->shadow_severed
) {
604 thread_interrupt_level(interruptible_state
);
605 return VM_FAULT_MEMORY_ERROR
;
609 * Absent page at bottom of shadow
610 * chain; zero fill the page we left
611 * busy in the first object, and flush
612 * the absent page. But first we
613 * need to allocate a real page.
615 if (VM_PAGE_THROTTLED() ||
616 (real_m
= vm_page_grab()) == VM_PAGE_NULL
) {
617 vm_fault_cleanup(object
, first_m
);
618 thread_interrupt_level(interruptible_state
);
619 return(VM_FAULT_MEMORY_SHORTAGE
);
623 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
624 (integer_t
)object
, offset
,
626 (integer_t
)first_object
, 0);
627 if (object
!= first_object
) {
629 vm_object_paging_end(object
);
630 vm_object_unlock(object
);
631 object
= first_object
;
632 offset
= first_offset
;
634 first_m
= VM_PAGE_NULL
;
635 vm_object_lock(object
);
639 assert(real_m
->busy
);
640 vm_page_insert(real_m
, object
, offset
);
644 * Drop the lock while zero filling
645 * page. Then break because this
646 * is the page we wanted. Checking
647 * the page lock is a waste of time;
648 * this page was either absent or
649 * newly allocated -- in both cases
650 * it can't be page locked by a pager.
655 vm_object_unlock(object
);
656 vm_page_zero_fill(m
);
658 *type_of_fault
= DBG_ZERO_FILL_FAULT
;
659 VM_STAT(zero_fill_count
++);
661 if (bumped_pagein
== TRUE
) {
663 current_task()->pageins
--;
665 vm_object_lock(object
);
667 pmap_clear_modify(m
->phys_addr
);
668 vm_page_lock_queues();
669 VM_PAGE_QUEUES_REMOVE(m
);
670 m
->page_ticket
= vm_page_ticket
;
671 if(m
->object
->size
> 0x80000) {
673 /* depends on the queues lock */
675 queue_enter(&vm_page_queue_zf
,
676 m
, vm_page_t
, pageq
);
679 &vm_page_queue_inactive
,
680 m
, vm_page_t
, pageq
);
682 vm_page_ticket_roll
++;
683 if(vm_page_ticket_roll
==
684 VM_PAGE_TICKETS_IN_ROLL
) {
685 vm_page_ticket_roll
= 0;
687 VM_PAGE_TICKET_ROLL_IDS
)
693 vm_page_inactive_count
++;
694 vm_page_unlock_queues();
697 if (must_be_resident
) {
698 vm_object_paging_end(object
);
699 } else if (object
!= first_object
) {
700 vm_object_paging_end(object
);
706 vm_object_absent_release(object
);
709 vm_page_lock_queues();
710 VM_PAGE_QUEUES_REMOVE(m
);
711 vm_page_unlock_queues();
714 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
715 (integer_t
)object
, offset
,
716 (integer_t
)next_object
,
717 offset
+object
->shadow_offset
,0);
718 offset
+= object
->shadow_offset
;
719 hi_offset
+= object
->shadow_offset
;
720 lo_offset
+= object
->shadow_offset
;
721 access_required
= VM_PROT_READ
;
722 vm_object_lock(next_object
);
723 vm_object_unlock(object
);
724 object
= next_object
;
725 vm_object_paging_begin(object
);
731 && ((object
!= first_object
) ||
732 (object
->copy
!= VM_OBJECT_NULL
))
733 && (fault_type
& VM_PROT_WRITE
)) {
735 * This is a copy-on-write fault that will
736 * cause us to revoke access to this page, but
737 * this page is in the process of being cleaned
738 * in a clustered pageout. We must wait until
739 * the cleaning operation completes before
740 * revoking access to the original page,
741 * otherwise we might attempt to remove a
745 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
748 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
749 (integer_t
)object
, offset
,
751 /* take an extra ref so that object won't die */
752 assert(object
->ref_count
> 0);
754 vm_object_res_reference(object
);
755 vm_fault_cleanup(object
, first_m
);
756 counter(c_vm_fault_page_block_backoff_kernel
++);
757 vm_object_lock(object
);
758 assert(object
->ref_count
> 0);
759 m
= vm_page_lookup(object
, offset
);
760 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
761 PAGE_ASSERT_WAIT(m
, interruptible
);
762 vm_object_unlock(object
);
763 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
764 vm_object_deallocate(object
);
767 vm_object_unlock(object
);
768 vm_object_deallocate(object
);
769 thread_interrupt_level(interruptible_state
);
770 return VM_FAULT_RETRY
;
775 * If the desired access to this page has
776 * been locked out, request that it be unlocked.
779 if (access_required
& m
->page_lock
) {
780 if ((access_required
& m
->unlock_request
) != access_required
) {
781 vm_prot_t new_unlock_request
;
785 dbgTrace(0xBEEF000A, (unsigned int) m
, (unsigned int) object
->pager_ready
); /* (TEST/DEBUG) */
787 if (!object
->pager_ready
) {
789 "vm_f_page: ready wait acc_req %d, obj 0x%X, offset 0x%X, page 0x%X\n",
791 (integer_t
)object
, offset
,
793 /* take an extra ref */
794 assert(object
->ref_count
> 0);
796 vm_object_res_reference(object
);
797 vm_fault_cleanup(object
,
799 counter(c_vm_fault_page_block_backoff_kernel
++);
800 vm_object_lock(object
);
801 assert(object
->ref_count
> 0);
802 if (!object
->pager_ready
) {
803 wait_result
= vm_object_assert_wait(
805 VM_OBJECT_EVENT_PAGER_READY
,
807 vm_object_unlock(object
);
808 if (wait_result
== THREAD_WAITING
)
809 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
810 vm_object_deallocate(object
);
813 vm_object_unlock(object
);
814 vm_object_deallocate(object
);
815 thread_interrupt_level(interruptible_state
);
816 return VM_FAULT_RETRY
;
820 new_unlock_request
= m
->unlock_request
=
821 (access_required
| m
->unlock_request
);
822 vm_object_unlock(object
);
824 "vm_f_page: unlock obj 0x%X, offset 0x%X, page 0x%X, unl_req %d\n",
825 (integer_t
)object
, offset
,
826 (integer_t
)m
, new_unlock_request
, 0);
827 if ((rc
= memory_object_data_unlock(
829 offset
+ object
->paging_offset
,
834 printf("vm_fault: memory_object_data_unlock failed\n");
835 vm_object_lock(object
);
836 vm_fault_cleanup(object
, first_m
);
837 thread_interrupt_level(interruptible_state
);
838 return((rc
== MACH_SEND_INTERRUPTED
) ?
839 VM_FAULT_INTERRUPTED
:
840 VM_FAULT_MEMORY_ERROR
);
842 vm_object_lock(object
);
847 "vm_f_page: access wait acc_req %d, obj 0x%X, offset 0x%X, page 0x%X\n",
848 access_required
, (integer_t
)object
,
849 offset
, (integer_t
)m
, 0);
850 /* take an extra ref so object won't die */
851 assert(object
->ref_count
> 0);
853 vm_object_res_reference(object
);
854 vm_fault_cleanup(object
, first_m
);
855 counter(c_vm_fault_page_block_backoff_kernel
++);
856 vm_object_lock(object
);
857 assert(object
->ref_count
> 0);
858 m
= vm_page_lookup(object
, offset
);
859 if (m
!= VM_PAGE_NULL
&&
860 (access_required
& m
->page_lock
) &&
861 !((access_required
& m
->unlock_request
) != access_required
)) {
862 PAGE_ASSERT_WAIT(m
, interruptible
);
863 vm_object_unlock(object
);
864 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
865 vm_object_deallocate(object
);
868 vm_object_unlock(object
);
869 vm_object_deallocate(object
);
870 thread_interrupt_level(interruptible_state
);
871 return VM_FAULT_RETRY
;
875 * We mark the page busy and leave it on
876 * the pageout queues. If the pageout
877 * deamon comes across it, then it will
882 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
885 #if !VM_FAULT_STATIC_CONFIG
886 if (!software_reference_bits
) {
887 vm_page_lock_queues();
889 vm_stat
.reactivations
++;
891 VM_PAGE_QUEUES_REMOVE(m
);
892 vm_page_unlock_queues();
896 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
897 (integer_t
)object
, offset
, (integer_t
)m
, 0, 0);
905 (object
->pager_created
) &&
906 LOOK_FOR(object
, offset
) &&
910 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
912 if ((look_for_page
|| (object
== first_object
))
914 && !(object
->phys_contiguous
)) {
916 * Allocate a new page for this object/offset
920 m
= vm_page_grab_fictitious();
922 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
924 if (m
== VM_PAGE_NULL
) {
925 vm_fault_cleanup(object
, first_m
);
926 thread_interrupt_level(interruptible_state
);
927 return(VM_FAULT_FICTITIOUS_SHORTAGE
);
929 vm_page_insert(m
, object
, offset
);
932 if ((look_for_page
&& !must_be_resident
)) {
936 * If the memory manager is not ready, we
937 * cannot make requests.
939 if (!object
->pager_ready
) {
941 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
943 if(m
!= VM_PAGE_NULL
)
946 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
947 (integer_t
)object
, offset
, 0, 0, 0);
948 /* take an extra ref so object won't die */
949 assert(object
->ref_count
> 0);
951 vm_object_res_reference(object
);
952 vm_fault_cleanup(object
, first_m
);
953 counter(c_vm_fault_page_block_backoff_kernel
++);
954 vm_object_lock(object
);
955 assert(object
->ref_count
> 0);
956 if (!object
->pager_ready
) {
957 wait_result
= vm_object_assert_wait(object
,
958 VM_OBJECT_EVENT_PAGER_READY
,
960 vm_object_unlock(object
);
961 if (wait_result
== THREAD_WAITING
)
962 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
963 vm_object_deallocate(object
);
966 vm_object_unlock(object
);
967 vm_object_deallocate(object
);
968 thread_interrupt_level(interruptible_state
);
969 return VM_FAULT_RETRY
;
973 if(object
->phys_contiguous
) {
974 if(m
!= VM_PAGE_NULL
) {
980 if (object
->internal
) {
982 * Requests to the default pager
983 * must reserve a real page in advance,
984 * because the pager's data-provided
985 * won't block for pages. IMPORTANT:
986 * this acts as a throttling mechanism
987 * for data_requests to the default
992 dbgTrace(0xBEEF000F, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
994 if (m
->fictitious
&& !vm_page_convert(m
)) {
996 vm_fault_cleanup(object
, first_m
);
997 thread_interrupt_level(interruptible_state
);
998 return(VM_FAULT_MEMORY_SHORTAGE
);
1000 } else if (object
->absent_count
>
1001 vm_object_absent_max
) {
1003 * If there are too many outstanding page
1004 * requests pending on this object, we
1005 * wait for them to be resolved now.
1009 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1011 if(m
!= VM_PAGE_NULL
)
1013 /* take an extra ref so object won't die */
1014 assert(object
->ref_count
> 0);
1015 object
->ref_count
++;
1016 vm_object_res_reference(object
);
1017 vm_fault_cleanup(object
, first_m
);
1018 counter(c_vm_fault_page_block_backoff_kernel
++);
1019 vm_object_lock(object
);
1020 assert(object
->ref_count
> 0);
1021 if (object
->absent_count
> vm_object_absent_max
) {
1022 vm_object_absent_assert_wait(object
,
1024 vm_object_unlock(object
);
1025 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1026 vm_object_deallocate(object
);
1029 vm_object_unlock(object
);
1030 vm_object_deallocate(object
);
1031 thread_interrupt_level(interruptible_state
);
1032 return VM_FAULT_RETRY
;
1037 * Indicate that the page is waiting for data
1038 * from the memory manager.
1041 if(m
!= VM_PAGE_NULL
) {
1043 m
->list_req_pending
= TRUE
;
1046 object
->absent_count
++;
1051 cluster_start
= offset
;
1055 * lengthen the cluster by the pages in the working set
1058 (current_task()->dynamic_working_set
!= 0)) {
1059 cluster_end
= cluster_start
+ length
;
1060 /* tws values for start and end are just a
1061 * suggestions. Therefore, as long as
1062 * build_cluster does not use pointers or
1063 * take action based on values that
1064 * could be affected by re-entrance we
1065 * do not need to take the map lock.
1067 cluster_end
= offset
+ PAGE_SIZE_64
;
1068 tws_build_cluster((tws_hash_t
)
1069 current_task()->dynamic_working_set
,
1070 object
, &cluster_start
,
1071 &cluster_end
, 0x40000);
1072 length
= cluster_end
- cluster_start
;
1075 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1078 * We have a busy page, so we can
1079 * release the object lock.
1081 vm_object_unlock(object
);
1084 * Call the memory manager to retrieve the data.
1088 *type_of_fault
= (length
<< 8) | DBG_PAGEIN_FAULT
;
1090 current_task()->pageins
++;
1091 bumped_pagein
= TRUE
;
1094 * If this object uses a copy_call strategy,
1095 * and we are interested in a copy of this object
1096 * (having gotten here only by following a
1097 * shadow chain), then tell the memory manager
1098 * via a flag added to the desired_access
1099 * parameter, so that it can detect a race
1100 * between our walking down the shadow chain
1101 * and its pushing pages up into a copy of
1102 * the object that it manages.
1105 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&&
1106 object
!= first_object
) {
1107 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1109 wants_copy_flag
= VM_PROT_NONE
;
1113 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1114 (integer_t
)object
, offset
, (integer_t
)m
,
1115 access_required
| wants_copy_flag
, 0);
1117 rc
= memory_object_data_request(object
->pager
,
1118 cluster_start
+ object
->paging_offset
,
1120 access_required
| wants_copy_flag
);
1124 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1126 if (rc
!= KERN_SUCCESS
) {
1127 if (rc
!= MACH_SEND_INTERRUPTED
1129 printf("%s(0x%x, 0x%x, 0x%x, 0x%x) failed, rc=%d\n",
1130 "memory_object_data_request",
1132 cluster_start
+ object
->paging_offset
,
1133 length
, access_required
, rc
);
1135 * Don't want to leave a busy page around,
1136 * but the data request may have blocked,
1137 * so check if it's still there and busy.
1139 if(!object
->phys_contiguous
) {
1140 vm_object_lock(object
);
1141 for (; length
; length
-= PAGE_SIZE
,
1142 cluster_start
+= PAGE_SIZE_64
) {
1144 if ((p
= vm_page_lookup(object
,
1146 && p
->absent
&& p
->busy
1152 vm_fault_cleanup(object
, first_m
);
1153 thread_interrupt_level(interruptible_state
);
1154 return((rc
== MACH_SEND_INTERRUPTED
) ?
1155 VM_FAULT_INTERRUPTED
:
1156 VM_FAULT_MEMORY_ERROR
);
1159 tws_hash_line_t line
;
1162 task
= current_task();
1165 (task
->dynamic_working_set
!= 0))
1166 && !(object
->private)) {
1167 vm_object_t base_object
;
1168 vm_object_offset_t base_offset
;
1169 base_object
= object
;
1170 base_offset
= offset
;
1171 while(base_object
->shadow
) {
1173 base_object
->shadow_offset
;
1175 base_object
->shadow
;
1179 task
->dynamic_working_set
,
1180 base_offset
, base_object
,
1181 &line
) == KERN_SUCCESS
) {
1182 tws_line_signal((tws_hash_t
)
1183 task
->dynamic_working_set
,
1191 * Retry with same object/offset, since new data may
1192 * be in a different page (i.e., m is meaningless at
1195 vm_object_lock(object
);
1196 if ((interruptible
!= THREAD_UNINT
) &&
1197 (current_thread()->state
& TH_ABORT
)) {
1198 vm_fault_cleanup(object
, first_m
);
1199 thread_interrupt_level(interruptible_state
);
1200 return(VM_FAULT_INTERRUPTED
);
1202 if(m
== VM_PAGE_NULL
)
1208 * The only case in which we get here is if
1209 * object has no pager (or unwiring). If the pager doesn't
1210 * have the page this is handled in the m->absent case above
1211 * (and if you change things here you should look above).
1214 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1216 if (object
== first_object
)
1219 assert(m
== VM_PAGE_NULL
);
1222 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1223 (integer_t
)object
, offset
, (integer_t
)m
,
1224 (integer_t
)object
->shadow
, 0);
1226 * Move on to the next object. Lock the next
1227 * object before unlocking the current one.
1229 next_object
= object
->shadow
;
1230 if (next_object
== VM_OBJECT_NULL
) {
1231 assert(!must_be_resident
);
1233 * If there's no object left, fill the page
1234 * in the top object with zeros. But first we
1235 * need to allocate a real page.
1238 if (object
!= first_object
) {
1239 vm_object_paging_end(object
);
1240 vm_object_unlock(object
);
1242 object
= first_object
;
1243 offset
= first_offset
;
1244 vm_object_lock(object
);
1248 assert(m
->object
== object
);
1249 first_m
= VM_PAGE_NULL
;
1251 if (object
->shadow_severed
) {
1253 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1254 thread_interrupt_level(interruptible_state
);
1255 return VM_FAULT_MEMORY_ERROR
;
1258 if (VM_PAGE_THROTTLED() ||
1259 (m
->fictitious
&& !vm_page_convert(m
))) {
1261 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1262 thread_interrupt_level(interruptible_state
);
1263 return(VM_FAULT_MEMORY_SHORTAGE
);
1265 m
->no_isync
= FALSE
;
1267 if (!no_zero_fill
) {
1268 vm_object_unlock(object
);
1269 vm_page_zero_fill(m
);
1271 *type_of_fault
= DBG_ZERO_FILL_FAULT
;
1272 VM_STAT(zero_fill_count
++);
1274 if (bumped_pagein
== TRUE
) {
1276 current_task()->pageins
--;
1278 vm_object_lock(object
);
1280 vm_page_lock_queues();
1281 VM_PAGE_QUEUES_REMOVE(m
);
1282 if(m
->object
->size
> 0x80000) {
1283 m
->zero_fill
= TRUE
;
1284 /* depends on the queues lock */
1286 queue_enter(&vm_page_queue_zf
,
1287 m
, vm_page_t
, pageq
);
1290 &vm_page_queue_inactive
,
1291 m
, vm_page_t
, pageq
);
1293 m
->page_ticket
= vm_page_ticket
;
1294 vm_page_ticket_roll
++;
1295 if(vm_page_ticket_roll
== VM_PAGE_TICKETS_IN_ROLL
) {
1296 vm_page_ticket_roll
= 0;
1297 if(vm_page_ticket
==
1298 VM_PAGE_TICKET_ROLL_IDS
)
1304 vm_page_inactive_count
++;
1305 vm_page_unlock_queues();
1306 pmap_clear_modify(m
->phys_addr
);
1310 if ((object
!= first_object
) || must_be_resident
)
1311 vm_object_paging_end(object
);
1312 offset
+= object
->shadow_offset
;
1313 hi_offset
+= object
->shadow_offset
;
1314 lo_offset
+= object
->shadow_offset
;
1315 access_required
= VM_PROT_READ
;
1316 vm_object_lock(next_object
);
1317 vm_object_unlock(object
);
1318 object
= next_object
;
1319 vm_object_paging_begin(object
);
1324 * PAGE HAS BEEN FOUND.
1327 * busy, so that we can play with it;
1328 * not absent, so that nobody else will fill it;
1329 * possibly eligible for pageout;
1331 * The top-level page (first_m) is:
1332 * VM_PAGE_NULL if the page was found in the
1334 * busy, not absent, and ineligible for pageout.
1336 * The current object (object) is locked. A paging
1337 * reference is held for the current and top-level
1342 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1344 #if EXTRA_ASSERTIONS
1345 if(m
!= VM_PAGE_NULL
) {
1346 assert(m
->busy
&& !m
->absent
);
1347 assert((first_m
== VM_PAGE_NULL
) ||
1348 (first_m
->busy
&& !first_m
->absent
&&
1349 !first_m
->active
&& !first_m
->inactive
));
1351 #endif /* EXTRA_ASSERTIONS */
1354 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1355 (integer_t
)object
, offset
, (integer_t
)m
,
1356 (integer_t
)first_object
, (integer_t
)first_m
);
1358 * If the page is being written, but isn't
1359 * already owned by the top-level object,
1360 * we have to copy it into a new page owned
1361 * by the top-level object.
1364 if ((object
!= first_object
) && (m
!= VM_PAGE_NULL
)) {
1366 * We only really need to copy if we
1371 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1373 if (fault_type
& VM_PROT_WRITE
) {
1376 assert(!must_be_resident
);
1379 * If we try to collapse first_object at this
1380 * point, we may deadlock when we try to get
1381 * the lock on an intermediate object (since we
1382 * have the bottom object locked). We can't
1383 * unlock the bottom object, because the page
1384 * we found may move (by collapse) if we do.
1386 * Instead, we first copy the page. Then, when
1387 * we have no more use for the bottom object,
1388 * we unlock it and try to collapse.
1390 * Note that we copy the page even if we didn't
1391 * need to... that's the breaks.
1395 * Allocate a page for the copy
1397 copy_m
= vm_page_grab();
1398 if (copy_m
== VM_PAGE_NULL
) {
1400 vm_fault_cleanup(object
, first_m
);
1401 thread_interrupt_level(interruptible_state
);
1402 return(VM_FAULT_MEMORY_SHORTAGE
);
1407 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
1408 (integer_t
)object
, offset
,
1409 (integer_t
)m
, (integer_t
)copy_m
, 0);
1410 vm_page_copy(m
, copy_m
);
1413 * If another map is truly sharing this
1414 * page with us, we have to flush all
1415 * uses of the original page, since we
1416 * can't distinguish those which want the
1417 * original from those which need the
1420 * XXXO If we know that only one map has
1421 * access to this page, then we could
1422 * avoid the pmap_page_protect() call.
1425 vm_page_lock_queues();
1426 assert(!m
->cleaning
);
1427 pmap_page_protect(m
->phys_addr
, VM_PROT_NONE
);
1428 vm_page_deactivate(m
);
1429 copy_m
->dirty
= TRUE
;
1431 * Setting reference here prevents this fault from
1432 * being counted as a (per-thread) reactivate as well
1433 * as a copy-on-write.
1435 first_m
->reference
= TRUE
;
1436 vm_page_unlock_queues();
1439 * We no longer need the old page or object.
1442 PAGE_WAKEUP_DONE(m
);
1443 vm_object_paging_end(object
);
1444 vm_object_unlock(object
);
1447 *type_of_fault
= DBG_COW_FAULT
;
1448 VM_STAT(cow_faults
++);
1449 current_task()->cow_faults
++;
1450 object
= first_object
;
1451 offset
= first_offset
;
1453 vm_object_lock(object
);
1454 VM_PAGE_FREE(first_m
);
1455 first_m
= VM_PAGE_NULL
;
1456 assert(copy_m
->busy
);
1457 vm_page_insert(copy_m
, object
, offset
);
1461 * Now that we've gotten the copy out of the
1462 * way, let's try to collapse the top object.
1463 * But we have to play ugly games with
1464 * paging_in_progress to do that...
1467 vm_object_paging_end(object
);
1468 vm_object_collapse(object
);
1469 vm_object_paging_begin(object
);
1473 *protection
&= (~VM_PROT_WRITE
);
1478 * Now check whether the page needs to be pushed into the
1479 * copy object. The use of asymmetric copy on write for
1480 * shared temporary objects means that we may do two copies to
1481 * satisfy the fault; one above to get the page from a
1482 * shadowed object, and one here to push it into the copy.
1485 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
&&
1486 (m
!= VM_PAGE_NULL
)) {
1487 vm_object_offset_t copy_offset
;
1491 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1494 * If the page is being written, but hasn't been
1495 * copied to the copy-object, we have to copy it there.
1498 if ((fault_type
& VM_PROT_WRITE
) == 0) {
1499 *protection
&= ~VM_PROT_WRITE
;
1504 * If the page was guaranteed to be resident,
1505 * we must have already performed the copy.
1508 if (must_be_resident
)
1512 * Try to get the lock on the copy_object.
1514 if (!vm_object_lock_try(copy_object
)) {
1515 vm_object_unlock(object
);
1517 mutex_pause(); /* wait a bit */
1519 vm_object_lock(object
);
1524 * Make another reference to the copy-object,
1525 * to keep it from disappearing during the
1528 assert(copy_object
->ref_count
> 0);
1529 copy_object
->ref_count
++;
1530 VM_OBJ_RES_INCR(copy_object
);
1533 * Does the page exist in the copy?
1535 copy_offset
= first_offset
- copy_object
->shadow_offset
;
1536 if (copy_object
->size
<= copy_offset
)
1538 * Copy object doesn't cover this page -- do nothing.
1542 vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
1543 /* Page currently exists in the copy object */
1546 * If the page is being brought
1547 * in, wait for it and then retry.
1550 /* take an extra ref so object won't die */
1551 assert(copy_object
->ref_count
> 0);
1552 copy_object
->ref_count
++;
1553 vm_object_res_reference(copy_object
);
1554 vm_object_unlock(copy_object
);
1555 vm_fault_cleanup(object
, first_m
);
1556 counter(c_vm_fault_page_block_backoff_kernel
++);
1557 vm_object_lock(copy_object
);
1558 assert(copy_object
->ref_count
> 0);
1559 VM_OBJ_RES_DECR(copy_object
);
1560 copy_object
->ref_count
--;
1561 assert(copy_object
->ref_count
> 0);
1562 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
1563 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
1564 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
1565 vm_object_unlock(copy_object
);
1566 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1567 vm_object_deallocate(copy_object
);
1570 vm_object_unlock(copy_object
);
1571 vm_object_deallocate(copy_object
);
1572 thread_interrupt_level(interruptible_state
);
1573 return VM_FAULT_RETRY
;
1577 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
1579 * If PAGED_OUT is TRUE, then the page used to exist
1580 * in the copy-object, and has already been paged out.
1581 * We don't need to repeat this. If PAGED_OUT is
1582 * FALSE, then either we don't know (!pager_created,
1583 * for example) or it hasn't been paged out.
1584 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
1585 * We must copy the page to the copy object.
1589 * Allocate a page for the copy
1591 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
1592 if (copy_m
== VM_PAGE_NULL
) {
1594 VM_OBJ_RES_DECR(copy_object
);
1595 copy_object
->ref_count
--;
1596 assert(copy_object
->ref_count
> 0);
1597 vm_object_unlock(copy_object
);
1598 vm_fault_cleanup(object
, first_m
);
1599 thread_interrupt_level(interruptible_state
);
1600 return(VM_FAULT_MEMORY_SHORTAGE
);
1604 * Must copy page into copy-object.
1607 vm_page_copy(m
, copy_m
);
1610 * If the old page was in use by any users
1611 * of the copy-object, it must be removed
1612 * from all pmaps. (We can't know which
1616 vm_page_lock_queues();
1617 assert(!m
->cleaning
);
1618 pmap_page_protect(m
->phys_addr
, VM_PROT_NONE
);
1619 copy_m
->dirty
= TRUE
;
1620 vm_page_unlock_queues();
1623 * If there's a pager, then immediately
1624 * page out this page, using the "initialize"
1625 * option. Else, we use the copy.
1630 ((!copy_object
->pager_created
) ||
1631 vm_external_state_get(
1632 copy_object
->existence_map
, copy_offset
)
1633 == VM_EXTERNAL_STATE_ABSENT
)
1635 (!copy_object
->pager_created
)
1638 vm_page_lock_queues();
1639 vm_page_activate(copy_m
);
1640 vm_page_unlock_queues();
1641 PAGE_WAKEUP_DONE(copy_m
);
1644 assert(copy_m
->busy
== TRUE
);
1647 * The page is already ready for pageout:
1648 * not on pageout queues and busy.
1649 * Unlock everything except the
1650 * copy_object itself.
1653 vm_object_unlock(object
);
1656 * Write the page to the copy-object,
1657 * flushing it from the kernel.
1660 vm_pageout_initialize_page(copy_m
);
1663 * Since the pageout may have
1664 * temporarily dropped the
1665 * copy_object's lock, we
1666 * check whether we'll have
1667 * to deallocate the hard way.
1670 if ((copy_object
->shadow
!= object
) ||
1671 (copy_object
->ref_count
== 1)) {
1672 vm_object_unlock(copy_object
);
1673 vm_object_deallocate(copy_object
);
1674 vm_object_lock(object
);
1679 * Pick back up the old object's
1680 * lock. [It is safe to do so,
1681 * since it must be deeper in the
1685 vm_object_lock(object
);
1689 * Because we're pushing a page upward
1690 * in the object tree, we must restart
1691 * any faults that are waiting here.
1692 * [Note that this is an expansion of
1693 * PAGE_WAKEUP that uses the THREAD_RESTART
1694 * wait result]. Can't turn off the page's
1695 * busy bit because we're not done with it.
1700 thread_wakeup_with_result((event_t
) m
,
1706 * The reference count on copy_object must be
1707 * at least 2: one for our extra reference,
1708 * and at least one from the outside world
1709 * (we checked that when we last locked
1712 copy_object
->ref_count
--;
1713 assert(copy_object
->ref_count
> 0);
1714 VM_OBJ_RES_DECR(copy_object
);
1715 vm_object_unlock(copy_object
);
1721 *top_page
= first_m
;
1724 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
1725 (integer_t
)object
, offset
, (integer_t
)m
, (integer_t
)first_m
, 0);
1727 * If the page can be written, assume that it will be.
1728 * [Earlier, we restrict the permission to allow write
1729 * access only if the fault so required, so we don't
1730 * mark read-only data as dirty.]
1733 #if !VM_FAULT_STATIC_CONFIG
1734 if (vm_fault_dirty_handling
&& (*protection
& VM_PROT_WRITE
) &&
1735 (m
!= VM_PAGE_NULL
)) {
1740 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_page_deactivate_behind
); /* (TEST/DEBUG) */
1742 if (vm_page_deactivate_behind
) {
1743 if (offset
&& /* don't underflow */
1744 (object
->last_alloc
== (offset
- PAGE_SIZE_64
))) {
1745 m
= vm_page_lookup(object
, object
->last_alloc
);
1746 if ((m
!= VM_PAGE_NULL
) && !m
->busy
) {
1747 vm_page_lock_queues();
1748 vm_page_deactivate(m
);
1749 vm_page_unlock_queues();
1752 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1755 object
->last_alloc
= offset
;
1758 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
1760 thread_interrupt_level(interruptible_state
);
1761 if(*result_page
== VM_PAGE_NULL
) {
1762 vm_object_unlock(object
);
1764 return(VM_FAULT_SUCCESS
);
1768 vm_fault_cleanup(object
, first_m
);
1770 counter(c_vm_fault_page_block_backoff_kernel
++);
1771 thread_block(THREAD_CONTINUE_NULL
);
1775 thread_interrupt_level(interruptible_state
);
1776 if (wait_result
== THREAD_INTERRUPTED
)
1777 return VM_FAULT_INTERRUPTED
;
1778 return VM_FAULT_RETRY
;
1786 * Handle page faults, including pseudo-faults
1787 * used to change the wiring status of pages.
1789 * Explicit continuations have been removed.
1791 * vm_fault and vm_fault_page save mucho state
1792 * in the moral equivalent of a closure. The state
1793 * structure is allocated when first entering vm_fault
1794 * and deallocated when leaving vm_fault.
1801 vm_prot_t fault_type
,
1802 boolean_t change_wiring
,
1805 vm_offset_t caller_pmap_addr
)
1807 vm_map_version_t version
; /* Map version for verificiation */
1808 boolean_t wired
; /* Should mapping be wired down? */
1809 vm_object_t object
; /* Top-level object */
1810 vm_object_offset_t offset
; /* Top-level offset */
1811 vm_prot_t prot
; /* Protection for mapping */
1812 vm_behavior_t behavior
; /* Expected paging behavior */
1813 vm_object_offset_t lo_offset
, hi_offset
;
1814 vm_object_t old_copy_object
; /* Saved copy object */
1815 vm_page_t result_page
; /* Result of vm_fault_page */
1816 vm_page_t top_page
; /* Placeholder page */
1820 vm_page_t m
; /* Fast access to result_page */
1821 kern_return_t error_code
; /* page error reasons */
1823 vm_object_t cur_object
;
1825 vm_object_offset_t cur_offset
;
1827 vm_object_t new_object
;
1829 vm_map_t pmap_map
= map
;
1830 vm_map_t original_map
= map
;
1832 boolean_t funnel_set
= FALSE
;
1834 thread_t cur_thread
;
1835 boolean_t interruptible_state
;
1836 unsigned int cache_attr
;
1837 int write_startup_file
= 0;
1838 vm_prot_t full_fault_type
;
1842 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 0)) | DBG_FUNC_START
,
1849 cur_thread
= current_thread();
1850 /* at present we do not fully check for execute permission */
1851 /* we generally treat it is read except in certain device */
1852 /* memory settings */
1853 full_fault_type
= fault_type
;
1854 if(fault_type
& VM_PROT_EXECUTE
) {
1855 fault_type
&= ~VM_PROT_EXECUTE
;
1856 fault_type
|= VM_PROT_READ
;
1859 interruptible_state
= thread_interrupt_level(interruptible
);
1862 * assume we will hit a page in the cache
1863 * otherwise, explicitly override with
1864 * the real fault type once we determine it
1866 type_of_fault
= DBG_CACHE_HIT_FAULT
;
1869 current_task()->faults
++;
1872 * drop funnel if it is already held. Then restore while returning
1874 if ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
) {
1876 curflock
= cur_thread
->funnel_lock
;
1877 thread_funnel_set( curflock
, FALSE
);
1883 * Find the backing store object and offset into
1884 * it to begin the search.
1887 vm_map_lock_read(map
);
1888 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
, &version
,
1891 &behavior
, &lo_offset
, &hi_offset
, &pmap_map
);
1893 pmap
= pmap_map
->pmap
;
1895 if (kr
!= KERN_SUCCESS
) {
1896 vm_map_unlock_read(map
);
1901 * If the page is wired, we must fault for the current protection
1902 * value, to avoid further faults.
1906 fault_type
= prot
| VM_PROT_WRITE
;
1908 #if VM_FAULT_CLASSIFY
1910 * Temporary data gathering code
1912 vm_fault_classify(object
, offset
, fault_type
);
1915 * Fast fault code. The basic idea is to do as much as
1916 * possible while holding the map lock and object locks.
1917 * Busy pages are not used until the object lock has to
1918 * be dropped to do something (copy, zero fill, pmap enter).
1919 * Similarly, paging references aren't acquired until that
1920 * point, and object references aren't used.
1922 * If we can figure out what to do
1923 * (zero fill, copy on write, pmap enter) while holding
1924 * the locks, then it gets done. Otherwise, we give up,
1925 * and use the original fault path (which doesn't hold
1926 * the map lock, and relies on busy pages).
1927 * The give up cases include:
1928 * - Have to talk to pager.
1929 * - Page is busy, absent or in error.
1930 * - Pager has locked out desired access.
1931 * - Fault needs to be restarted.
1932 * - Have to push page into copy object.
1934 * The code is an infinite loop that moves one level down
1935 * the shadow chain each time. cur_object and cur_offset
1936 * refer to the current object being examined. object and offset
1937 * are the original object from the map. The loop is at the
1938 * top level if and only if object and cur_object are the same.
1940 * Invariants: Map lock is held throughout. Lock is held on
1941 * original object and cur_object (if different) when
1942 * continuing or exiting loop.
1948 * If this page is to be inserted in a copy delay object
1949 * for writing, and if the object has a copy, then the
1950 * copy delay strategy is implemented in the slow fault page.
1952 if (object
->copy_strategy
!= MEMORY_OBJECT_COPY_DELAY
||
1953 object
->copy
== VM_OBJECT_NULL
||
1954 (fault_type
& VM_PROT_WRITE
) == 0) {
1955 cur_object
= object
;
1956 cur_offset
= offset
;
1959 m
= vm_page_lookup(cur_object
, cur_offset
);
1960 if (m
!= VM_PAGE_NULL
) {
1962 wait_result_t result
;
1964 if (object
!= cur_object
)
1965 vm_object_unlock(object
);
1967 vm_map_unlock_read(map
);
1968 if (pmap_map
!= map
)
1969 vm_map_unlock(pmap_map
);
1971 #if !VM_FAULT_STATIC_CONFIG
1972 if (!vm_fault_interruptible
)
1973 interruptible
= THREAD_UNINT
;
1975 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
1977 vm_object_unlock(cur_object
);
1979 if (result
== THREAD_WAITING
) {
1980 result
= thread_block(THREAD_CONTINUE_NULL
);
1982 counter(c_vm_fault_page_block_busy_kernel
++);
1984 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
1990 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private
1991 || m
->absent
|| (fault_type
& m
->page_lock
))) {
1994 * Unusual case. Give up.
2000 * Two cases of map in faults:
2001 * - At top level w/o copy object.
2002 * - Read fault anywhere.
2003 * --> must disallow write.
2006 if (object
== cur_object
&&
2007 object
->copy
== VM_OBJECT_NULL
)
2008 goto FastMapInFault
;
2010 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2012 prot
&= ~VM_PROT_WRITE
;
2015 * Set up to map the page ...
2016 * mark the page busy, drop
2017 * locks and take a paging reference
2018 * on the object with the page.
2021 if (object
!= cur_object
) {
2022 vm_object_unlock(object
);
2023 object
= cur_object
;
2028 vm_object_paging_begin(object
);
2032 * Check a couple of global reasons to
2033 * be conservative about write access.
2034 * Then do the pmap_enter.
2036 #if !VM_FAULT_STATIC_CONFIG
2037 if (vm_fault_dirty_handling
2039 || db_watchpoint_list
2041 && (fault_type
& VM_PROT_WRITE
) == 0)
2042 prot
&= ~VM_PROT_WRITE
;
2043 #else /* STATIC_CONFIG */
2045 if (db_watchpoint_list
2046 && (fault_type
& VM_PROT_WRITE
) == 0)
2047 prot
&= ~VM_PROT_WRITE
;
2048 #endif /* MACH_KDB */
2049 #endif /* STATIC_CONFIG */
2050 if (m
->no_isync
== TRUE
) {
2051 pmap_sync_caches_phys(m
->phys_addr
);
2052 m
->no_isync
= FALSE
;
2055 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
2057 PMAP_ENTER(caller_pmap
,
2058 caller_pmap_addr
, m
,
2059 prot
, cache_attr
, wired
);
2061 PMAP_ENTER(pmap
, vaddr
, m
,
2062 prot
, cache_attr
, wired
);
2066 * Grab the queues lock to manipulate
2067 * the page queues. Change wiring
2068 * case is obvious. In soft ref bits
2069 * case activate page only if it fell
2070 * off paging queues, otherwise just
2071 * activate it if it's inactive.
2073 * NOTE: original vm_fault code will
2074 * move active page to back of active
2075 * queue. This code doesn't.
2077 vm_page_lock_queues();
2080 vm_pagein_cluster_used
++;
2081 m
->clustered
= FALSE
;
2083 m
->reference
= TRUE
;
2085 if (change_wiring
) {
2091 #if VM_FAULT_STATIC_CONFIG
2093 if (!m
->active
&& !m
->inactive
)
2094 vm_page_activate(m
);
2097 else if (software_reference_bits
) {
2098 if (!m
->active
&& !m
->inactive
)
2099 vm_page_activate(m
);
2101 else if (!m
->active
) {
2102 vm_page_activate(m
);
2105 vm_page_unlock_queues();
2108 * That's it, clean up and return.
2110 PAGE_WAKEUP_DONE(m
);
2111 vm_object_paging_end(object
);
2114 tws_hash_line_t line
;
2117 task
= current_task();
2119 (task
->dynamic_working_set
!= 0) &&
2120 !(object
->private)) {
2122 vm_object_t base_object
;
2123 vm_object_offset_t base_offset
;
2124 base_object
= object
;
2125 base_offset
= cur_offset
;
2126 while(base_object
->shadow
) {
2128 base_object
->shadow_offset
;
2130 base_object
->shadow
;
2132 kr
= tws_lookup((tws_hash_t
)
2133 task
->dynamic_working_set
,
2134 base_offset
, base_object
,
2136 if(kr
== KERN_OPERATION_TIMED_OUT
){
2137 write_startup_file
= 1;
2138 } else if (kr
!= KERN_SUCCESS
) {
2139 kr
= tws_insert((tws_hash_t
)
2140 task
->dynamic_working_set
,
2141 base_offset
, base_object
,
2143 if(kr
== KERN_NO_SPACE
) {
2144 vm_object_unlock(object
);
2146 tws_expand_working_set(
2147 task
->dynamic_working_set
,
2148 TWS_HASH_LINE_COUNT
,
2151 vm_object_lock(object
);
2154 KERN_OPERATION_TIMED_OUT
) {
2155 write_startup_file
= 1;
2160 vm_object_unlock(object
);
2162 vm_map_unlock_read(map
);
2164 vm_map_unlock(pmap_map
);
2166 if(write_startup_file
)
2167 tws_send_startup_info(current_task());
2170 thread_funnel_set( curflock
, TRUE
);
2172 thread_interrupt_level(interruptible_state
);
2175 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 0)) | DBG_FUNC_END
,
2177 type_of_fault
& 0xff,
2182 return KERN_SUCCESS
;
2186 * Copy on write fault. If objects match, then
2187 * object->copy must not be NULL (else control
2188 * would be in previous code block), and we
2189 * have a potential push into the copy object
2190 * with which we won't cope here.
2193 if (cur_object
== object
)
2196 * This is now a shadow based copy on write
2197 * fault -- it requires a copy up the shadow
2200 * Allocate a page in the original top level
2201 * object. Give up if allocate fails. Also
2202 * need to remember current page, as it's the
2203 * source of the copy.
2207 if (m
== VM_PAGE_NULL
) {
2211 * Now do the copy. Mark the source busy
2212 * and take out paging references on both
2215 * NOTE: This code holds the map lock across
2220 vm_page_copy(cur_m
, m
);
2221 vm_page_insert(m
, object
, offset
);
2223 vm_object_paging_begin(cur_object
);
2224 vm_object_paging_begin(object
);
2226 type_of_fault
= DBG_COW_FAULT
;
2227 VM_STAT(cow_faults
++);
2228 current_task()->cow_faults
++;
2231 * Now cope with the source page and object
2232 * If the top object has a ref count of 1
2233 * then no other map can access it, and hence
2234 * it's not necessary to do the pmap_page_protect.
2238 vm_page_lock_queues();
2239 vm_page_deactivate(cur_m
);
2241 pmap_page_protect(cur_m
->phys_addr
,
2243 vm_page_unlock_queues();
2245 PAGE_WAKEUP_DONE(cur_m
);
2246 vm_object_paging_end(cur_object
);
2247 vm_object_unlock(cur_object
);
2250 * Slight hack to call vm_object collapse
2251 * and then reuse common map in code.
2252 * note that the object lock was taken above.
2255 vm_object_paging_end(object
);
2256 vm_object_collapse(object
);
2257 vm_object_paging_begin(object
);
2264 * No page at cur_object, cur_offset
2267 if (cur_object
->pager_created
) {
2270 * Have to talk to the pager. Give up.
2276 if (cur_object
->shadow
== VM_OBJECT_NULL
) {
2278 if (cur_object
->shadow_severed
) {
2279 vm_object_paging_end(object
);
2280 vm_object_unlock(object
);
2281 vm_map_unlock_read(map
);
2283 vm_map_unlock(pmap_map
);
2285 if(write_startup_file
)
2286 tws_send_startup_info(
2290 thread_funnel_set( curflock
, TRUE
);
2293 thread_interrupt_level(interruptible_state
);
2295 return VM_FAULT_MEMORY_ERROR
;
2299 * Zero fill fault. Page gets
2300 * filled in top object. Insert
2301 * page, then drop any lower lock.
2302 * Give up if no page.
2304 if ((vm_page_free_target
-
2305 ((vm_page_free_target
-vm_page_free_min
)>>2))
2306 > vm_page_free_count
) {
2309 m
= vm_page_alloc(object
, offset
);
2310 if (m
== VM_PAGE_NULL
) {
2314 * This is a zero-fill or initial fill
2315 * page fault. As such, we consider it
2316 * undefined with respect to instruction
2317 * execution. i.e. it is the responsibility
2318 * of higher layers to call for an instruction
2319 * sync after changing the contents and before
2320 * sending a program into this area. We
2321 * choose this approach for performance
2324 m
->no_isync
= FALSE
;
2326 if (cur_object
!= object
)
2327 vm_object_unlock(cur_object
);
2329 vm_object_paging_begin(object
);
2330 vm_object_unlock(object
);
2333 * Now zero fill page and map it.
2334 * the page is probably going to
2335 * be written soon, so don't bother
2336 * to clear the modified bit
2338 * NOTE: This code holds the map
2339 * lock across the zero fill.
2342 if (!map
->no_zero_fill
) {
2343 vm_page_zero_fill(m
);
2344 type_of_fault
= DBG_ZERO_FILL_FAULT
;
2345 VM_STAT(zero_fill_count
++);
2347 vm_page_lock_queues();
2348 VM_PAGE_QUEUES_REMOVE(m
);
2350 m
->page_ticket
= vm_page_ticket
;
2351 if(m
->object
->size
> 0x80000) {
2352 m
->zero_fill
= TRUE
;
2353 /* depends on the queues lock */
2355 queue_enter(&vm_page_queue_zf
,
2356 m
, vm_page_t
, pageq
);
2359 &vm_page_queue_inactive
,
2360 m
, vm_page_t
, pageq
);
2362 vm_page_ticket_roll
++;
2363 if(vm_page_ticket_roll
==
2364 VM_PAGE_TICKETS_IN_ROLL
) {
2365 vm_page_ticket_roll
= 0;
2366 if(vm_page_ticket
==
2367 VM_PAGE_TICKET_ROLL_IDS
)
2374 vm_page_inactive_count
++;
2375 vm_page_unlock_queues();
2376 vm_object_lock(object
);
2382 * On to the next level
2385 cur_offset
+= cur_object
->shadow_offset
;
2386 new_object
= cur_object
->shadow
;
2387 vm_object_lock(new_object
);
2388 if (cur_object
!= object
)
2389 vm_object_unlock(cur_object
);
2390 cur_object
= new_object
;
2397 * Cleanup from fast fault failure. Drop any object
2398 * lock other than original and drop map lock.
2401 if (object
!= cur_object
)
2402 vm_object_unlock(cur_object
);
2404 vm_map_unlock_read(map
);
2407 vm_map_unlock(pmap_map
);
2410 * Make a reference to this object to
2411 * prevent its disposal while we are messing with
2412 * it. Once we have the reference, the map is free
2413 * to be diddled. Since objects reference their
2414 * shadows (and copies), they will stay around as well.
2417 assert(object
->ref_count
> 0);
2418 object
->ref_count
++;
2419 vm_object_res_reference(object
);
2420 vm_object_paging_begin(object
);
2422 XPR(XPR_VM_FAULT
,"vm_fault -> vm_fault_page\n",0,0,0,0,0);
2424 tws_hash_line_t line
;
2428 task
= current_task();
2430 (task
->dynamic_working_set
!= 0)
2431 && !(object
->private)) {
2432 vm_object_t base_object
;
2433 vm_object_offset_t base_offset
;
2434 base_object
= object
;
2435 base_offset
= offset
;
2436 while(base_object
->shadow
) {
2438 base_object
->shadow_offset
;
2440 base_object
->shadow
;
2442 kr
= tws_lookup((tws_hash_t
)
2443 task
->dynamic_working_set
,
2444 base_offset
, base_object
,
2446 if(kr
== KERN_OPERATION_TIMED_OUT
){
2447 write_startup_file
= 1;
2448 } else if (kr
!= KERN_SUCCESS
) {
2449 tws_insert((tws_hash_t
)
2450 task
->dynamic_working_set
,
2451 base_offset
, base_object
,
2453 kr
= tws_insert((tws_hash_t
)
2454 task
->dynamic_working_set
,
2455 base_offset
, base_object
,
2457 if(kr
== KERN_NO_SPACE
) {
2458 vm_object_unlock(object
);
2459 tws_expand_working_set(
2460 task
->dynamic_working_set
,
2461 TWS_HASH_LINE_COUNT
,
2463 vm_object_lock(object
);
2465 if(kr
== KERN_OPERATION_TIMED_OUT
) {
2466 write_startup_file
= 1;
2471 kr
= vm_fault_page(object
, offset
, fault_type
,
2472 (change_wiring
&& !wired
),
2474 lo_offset
, hi_offset
, behavior
,
2475 &prot
, &result_page
, &top_page
,
2477 &error_code
, map
->no_zero_fill
, FALSE
, map
, vaddr
);
2480 * If we didn't succeed, lose the object reference immediately.
2483 if (kr
!= VM_FAULT_SUCCESS
)
2484 vm_object_deallocate(object
);
2487 * See why we failed, and take corrective action.
2491 case VM_FAULT_SUCCESS
:
2493 case VM_FAULT_MEMORY_SHORTAGE
:
2494 if (vm_page_wait((change_wiring
) ?
2499 case VM_FAULT_INTERRUPTED
:
2502 case VM_FAULT_RETRY
:
2504 case VM_FAULT_FICTITIOUS_SHORTAGE
:
2505 vm_page_more_fictitious();
2507 case VM_FAULT_MEMORY_ERROR
:
2511 kr
= KERN_MEMORY_ERROR
;
2517 if(m
!= VM_PAGE_NULL
) {
2518 assert((change_wiring
&& !wired
) ?
2519 (top_page
== VM_PAGE_NULL
) :
2520 ((top_page
== VM_PAGE_NULL
) == (m
->object
== object
)));
2524 * How to clean up the result of vm_fault_page. This
2525 * happens whether the mapping is entered or not.
2528 #define UNLOCK_AND_DEALLOCATE \
2530 vm_fault_cleanup(m->object, top_page); \
2531 vm_object_deallocate(object); \
2535 * What to do with the resulting page from vm_fault_page
2536 * if it doesn't get entered into the physical map:
2539 #define RELEASE_PAGE(m) \
2541 PAGE_WAKEUP_DONE(m); \
2542 vm_page_lock_queues(); \
2543 if (!m->active && !m->inactive) \
2544 vm_page_activate(m); \
2545 vm_page_unlock_queues(); \
2549 * We must verify that the maps have not changed
2550 * since our last lookup.
2553 if(m
!= VM_PAGE_NULL
) {
2554 old_copy_object
= m
->object
->copy
;
2555 vm_object_unlock(m
->object
);
2557 old_copy_object
= VM_OBJECT_NULL
;
2559 if ((map
!= original_map
) || !vm_map_verify(map
, &version
)) {
2560 vm_object_t retry_object
;
2561 vm_object_offset_t retry_offset
;
2562 vm_prot_t retry_prot
;
2565 * To avoid trying to write_lock the map while another
2566 * thread has it read_locked (in vm_map_pageable), we
2567 * do not try for write permission. If the page is
2568 * still writable, we will get write permission. If it
2569 * is not, or has been marked needs_copy, we enter the
2570 * mapping without write permission, and will merely
2571 * take another fault.
2574 vm_map_lock_read(map
);
2575 kr
= vm_map_lookup_locked(&map
, vaddr
,
2576 fault_type
& ~VM_PROT_WRITE
, &version
,
2577 &retry_object
, &retry_offset
, &retry_prot
,
2578 &wired
, &behavior
, &lo_offset
, &hi_offset
,
2580 pmap
= pmap_map
->pmap
;
2582 if (kr
!= KERN_SUCCESS
) {
2583 vm_map_unlock_read(map
);
2584 if(m
!= VM_PAGE_NULL
) {
2585 vm_object_lock(m
->object
);
2587 UNLOCK_AND_DEALLOCATE
;
2589 vm_object_deallocate(object
);
2594 vm_object_unlock(retry_object
);
2595 if(m
!= VM_PAGE_NULL
) {
2596 vm_object_lock(m
->object
);
2598 vm_object_lock(object
);
2601 if ((retry_object
!= object
) ||
2602 (retry_offset
!= offset
)) {
2603 vm_map_unlock_read(map
);
2605 vm_map_unlock(pmap_map
);
2606 if(m
!= VM_PAGE_NULL
) {
2608 UNLOCK_AND_DEALLOCATE
;
2610 vm_object_deallocate(object
);
2616 * Check whether the protection has changed or the object
2617 * has been copied while we left the map unlocked.
2620 if(m
!= VM_PAGE_NULL
) {
2621 vm_object_unlock(m
->object
);
2623 vm_object_unlock(object
);
2626 if(m
!= VM_PAGE_NULL
) {
2627 vm_object_lock(m
->object
);
2629 vm_object_lock(object
);
2633 * If the copy object changed while the top-level object
2634 * was unlocked, then we must take away write permission.
2637 if(m
!= VM_PAGE_NULL
) {
2638 if (m
->object
->copy
!= old_copy_object
)
2639 prot
&= ~VM_PROT_WRITE
;
2643 * If we want to wire down this page, but no longer have
2644 * adequate permissions, we must start all over.
2647 if (wired
&& (fault_type
!= (prot
|VM_PROT_WRITE
))) {
2648 vm_map_verify_done(map
, &version
);
2650 vm_map_unlock(pmap_map
);
2651 if(m
!= VM_PAGE_NULL
) {
2653 UNLOCK_AND_DEALLOCATE
;
2655 vm_object_deallocate(object
);
2661 * Put this page into the physical map.
2662 * We had to do the unlock above because pmap_enter
2663 * may cause other faults. The page may be on
2664 * the pageout queues. If the pageout daemon comes
2665 * across the page, it will remove it from the queues.
2667 if (m
!= VM_PAGE_NULL
) {
2668 if (m
->no_isync
== TRUE
) {
2669 pmap_sync_caches_phys(m
->phys_addr
);
2671 m
->no_isync
= FALSE
;
2674 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
2677 PMAP_ENTER(caller_pmap
,
2678 caller_pmap_addr
, m
,
2679 prot
, cache_attr
, wired
);
2681 PMAP_ENTER(pmap
, vaddr
, m
,
2682 prot
, cache_attr
, wired
);
2685 tws_hash_line_t line
;
2689 task
= current_task();
2691 (task
->dynamic_working_set
!= 0)
2692 && (object
->private)) {
2693 vm_object_t base_object
;
2694 vm_object_offset_t base_offset
;
2695 base_object
= m
->object
;
2696 base_offset
= m
->offset
;
2697 while(base_object
->shadow
) {
2699 base_object
->shadow_offset
;
2701 base_object
->shadow
;
2703 kr
= tws_lookup((tws_hash_t
)
2704 task
->dynamic_working_set
,
2705 base_offset
, base_object
, &line
);
2706 if(kr
== KERN_OPERATION_TIMED_OUT
){
2707 write_startup_file
= 1;
2708 } else if (kr
!= KERN_SUCCESS
) {
2709 tws_insert((tws_hash_t
)
2710 task
->dynamic_working_set
,
2711 base_offset
, base_object
,
2713 kr
= tws_insert((tws_hash_t
)
2714 task
->dynamic_working_set
,
2715 base_offset
, base_object
,
2717 if(kr
== KERN_NO_SPACE
) {
2718 vm_object_unlock(m
->object
);
2719 tws_expand_working_set(
2720 task
->dynamic_working_set
,
2721 TWS_HASH_LINE_COUNT
,
2723 vm_object_lock(m
->object
);
2725 if(kr
== KERN_OPERATION_TIMED_OUT
) {
2726 write_startup_file
= 1;
2735 struct phys_entry
*pp
;
2736 vm_map_entry_t entry
;
2738 vm_offset_t ldelta
, hdelta
;
2741 * do a pmap block mapping from the physical address
2744 if(pp
= pmap_find_physentry(
2745 (vm_offset_t
)object
->shadow_offset
)) {
2746 memattr
= ((pp
->pte1
& 0x00000078) >> 3);
2748 memattr
= VM_WIMG_MASK
& (int)object
->wimg_bits
;
2752 /* While we do not worry about execution protection in */
2753 /* general, we may be able to read device memory and */
2754 /* still not be able to execute it. Here we check for */
2755 /* the guarded bit. If its set and we are attempting */
2756 /* to execute, we return with a protection failure. */
2758 if((memattr
& VM_MEM_GUARDED
) &&
2759 (full_fault_type
& VM_PROT_EXECUTE
)) {
2760 vm_map_verify_done(map
, &version
);
2762 vm_map_unlock(pmap_map
);
2763 vm_fault_cleanup(object
, top_page
);
2764 vm_object_deallocate(object
);
2765 kr
= KERN_PROTECTION_FAILURE
;
2771 if(pmap_map
!= map
) {
2772 vm_map_unlock(pmap_map
);
2774 if (original_map
!= map
) {
2775 vm_map_unlock_read(map
);
2776 vm_map_lock_read(original_map
);
2782 hdelta
= 0xFFFFF000;
2783 ldelta
= 0xFFFFF000;
2786 while(vm_map_lookup_entry(map
, laddr
, &entry
)) {
2787 if(ldelta
> (laddr
- entry
->vme_start
))
2788 ldelta
= laddr
- entry
->vme_start
;
2789 if(hdelta
> (entry
->vme_end
- laddr
))
2790 hdelta
= entry
->vme_end
- laddr
;
2791 if(entry
->is_sub_map
) {
2793 laddr
= (laddr
- entry
->vme_start
)
2795 vm_map_lock_read(entry
->object
.sub_map
);
2797 vm_map_unlock_read(map
);
2798 if(entry
->use_pmap
) {
2799 vm_map_unlock_read(pmap_map
);
2800 pmap_map
= entry
->object
.sub_map
;
2802 map
= entry
->object
.sub_map
;
2809 if(vm_map_lookup_entry(map
, laddr
, &entry
) &&
2810 (entry
->object
.vm_object
!= NULL
) &&
2811 (entry
->object
.vm_object
== object
)) {
2815 pmap_map_block(caller_pmap
,
2816 caller_pmap_addr
- ldelta
,
2818 (entry
->object
.vm_object
->shadow_offset
))
2820 (laddr
- entry
->vme_start
) - ldelta
,
2821 ldelta
+ hdelta
, prot
,
2822 memattr
, 0); /* Set up a block mapped area */
2824 pmap_map_block(pmap_map
->pmap
, vaddr
- ldelta
,
2826 (entry
->object
.vm_object
->shadow_offset
))
2828 (laddr
- entry
->vme_start
) - ldelta
,
2829 ldelta
+ hdelta
, prot
,
2830 memattr
, 0); /* Set up a block mapped area */
2836 pmap_enter(caller_pmap
, caller_pmap_addr
,
2837 object
->shadow_offset
, prot
, 0, TRUE
);
2839 pmap_enter(pmap
, vaddr
,
2840 object
->shadow_offset
, prot
, 0, TRUE
);
2849 * If the page is not wired down and isn't already
2850 * on a pageout queue, then put it where the
2851 * pageout daemon can find it.
2853 if(m
!= VM_PAGE_NULL
) {
2854 vm_page_lock_queues();
2856 if (change_wiring
) {
2862 #if VM_FAULT_STATIC_CONFIG
2864 if (!m
->active
&& !m
->inactive
)
2865 vm_page_activate(m
);
2866 m
->reference
= TRUE
;
2869 else if (software_reference_bits
) {
2870 if (!m
->active
&& !m
->inactive
)
2871 vm_page_activate(m
);
2872 m
->reference
= TRUE
;
2874 vm_page_activate(m
);
2877 vm_page_unlock_queues();
2881 * Unlock everything, and return
2884 vm_map_verify_done(map
, &version
);
2886 vm_map_unlock(pmap_map
);
2887 if(m
!= VM_PAGE_NULL
) {
2888 PAGE_WAKEUP_DONE(m
);
2889 UNLOCK_AND_DEALLOCATE
;
2891 vm_fault_cleanup(object
, top_page
);
2892 vm_object_deallocate(object
);
2896 #undef UNLOCK_AND_DEALLOCATE
2900 if(write_startup_file
)
2901 tws_send_startup_info(current_task());
2903 thread_funnel_set( curflock
, TRUE
);
2906 thread_interrupt_level(interruptible_state
);
2908 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 0)) | DBG_FUNC_END
,
2910 type_of_fault
& 0xff,
2921 * Wire down a range of virtual addresses in a map.
2926 vm_map_entry_t entry
,
2928 vm_offset_t pmap_addr
)
2931 register vm_offset_t va
;
2932 register vm_offset_t end_addr
= entry
->vme_end
;
2933 register kern_return_t rc
;
2935 assert(entry
->in_transition
);
2937 if ((entry
->object
.vm_object
!= NULL
) &&
2938 !entry
->is_sub_map
&&
2939 entry
->object
.vm_object
->phys_contiguous
) {
2940 return KERN_SUCCESS
;
2944 * Inform the physical mapping system that the
2945 * range of addresses may not fault, so that
2946 * page tables and such can be locked down as well.
2949 pmap_pageable(pmap
, pmap_addr
,
2950 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
2953 * We simulate a fault to get the page and enter it
2954 * in the physical map.
2957 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
2958 if ((rc
= vm_fault_wire_fast(
2959 map
, va
, entry
, pmap
,
2960 pmap_addr
+ (va
- entry
->vme_start
)
2961 )) != KERN_SUCCESS
) {
2962 rc
= vm_fault(map
, va
, VM_PROT_NONE
, TRUE
,
2963 (pmap
== kernel_pmap
) ?
2964 THREAD_UNINT
: THREAD_ABORTSAFE
,
2965 pmap
, pmap_addr
+ (va
- entry
->vme_start
));
2968 if (rc
!= KERN_SUCCESS
) {
2969 struct vm_map_entry tmp_entry
= *entry
;
2971 /* unwire wired pages */
2972 tmp_entry
.vme_end
= va
;
2973 vm_fault_unwire(map
,
2974 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
2979 return KERN_SUCCESS
;
2985 * Unwire a range of virtual addresses in a map.
2990 vm_map_entry_t entry
,
2991 boolean_t deallocate
,
2993 vm_offset_t pmap_addr
)
2995 register vm_offset_t va
;
2996 register vm_offset_t end_addr
= entry
->vme_end
;
2999 object
= (entry
->is_sub_map
)
3000 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
3003 * Since the pages are wired down, we must be able to
3004 * get their mappings from the physical map system.
3007 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3008 pmap_change_wiring(pmap
,
3009 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3011 if (object
== VM_OBJECT_NULL
) {
3012 (void) vm_fault(map
, va
, VM_PROT_NONE
,
3013 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
3014 } else if (object
->phys_contiguous
) {
3018 vm_page_t result_page
;
3020 vm_object_t result_object
;
3021 vm_fault_return_t result
;
3024 prot
= VM_PROT_NONE
;
3026 vm_object_lock(object
);
3027 vm_object_paging_begin(object
);
3029 "vm_fault_unwire -> vm_fault_page\n",
3031 result
= vm_fault_page(object
,
3033 (va
- entry
->vme_start
),
3039 - entry
->vme_start
),
3045 0, map
->no_zero_fill
,
3047 } while (result
== VM_FAULT_RETRY
);
3049 if (result
!= VM_FAULT_SUCCESS
)
3050 panic("vm_fault_unwire: failure");
3052 result_object
= result_page
->object
;
3054 assert(!result_page
->fictitious
);
3055 pmap_page_protect(result_page
->phys_addr
,
3057 VM_PAGE_FREE(result_page
);
3059 vm_page_lock_queues();
3060 vm_page_unwire(result_page
);
3061 vm_page_unlock_queues();
3062 PAGE_WAKEUP_DONE(result_page
);
3065 vm_fault_cleanup(result_object
, top_page
);
3070 * Inform the physical mapping system that the range
3071 * of addresses may fault, so that page tables and
3072 * such may be unwired themselves.
3075 pmap_pageable(pmap
, pmap_addr
,
3076 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
3081 * vm_fault_wire_fast:
3083 * Handle common case of a wire down page fault at the given address.
3084 * If successful, the page is inserted into the associated physical map.
3085 * The map entry is passed in to avoid the overhead of a map lookup.
3087 * NOTE: the given address should be truncated to the
3088 * proper page address.
3090 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
3091 * a standard error specifying why the fault is fatal is returned.
3093 * The map in question must be referenced, and remains so.
3094 * Caller has a read lock on the map.
3096 * This is a stripped version of vm_fault() for wiring pages. Anything
3097 * other than the common case will return KERN_FAILURE, and the caller
3098 * is expected to call vm_fault().
3104 vm_map_entry_t entry
,
3106 vm_offset_t pmap_addr
)
3109 vm_object_offset_t offset
;
3110 register vm_page_t m
;
3112 thread_act_t thr_act
;
3113 unsigned int cache_attr
;
3117 if((thr_act
=current_act()) && (thr_act
->task
!= TASK_NULL
))
3118 thr_act
->task
->faults
++;
3125 #define RELEASE_PAGE(m) { \
3126 PAGE_WAKEUP_DONE(m); \
3127 vm_page_lock_queues(); \
3128 vm_page_unwire(m); \
3129 vm_page_unlock_queues(); \
3133 #undef UNLOCK_THINGS
3134 #define UNLOCK_THINGS { \
3135 object->paging_in_progress--; \
3136 vm_object_unlock(object); \
3139 #undef UNLOCK_AND_DEALLOCATE
3140 #define UNLOCK_AND_DEALLOCATE { \
3142 vm_object_deallocate(object); \
3145 * Give up and have caller do things the hard way.
3149 UNLOCK_AND_DEALLOCATE; \
3150 return(KERN_FAILURE); \
3155 * If this entry is not directly to a vm_object, bail out.
3157 if (entry
->is_sub_map
)
3158 return(KERN_FAILURE
);
3161 * Find the backing store object and offset into it.
3164 object
= entry
->object
.vm_object
;
3165 offset
= (va
- entry
->vme_start
) + entry
->offset
;
3166 prot
= entry
->protection
;
3169 * Make a reference to this object to prevent its
3170 * disposal while we are messing with it.
3173 vm_object_lock(object
);
3174 assert(object
->ref_count
> 0);
3175 object
->ref_count
++;
3176 vm_object_res_reference(object
);
3177 object
->paging_in_progress
++;
3180 * INVARIANTS (through entire routine):
3182 * 1) At all times, we must either have the object
3183 * lock or a busy page in some object to prevent
3184 * some other thread from trying to bring in
3187 * 2) Once we have a busy page, we must remove it from
3188 * the pageout queues, so that the pageout daemon
3189 * will not grab it away.
3194 * Look for page in top-level object. If it's not there or
3195 * there's something going on, give up.
3197 m
= vm_page_lookup(object
, offset
);
3198 if ((m
== VM_PAGE_NULL
) || (m
->busy
) ||
3199 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
||
3200 prot
& m
->page_lock
))) {
3206 * Wire the page down now. All bail outs beyond this
3207 * point must unwire the page.
3210 vm_page_lock_queues();
3212 vm_page_unlock_queues();
3215 * Mark page busy for other threads.
3222 * Give up if the page is being written and there's a copy object
3224 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
3230 * Put this page into the physical map.
3231 * We have to unlock the object because pmap_enter
3232 * may cause other faults.
3234 if (m
->no_isync
== TRUE
) {
3235 pmap_sync_caches_phys(m
->phys_addr
);
3237 m
->no_isync
= FALSE
;
3240 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
3242 PMAP_ENTER(pmap
, pmap_addr
, m
, prot
, cache_attr
, TRUE
);
3245 * Unlock everything, and return
3248 PAGE_WAKEUP_DONE(m
);
3249 UNLOCK_AND_DEALLOCATE
;
3251 return(KERN_SUCCESS
);
3256 * Routine: vm_fault_copy_cleanup
3258 * Release a page used by vm_fault_copy.
3262 vm_fault_copy_cleanup(
3266 vm_object_t object
= page
->object
;
3268 vm_object_lock(object
);
3269 PAGE_WAKEUP_DONE(page
);
3270 vm_page_lock_queues();
3271 if (!page
->active
&& !page
->inactive
)
3272 vm_page_activate(page
);
3273 vm_page_unlock_queues();
3274 vm_fault_cleanup(object
, top_page
);
3278 vm_fault_copy_dst_cleanup(
3283 if (page
!= VM_PAGE_NULL
) {
3284 object
= page
->object
;
3285 vm_object_lock(object
);
3286 vm_page_lock_queues();
3287 vm_page_unwire(page
);
3288 vm_page_unlock_queues();
3289 vm_object_paging_end(object
);
3290 vm_object_unlock(object
);
3295 * Routine: vm_fault_copy
3298 * Copy pages from one virtual memory object to another --
3299 * neither the source nor destination pages need be resident.
3301 * Before actually copying a page, the version associated with
3302 * the destination address map wil be verified.
3304 * In/out conditions:
3305 * The caller must hold a reference, but not a lock, to
3306 * each of the source and destination objects and to the
3310 * Returns KERN_SUCCESS if no errors were encountered in
3311 * reading or writing the data. Returns KERN_INTERRUPTED if
3312 * the operation was interrupted (only possible if the
3313 * "interruptible" argument is asserted). Other return values
3314 * indicate a permanent error in copying the data.
3316 * The actual amount of data copied will be returned in the
3317 * "copy_size" argument. In the event that the destination map
3318 * verification failed, this amount may be less than the amount
3323 vm_object_t src_object
,
3324 vm_object_offset_t src_offset
,
3325 vm_size_t
*src_size
, /* INOUT */
3326 vm_object_t dst_object
,
3327 vm_object_offset_t dst_offset
,
3329 vm_map_version_t
*dst_version
,
3332 vm_page_t result_page
;
3335 vm_page_t src_top_page
;
3339 vm_page_t dst_top_page
;
3342 vm_size_t amount_left
;
3343 vm_object_t old_copy_object
;
3344 kern_return_t error
= 0;
3346 vm_size_t part_size
;
3349 * In order not to confuse the clustered pageins, align
3350 * the different offsets on a page boundary.
3352 vm_object_offset_t src_lo_offset
= trunc_page_64(src_offset
);
3353 vm_object_offset_t dst_lo_offset
= trunc_page_64(dst_offset
);
3354 vm_object_offset_t src_hi_offset
= round_page_64(src_offset
+ *src_size
);
3355 vm_object_offset_t dst_hi_offset
= round_page_64(dst_offset
+ *src_size
);
3359 *src_size -= amount_left; \
3363 amount_left
= *src_size
;
3364 do { /* while (amount_left > 0) */
3366 * There may be a deadlock if both source and destination
3367 * pages are the same. To avoid this deadlock, the copy must
3368 * start by getting the destination page in order to apply
3369 * COW semantics if any.
3372 RetryDestinationFault
: ;
3374 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
3376 vm_object_lock(dst_object
);
3377 vm_object_paging_begin(dst_object
);
3379 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
3380 switch (vm_fault_page(dst_object
,
3381 trunc_page_64(dst_offset
),
3382 VM_PROT_WRITE
|VM_PROT_READ
,
3387 VM_BEHAVIOR_SEQUENTIAL
,
3393 dst_map
->no_zero_fill
,
3395 case VM_FAULT_SUCCESS
:
3397 case VM_FAULT_RETRY
:
3398 goto RetryDestinationFault
;
3399 case VM_FAULT_MEMORY_SHORTAGE
:
3400 if (vm_page_wait(interruptible
))
3401 goto RetryDestinationFault
;
3403 case VM_FAULT_INTERRUPTED
:
3404 RETURN(MACH_SEND_INTERRUPTED
);
3405 case VM_FAULT_FICTITIOUS_SHORTAGE
:
3406 vm_page_more_fictitious();
3407 goto RetryDestinationFault
;
3408 case VM_FAULT_MEMORY_ERROR
:
3412 return(KERN_MEMORY_ERROR
);
3414 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
3416 old_copy_object
= dst_page
->object
->copy
;
3419 * There exists the possiblity that the source and
3420 * destination page are the same. But we can't
3421 * easily determine that now. If they are the
3422 * same, the call to vm_fault_page() for the
3423 * destination page will deadlock. To prevent this we
3424 * wire the page so we can drop busy without having
3425 * the page daemon steal the page. We clean up the
3426 * top page but keep the paging reference on the object
3427 * holding the dest page so it doesn't go away.
3430 vm_page_lock_queues();
3431 vm_page_wire(dst_page
);
3432 vm_page_unlock_queues();
3433 PAGE_WAKEUP_DONE(dst_page
);
3434 vm_object_unlock(dst_page
->object
);
3436 if (dst_top_page
!= VM_PAGE_NULL
) {
3437 vm_object_lock(dst_object
);
3438 VM_PAGE_FREE(dst_top_page
);
3439 vm_object_paging_end(dst_object
);
3440 vm_object_unlock(dst_object
);
3445 if (src_object
== VM_OBJECT_NULL
) {
3447 * No source object. We will just
3448 * zero-fill the page in dst_object.
3450 src_page
= VM_PAGE_NULL
;
3451 result_page
= VM_PAGE_NULL
;
3453 vm_object_lock(src_object
);
3454 src_page
= vm_page_lookup(src_object
,
3455 trunc_page_64(src_offset
));
3456 if (src_page
== dst_page
) {
3457 src_prot
= dst_prot
;
3458 result_page
= VM_PAGE_NULL
;
3460 src_prot
= VM_PROT_READ
;
3461 vm_object_paging_begin(src_object
);
3464 "vm_fault_copy(2) -> vm_fault_page\n",
3466 switch (vm_fault_page(src_object
,
3467 trunc_page_64(src_offset
),
3473 VM_BEHAVIOR_SEQUENTIAL
,
3482 case VM_FAULT_SUCCESS
:
3484 case VM_FAULT_RETRY
:
3485 goto RetrySourceFault
;
3486 case VM_FAULT_MEMORY_SHORTAGE
:
3487 if (vm_page_wait(interruptible
))
3488 goto RetrySourceFault
;
3490 case VM_FAULT_INTERRUPTED
:
3491 vm_fault_copy_dst_cleanup(dst_page
);
3492 RETURN(MACH_SEND_INTERRUPTED
);
3493 case VM_FAULT_FICTITIOUS_SHORTAGE
:
3494 vm_page_more_fictitious();
3495 goto RetrySourceFault
;
3496 case VM_FAULT_MEMORY_ERROR
:
3497 vm_fault_copy_dst_cleanup(dst_page
);
3501 return(KERN_MEMORY_ERROR
);
3505 assert((src_top_page
== VM_PAGE_NULL
) ==
3506 (result_page
->object
== src_object
));
3508 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
3509 vm_object_unlock(result_page
->object
);
3512 if (!vm_map_verify(dst_map
, dst_version
)) {
3513 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3514 vm_fault_copy_cleanup(result_page
, src_top_page
);
3515 vm_fault_copy_dst_cleanup(dst_page
);
3519 vm_object_lock(dst_page
->object
);
3521 if (dst_page
->object
->copy
!= old_copy_object
) {
3522 vm_object_unlock(dst_page
->object
);
3523 vm_map_verify_done(dst_map
, dst_version
);
3524 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3525 vm_fault_copy_cleanup(result_page
, src_top_page
);
3526 vm_fault_copy_dst_cleanup(dst_page
);
3529 vm_object_unlock(dst_page
->object
);
3532 * Copy the page, and note that it is dirty
3536 if (!page_aligned(src_offset
) ||
3537 !page_aligned(dst_offset
) ||
3538 !page_aligned(amount_left
)) {
3540 vm_object_offset_t src_po
,
3543 src_po
= src_offset
- trunc_page_64(src_offset
);
3544 dst_po
= dst_offset
- trunc_page_64(dst_offset
);
3546 if (dst_po
> src_po
) {
3547 part_size
= PAGE_SIZE
- dst_po
;
3549 part_size
= PAGE_SIZE
- src_po
;
3551 if (part_size
> (amount_left
)){
3552 part_size
= amount_left
;
3555 if (result_page
== VM_PAGE_NULL
) {
3556 vm_page_part_zero_fill(dst_page
,
3559 vm_page_part_copy(result_page
, src_po
,
3560 dst_page
, dst_po
, part_size
);
3561 if(!dst_page
->dirty
){
3562 vm_object_lock(dst_object
);
3563 dst_page
->dirty
= TRUE
;
3564 vm_object_unlock(dst_page
->object
);
3569 part_size
= PAGE_SIZE
;
3571 if (result_page
== VM_PAGE_NULL
)
3572 vm_page_zero_fill(dst_page
);
3574 vm_page_copy(result_page
, dst_page
);
3575 if(!dst_page
->dirty
){
3576 vm_object_lock(dst_object
);
3577 dst_page
->dirty
= TRUE
;
3578 vm_object_unlock(dst_page
->object
);
3585 * Unlock everything, and return
3588 vm_map_verify_done(dst_map
, dst_version
);
3590 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3591 vm_fault_copy_cleanup(result_page
, src_top_page
);
3592 vm_fault_copy_dst_cleanup(dst_page
);
3594 amount_left
-= part_size
;
3595 src_offset
+= part_size
;
3596 dst_offset
+= part_size
;
3597 } while (amount_left
> 0);
3599 RETURN(KERN_SUCCESS
);
3608 * Routine: vm_fault_page_overwrite
3611 * A form of vm_fault_page that assumes that the
3612 * resulting page will be overwritten in its entirety,
3613 * making it unnecessary to obtain the correct *contents*
3617 * XXX Untested. Also unused. Eventually, this technology
3618 * could be used in vm_fault_copy() to advantage.
3621 vm_fault_page_overwrite(
3623 vm_object_t dst_object
,
3624 vm_object_offset_t dst_offset
,
3625 vm_page_t
*result_page
) /* OUT */
3629 kern_return_t wait_result
;
3631 #define interruptible THREAD_UNINT /* XXX */
3635 * Look for a page at this offset
3638 while ((dst_page
= vm_page_lookup(dst_object
, dst_offset
))
3641 * No page, no problem... just allocate one.
3644 dst_page
= vm_page_alloc(dst_object
, dst_offset
);
3645 if (dst_page
== VM_PAGE_NULL
) {
3646 vm_object_unlock(dst_object
);
3648 vm_object_lock(dst_object
);
3653 * Pretend that the memory manager
3654 * write-protected the page.
3656 * Note that we will be asking for write
3657 * permission without asking for the data
3661 dst_page
->overwriting
= TRUE
;
3662 dst_page
->page_lock
= VM_PROT_WRITE
;
3663 dst_page
->absent
= TRUE
;
3664 dst_page
->unusual
= TRUE
;
3665 dst_object
->absent_count
++;
3670 * When we bail out, we might have to throw
3671 * away the page created here.
3674 #define DISCARD_PAGE \
3676 vm_object_lock(dst_object); \
3677 dst_page = vm_page_lookup(dst_object, dst_offset); \
3678 if ((dst_page != VM_PAGE_NULL) && dst_page->overwriting) \
3679 VM_PAGE_FREE(dst_page); \
3680 vm_object_unlock(dst_object); \
3685 * If the page is write-protected...
3688 if (dst_page
->page_lock
& VM_PROT_WRITE
) {
3690 * ... and an unlock request hasn't been sent
3693 if ( ! (dst_page
->unlock_request
& VM_PROT_WRITE
)) {
3698 * ... then send one now.
3701 if (!dst_object
->pager_ready
) {
3702 wait_result
= vm_object_assert_wait(dst_object
,
3703 VM_OBJECT_EVENT_PAGER_READY
,
3705 vm_object_unlock(dst_object
);
3706 if (wait_result
== THREAD_WAITING
)
3707 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
3708 if (wait_result
!= THREAD_AWAKENED
) {
3710 return(VM_FAULT_INTERRUPTED
);
3715 u
= dst_page
->unlock_request
|= VM_PROT_WRITE
;
3716 vm_object_unlock(dst_object
);
3718 if ((rc
= memory_object_data_unlock(
3720 dst_offset
+ dst_object
->paging_offset
,
3722 u
)) != KERN_SUCCESS
) {
3724 printf("vm_object_overwrite: memory_object_data_unlock failed\n");
3726 return((rc
== MACH_SEND_INTERRUPTED
) ?
3727 VM_FAULT_INTERRUPTED
:
3728 VM_FAULT_MEMORY_ERROR
);
3730 vm_object_lock(dst_object
);
3734 /* ... fall through to wait below */
3737 * If the page isn't being used for other
3738 * purposes, then we're done.
3740 if ( ! (dst_page
->busy
|| dst_page
->absent
||
3741 dst_page
->error
|| dst_page
->restart
) )
3745 wait_result
= PAGE_ASSERT_WAIT(dst_page
, interruptible
);
3746 vm_object_unlock(dst_object
);
3747 if (wait_result
== THREAD_WAITING
)
3748 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
3749 if (wait_result
!= THREAD_AWAKENED
) {
3751 return(VM_FAULT_INTERRUPTED
);
3755 *result_page
= dst_page
;
3756 return(VM_FAULT_SUCCESS
);
3758 #undef interruptible
3764 #if VM_FAULT_CLASSIFY
3766 * Temporary statistics gathering support.
3770 * Statistics arrays:
3772 #define VM_FAULT_TYPES_MAX 5
3773 #define VM_FAULT_LEVEL_MAX 8
3775 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
3777 #define VM_FAULT_TYPE_ZERO_FILL 0
3778 #define VM_FAULT_TYPE_MAP_IN 1
3779 #define VM_FAULT_TYPE_PAGER 2
3780 #define VM_FAULT_TYPE_COPY 3
3781 #define VM_FAULT_TYPE_OTHER 4
3785 vm_fault_classify(vm_object_t object
,
3786 vm_object_offset_t offset
,
3787 vm_prot_t fault_type
)
3789 int type
, level
= 0;
3793 m
= vm_page_lookup(object
, offset
);
3794 if (m
!= VM_PAGE_NULL
) {
3795 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
||
3796 fault_type
& m
->page_lock
) {
3797 type
= VM_FAULT_TYPE_OTHER
;
3800 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
3801 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
3802 type
= VM_FAULT_TYPE_MAP_IN
;
3805 type
= VM_FAULT_TYPE_COPY
;
3809 if (object
->pager_created
) {
3810 type
= VM_FAULT_TYPE_PAGER
;
3813 if (object
->shadow
== VM_OBJECT_NULL
) {
3814 type
= VM_FAULT_TYPE_ZERO_FILL
;
3818 offset
+= object
->shadow_offset
;
3819 object
= object
->shadow
;
3825 if (level
> VM_FAULT_LEVEL_MAX
)
3826 level
= VM_FAULT_LEVEL_MAX
;
3828 vm_fault_stats
[type
][level
] += 1;
3833 /* cleanup routine to call from debugger */
3836 vm_fault_classify_init(void)
3840 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
3841 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
3842 vm_fault_stats
[type
][level
] = 0;
3848 #endif /* VM_FAULT_CLASSIFY */