2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
57 * Author: Avadis Tevanian, Jr., Michael Wayne Young
59 * Page fault handling module.
62 /* remove after component interface available */
63 extern int vnode_pager_workaround
;
64 extern int device_pager_workaround
;
67 #include <mach_cluster_stats.h>
68 #include <mach_pagemap.h>
71 #include <vm/vm_fault.h>
72 #include <mach/kern_return.h>
73 #include <mach/message.h> /* for error codes */
74 #include <kern/host_statistics.h>
75 #include <kern/counters.h>
76 #include <kern/task.h>
77 #include <kern/thread.h>
78 #include <kern/sched_prim.h>
79 #include <kern/host.h>
81 #include <ppc/proc_reg.h>
82 #include <vm/task_working_set.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
87 #include <vm/vm_pageout.h>
88 #include <mach/vm_param.h>
89 #include <mach/vm_behavior.h>
90 #include <mach/memory_object.h>
91 /* For memory_object_data_{request,unlock} */
92 #include <kern/mach_param.h>
93 #include <kern/macro_help.h>
94 #include <kern/zalloc.h>
95 #include <kern/misc_protos.h>
97 #include <sys/kdebug.h>
99 #define VM_FAULT_CLASSIFY 0
100 #define VM_FAULT_STATIC_CONFIG 1
102 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */
104 int vm_object_absent_max
= 50;
106 int vm_fault_debug
= 0;
107 boolean_t vm_page_deactivate_behind
= TRUE
;
110 #if !VM_FAULT_STATIC_CONFIG
111 boolean_t vm_fault_dirty_handling
= FALSE
;
112 boolean_t vm_fault_interruptible
= FALSE
;
113 boolean_t software_reference_bits
= TRUE
;
117 extern struct db_watchpoint
*db_watchpoint_list
;
118 #endif /* MACH_KDB */
120 /* Forward declarations of internal routines. */
121 extern kern_return_t
vm_fault_wire_fast(
124 vm_map_entry_t entry
,
126 vm_offset_t pmap_addr
);
128 extern void vm_fault_continue(void);
130 extern void vm_fault_copy_cleanup(
134 extern void vm_fault_copy_dst_cleanup(
137 #if VM_FAULT_CLASSIFY
138 extern void vm_fault_classify(vm_object_t object
,
139 vm_object_offset_t offset
,
140 vm_prot_t fault_type
);
142 extern void vm_fault_classify_init(void);
146 * Routine: vm_fault_init
148 * Initialize our private data structures.
156 * Routine: vm_fault_cleanup
158 * Clean up the result of vm_fault_page.
160 * The paging reference for "object" is released.
161 * "object" is unlocked.
162 * If "top_page" is not null, "top_page" is
163 * freed and the paging reference for the object
164 * containing it is released.
167 * "object" must be locked.
171 register vm_object_t object
,
172 register vm_page_t top_page
)
174 vm_object_paging_end(object
);
175 vm_object_unlock(object
);
177 if (top_page
!= VM_PAGE_NULL
) {
178 object
= top_page
->object
;
179 vm_object_lock(object
);
180 VM_PAGE_FREE(top_page
);
181 vm_object_paging_end(object
);
182 vm_object_unlock(object
);
186 #if MACH_CLUSTER_STATS
187 #define MAXCLUSTERPAGES 16
189 unsigned long pages_in_cluster
;
190 unsigned long pages_at_higher_offsets
;
191 unsigned long pages_at_lower_offsets
;
192 } cluster_stats_in
[MAXCLUSTERPAGES
];
193 #define CLUSTER_STAT(clause) clause
194 #define CLUSTER_STAT_HIGHER(x) \
195 ((cluster_stats_in[(x)].pages_at_higher_offsets)++)
196 #define CLUSTER_STAT_LOWER(x) \
197 ((cluster_stats_in[(x)].pages_at_lower_offsets)++)
198 #define CLUSTER_STAT_CLUSTER(x) \
199 ((cluster_stats_in[(x)].pages_in_cluster)++)
200 #else /* MACH_CLUSTER_STATS */
201 #define CLUSTER_STAT(clause)
202 #endif /* MACH_CLUSTER_STATS */
204 /* XXX - temporary */
205 boolean_t vm_allow_clustered_pagein
= FALSE
;
206 int vm_pagein_cluster_used
= 0;
209 * Prepage default sizes given VM_BEHAVIOR_DEFAULT reference behavior
211 int vm_default_ahead
= 1; /* Number of pages to prepage ahead */
212 int vm_default_behind
= 0; /* Number of pages to prepage behind */
214 #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0)
217 * Routine: vm_fault_page
219 * Find the resident page for the virtual memory
220 * specified by the given virtual memory object
222 * Additional arguments:
223 * The required permissions for the page is given
224 * in "fault_type". Desired permissions are included
225 * in "protection". The minimum and maximum valid offsets
226 * within the object for the relevant map entry are
227 * passed in "lo_offset" and "hi_offset" respectively and
228 * the expected page reference pattern is passed in "behavior".
229 * These three parameters are used to determine pagein cluster
232 * If the desired page is known to be resident (for
233 * example, because it was previously wired down), asserting
234 * the "unwiring" parameter will speed the search.
236 * If the operation can be interrupted (by thread_abort
237 * or thread_terminate), then the "interruptible"
238 * parameter should be asserted.
241 * The page containing the proper data is returned
245 * The source object must be locked and referenced,
246 * and must donate one paging reference. The reference
247 * is not affected. The paging reference and lock are
250 * If the call succeeds, the object in which "result_page"
251 * resides is left locked and holding a paging reference.
252 * If this is not the original object, a busy page in the
253 * original object is returned in "top_page", to prevent other
254 * callers from pursuing this same data, along with a paging
255 * reference for the original object. The "top_page" should
256 * be destroyed when this guarantee is no longer required.
257 * The "result_page" is also left busy. It is not removed
258 * from the pageout queues.
264 vm_object_t first_object
, /* Object to begin search */
265 vm_object_offset_t first_offset
, /* Offset into object */
266 vm_prot_t fault_type
, /* What access is requested */
267 boolean_t must_be_resident
,/* Must page be resident? */
268 int interruptible
, /* how may fault be interrupted? */
269 vm_object_offset_t lo_offset
, /* Map entry start */
270 vm_object_offset_t hi_offset
, /* Map entry end */
271 vm_behavior_t behavior
, /* Page reference behavior */
272 /* Modifies in place: */
273 vm_prot_t
*protection
, /* Protection for mapping */
275 vm_page_t
*result_page
, /* Page found, if successful */
276 vm_page_t
*top_page
, /* Page in top object, if
277 * not result_page. */
278 int *type_of_fault
, /* if non-null, fill in with type of fault
279 * COW, zero-fill, etc... returned in trace point */
280 /* More arguments: */
281 kern_return_t
*error_code
, /* code if page is in error */
282 boolean_t no_zero_fill
, /* don't zero fill absent pages */
283 boolean_t data_supply
, /* treat as data_supply if
284 * it is a write fault and a full
285 * page is provided */
294 vm_object_offset_t offset
;
296 vm_object_t next_object
;
297 vm_object_t copy_object
;
298 boolean_t look_for_page
;
299 vm_prot_t access_required
= fault_type
;
300 vm_prot_t wants_copy_flag
;
301 vm_size_t cluster_size
, length
;
302 vm_object_offset_t cluster_offset
;
303 vm_object_offset_t cluster_start
, cluster_end
, paging_offset
;
304 vm_object_offset_t align_offset
;
305 CLUSTER_STAT(int pages_at_higher_offsets
;)
306 CLUSTER_STAT(int pages_at_lower_offsets
;)
307 kern_return_t wait_result
;
308 boolean_t interruptible_state
;
309 boolean_t bumped_pagein
= FALSE
;
314 * MACH page map - an optional optimization where a bit map is maintained
315 * by the VM subsystem for internal objects to indicate which pages of
316 * the object currently reside on backing store. This existence map
317 * duplicates information maintained by the vnode pager. It is
318 * created at the time of the first pageout against the object, i.e.
319 * at the same time pager for the object is created. The optimization
320 * is designed to eliminate pager interaction overhead, if it is
321 * 'known' that the page does not exist on backing store.
323 * LOOK_FOR() evaluates to TRUE if the page specified by object/offset is
324 * either marked as paged out in the existence map for the object or no
325 * existence map exists for the object. LOOK_FOR() is one of the
326 * criteria in the decision to invoke the pager. It is also used as one
327 * of the criteria to terminate the scan for adjacent pages in a clustered
328 * pagein operation. Note that LOOK_FOR() always evaluates to TRUE for
329 * permanent objects. Note also that if the pager for an internal object
330 * has not been created, the pager is not invoked regardless of the value
331 * of LOOK_FOR() and that clustered pagein scans are only done on an object
332 * for which a pager has been created.
334 * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset
335 * is marked as paged out in the existence map for the object. PAGED_OUT()
336 * PAGED_OUT() is used to determine if a page has already been pushed
337 * into a copy object in order to avoid a redundant page out operation.
339 #define LOOK_FOR(o, f) (vm_external_state_get((o)->existence_map, (f)) \
340 != VM_EXTERNAL_STATE_ABSENT)
341 #define PAGED_OUT(o, f) (vm_external_state_get((o)->existence_map, (f)) \
342 == VM_EXTERNAL_STATE_EXISTS)
343 #else /* MACH_PAGEMAP */
345 * If the MACH page map optimization is not enabled,
346 * LOOK_FOR() always evaluates to TRUE. The pager will always be
347 * invoked to resolve missing pages in an object, assuming the pager
348 * has been created for the object. In a clustered page operation, the
349 * absence of a page on backing backing store cannot be used to terminate
350 * a scan for adjacent pages since that information is available only in
351 * the pager. Hence pages that may not be paged out are potentially
352 * included in a clustered request. The vnode pager is coded to deal
353 * with any combination of absent/present pages in a clustered
354 * pagein request. PAGED_OUT() always evaluates to FALSE, i.e. the pager
355 * will always be invoked to push a dirty page into a copy object assuming
356 * a pager has been created. If the page has already been pushed, the
357 * pager will ingore the new request.
359 #define LOOK_FOR(o, f) TRUE
360 #define PAGED_OUT(o, f) FALSE
361 #endif /* MACH_PAGEMAP */
366 #define PREPARE_RELEASE_PAGE(m) \
368 vm_page_lock_queues(); \
371 #define DO_RELEASE_PAGE(m) \
373 PAGE_WAKEUP_DONE(m); \
374 if (!m->active && !m->inactive) \
375 vm_page_activate(m); \
376 vm_page_unlock_queues(); \
379 #define RELEASE_PAGE(m) \
381 PREPARE_RELEASE_PAGE(m); \
382 DO_RELEASE_PAGE(m); \
386 dbgTrace(0xBEEF0002, (unsigned int) first_object
, (unsigned int) first_offset
); /* (TEST/DEBUG) */
391 #if !VM_FAULT_STATIC_CONFIG
392 if (vm_fault_dirty_handling
395 * If there are watchpoints set, then
396 * we don't want to give away write permission
397 * on a read fault. Make the task write fault,
398 * so that the watchpoint code notices the access.
400 || db_watchpoint_list
401 #endif /* MACH_KDB */
404 * If we aren't asking for write permission,
405 * then don't give it away. We're using write
406 * faults to set the dirty bit.
408 if (!(fault_type
& VM_PROT_WRITE
))
409 *protection
&= ~VM_PROT_WRITE
;
412 if (!vm_fault_interruptible
)
413 interruptible
= THREAD_UNINT
;
414 #else /* STATIC_CONFIG */
417 * If there are watchpoints set, then
418 * we don't want to give away write permission
419 * on a read fault. Make the task write fault,
420 * so that the watchpoint code notices the access.
422 if (db_watchpoint_list
) {
424 * If we aren't asking for write permission,
425 * then don't give it away. We're using write
426 * faults to set the dirty bit.
428 if (!(fault_type
& VM_PROT_WRITE
))
429 *protection
&= ~VM_PROT_WRITE
;
432 #endif /* MACH_KDB */
433 #endif /* STATIC_CONFIG */
435 interruptible_state
= thread_interrupt_level(interruptible
);
438 * INVARIANTS (through entire routine):
440 * 1) At all times, we must either have the object
441 * lock or a busy page in some object to prevent
442 * some other thread from trying to bring in
445 * Note that we cannot hold any locks during the
446 * pager access or when waiting for memory, so
447 * we use a busy page then.
449 * Note also that we aren't as concerned about more than
450 * one thread attempting to memory_object_data_unlock
451 * the same page at once, so we don't hold the page
452 * as busy then, but do record the highest unlock
453 * value so far. [Unlock requests may also be delivered
456 * 2) To prevent another thread from racing us down the
457 * shadow chain and entering a new page in the top
458 * object before we do, we must keep a busy page in
459 * the top object while following the shadow chain.
461 * 3) We must increment paging_in_progress on any object
462 * for which we have a busy page
464 * 4) We leave busy pages on the pageout queues.
465 * If the pageout daemon comes across a busy page,
466 * it will remove the page from the pageout queues.
470 * Search for the page at object/offset.
473 object
= first_object
;
474 offset
= first_offset
;
475 first_m
= VM_PAGE_NULL
;
476 access_required
= fault_type
;
479 "vm_f_page: obj 0x%X, offset 0x%X, type %d, prot %d\n",
480 (integer_t
)object
, offset
, fault_type
, *protection
, 0);
483 * See whether this page is resident
488 dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
490 if (!object
->alive
) {
491 vm_fault_cleanup(object
, first_m
);
492 thread_interrupt_level(interruptible_state
);
493 return(VM_FAULT_MEMORY_ERROR
);
495 m
= vm_page_lookup(object
, offset
);
497 dbgTrace(0xBEEF0004, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
499 if (m
!= VM_PAGE_NULL
) {
501 * If the page was pre-paged as part of a
502 * cluster, record the fact.
505 vm_pagein_cluster_used
++;
506 m
->clustered
= FALSE
;
510 * If the page is being brought in,
511 * wait for it and then retry.
513 * A possible optimization: if the page
514 * is known to be resident, we can ignore
515 * pages that are absent (regardless of
516 * whether they're busy).
521 dbgTrace(0xBEEF0005, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
523 wait_result
= PAGE_SLEEP(object
, m
, interruptible
);
525 "vm_f_page: block busy obj 0x%X, offset 0x%X, page 0x%X\n",
526 (integer_t
)object
, offset
,
528 counter(c_vm_fault_page_block_busy_kernel
++);
530 if (wait_result
!= THREAD_AWAKENED
) {
531 vm_fault_cleanup(object
, first_m
);
532 thread_interrupt_level(interruptible_state
);
533 if (wait_result
== THREAD_RESTART
)
535 return(VM_FAULT_RETRY
);
539 return(VM_FAULT_INTERRUPTED
);
546 * If the page is in error, give up now.
551 dbgTrace(0xBEEF0006, (unsigned int) m
, (unsigned int) error_code
); /* (TEST/DEBUG) */
554 *error_code
= m
->page_error
;
556 vm_fault_cleanup(object
, first_m
);
557 thread_interrupt_level(interruptible_state
);
558 return(VM_FAULT_MEMORY_ERROR
);
562 * If the pager wants us to restart
563 * at the top of the chain,
564 * typically because it has moved the
565 * page to another pager, then do so.
570 dbgTrace(0xBEEF0007, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
573 vm_fault_cleanup(object
, first_m
);
574 thread_interrupt_level(interruptible_state
);
575 return(VM_FAULT_RETRY
);
579 * If the page isn't busy, but is absent,
580 * then it was deemed "unavailable".
585 * Remove the non-existent page (unless it's
586 * in the top object) and move on down to the
587 * next object (if there is one).
590 dbgTrace(0xBEEF0008, (unsigned int) m
, (unsigned int) object
->shadow
); /* (TEST/DEBUG) */
593 next_object
= object
->shadow
;
594 if (next_object
== VM_OBJECT_NULL
) {
597 assert(!must_be_resident
);
599 if (object
->shadow_severed
) {
602 thread_interrupt_level(interruptible_state
);
603 return VM_FAULT_MEMORY_ERROR
;
607 * Absent page at bottom of shadow
608 * chain; zero fill the page we left
609 * busy in the first object, and flush
610 * the absent page. But first we
611 * need to allocate a real page.
613 if (VM_PAGE_THROTTLED() ||
614 (real_m
= vm_page_grab()) == VM_PAGE_NULL
) {
615 vm_fault_cleanup(object
, first_m
);
616 thread_interrupt_level(interruptible_state
);
617 return(VM_FAULT_MEMORY_SHORTAGE
);
622 "vm_f_page: zero obj 0x%X, off 0x%X, page 0x%X, first_obj 0x%X\n",
623 (integer_t
)object
, offset
,
625 (integer_t
)first_object
, 0);
626 if (object
!= first_object
) {
628 vm_object_paging_end(object
);
629 vm_object_unlock(object
);
630 object
= first_object
;
631 offset
= first_offset
;
633 first_m
= VM_PAGE_NULL
;
634 vm_object_lock(object
);
638 assert(real_m
->busy
);
639 vm_page_insert(real_m
, object
, offset
);
643 * Drop the lock while zero filling
644 * page. Then break because this
645 * is the page we wanted. Checking
646 * the page lock is a waste of time;
647 * this page was either absent or
648 * newly allocated -- in both cases
649 * it can't be page locked by a pager.
654 vm_object_unlock(object
);
655 vm_page_zero_fill(m
);
657 *type_of_fault
= DBG_ZERO_FILL_FAULT
;
658 VM_STAT(zero_fill_count
++);
660 if (bumped_pagein
== TRUE
) {
662 current_task()->pageins
--;
664 vm_object_lock(object
);
666 pmap_clear_modify(m
->phys_page
);
667 vm_page_lock_queues();
668 VM_PAGE_QUEUES_REMOVE(m
);
669 m
->page_ticket
= vm_page_ticket
;
670 if(m
->object
->size
> 0x80000) {
672 /* depends on the queues lock */
674 queue_enter(&vm_page_queue_zf
,
675 m
, vm_page_t
, pageq
);
678 &vm_page_queue_inactive
,
679 m
, vm_page_t
, pageq
);
681 vm_page_ticket_roll
++;
682 if(vm_page_ticket_roll
==
683 VM_PAGE_TICKETS_IN_ROLL
) {
684 vm_page_ticket_roll
= 0;
686 VM_PAGE_TICKET_ROLL_IDS
)
692 vm_page_inactive_count
++;
693 vm_page_unlock_queues();
696 if (must_be_resident
) {
697 vm_object_paging_end(object
);
698 } else if (object
!= first_object
) {
699 vm_object_paging_end(object
);
705 vm_object_absent_release(object
);
708 vm_page_lock_queues();
709 VM_PAGE_QUEUES_REMOVE(m
);
710 vm_page_unlock_queues();
713 "vm_f_page: unavail obj 0x%X, off 0x%X, next_obj 0x%X, newoff 0x%X\n",
714 (integer_t
)object
, offset
,
715 (integer_t
)next_object
,
716 offset
+object
->shadow_offset
,0);
717 offset
+= object
->shadow_offset
;
718 hi_offset
+= object
->shadow_offset
;
719 lo_offset
+= object
->shadow_offset
;
720 access_required
= VM_PROT_READ
;
721 vm_object_lock(next_object
);
722 vm_object_unlock(object
);
723 object
= next_object
;
724 vm_object_paging_begin(object
);
730 && ((object
!= first_object
) ||
731 (object
->copy
!= VM_OBJECT_NULL
))
732 && (fault_type
& VM_PROT_WRITE
)) {
734 * This is a copy-on-write fault that will
735 * cause us to revoke access to this page, but
736 * this page is in the process of being cleaned
737 * in a clustered pageout. We must wait until
738 * the cleaning operation completes before
739 * revoking access to the original page,
740 * otherwise we might attempt to remove a
744 dbgTrace(0xBEEF0009, (unsigned int) m
, (unsigned int) offset
); /* (TEST/DEBUG) */
747 "vm_f_page: cleaning obj 0x%X, offset 0x%X, page 0x%X\n",
748 (integer_t
)object
, offset
,
750 /* take an extra ref so that object won't die */
751 assert(object
->ref_count
> 0);
753 vm_object_res_reference(object
);
754 vm_fault_cleanup(object
, first_m
);
755 counter(c_vm_fault_page_block_backoff_kernel
++);
756 vm_object_lock(object
);
757 assert(object
->ref_count
> 0);
758 m
= vm_page_lookup(object
, offset
);
759 if (m
!= VM_PAGE_NULL
&& m
->cleaning
) {
760 PAGE_ASSERT_WAIT(m
, interruptible
);
761 vm_object_unlock(object
);
762 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
763 vm_object_deallocate(object
);
766 vm_object_unlock(object
);
767 vm_object_deallocate(object
);
768 thread_interrupt_level(interruptible_state
);
769 return VM_FAULT_RETRY
;
774 * If the desired access to this page has
775 * been locked out, request that it be unlocked.
778 if (access_required
& m
->page_lock
) {
779 if ((access_required
& m
->unlock_request
) != access_required
) {
780 vm_prot_t new_unlock_request
;
784 dbgTrace(0xBEEF000A, (unsigned int) m
, (unsigned int) object
->pager_ready
); /* (TEST/DEBUG) */
786 if (!object
->pager_ready
) {
788 "vm_f_page: ready wait acc_req %d, obj 0x%X, offset 0x%X, page 0x%X\n",
790 (integer_t
)object
, offset
,
792 /* take an extra ref */
793 assert(object
->ref_count
> 0);
795 vm_object_res_reference(object
);
796 vm_fault_cleanup(object
,
798 counter(c_vm_fault_page_block_backoff_kernel
++);
799 vm_object_lock(object
);
800 assert(object
->ref_count
> 0);
801 if (!object
->pager_ready
) {
802 wait_result
= vm_object_assert_wait(
804 VM_OBJECT_EVENT_PAGER_READY
,
806 vm_object_unlock(object
);
807 if (wait_result
== THREAD_WAITING
)
808 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
809 vm_object_deallocate(object
);
812 vm_object_unlock(object
);
813 vm_object_deallocate(object
);
814 thread_interrupt_level(interruptible_state
);
815 return VM_FAULT_RETRY
;
819 new_unlock_request
= m
->unlock_request
=
820 (access_required
| m
->unlock_request
);
821 vm_object_unlock(object
);
823 "vm_f_page: unlock obj 0x%X, offset 0x%X, page 0x%X, unl_req %d\n",
824 (integer_t
)object
, offset
,
825 (integer_t
)m
, new_unlock_request
, 0);
826 if ((rc
= memory_object_data_unlock(
828 offset
+ object
->paging_offset
,
833 printf("vm_fault: memory_object_data_unlock failed\n");
834 vm_object_lock(object
);
835 vm_fault_cleanup(object
, first_m
);
836 thread_interrupt_level(interruptible_state
);
837 return((rc
== MACH_SEND_INTERRUPTED
) ?
838 VM_FAULT_INTERRUPTED
:
839 VM_FAULT_MEMORY_ERROR
);
841 vm_object_lock(object
);
846 "vm_f_page: access wait acc_req %d, obj 0x%X, offset 0x%X, page 0x%X\n",
847 access_required
, (integer_t
)object
,
848 offset
, (integer_t
)m
, 0);
849 /* take an extra ref so object won't die */
850 assert(object
->ref_count
> 0);
852 vm_object_res_reference(object
);
853 vm_fault_cleanup(object
, first_m
);
854 counter(c_vm_fault_page_block_backoff_kernel
++);
855 vm_object_lock(object
);
856 assert(object
->ref_count
> 0);
857 m
= vm_page_lookup(object
, offset
);
858 if (m
!= VM_PAGE_NULL
&&
859 (access_required
& m
->page_lock
) &&
860 !((access_required
& m
->unlock_request
) != access_required
)) {
861 PAGE_ASSERT_WAIT(m
, interruptible
);
862 vm_object_unlock(object
);
863 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
864 vm_object_deallocate(object
);
867 vm_object_unlock(object
);
868 vm_object_deallocate(object
);
869 thread_interrupt_level(interruptible_state
);
870 return VM_FAULT_RETRY
;
874 * We mark the page busy and leave it on
875 * the pageout queues. If the pageout
876 * deamon comes across it, then it will
881 dbgTrace(0xBEEF000B, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
884 #if !VM_FAULT_STATIC_CONFIG
885 if (!software_reference_bits
) {
886 vm_page_lock_queues();
888 vm_stat
.reactivations
++;
890 VM_PAGE_QUEUES_REMOVE(m
);
891 vm_page_unlock_queues();
895 "vm_f_page: found page obj 0x%X, offset 0x%X, page 0x%X\n",
896 (integer_t
)object
, offset
, (integer_t
)m
, 0, 0);
904 (object
->pager_created
) &&
905 LOOK_FOR(object
, offset
) &&
909 dbgTrace(0xBEEF000C, (unsigned int) look_for_page
, (unsigned int) object
); /* (TEST/DEBUG) */
911 if ((look_for_page
|| (object
== first_object
))
913 && !(object
->phys_contiguous
)) {
915 * Allocate a new page for this object/offset
919 m
= vm_page_grab_fictitious();
921 dbgTrace(0xBEEF000D, (unsigned int) m
, (unsigned int) object
); /* (TEST/DEBUG) */
923 if (m
== VM_PAGE_NULL
) {
924 vm_fault_cleanup(object
, first_m
);
925 thread_interrupt_level(interruptible_state
);
926 return(VM_FAULT_FICTITIOUS_SHORTAGE
);
928 vm_page_insert(m
, object
, offset
);
931 if ((look_for_page
&& !must_be_resident
)) {
935 * If the memory manager is not ready, we
936 * cannot make requests.
938 if (!object
->pager_ready
) {
940 dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */
942 if(m
!= VM_PAGE_NULL
)
945 "vm_f_page: ready wait obj 0x%X, offset 0x%X\n",
946 (integer_t
)object
, offset
, 0, 0, 0);
947 /* take an extra ref so object won't die */
948 assert(object
->ref_count
> 0);
950 vm_object_res_reference(object
);
951 vm_fault_cleanup(object
, first_m
);
952 counter(c_vm_fault_page_block_backoff_kernel
++);
953 vm_object_lock(object
);
954 assert(object
->ref_count
> 0);
955 if (!object
->pager_ready
) {
956 wait_result
= vm_object_assert_wait(object
,
957 VM_OBJECT_EVENT_PAGER_READY
,
959 vm_object_unlock(object
);
960 if (wait_result
== THREAD_WAITING
)
961 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
962 vm_object_deallocate(object
);
965 vm_object_unlock(object
);
966 vm_object_deallocate(object
);
967 thread_interrupt_level(interruptible_state
);
968 return VM_FAULT_RETRY
;
972 if(object
->phys_contiguous
) {
973 if(m
!= VM_PAGE_NULL
) {
979 if (object
->internal
) {
981 * Requests to the default pager
982 * must reserve a real page in advance,
983 * because the pager's data-provided
984 * won't block for pages. IMPORTANT:
985 * this acts as a throttling mechanism
986 * for data_requests to the default
991 dbgTrace(0xBEEF000F, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
993 if (m
->fictitious
&& !vm_page_convert(m
)) {
995 vm_fault_cleanup(object
, first_m
);
996 thread_interrupt_level(interruptible_state
);
997 return(VM_FAULT_MEMORY_SHORTAGE
);
999 } else if (object
->absent_count
>
1000 vm_object_absent_max
) {
1002 * If there are too many outstanding page
1003 * requests pending on this object, we
1004 * wait for them to be resolved now.
1008 dbgTrace(0xBEEF0010, (unsigned int) m
, (unsigned int) 0); /* (TEST/DEBUG) */
1010 if(m
!= VM_PAGE_NULL
)
1012 /* take an extra ref so object won't die */
1013 assert(object
->ref_count
> 0);
1014 object
->ref_count
++;
1015 vm_object_res_reference(object
);
1016 vm_fault_cleanup(object
, first_m
);
1017 counter(c_vm_fault_page_block_backoff_kernel
++);
1018 vm_object_lock(object
);
1019 assert(object
->ref_count
> 0);
1020 if (object
->absent_count
> vm_object_absent_max
) {
1021 vm_object_absent_assert_wait(object
,
1023 vm_object_unlock(object
);
1024 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1025 vm_object_deallocate(object
);
1028 vm_object_unlock(object
);
1029 vm_object_deallocate(object
);
1030 thread_interrupt_level(interruptible_state
);
1031 return VM_FAULT_RETRY
;
1036 * Indicate that the page is waiting for data
1037 * from the memory manager.
1040 if(m
!= VM_PAGE_NULL
) {
1042 m
->list_req_pending
= TRUE
;
1045 object
->absent_count
++;
1050 cluster_start
= offset
;
1054 * lengthen the cluster by the pages in the working set
1057 (current_task()->dynamic_working_set
!= 0)) {
1058 cluster_end
= cluster_start
+ length
;
1059 /* tws values for start and end are just a
1060 * suggestions. Therefore, as long as
1061 * build_cluster does not use pointers or
1062 * take action based on values that
1063 * could be affected by re-entrance we
1064 * do not need to take the map lock.
1066 cluster_end
= offset
+ PAGE_SIZE_64
;
1067 tws_build_cluster((tws_hash_t
)
1068 current_task()->dynamic_working_set
,
1069 object
, &cluster_start
,
1070 &cluster_end
, 0x40000);
1071 length
= cluster_end
- cluster_start
;
1074 dbgTrace(0xBEEF0012, (unsigned int) object
, (unsigned int) 0); /* (TEST/DEBUG) */
1077 * We have a busy page, so we can
1078 * release the object lock.
1080 vm_object_unlock(object
);
1083 * Call the memory manager to retrieve the data.
1087 *type_of_fault
= (length
<< 8) | DBG_PAGEIN_FAULT
;
1089 current_task()->pageins
++;
1090 bumped_pagein
= TRUE
;
1093 * If this object uses a copy_call strategy,
1094 * and we are interested in a copy of this object
1095 * (having gotten here only by following a
1096 * shadow chain), then tell the memory manager
1097 * via a flag added to the desired_access
1098 * parameter, so that it can detect a race
1099 * between our walking down the shadow chain
1100 * and its pushing pages up into a copy of
1101 * the object that it manages.
1104 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_CALL
&&
1105 object
!= first_object
) {
1106 wants_copy_flag
= VM_PROT_WANTS_COPY
;
1108 wants_copy_flag
= VM_PROT_NONE
;
1112 "vm_f_page: data_req obj 0x%X, offset 0x%X, page 0x%X, acc %d\n",
1113 (integer_t
)object
, offset
, (integer_t
)m
,
1114 access_required
| wants_copy_flag
, 0);
1116 rc
= memory_object_data_request(object
->pager
,
1117 cluster_start
+ object
->paging_offset
,
1119 access_required
| wants_copy_flag
);
1123 dbgTrace(0xBEEF0013, (unsigned int) object
, (unsigned int) rc
); /* (TEST/DEBUG) */
1125 if (rc
!= KERN_SUCCESS
) {
1126 if (rc
!= MACH_SEND_INTERRUPTED
1128 printf("%s(0x%x, 0x%x, 0x%x, 0x%x) failed, rc=%d\n",
1129 "memory_object_data_request",
1131 cluster_start
+ object
->paging_offset
,
1132 length
, access_required
, rc
);
1134 * Don't want to leave a busy page around,
1135 * but the data request may have blocked,
1136 * so check if it's still there and busy.
1138 if(!object
->phys_contiguous
) {
1139 vm_object_lock(object
);
1140 for (; length
; length
-= PAGE_SIZE
,
1141 cluster_start
+= PAGE_SIZE_64
) {
1143 if ((p
= vm_page_lookup(object
,
1145 && p
->absent
&& p
->busy
1151 vm_fault_cleanup(object
, first_m
);
1152 thread_interrupt_level(interruptible_state
);
1153 return((rc
== MACH_SEND_INTERRUPTED
) ?
1154 VM_FAULT_INTERRUPTED
:
1155 VM_FAULT_MEMORY_ERROR
);
1158 tws_hash_line_t line
;
1161 task
= current_task();
1164 (task
->dynamic_working_set
!= 0))
1165 && !(object
->private)) {
1166 vm_object_t base_object
;
1167 vm_object_offset_t base_offset
;
1168 base_object
= object
;
1169 base_offset
= offset
;
1170 while(base_object
->shadow
) {
1172 base_object
->shadow_offset
;
1174 base_object
->shadow
;
1178 task
->dynamic_working_set
,
1179 base_offset
, base_object
,
1180 &line
) == KERN_SUCCESS
) {
1181 tws_line_signal((tws_hash_t
)
1182 task
->dynamic_working_set
,
1190 * Retry with same object/offset, since new data may
1191 * be in a different page (i.e., m is meaningless at
1194 vm_object_lock(object
);
1195 if ((interruptible
!= THREAD_UNINT
) &&
1196 (current_thread()->state
& TH_ABORT
)) {
1197 vm_fault_cleanup(object
, first_m
);
1198 thread_interrupt_level(interruptible_state
);
1199 return(VM_FAULT_INTERRUPTED
);
1201 if(m
== VM_PAGE_NULL
)
1207 * The only case in which we get here is if
1208 * object has no pager (or unwiring). If the pager doesn't
1209 * have the page this is handled in the m->absent case above
1210 * (and if you change things here you should look above).
1213 dbgTrace(0xBEEF0014, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1215 if (object
== first_object
)
1218 assert(m
== VM_PAGE_NULL
);
1221 "vm_f_page: no pager obj 0x%X, offset 0x%X, page 0x%X, next_obj 0x%X\n",
1222 (integer_t
)object
, offset
, (integer_t
)m
,
1223 (integer_t
)object
->shadow
, 0);
1225 * Move on to the next object. Lock the next
1226 * object before unlocking the current one.
1228 next_object
= object
->shadow
;
1229 if (next_object
== VM_OBJECT_NULL
) {
1230 assert(!must_be_resident
);
1232 * If there's no object left, fill the page
1233 * in the top object with zeros. But first we
1234 * need to allocate a real page.
1237 if (object
!= first_object
) {
1238 vm_object_paging_end(object
);
1239 vm_object_unlock(object
);
1241 object
= first_object
;
1242 offset
= first_offset
;
1243 vm_object_lock(object
);
1247 assert(m
->object
== object
);
1248 first_m
= VM_PAGE_NULL
;
1250 if (object
->shadow_severed
) {
1252 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1253 thread_interrupt_level(interruptible_state
);
1254 return VM_FAULT_MEMORY_ERROR
;
1257 if (VM_PAGE_THROTTLED() ||
1258 (m
->fictitious
&& !vm_page_convert(m
))) {
1260 vm_fault_cleanup(object
, VM_PAGE_NULL
);
1261 thread_interrupt_level(interruptible_state
);
1262 return(VM_FAULT_MEMORY_SHORTAGE
);
1264 m
->no_isync
= FALSE
;
1266 if (!no_zero_fill
) {
1267 vm_object_unlock(object
);
1268 vm_page_zero_fill(m
);
1270 *type_of_fault
= DBG_ZERO_FILL_FAULT
;
1271 VM_STAT(zero_fill_count
++);
1273 if (bumped_pagein
== TRUE
) {
1275 current_task()->pageins
--;
1277 vm_object_lock(object
);
1279 vm_page_lock_queues();
1280 VM_PAGE_QUEUES_REMOVE(m
);
1281 if(m
->object
->size
> 0x80000) {
1282 m
->zero_fill
= TRUE
;
1283 /* depends on the queues lock */
1285 queue_enter(&vm_page_queue_zf
,
1286 m
, vm_page_t
, pageq
);
1289 &vm_page_queue_inactive
,
1290 m
, vm_page_t
, pageq
);
1292 m
->page_ticket
= vm_page_ticket
;
1293 vm_page_ticket_roll
++;
1294 if(vm_page_ticket_roll
== VM_PAGE_TICKETS_IN_ROLL
) {
1295 vm_page_ticket_roll
= 0;
1296 if(vm_page_ticket
==
1297 VM_PAGE_TICKET_ROLL_IDS
)
1303 vm_page_inactive_count
++;
1304 vm_page_unlock_queues();
1305 pmap_clear_modify(m
->phys_page
);
1309 if ((object
!= first_object
) || must_be_resident
)
1310 vm_object_paging_end(object
);
1311 offset
+= object
->shadow_offset
;
1312 hi_offset
+= object
->shadow_offset
;
1313 lo_offset
+= object
->shadow_offset
;
1314 access_required
= VM_PROT_READ
;
1315 vm_object_lock(next_object
);
1316 vm_object_unlock(object
);
1317 object
= next_object
;
1318 vm_object_paging_begin(object
);
1323 * PAGE HAS BEEN FOUND.
1326 * busy, so that we can play with it;
1327 * not absent, so that nobody else will fill it;
1328 * possibly eligible for pageout;
1330 * The top-level page (first_m) is:
1331 * VM_PAGE_NULL if the page was found in the
1333 * busy, not absent, and ineligible for pageout.
1335 * The current object (object) is locked. A paging
1336 * reference is held for the current and top-level
1341 dbgTrace(0xBEEF0015, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1343 #if EXTRA_ASSERTIONS
1344 if(m
!= VM_PAGE_NULL
) {
1345 assert(m
->busy
&& !m
->absent
);
1346 assert((first_m
== VM_PAGE_NULL
) ||
1347 (first_m
->busy
&& !first_m
->absent
&&
1348 !first_m
->active
&& !first_m
->inactive
));
1350 #endif /* EXTRA_ASSERTIONS */
1353 "vm_f_page: FOUND obj 0x%X, off 0x%X, page 0x%X, 1_obj 0x%X, 1_m 0x%X\n",
1354 (integer_t
)object
, offset
, (integer_t
)m
,
1355 (integer_t
)first_object
, (integer_t
)first_m
);
1357 * If the page is being written, but isn't
1358 * already owned by the top-level object,
1359 * we have to copy it into a new page owned
1360 * by the top-level object.
1363 if ((object
!= first_object
) && (m
!= VM_PAGE_NULL
)) {
1365 * We only really need to copy if we
1370 dbgTrace(0xBEEF0016, (unsigned int) object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1372 if (fault_type
& VM_PROT_WRITE
) {
1375 assert(!must_be_resident
);
1378 * If we try to collapse first_object at this
1379 * point, we may deadlock when we try to get
1380 * the lock on an intermediate object (since we
1381 * have the bottom object locked). We can't
1382 * unlock the bottom object, because the page
1383 * we found may move (by collapse) if we do.
1385 * Instead, we first copy the page. Then, when
1386 * we have no more use for the bottom object,
1387 * we unlock it and try to collapse.
1389 * Note that we copy the page even if we didn't
1390 * need to... that's the breaks.
1394 * Allocate a page for the copy
1396 copy_m
= vm_page_grab();
1397 if (copy_m
== VM_PAGE_NULL
) {
1399 vm_fault_cleanup(object
, first_m
);
1400 thread_interrupt_level(interruptible_state
);
1401 return(VM_FAULT_MEMORY_SHORTAGE
);
1406 "vm_f_page: page_copy obj 0x%X, offset 0x%X, m 0x%X, copy_m 0x%X\n",
1407 (integer_t
)object
, offset
,
1408 (integer_t
)m
, (integer_t
)copy_m
, 0);
1409 vm_page_copy(m
, copy_m
);
1412 * If another map is truly sharing this
1413 * page with us, we have to flush all
1414 * uses of the original page, since we
1415 * can't distinguish those which want the
1416 * original from those which need the
1419 * XXXO If we know that only one map has
1420 * access to this page, then we could
1421 * avoid the pmap_page_protect() call.
1424 vm_page_lock_queues();
1425 assert(!m
->cleaning
);
1426 pmap_page_protect(m
->phys_page
, VM_PROT_NONE
);
1427 vm_page_deactivate(m
);
1428 copy_m
->dirty
= TRUE
;
1430 * Setting reference here prevents this fault from
1431 * being counted as a (per-thread) reactivate as well
1432 * as a copy-on-write.
1434 first_m
->reference
= TRUE
;
1435 vm_page_unlock_queues();
1438 * We no longer need the old page or object.
1441 PAGE_WAKEUP_DONE(m
);
1442 vm_object_paging_end(object
);
1443 vm_object_unlock(object
);
1446 *type_of_fault
= DBG_COW_FAULT
;
1447 VM_STAT(cow_faults
++);
1448 current_task()->cow_faults
++;
1449 object
= first_object
;
1450 offset
= first_offset
;
1452 vm_object_lock(object
);
1453 VM_PAGE_FREE(first_m
);
1454 first_m
= VM_PAGE_NULL
;
1455 assert(copy_m
->busy
);
1456 vm_page_insert(copy_m
, object
, offset
);
1460 * Now that we've gotten the copy out of the
1461 * way, let's try to collapse the top object.
1462 * But we have to play ugly games with
1463 * paging_in_progress to do that...
1466 vm_object_paging_end(object
);
1467 vm_object_collapse(object
);
1468 vm_object_paging_begin(object
);
1472 *protection
&= (~VM_PROT_WRITE
);
1477 * Now check whether the page needs to be pushed into the
1478 * copy object. The use of asymmetric copy on write for
1479 * shared temporary objects means that we may do two copies to
1480 * satisfy the fault; one above to get the page from a
1481 * shadowed object, and one here to push it into the copy.
1484 while ((copy_object
= first_object
->copy
) != VM_OBJECT_NULL
&&
1485 (m
!= VM_PAGE_NULL
)) {
1486 vm_object_offset_t copy_offset
;
1490 dbgTrace(0xBEEF0017, (unsigned int) copy_object
, (unsigned int) fault_type
); /* (TEST/DEBUG) */
1493 * If the page is being written, but hasn't been
1494 * copied to the copy-object, we have to copy it there.
1497 if ((fault_type
& VM_PROT_WRITE
) == 0) {
1498 *protection
&= ~VM_PROT_WRITE
;
1503 * If the page was guaranteed to be resident,
1504 * we must have already performed the copy.
1507 if (must_be_resident
)
1511 * Try to get the lock on the copy_object.
1513 if (!vm_object_lock_try(copy_object
)) {
1514 vm_object_unlock(object
);
1516 mutex_pause(); /* wait a bit */
1518 vm_object_lock(object
);
1523 * Make another reference to the copy-object,
1524 * to keep it from disappearing during the
1527 assert(copy_object
->ref_count
> 0);
1528 copy_object
->ref_count
++;
1529 VM_OBJ_RES_INCR(copy_object
);
1532 * Does the page exist in the copy?
1534 copy_offset
= first_offset
- copy_object
->shadow_offset
;
1535 if (copy_object
->size
<= copy_offset
)
1537 * Copy object doesn't cover this page -- do nothing.
1541 vm_page_lookup(copy_object
, copy_offset
)) != VM_PAGE_NULL
) {
1542 /* Page currently exists in the copy object */
1545 * If the page is being brought
1546 * in, wait for it and then retry.
1549 /* take an extra ref so object won't die */
1550 assert(copy_object
->ref_count
> 0);
1551 copy_object
->ref_count
++;
1552 vm_object_res_reference(copy_object
);
1553 vm_object_unlock(copy_object
);
1554 vm_fault_cleanup(object
, first_m
);
1555 counter(c_vm_fault_page_block_backoff_kernel
++);
1556 vm_object_lock(copy_object
);
1557 assert(copy_object
->ref_count
> 0);
1558 VM_OBJ_RES_DECR(copy_object
);
1559 copy_object
->ref_count
--;
1560 assert(copy_object
->ref_count
> 0);
1561 copy_m
= vm_page_lookup(copy_object
, copy_offset
);
1562 if (copy_m
!= VM_PAGE_NULL
&& copy_m
->busy
) {
1563 PAGE_ASSERT_WAIT(copy_m
, interruptible
);
1564 vm_object_unlock(copy_object
);
1565 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1566 vm_object_deallocate(copy_object
);
1569 vm_object_unlock(copy_object
);
1570 vm_object_deallocate(copy_object
);
1571 thread_interrupt_level(interruptible_state
);
1572 return VM_FAULT_RETRY
;
1576 else if (!PAGED_OUT(copy_object
, copy_offset
)) {
1578 * If PAGED_OUT is TRUE, then the page used to exist
1579 * in the copy-object, and has already been paged out.
1580 * We don't need to repeat this. If PAGED_OUT is
1581 * FALSE, then either we don't know (!pager_created,
1582 * for example) or it hasn't been paged out.
1583 * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT)
1584 * We must copy the page to the copy object.
1588 * Allocate a page for the copy
1590 copy_m
= vm_page_alloc(copy_object
, copy_offset
);
1591 if (copy_m
== VM_PAGE_NULL
) {
1593 VM_OBJ_RES_DECR(copy_object
);
1594 copy_object
->ref_count
--;
1595 assert(copy_object
->ref_count
> 0);
1596 vm_object_unlock(copy_object
);
1597 vm_fault_cleanup(object
, first_m
);
1598 thread_interrupt_level(interruptible_state
);
1599 return(VM_FAULT_MEMORY_SHORTAGE
);
1603 * Must copy page into copy-object.
1606 vm_page_copy(m
, copy_m
);
1609 * If the old page was in use by any users
1610 * of the copy-object, it must be removed
1611 * from all pmaps. (We can't know which
1615 vm_page_lock_queues();
1616 assert(!m
->cleaning
);
1617 pmap_page_protect(m
->phys_page
, VM_PROT_NONE
);
1618 copy_m
->dirty
= TRUE
;
1619 vm_page_unlock_queues();
1622 * If there's a pager, then immediately
1623 * page out this page, using the "initialize"
1624 * option. Else, we use the copy.
1629 ((!copy_object
->pager_created
) ||
1630 vm_external_state_get(
1631 copy_object
->existence_map
, copy_offset
)
1632 == VM_EXTERNAL_STATE_ABSENT
)
1634 (!copy_object
->pager_created
)
1637 vm_page_lock_queues();
1638 vm_page_activate(copy_m
);
1639 vm_page_unlock_queues();
1640 PAGE_WAKEUP_DONE(copy_m
);
1643 assert(copy_m
->busy
== TRUE
);
1646 * The page is already ready for pageout:
1647 * not on pageout queues and busy.
1648 * Unlock everything except the
1649 * copy_object itself.
1652 vm_object_unlock(object
);
1655 * Write the page to the copy-object,
1656 * flushing it from the kernel.
1659 vm_pageout_initialize_page(copy_m
);
1662 * Since the pageout may have
1663 * temporarily dropped the
1664 * copy_object's lock, we
1665 * check whether we'll have
1666 * to deallocate the hard way.
1669 if ((copy_object
->shadow
!= object
) ||
1670 (copy_object
->ref_count
== 1)) {
1671 vm_object_unlock(copy_object
);
1672 vm_object_deallocate(copy_object
);
1673 vm_object_lock(object
);
1678 * Pick back up the old object's
1679 * lock. [It is safe to do so,
1680 * since it must be deeper in the
1684 vm_object_lock(object
);
1688 * Because we're pushing a page upward
1689 * in the object tree, we must restart
1690 * any faults that are waiting here.
1691 * [Note that this is an expansion of
1692 * PAGE_WAKEUP that uses the THREAD_RESTART
1693 * wait result]. Can't turn off the page's
1694 * busy bit because we're not done with it.
1699 thread_wakeup_with_result((event_t
) m
,
1705 * The reference count on copy_object must be
1706 * at least 2: one for our extra reference,
1707 * and at least one from the outside world
1708 * (we checked that when we last locked
1711 copy_object
->ref_count
--;
1712 assert(copy_object
->ref_count
> 0);
1713 VM_OBJ_RES_DECR(copy_object
);
1714 vm_object_unlock(copy_object
);
1720 *top_page
= first_m
;
1723 "vm_f_page: DONE obj 0x%X, offset 0x%X, m 0x%X, first_m 0x%X\n",
1724 (integer_t
)object
, offset
, (integer_t
)m
, (integer_t
)first_m
, 0);
1726 * If the page can be written, assume that it will be.
1727 * [Earlier, we restrict the permission to allow write
1728 * access only if the fault so required, so we don't
1729 * mark read-only data as dirty.]
1732 #if !VM_FAULT_STATIC_CONFIG
1733 if (vm_fault_dirty_handling
&& (*protection
& VM_PROT_WRITE
) &&
1734 (m
!= VM_PAGE_NULL
)) {
1739 dbgTrace(0xBEEF0018, (unsigned int) object
, (unsigned int) vm_page_deactivate_behind
); /* (TEST/DEBUG) */
1741 if (vm_page_deactivate_behind
) {
1742 if (offset
&& /* don't underflow */
1743 (object
->last_alloc
== (offset
- PAGE_SIZE_64
))) {
1744 m
= vm_page_lookup(object
, object
->last_alloc
);
1745 if ((m
!= VM_PAGE_NULL
) && !m
->busy
) {
1746 vm_page_lock_queues();
1747 vm_page_deactivate(m
);
1748 vm_page_unlock_queues();
1751 dbgTrace(0xBEEF0019, (unsigned int) object
, (unsigned int) m
); /* (TEST/DEBUG) */
1754 object
->last_alloc
= offset
;
1757 dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS
, 0); /* (TEST/DEBUG) */
1759 thread_interrupt_level(interruptible_state
);
1760 if(*result_page
== VM_PAGE_NULL
) {
1761 vm_object_unlock(object
);
1763 return(VM_FAULT_SUCCESS
);
1767 vm_fault_cleanup(object
, first_m
);
1769 counter(c_vm_fault_page_block_backoff_kernel
++);
1770 thread_block(THREAD_CONTINUE_NULL
);
1774 thread_interrupt_level(interruptible_state
);
1775 if (wait_result
== THREAD_INTERRUPTED
)
1776 return VM_FAULT_INTERRUPTED
;
1777 return VM_FAULT_RETRY
;
1785 * Handle page faults, including pseudo-faults
1786 * used to change the wiring status of pages.
1788 * Explicit continuations have been removed.
1790 * vm_fault and vm_fault_page save mucho state
1791 * in the moral equivalent of a closure. The state
1792 * structure is allocated when first entering vm_fault
1793 * and deallocated when leaving vm_fault.
1800 vm_prot_t fault_type
,
1801 boolean_t change_wiring
,
1804 vm_offset_t caller_pmap_addr
)
1806 vm_map_version_t version
; /* Map version for verificiation */
1807 boolean_t wired
; /* Should mapping be wired down? */
1808 vm_object_t object
; /* Top-level object */
1809 vm_object_offset_t offset
; /* Top-level offset */
1810 vm_prot_t prot
; /* Protection for mapping */
1811 vm_behavior_t behavior
; /* Expected paging behavior */
1812 vm_object_offset_t lo_offset
, hi_offset
;
1813 vm_object_t old_copy_object
; /* Saved copy object */
1814 vm_page_t result_page
; /* Result of vm_fault_page */
1815 vm_page_t top_page
; /* Placeholder page */
1819 vm_page_t m
; /* Fast access to result_page */
1820 kern_return_t error_code
; /* page error reasons */
1822 vm_object_t cur_object
;
1824 vm_object_offset_t cur_offset
;
1826 vm_object_t new_object
;
1828 vm_map_t pmap_map
= map
;
1829 vm_map_t original_map
= map
;
1831 boolean_t funnel_set
= FALSE
;
1833 thread_t cur_thread
;
1834 boolean_t interruptible_state
;
1835 unsigned int cache_attr
;
1836 int write_startup_file
= 0;
1837 vm_prot_t full_fault_type
;
1840 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 0)) | DBG_FUNC_START
,
1847 /* at present we do not fully check for execute permission */
1848 /* we generally treat it is read except in certain device */
1849 /* memory settings */
1850 full_fault_type
= fault_type
;
1851 if(fault_type
& VM_PROT_EXECUTE
) {
1852 fault_type
&= ~VM_PROT_EXECUTE
;
1853 fault_type
|= VM_PROT_READ
;
1856 interruptible_state
= thread_interrupt_level(interruptible
);
1859 * assume we will hit a page in the cache
1860 * otherwise, explicitly override with
1861 * the real fault type once we determine it
1863 type_of_fault
= DBG_CACHE_HIT_FAULT
;
1866 current_task()->faults
++;
1869 * drop funnel if it is already held. Then restore while returning
1871 cur_thread
= current_thread();
1873 if ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
) {
1875 curflock
= cur_thread
->funnel_lock
;
1876 thread_funnel_set( curflock
, FALSE
);
1882 * Find the backing store object and offset into
1883 * it to begin the search.
1886 vm_map_lock_read(map
);
1887 kr
= vm_map_lookup_locked(&map
, vaddr
, fault_type
, &version
,
1890 &behavior
, &lo_offset
, &hi_offset
, &pmap_map
);
1892 pmap
= pmap_map
->pmap
;
1894 if (kr
!= KERN_SUCCESS
) {
1895 vm_map_unlock_read(map
);
1900 * If the page is wired, we must fault for the current protection
1901 * value, to avoid further faults.
1905 fault_type
= prot
| VM_PROT_WRITE
;
1907 #if VM_FAULT_CLASSIFY
1909 * Temporary data gathering code
1911 vm_fault_classify(object
, offset
, fault_type
);
1914 * Fast fault code. The basic idea is to do as much as
1915 * possible while holding the map lock and object locks.
1916 * Busy pages are not used until the object lock has to
1917 * be dropped to do something (copy, zero fill, pmap enter).
1918 * Similarly, paging references aren't acquired until that
1919 * point, and object references aren't used.
1921 * If we can figure out what to do
1922 * (zero fill, copy on write, pmap enter) while holding
1923 * the locks, then it gets done. Otherwise, we give up,
1924 * and use the original fault path (which doesn't hold
1925 * the map lock, and relies on busy pages).
1926 * The give up cases include:
1927 * - Have to talk to pager.
1928 * - Page is busy, absent or in error.
1929 * - Pager has locked out desired access.
1930 * - Fault needs to be restarted.
1931 * - Have to push page into copy object.
1933 * The code is an infinite loop that moves one level down
1934 * the shadow chain each time. cur_object and cur_offset
1935 * refer to the current object being examined. object and offset
1936 * are the original object from the map. The loop is at the
1937 * top level if and only if object and cur_object are the same.
1939 * Invariants: Map lock is held throughout. Lock is held on
1940 * original object and cur_object (if different) when
1941 * continuing or exiting loop.
1947 * If this page is to be inserted in a copy delay object
1948 * for writing, and if the object has a copy, then the
1949 * copy delay strategy is implemented in the slow fault page.
1951 if (object
->copy_strategy
!= MEMORY_OBJECT_COPY_DELAY
||
1952 object
->copy
== VM_OBJECT_NULL
||
1953 (fault_type
& VM_PROT_WRITE
) == 0) {
1954 cur_object
= object
;
1955 cur_offset
= offset
;
1958 m
= vm_page_lookup(cur_object
, cur_offset
);
1959 if (m
!= VM_PAGE_NULL
) {
1961 wait_result_t result
;
1963 if (object
!= cur_object
)
1964 vm_object_unlock(object
);
1966 vm_map_unlock_read(map
);
1967 if (pmap_map
!= map
)
1968 vm_map_unlock(pmap_map
);
1970 #if !VM_FAULT_STATIC_CONFIG
1971 if (!vm_fault_interruptible
)
1972 interruptible
= THREAD_UNINT
;
1974 result
= PAGE_ASSERT_WAIT(m
, interruptible
);
1976 vm_object_unlock(cur_object
);
1978 if (result
== THREAD_WAITING
) {
1979 result
= thread_block(THREAD_CONTINUE_NULL
);
1981 counter(c_vm_fault_page_block_busy_kernel
++);
1983 if (result
== THREAD_AWAKENED
|| result
== THREAD_RESTART
)
1989 if (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private
1990 || m
->absent
|| (fault_type
& m
->page_lock
))) {
1993 * Unusual case. Give up.
1999 * Two cases of map in faults:
2000 * - At top level w/o copy object.
2001 * - Read fault anywhere.
2002 * --> must disallow write.
2005 if (object
== cur_object
&&
2006 object
->copy
== VM_OBJECT_NULL
)
2007 goto FastMapInFault
;
2009 if ((fault_type
& VM_PROT_WRITE
) == 0) {
2011 prot
&= ~VM_PROT_WRITE
;
2014 * Set up to map the page ...
2015 * mark the page busy, drop
2016 * locks and take a paging reference
2017 * on the object with the page.
2020 if (object
!= cur_object
) {
2021 vm_object_unlock(object
);
2022 object
= cur_object
;
2027 vm_object_paging_begin(object
);
2031 * Check a couple of global reasons to
2032 * be conservative about write access.
2033 * Then do the pmap_enter.
2035 #if !VM_FAULT_STATIC_CONFIG
2036 if (vm_fault_dirty_handling
2038 || db_watchpoint_list
2040 && (fault_type
& VM_PROT_WRITE
) == 0)
2041 prot
&= ~VM_PROT_WRITE
;
2042 #else /* STATIC_CONFIG */
2044 if (db_watchpoint_list
2045 && (fault_type
& VM_PROT_WRITE
) == 0)
2046 prot
&= ~VM_PROT_WRITE
;
2047 #endif /* MACH_KDB */
2048 #endif /* STATIC_CONFIG */
2049 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
2050 if ((m
->no_isync
== TRUE
) ||
2051 (cache_attr
!= VM_WIMG_DEFAULT
)) {
2052 pmap_sync_caches_phys(m
->phys_page
);
2053 m
->no_isync
= FALSE
;
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_page
,
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_page
);
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 vm_map_entry_t entry
;
2737 vm_offset_t ldelta
, hdelta
;
2740 * do a pmap block mapping from the physical address
2744 /* While we do not worry about execution protection in */
2745 /* general, certian pages may have instruction execution */
2746 /* disallowed. We will check here, and if not allowed */
2747 /* to execute, we return with a protection failure. */
2749 if((full_fault_type
& VM_PROT_EXECUTE
) &&
2750 (pmap_canExecute((ppnum_t
)
2751 (object
->shadow_offset
>> 12)) < 1)) {
2753 vm_map_verify_done(map
, &version
);
2755 vm_map_unlock(pmap_map
);
2756 vm_fault_cleanup(object
, top_page
);
2757 vm_object_deallocate(object
);
2758 kr
= KERN_PROTECTION_FAILURE
;
2762 if(pmap_map
!= map
) {
2763 vm_map_unlock(pmap_map
);
2765 if (original_map
!= map
) {
2766 vm_map_unlock_read(map
);
2767 vm_map_lock_read(original_map
);
2773 hdelta
= 0xFFFFF000;
2774 ldelta
= 0xFFFFF000;
2777 while(vm_map_lookup_entry(map
, laddr
, &entry
)) {
2778 if(ldelta
> (laddr
- entry
->vme_start
))
2779 ldelta
= laddr
- entry
->vme_start
;
2780 if(hdelta
> (entry
->vme_end
- laddr
))
2781 hdelta
= entry
->vme_end
- laddr
;
2782 if(entry
->is_sub_map
) {
2784 laddr
= (laddr
- entry
->vme_start
)
2786 vm_map_lock_read(entry
->object
.sub_map
);
2788 vm_map_unlock_read(map
);
2789 if(entry
->use_pmap
) {
2790 vm_map_unlock_read(pmap_map
);
2791 pmap_map
= entry
->object
.sub_map
;
2793 map
= entry
->object
.sub_map
;
2800 if(vm_map_lookup_entry(map
, laddr
, &entry
) &&
2801 (entry
->object
.vm_object
!= NULL
) &&
2802 (entry
->object
.vm_object
== object
)) {
2806 /* Set up a block mapped area */
2807 pmap_map_block(caller_pmap
,
2808 (addr64_t
)(caller_pmap_addr
- ldelta
),
2810 (entry
->object
.vm_object
->shadow_offset
))
2812 (laddr
- entry
->vme_start
)
2814 ldelta
+ hdelta
, prot
,
2815 (VM_WIMG_MASK
& (int)object
->wimg_bits
), 0);
2817 /* Set up a block mapped area */
2818 pmap_map_block(pmap_map
->pmap
,
2819 (addr64_t
)(vaddr
- ldelta
),
2821 (entry
->object
.vm_object
->shadow_offset
))
2823 (laddr
- entry
->vme_start
) - ldelta
)>>12,
2824 ldelta
+ hdelta
, prot
,
2825 (VM_WIMG_MASK
& (int)object
->wimg_bits
), 0);
2831 pmap_enter(caller_pmap
, caller_pmap_addr
,
2832 object
->shadow_offset
>>12, prot
, 0, TRUE
);
2834 pmap_enter(pmap
, vaddr
,
2835 object
->shadow_offset
>>12, prot
, 0, TRUE
);
2844 * If the page is not wired down and isn't already
2845 * on a pageout queue, then put it where the
2846 * pageout daemon can find it.
2848 if(m
!= VM_PAGE_NULL
) {
2849 vm_page_lock_queues();
2851 if (change_wiring
) {
2857 #if VM_FAULT_STATIC_CONFIG
2859 if (!m
->active
&& !m
->inactive
)
2860 vm_page_activate(m
);
2861 m
->reference
= TRUE
;
2864 else if (software_reference_bits
) {
2865 if (!m
->active
&& !m
->inactive
)
2866 vm_page_activate(m
);
2867 m
->reference
= TRUE
;
2869 vm_page_activate(m
);
2872 vm_page_unlock_queues();
2876 * Unlock everything, and return
2879 vm_map_verify_done(map
, &version
);
2881 vm_map_unlock(pmap_map
);
2882 if(m
!= VM_PAGE_NULL
) {
2883 PAGE_WAKEUP_DONE(m
);
2884 UNLOCK_AND_DEALLOCATE
;
2886 vm_fault_cleanup(object
, top_page
);
2887 vm_object_deallocate(object
);
2891 #undef UNLOCK_AND_DEALLOCATE
2895 if(write_startup_file
)
2896 tws_send_startup_info(current_task());
2898 thread_funnel_set( curflock
, TRUE
);
2901 thread_interrupt_level(interruptible_state
);
2903 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 0)) | DBG_FUNC_END
,
2905 type_of_fault
& 0xff,
2916 * Wire down a range of virtual addresses in a map.
2921 vm_map_entry_t entry
,
2923 vm_offset_t pmap_addr
)
2926 register vm_offset_t va
;
2927 register vm_offset_t end_addr
= entry
->vme_end
;
2928 register kern_return_t rc
;
2930 assert(entry
->in_transition
);
2932 if ((entry
->object
.vm_object
!= NULL
) &&
2933 !entry
->is_sub_map
&&
2934 entry
->object
.vm_object
->phys_contiguous
) {
2935 return KERN_SUCCESS
;
2939 * Inform the physical mapping system that the
2940 * range of addresses may not fault, so that
2941 * page tables and such can be locked down as well.
2944 pmap_pageable(pmap
, pmap_addr
,
2945 pmap_addr
+ (end_addr
- entry
->vme_start
), FALSE
);
2948 * We simulate a fault to get the page and enter it
2949 * in the physical map.
2952 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
2953 if ((rc
= vm_fault_wire_fast(
2954 map
, va
, entry
, pmap
,
2955 pmap_addr
+ (va
- entry
->vme_start
)
2956 )) != KERN_SUCCESS
) {
2957 rc
= vm_fault(map
, va
, VM_PROT_NONE
, TRUE
,
2958 (pmap
== kernel_pmap
) ?
2959 THREAD_UNINT
: THREAD_ABORTSAFE
,
2960 pmap
, pmap_addr
+ (va
- entry
->vme_start
));
2963 if (rc
!= KERN_SUCCESS
) {
2964 struct vm_map_entry tmp_entry
= *entry
;
2966 /* unwire wired pages */
2967 tmp_entry
.vme_end
= va
;
2968 vm_fault_unwire(map
,
2969 &tmp_entry
, FALSE
, pmap
, pmap_addr
);
2974 return KERN_SUCCESS
;
2980 * Unwire a range of virtual addresses in a map.
2985 vm_map_entry_t entry
,
2986 boolean_t deallocate
,
2988 vm_offset_t pmap_addr
)
2990 register vm_offset_t va
;
2991 register vm_offset_t end_addr
= entry
->vme_end
;
2994 object
= (entry
->is_sub_map
)
2995 ? VM_OBJECT_NULL
: entry
->object
.vm_object
;
2998 * Since the pages are wired down, we must be able to
2999 * get their mappings from the physical map system.
3002 for (va
= entry
->vme_start
; va
< end_addr
; va
+= PAGE_SIZE
) {
3003 pmap_change_wiring(pmap
,
3004 pmap_addr
+ (va
- entry
->vme_start
), FALSE
);
3006 if (object
== VM_OBJECT_NULL
) {
3007 (void) vm_fault(map
, va
, VM_PROT_NONE
,
3008 TRUE
, THREAD_UNINT
, pmap
, pmap_addr
);
3009 } else if (object
->phys_contiguous
) {
3013 vm_page_t result_page
;
3015 vm_object_t result_object
;
3016 vm_fault_return_t result
;
3019 prot
= VM_PROT_NONE
;
3021 vm_object_lock(object
);
3022 vm_object_paging_begin(object
);
3024 "vm_fault_unwire -> vm_fault_page\n",
3026 result
= vm_fault_page(object
,
3028 (va
- entry
->vme_start
),
3034 - entry
->vme_start
),
3040 0, map
->no_zero_fill
,
3042 } while (result
== VM_FAULT_RETRY
);
3044 if (result
!= VM_FAULT_SUCCESS
)
3045 panic("vm_fault_unwire: failure");
3047 result_object
= result_page
->object
;
3049 assert(!result_page
->fictitious
);
3050 pmap_page_protect(result_page
->phys_page
,
3052 VM_PAGE_FREE(result_page
);
3054 vm_page_lock_queues();
3055 vm_page_unwire(result_page
);
3056 vm_page_unlock_queues();
3057 PAGE_WAKEUP_DONE(result_page
);
3060 vm_fault_cleanup(result_object
, top_page
);
3065 * Inform the physical mapping system that the range
3066 * of addresses may fault, so that page tables and
3067 * such may be unwired themselves.
3070 pmap_pageable(pmap
, pmap_addr
,
3071 pmap_addr
+ (end_addr
- entry
->vme_start
), TRUE
);
3076 * vm_fault_wire_fast:
3078 * Handle common case of a wire down page fault at the given address.
3079 * If successful, the page is inserted into the associated physical map.
3080 * The map entry is passed in to avoid the overhead of a map lookup.
3082 * NOTE: the given address should be truncated to the
3083 * proper page address.
3085 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
3086 * a standard error specifying why the fault is fatal is returned.
3088 * The map in question must be referenced, and remains so.
3089 * Caller has a read lock on the map.
3091 * This is a stripped version of vm_fault() for wiring pages. Anything
3092 * other than the common case will return KERN_FAILURE, and the caller
3093 * is expected to call vm_fault().
3099 vm_map_entry_t entry
,
3101 vm_offset_t pmap_addr
)
3104 vm_object_offset_t offset
;
3105 register vm_page_t m
;
3107 thread_act_t thr_act
;
3108 unsigned int cache_attr
;
3112 if((thr_act
=current_act()) && (thr_act
->task
!= TASK_NULL
))
3113 thr_act
->task
->faults
++;
3120 #define RELEASE_PAGE(m) { \
3121 PAGE_WAKEUP_DONE(m); \
3122 vm_page_lock_queues(); \
3123 vm_page_unwire(m); \
3124 vm_page_unlock_queues(); \
3128 #undef UNLOCK_THINGS
3129 #define UNLOCK_THINGS { \
3130 object->paging_in_progress--; \
3131 vm_object_unlock(object); \
3134 #undef UNLOCK_AND_DEALLOCATE
3135 #define UNLOCK_AND_DEALLOCATE { \
3137 vm_object_deallocate(object); \
3140 * Give up and have caller do things the hard way.
3144 UNLOCK_AND_DEALLOCATE; \
3145 return(KERN_FAILURE); \
3150 * If this entry is not directly to a vm_object, bail out.
3152 if (entry
->is_sub_map
)
3153 return(KERN_FAILURE
);
3156 * Find the backing store object and offset into it.
3159 object
= entry
->object
.vm_object
;
3160 offset
= (va
- entry
->vme_start
) + entry
->offset
;
3161 prot
= entry
->protection
;
3164 * Make a reference to this object to prevent its
3165 * disposal while we are messing with it.
3168 vm_object_lock(object
);
3169 assert(object
->ref_count
> 0);
3170 object
->ref_count
++;
3171 vm_object_res_reference(object
);
3172 object
->paging_in_progress
++;
3175 * INVARIANTS (through entire routine):
3177 * 1) At all times, we must either have the object
3178 * lock or a busy page in some object to prevent
3179 * some other thread from trying to bring in
3182 * 2) Once we have a busy page, we must remove it from
3183 * the pageout queues, so that the pageout daemon
3184 * will not grab it away.
3189 * Look for page in top-level object. If it's not there or
3190 * there's something going on, give up.
3192 m
= vm_page_lookup(object
, offset
);
3193 if ((m
== VM_PAGE_NULL
) || (m
->busy
) ||
3194 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
||
3195 prot
& m
->page_lock
))) {
3201 * Wire the page down now. All bail outs beyond this
3202 * point must unwire the page.
3205 vm_page_lock_queues();
3207 vm_page_unlock_queues();
3210 * Mark page busy for other threads.
3217 * Give up if the page is being written and there's a copy object
3219 if ((object
->copy
!= VM_OBJECT_NULL
) && (prot
& VM_PROT_WRITE
)) {
3225 * Put this page into the physical map.
3226 * We have to unlock the object because pmap_enter
3227 * may cause other faults.
3229 if (m
->no_isync
== TRUE
) {
3230 pmap_sync_caches_phys(m
->phys_page
);
3232 m
->no_isync
= FALSE
;
3235 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
3237 PMAP_ENTER(pmap
, pmap_addr
, m
, prot
, cache_attr
, TRUE
);
3240 * Unlock everything, and return
3243 PAGE_WAKEUP_DONE(m
);
3244 UNLOCK_AND_DEALLOCATE
;
3246 return(KERN_SUCCESS
);
3251 * Routine: vm_fault_copy_cleanup
3253 * Release a page used by vm_fault_copy.
3257 vm_fault_copy_cleanup(
3261 vm_object_t object
= page
->object
;
3263 vm_object_lock(object
);
3264 PAGE_WAKEUP_DONE(page
);
3265 vm_page_lock_queues();
3266 if (!page
->active
&& !page
->inactive
)
3267 vm_page_activate(page
);
3268 vm_page_unlock_queues();
3269 vm_fault_cleanup(object
, top_page
);
3273 vm_fault_copy_dst_cleanup(
3278 if (page
!= VM_PAGE_NULL
) {
3279 object
= page
->object
;
3280 vm_object_lock(object
);
3281 vm_page_lock_queues();
3282 vm_page_unwire(page
);
3283 vm_page_unlock_queues();
3284 vm_object_paging_end(object
);
3285 vm_object_unlock(object
);
3290 * Routine: vm_fault_copy
3293 * Copy pages from one virtual memory object to another --
3294 * neither the source nor destination pages need be resident.
3296 * Before actually copying a page, the version associated with
3297 * the destination address map wil be verified.
3299 * In/out conditions:
3300 * The caller must hold a reference, but not a lock, to
3301 * each of the source and destination objects and to the
3305 * Returns KERN_SUCCESS if no errors were encountered in
3306 * reading or writing the data. Returns KERN_INTERRUPTED if
3307 * the operation was interrupted (only possible if the
3308 * "interruptible" argument is asserted). Other return values
3309 * indicate a permanent error in copying the data.
3311 * The actual amount of data copied will be returned in the
3312 * "copy_size" argument. In the event that the destination map
3313 * verification failed, this amount may be less than the amount
3318 vm_object_t src_object
,
3319 vm_object_offset_t src_offset
,
3320 vm_size_t
*src_size
, /* INOUT */
3321 vm_object_t dst_object
,
3322 vm_object_offset_t dst_offset
,
3324 vm_map_version_t
*dst_version
,
3327 vm_page_t result_page
;
3330 vm_page_t src_top_page
;
3334 vm_page_t dst_top_page
;
3337 vm_size_t amount_left
;
3338 vm_object_t old_copy_object
;
3339 kern_return_t error
= 0;
3341 vm_size_t part_size
;
3344 * In order not to confuse the clustered pageins, align
3345 * the different offsets on a page boundary.
3347 vm_object_offset_t src_lo_offset
= trunc_page_64(src_offset
);
3348 vm_object_offset_t dst_lo_offset
= trunc_page_64(dst_offset
);
3349 vm_object_offset_t src_hi_offset
= round_page_64(src_offset
+ *src_size
);
3350 vm_object_offset_t dst_hi_offset
= round_page_64(dst_offset
+ *src_size
);
3354 *src_size -= amount_left; \
3358 amount_left
= *src_size
;
3359 do { /* while (amount_left > 0) */
3361 * There may be a deadlock if both source and destination
3362 * pages are the same. To avoid this deadlock, the copy must
3363 * start by getting the destination page in order to apply
3364 * COW semantics if any.
3367 RetryDestinationFault
: ;
3369 dst_prot
= VM_PROT_WRITE
|VM_PROT_READ
;
3371 vm_object_lock(dst_object
);
3372 vm_object_paging_begin(dst_object
);
3374 XPR(XPR_VM_FAULT
,"vm_fault_copy -> vm_fault_page\n",0,0,0,0,0);
3375 switch (vm_fault_page(dst_object
,
3376 trunc_page_64(dst_offset
),
3377 VM_PROT_WRITE
|VM_PROT_READ
,
3382 VM_BEHAVIOR_SEQUENTIAL
,
3388 dst_map
->no_zero_fill
,
3390 case VM_FAULT_SUCCESS
:
3392 case VM_FAULT_RETRY
:
3393 goto RetryDestinationFault
;
3394 case VM_FAULT_MEMORY_SHORTAGE
:
3395 if (vm_page_wait(interruptible
))
3396 goto RetryDestinationFault
;
3398 case VM_FAULT_INTERRUPTED
:
3399 RETURN(MACH_SEND_INTERRUPTED
);
3400 case VM_FAULT_FICTITIOUS_SHORTAGE
:
3401 vm_page_more_fictitious();
3402 goto RetryDestinationFault
;
3403 case VM_FAULT_MEMORY_ERROR
:
3407 return(KERN_MEMORY_ERROR
);
3409 assert ((dst_prot
& VM_PROT_WRITE
) != VM_PROT_NONE
);
3411 old_copy_object
= dst_page
->object
->copy
;
3414 * There exists the possiblity that the source and
3415 * destination page are the same. But we can't
3416 * easily determine that now. If they are the
3417 * same, the call to vm_fault_page() for the
3418 * destination page will deadlock. To prevent this we
3419 * wire the page so we can drop busy without having
3420 * the page daemon steal the page. We clean up the
3421 * top page but keep the paging reference on the object
3422 * holding the dest page so it doesn't go away.
3425 vm_page_lock_queues();
3426 vm_page_wire(dst_page
);
3427 vm_page_unlock_queues();
3428 PAGE_WAKEUP_DONE(dst_page
);
3429 vm_object_unlock(dst_page
->object
);
3431 if (dst_top_page
!= VM_PAGE_NULL
) {
3432 vm_object_lock(dst_object
);
3433 VM_PAGE_FREE(dst_top_page
);
3434 vm_object_paging_end(dst_object
);
3435 vm_object_unlock(dst_object
);
3440 if (src_object
== VM_OBJECT_NULL
) {
3442 * No source object. We will just
3443 * zero-fill the page in dst_object.
3445 src_page
= VM_PAGE_NULL
;
3446 result_page
= VM_PAGE_NULL
;
3448 vm_object_lock(src_object
);
3449 src_page
= vm_page_lookup(src_object
,
3450 trunc_page_64(src_offset
));
3451 if (src_page
== dst_page
) {
3452 src_prot
= dst_prot
;
3453 result_page
= VM_PAGE_NULL
;
3455 src_prot
= VM_PROT_READ
;
3456 vm_object_paging_begin(src_object
);
3459 "vm_fault_copy(2) -> vm_fault_page\n",
3461 switch (vm_fault_page(src_object
,
3462 trunc_page_64(src_offset
),
3468 VM_BEHAVIOR_SEQUENTIAL
,
3477 case VM_FAULT_SUCCESS
:
3479 case VM_FAULT_RETRY
:
3480 goto RetrySourceFault
;
3481 case VM_FAULT_MEMORY_SHORTAGE
:
3482 if (vm_page_wait(interruptible
))
3483 goto RetrySourceFault
;
3485 case VM_FAULT_INTERRUPTED
:
3486 vm_fault_copy_dst_cleanup(dst_page
);
3487 RETURN(MACH_SEND_INTERRUPTED
);
3488 case VM_FAULT_FICTITIOUS_SHORTAGE
:
3489 vm_page_more_fictitious();
3490 goto RetrySourceFault
;
3491 case VM_FAULT_MEMORY_ERROR
:
3492 vm_fault_copy_dst_cleanup(dst_page
);
3496 return(KERN_MEMORY_ERROR
);
3500 assert((src_top_page
== VM_PAGE_NULL
) ==
3501 (result_page
->object
== src_object
));
3503 assert ((src_prot
& VM_PROT_READ
) != VM_PROT_NONE
);
3504 vm_object_unlock(result_page
->object
);
3507 if (!vm_map_verify(dst_map
, dst_version
)) {
3508 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3509 vm_fault_copy_cleanup(result_page
, src_top_page
);
3510 vm_fault_copy_dst_cleanup(dst_page
);
3514 vm_object_lock(dst_page
->object
);
3516 if (dst_page
->object
->copy
!= old_copy_object
) {
3517 vm_object_unlock(dst_page
->object
);
3518 vm_map_verify_done(dst_map
, dst_version
);
3519 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3520 vm_fault_copy_cleanup(result_page
, src_top_page
);
3521 vm_fault_copy_dst_cleanup(dst_page
);
3524 vm_object_unlock(dst_page
->object
);
3527 * Copy the page, and note that it is dirty
3531 if (!page_aligned(src_offset
) ||
3532 !page_aligned(dst_offset
) ||
3533 !page_aligned(amount_left
)) {
3535 vm_object_offset_t src_po
,
3538 src_po
= src_offset
- trunc_page_64(src_offset
);
3539 dst_po
= dst_offset
- trunc_page_64(dst_offset
);
3541 if (dst_po
> src_po
) {
3542 part_size
= PAGE_SIZE
- dst_po
;
3544 part_size
= PAGE_SIZE
- src_po
;
3546 if (part_size
> (amount_left
)){
3547 part_size
= amount_left
;
3550 if (result_page
== VM_PAGE_NULL
) {
3551 vm_page_part_zero_fill(dst_page
,
3554 vm_page_part_copy(result_page
, src_po
,
3555 dst_page
, dst_po
, part_size
);
3556 if(!dst_page
->dirty
){
3557 vm_object_lock(dst_object
);
3558 dst_page
->dirty
= TRUE
;
3559 vm_object_unlock(dst_page
->object
);
3564 part_size
= PAGE_SIZE
;
3566 if (result_page
== VM_PAGE_NULL
)
3567 vm_page_zero_fill(dst_page
);
3569 vm_page_copy(result_page
, dst_page
);
3570 if(!dst_page
->dirty
){
3571 vm_object_lock(dst_object
);
3572 dst_page
->dirty
= TRUE
;
3573 vm_object_unlock(dst_page
->object
);
3580 * Unlock everything, and return
3583 vm_map_verify_done(dst_map
, dst_version
);
3585 if (result_page
!= VM_PAGE_NULL
&& src_page
!= dst_page
)
3586 vm_fault_copy_cleanup(result_page
, src_top_page
);
3587 vm_fault_copy_dst_cleanup(dst_page
);
3589 amount_left
-= part_size
;
3590 src_offset
+= part_size
;
3591 dst_offset
+= part_size
;
3592 } while (amount_left
> 0);
3594 RETURN(KERN_SUCCESS
);
3603 * Routine: vm_fault_page_overwrite
3606 * A form of vm_fault_page that assumes that the
3607 * resulting page will be overwritten in its entirety,
3608 * making it unnecessary to obtain the correct *contents*
3612 * XXX Untested. Also unused. Eventually, this technology
3613 * could be used in vm_fault_copy() to advantage.
3616 vm_fault_page_overwrite(
3618 vm_object_t dst_object
,
3619 vm_object_offset_t dst_offset
,
3620 vm_page_t
*result_page
) /* OUT */
3624 kern_return_t wait_result
;
3626 #define interruptible THREAD_UNINT /* XXX */
3630 * Look for a page at this offset
3633 while ((dst_page
= vm_page_lookup(dst_object
, dst_offset
))
3636 * No page, no problem... just allocate one.
3639 dst_page
= vm_page_alloc(dst_object
, dst_offset
);
3640 if (dst_page
== VM_PAGE_NULL
) {
3641 vm_object_unlock(dst_object
);
3643 vm_object_lock(dst_object
);
3648 * Pretend that the memory manager
3649 * write-protected the page.
3651 * Note that we will be asking for write
3652 * permission without asking for the data
3656 dst_page
->overwriting
= TRUE
;
3657 dst_page
->page_lock
= VM_PROT_WRITE
;
3658 dst_page
->absent
= TRUE
;
3659 dst_page
->unusual
= TRUE
;
3660 dst_object
->absent_count
++;
3665 * When we bail out, we might have to throw
3666 * away the page created here.
3669 #define DISCARD_PAGE \
3671 vm_object_lock(dst_object); \
3672 dst_page = vm_page_lookup(dst_object, dst_offset); \
3673 if ((dst_page != VM_PAGE_NULL) && dst_page->overwriting) \
3674 VM_PAGE_FREE(dst_page); \
3675 vm_object_unlock(dst_object); \
3680 * If the page is write-protected...
3683 if (dst_page
->page_lock
& VM_PROT_WRITE
) {
3685 * ... and an unlock request hasn't been sent
3688 if ( ! (dst_page
->unlock_request
& VM_PROT_WRITE
)) {
3693 * ... then send one now.
3696 if (!dst_object
->pager_ready
) {
3697 wait_result
= vm_object_assert_wait(dst_object
,
3698 VM_OBJECT_EVENT_PAGER_READY
,
3700 vm_object_unlock(dst_object
);
3701 if (wait_result
== THREAD_WAITING
)
3702 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
3703 if (wait_result
!= THREAD_AWAKENED
) {
3705 return(VM_FAULT_INTERRUPTED
);
3710 u
= dst_page
->unlock_request
|= VM_PROT_WRITE
;
3711 vm_object_unlock(dst_object
);
3713 if ((rc
= memory_object_data_unlock(
3715 dst_offset
+ dst_object
->paging_offset
,
3717 u
)) != KERN_SUCCESS
) {
3719 printf("vm_object_overwrite: memory_object_data_unlock failed\n");
3721 return((rc
== MACH_SEND_INTERRUPTED
) ?
3722 VM_FAULT_INTERRUPTED
:
3723 VM_FAULT_MEMORY_ERROR
);
3725 vm_object_lock(dst_object
);
3729 /* ... fall through to wait below */
3732 * If the page isn't being used for other
3733 * purposes, then we're done.
3735 if ( ! (dst_page
->busy
|| dst_page
->absent
||
3736 dst_page
->error
|| dst_page
->restart
) )
3740 wait_result
= PAGE_ASSERT_WAIT(dst_page
, interruptible
);
3741 vm_object_unlock(dst_object
);
3742 if (wait_result
== THREAD_WAITING
)
3743 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
3744 if (wait_result
!= THREAD_AWAKENED
) {
3746 return(VM_FAULT_INTERRUPTED
);
3750 *result_page
= dst_page
;
3751 return(VM_FAULT_SUCCESS
);
3753 #undef interruptible
3759 #if VM_FAULT_CLASSIFY
3761 * Temporary statistics gathering support.
3765 * Statistics arrays:
3767 #define VM_FAULT_TYPES_MAX 5
3768 #define VM_FAULT_LEVEL_MAX 8
3770 int vm_fault_stats
[VM_FAULT_TYPES_MAX
][VM_FAULT_LEVEL_MAX
];
3772 #define VM_FAULT_TYPE_ZERO_FILL 0
3773 #define VM_FAULT_TYPE_MAP_IN 1
3774 #define VM_FAULT_TYPE_PAGER 2
3775 #define VM_FAULT_TYPE_COPY 3
3776 #define VM_FAULT_TYPE_OTHER 4
3780 vm_fault_classify(vm_object_t object
,
3781 vm_object_offset_t offset
,
3782 vm_prot_t fault_type
)
3784 int type
, level
= 0;
3788 m
= vm_page_lookup(object
, offset
);
3789 if (m
!= VM_PAGE_NULL
) {
3790 if (m
->busy
|| m
->error
|| m
->restart
|| m
->absent
||
3791 fault_type
& m
->page_lock
) {
3792 type
= VM_FAULT_TYPE_OTHER
;
3795 if (((fault_type
& VM_PROT_WRITE
) == 0) ||
3796 ((level
== 0) && object
->copy
== VM_OBJECT_NULL
)) {
3797 type
= VM_FAULT_TYPE_MAP_IN
;
3800 type
= VM_FAULT_TYPE_COPY
;
3804 if (object
->pager_created
) {
3805 type
= VM_FAULT_TYPE_PAGER
;
3808 if (object
->shadow
== VM_OBJECT_NULL
) {
3809 type
= VM_FAULT_TYPE_ZERO_FILL
;
3813 offset
+= object
->shadow_offset
;
3814 object
= object
->shadow
;
3820 if (level
> VM_FAULT_LEVEL_MAX
)
3821 level
= VM_FAULT_LEVEL_MAX
;
3823 vm_fault_stats
[type
][level
] += 1;
3828 /* cleanup routine to call from debugger */
3831 vm_fault_classify_init(void)
3835 for (type
= 0; type
< VM_FAULT_TYPES_MAX
; type
++) {
3836 for (level
= 0; level
< VM_FAULT_LEVEL_MAX
; level
++) {
3837 vm_fault_stats
[type
][level
] = 0;
3843 #endif /* VM_FAULT_CLASSIFY */