2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: vm/vm_pageout.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * The proverbial page-out daemon.
69 #include <mach_pagemap.h>
70 #include <mach_cluster_stats.h>
72 #include <advisory_pageout.h>
74 #include <mach/mach_types.h>
75 #include <mach/memory_object.h>
76 #include <mach/memory_object_default.h>
77 #include <mach/memory_object_control_server.h>
78 #include <mach/mach_host_server.h>
80 #include <mach/vm_map.h>
81 #include <mach/vm_param.h>
82 #include <mach/vm_statistics.h>
85 #include <kern/kern_types.h>
86 #include <kern/counters.h>
87 #include <kern/host_statistics.h>
88 #include <kern/machine.h>
89 #include <kern/misc_protos.h>
90 #include <kern/sched.h>
91 #include <kern/thread.h>
93 #include <kern/kalloc.h>
95 #include <machine/vm_tuning.h>
96 #include <machine/commpage.h>
98 #include <sys/kern_memorystatus.h>
101 #include <vm/vm_fault.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_page.h>
105 #include <vm/vm_pageout.h>
106 #include <vm/vm_protos.h> /* must be last */
107 #include <vm/memory_object.h>
108 #include <vm/vm_purgeable_internal.h>
109 #include <vm/vm_shared_region.h>
113 #include <../bsd/crypto/aes/aes.h>
114 extern u_int32_t
random(void); /* from <libkern/libkern.h> */
117 #include <libkern/OSDebug.h>
120 extern void consider_pressure_events(void);
122 #ifndef VM_PAGEOUT_BURST_ACTIVE_THROTTLE /* maximum iterations of the active queue to move pages to inactive */
123 #define VM_PAGEOUT_BURST_ACTIVE_THROTTLE 100
126 #ifndef VM_PAGEOUT_BURST_INACTIVE_THROTTLE /* maximum iterations of the inactive queue w/o stealing/cleaning a page */
127 #ifdef CONFIG_EMBEDDED
128 #define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 1024
130 #define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 4096
134 #ifndef VM_PAGEOUT_DEADLOCK_RELIEF
135 #define VM_PAGEOUT_DEADLOCK_RELIEF 100 /* number of pages to move to break deadlock */
138 #ifndef VM_PAGEOUT_INACTIVE_RELIEF
139 #define VM_PAGEOUT_INACTIVE_RELIEF 50 /* minimum number of pages to move to the inactive q */
142 #ifndef VM_PAGE_LAUNDRY_MAX
143 #define VM_PAGE_LAUNDRY_MAX 128UL /* maximum pageouts on a given pageout queue */
144 #endif /* VM_PAGEOUT_LAUNDRY_MAX */
146 #ifndef VM_PAGEOUT_BURST_WAIT
147 #define VM_PAGEOUT_BURST_WAIT 30 /* milliseconds */
148 #endif /* VM_PAGEOUT_BURST_WAIT */
150 #ifndef VM_PAGEOUT_EMPTY_WAIT
151 #define VM_PAGEOUT_EMPTY_WAIT 200 /* milliseconds */
152 #endif /* VM_PAGEOUT_EMPTY_WAIT */
154 #ifndef VM_PAGEOUT_DEADLOCK_WAIT
155 #define VM_PAGEOUT_DEADLOCK_WAIT 300 /* milliseconds */
156 #endif /* VM_PAGEOUT_DEADLOCK_WAIT */
158 #ifndef VM_PAGEOUT_IDLE_WAIT
159 #define VM_PAGEOUT_IDLE_WAIT 10 /* milliseconds */
160 #endif /* VM_PAGEOUT_IDLE_WAIT */
162 unsigned int vm_page_speculative_q_age_ms
= VM_PAGE_SPECULATIVE_Q_AGE_MS
;
163 unsigned int vm_page_speculative_percentage
= 5;
165 #ifndef VM_PAGE_SPECULATIVE_TARGET
166 #define VM_PAGE_SPECULATIVE_TARGET(total) ((total) * 1 / (100 / vm_page_speculative_percentage))
167 #endif /* VM_PAGE_SPECULATIVE_TARGET */
170 #ifndef VM_PAGE_INACTIVE_HEALTHY_LIMIT
171 #define VM_PAGE_INACTIVE_HEALTHY_LIMIT(total) ((total) * 1 / 200)
172 #endif /* VM_PAGE_INACTIVE_HEALTHY_LIMIT */
176 * To obtain a reasonable LRU approximation, the inactive queue
177 * needs to be large enough to give pages on it a chance to be
178 * referenced a second time. This macro defines the fraction
179 * of active+inactive pages that should be inactive.
180 * The pageout daemon uses it to update vm_page_inactive_target.
182 * If vm_page_free_count falls below vm_page_free_target and
183 * vm_page_inactive_count is below vm_page_inactive_target,
184 * then the pageout daemon starts running.
187 #ifndef VM_PAGE_INACTIVE_TARGET
188 #define VM_PAGE_INACTIVE_TARGET(avail) ((avail) * 1 / 3)
189 #endif /* VM_PAGE_INACTIVE_TARGET */
192 * Once the pageout daemon starts running, it keeps going
193 * until vm_page_free_count meets or exceeds vm_page_free_target.
196 #ifndef VM_PAGE_FREE_TARGET
197 #ifdef CONFIG_EMBEDDED
198 #define VM_PAGE_FREE_TARGET(free) (15 + (free) / 100)
200 #define VM_PAGE_FREE_TARGET(free) (15 + (free) / 80)
202 #endif /* VM_PAGE_FREE_TARGET */
205 * The pageout daemon always starts running once vm_page_free_count
206 * falls below vm_page_free_min.
209 #ifndef VM_PAGE_FREE_MIN
210 #ifdef CONFIG_EMBEDDED
211 #define VM_PAGE_FREE_MIN(free) (10 + (free) / 200)
213 #define VM_PAGE_FREE_MIN(free) (10 + (free) / 100)
215 #endif /* VM_PAGE_FREE_MIN */
217 #define VM_PAGE_FREE_RESERVED_LIMIT 100
218 #define VM_PAGE_FREE_MIN_LIMIT 1500
219 #define VM_PAGE_FREE_TARGET_LIMIT 2000
223 * When vm_page_free_count falls below vm_page_free_reserved,
224 * only vm-privileged threads can allocate pages. vm-privilege
225 * allows the pageout daemon and default pager (and any other
226 * associated threads needed for default pageout) to continue
227 * operation by dipping into the reserved pool of pages.
230 #ifndef VM_PAGE_FREE_RESERVED
231 #define VM_PAGE_FREE_RESERVED(n) \
232 ((unsigned) (6 * VM_PAGE_LAUNDRY_MAX) + (n))
233 #endif /* VM_PAGE_FREE_RESERVED */
236 * When we dequeue pages from the inactive list, they are
237 * reactivated (ie, put back on the active queue) if referenced.
238 * However, it is possible to starve the free list if other
239 * processors are referencing pages faster than we can turn off
240 * the referenced bit. So we limit the number of reactivations
241 * we will make per call of vm_pageout_scan().
243 #define VM_PAGE_REACTIVATE_LIMIT_MAX 20000
244 #ifndef VM_PAGE_REACTIVATE_LIMIT
245 #ifdef CONFIG_EMBEDDED
246 #define VM_PAGE_REACTIVATE_LIMIT(avail) (VM_PAGE_INACTIVE_TARGET(avail) / 2)
248 #define VM_PAGE_REACTIVATE_LIMIT(avail) (MAX((avail) * 1 / 20,VM_PAGE_REACTIVATE_LIMIT_MAX))
250 #endif /* VM_PAGE_REACTIVATE_LIMIT */
251 #define VM_PAGEOUT_INACTIVE_FORCE_RECLAIM 100
255 * Exported variable used to broadcast the activation of the pageout scan
256 * Working Set uses this to throttle its use of pmap removes. In this
257 * way, code which runs within memory in an uncontested context does
258 * not keep encountering soft faults.
261 unsigned int vm_pageout_scan_event_counter
= 0;
264 * Forward declarations for internal routines.
267 static void vm_pageout_garbage_collect(int);
268 static void vm_pageout_iothread_continue(struct vm_pageout_queue
*);
269 static void vm_pageout_iothread_external(void);
270 static void vm_pageout_iothread_internal(void);
272 extern void vm_pageout_continue(void);
273 extern void vm_pageout_scan(void);
275 static thread_t vm_pageout_external_iothread
= THREAD_NULL
;
276 static thread_t vm_pageout_internal_iothread
= THREAD_NULL
;
278 unsigned int vm_pageout_reserved_internal
= 0;
279 unsigned int vm_pageout_reserved_really
= 0;
281 unsigned int vm_pageout_idle_wait
= 0; /* milliseconds */
282 unsigned int vm_pageout_empty_wait
= 0; /* milliseconds */
283 unsigned int vm_pageout_burst_wait
= 0; /* milliseconds */
284 unsigned int vm_pageout_deadlock_wait
= 0; /* milliseconds */
285 unsigned int vm_pageout_deadlock_relief
= 0;
286 unsigned int vm_pageout_inactive_relief
= 0;
287 unsigned int vm_pageout_burst_active_throttle
= 0;
288 unsigned int vm_pageout_burst_inactive_throttle
= 0;
290 int vm_upl_wait_for_pages
= 0;
293 * Protection against zero fill flushing live working sets derived
294 * from existing backing store and files
296 unsigned int vm_accellerate_zf_pageout_trigger
= 400;
297 unsigned int zf_queue_min_count
= 100;
298 unsigned int vm_zf_queue_count
= 0;
300 uint64_t vm_zf_count
__attribute__((aligned(8))) = 0;
303 * These variables record the pageout daemon's actions:
304 * how many pages it looks at and what happens to those pages.
305 * No locking needed because only one thread modifies the variables.
308 unsigned int vm_pageout_active
= 0; /* debugging */
309 unsigned int vm_pageout_active_busy
= 0; /* debugging */
310 unsigned int vm_pageout_inactive
= 0; /* debugging */
311 unsigned int vm_pageout_inactive_throttled
= 0; /* debugging */
312 unsigned int vm_pageout_inactive_forced
= 0; /* debugging */
313 unsigned int vm_pageout_inactive_nolock
= 0; /* debugging */
314 unsigned int vm_pageout_inactive_avoid
= 0; /* debugging */
315 unsigned int vm_pageout_inactive_busy
= 0; /* debugging */
316 unsigned int vm_pageout_inactive_error
= 0; /* debugging */
317 unsigned int vm_pageout_inactive_absent
= 0; /* debugging */
318 unsigned int vm_pageout_inactive_notalive
= 0; /* debugging */
319 unsigned int vm_pageout_inactive_used
= 0; /* debugging */
320 unsigned int vm_pageout_cache_evicted
= 0; /* debugging */
321 unsigned int vm_pageout_inactive_clean
= 0; /* debugging */
322 unsigned int vm_pageout_speculative_clean
= 0; /* debugging */
323 unsigned int vm_pageout_inactive_dirty_internal
= 0; /* debugging */
324 unsigned int vm_pageout_inactive_dirty_external
= 0; /* debugging */
325 unsigned int vm_pageout_inactive_deactivated
= 0; /* debugging */
326 unsigned int vm_pageout_inactive_zf
= 0; /* debugging */
327 unsigned int vm_pageout_dirty_no_pager
= 0; /* debugging */
328 unsigned int vm_pageout_purged_objects
= 0; /* debugging */
329 unsigned int vm_stat_discard
= 0; /* debugging */
330 unsigned int vm_stat_discard_sent
= 0; /* debugging */
331 unsigned int vm_stat_discard_failure
= 0; /* debugging */
332 unsigned int vm_stat_discard_throttle
= 0; /* debugging */
333 unsigned int vm_pageout_reactivation_limit_exceeded
= 0; /* debugging */
334 unsigned int vm_pageout_catch_ups
= 0; /* debugging */
335 unsigned int vm_pageout_inactive_force_reclaim
= 0; /* debugging */
337 unsigned int vm_pageout_scan_reclaimed_throttled
= 0;
338 unsigned int vm_pageout_scan_active_throttled
= 0;
339 unsigned int vm_pageout_scan_inactive_throttled_internal
= 0;
340 unsigned int vm_pageout_scan_inactive_throttled_external
= 0;
341 unsigned int vm_pageout_scan_throttle
= 0; /* debugging */
342 unsigned int vm_pageout_scan_throttle_aborted
= 0; /* debugging */
343 unsigned int vm_pageout_scan_burst_throttle
= 0; /* debugging */
344 unsigned int vm_pageout_scan_empty_throttle
= 0; /* debugging */
345 unsigned int vm_pageout_scan_deadlock_detected
= 0; /* debugging */
346 unsigned int vm_pageout_scan_active_throttle_success
= 0; /* debugging */
347 unsigned int vm_pageout_scan_inactive_throttle_success
= 0; /* debugging */
348 unsigned int vm_pageout_inactive_external_forced_reactivate_count
= 0; /* debugging */
349 unsigned int vm_page_speculative_count_drifts
= 0;
350 unsigned int vm_page_speculative_count_drift_max
= 0;
353 * Backing store throttle when BS is exhausted
355 unsigned int vm_backing_store_low
= 0;
357 unsigned int vm_pageout_out_of_line
= 0;
358 unsigned int vm_pageout_in_place
= 0;
360 unsigned int vm_page_steal_pageout_page
= 0;
364 * counters and statistics...
366 unsigned long vm_page_decrypt_counter
= 0;
367 unsigned long vm_page_decrypt_for_upl_counter
= 0;
368 unsigned long vm_page_encrypt_counter
= 0;
369 unsigned long vm_page_encrypt_abort_counter
= 0;
370 unsigned long vm_page_encrypt_already_encrypted_counter
= 0;
371 boolean_t vm_pages_encrypted
= FALSE
; /* are there encrypted pages ? */
373 struct vm_pageout_queue vm_pageout_queue_internal
;
374 struct vm_pageout_queue vm_pageout_queue_external
;
376 unsigned int vm_page_speculative_target
= 0;
378 vm_object_t vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
380 boolean_t (* volatile consider_buffer_cache_collect
)(int) = NULL
;
382 #if DEVELOPMENT || DEBUG
383 unsigned long vm_cs_validated_resets
= 0;
386 int vm_debug_events
= 0;
390 * Routine: vm_backing_store_disable
392 * Suspend non-privileged threads wishing to extend
393 * backing store when we are low on backing store
394 * (Synchronized by caller)
397 vm_backing_store_disable(
401 vm_backing_store_low
= 1;
403 if(vm_backing_store_low
) {
404 vm_backing_store_low
= 0;
405 thread_wakeup((event_t
) &vm_backing_store_low
);
411 #if MACH_CLUSTER_STATS
412 unsigned long vm_pageout_cluster_dirtied
= 0;
413 unsigned long vm_pageout_cluster_cleaned
= 0;
414 unsigned long vm_pageout_cluster_collisions
= 0;
415 unsigned long vm_pageout_cluster_clusters
= 0;
416 unsigned long vm_pageout_cluster_conversions
= 0;
417 unsigned long vm_pageout_target_collisions
= 0;
418 unsigned long vm_pageout_target_page_dirtied
= 0;
419 unsigned long vm_pageout_target_page_freed
= 0;
420 #define CLUSTER_STAT(clause) clause
421 #else /* MACH_CLUSTER_STATS */
422 #define CLUSTER_STAT(clause)
423 #endif /* MACH_CLUSTER_STATS */
426 * Routine: vm_pageout_object_terminate
428 * Destroy the pageout_object, and perform all of the
429 * required cleanup actions.
432 * The object must be locked, and will be returned locked.
435 vm_pageout_object_terminate(
438 vm_object_t shadow_object
;
441 * Deal with the deallocation (last reference) of a pageout object
442 * (used for cleaning-in-place) by dropping the paging references/
443 * freeing pages in the original object.
446 assert(object
->pageout
);
447 shadow_object
= object
->shadow
;
448 vm_object_lock(shadow_object
);
450 while (!queue_empty(&object
->memq
)) {
452 vm_object_offset_t offset
;
454 p
= (vm_page_t
) queue_first(&object
->memq
);
459 assert(!p
->cleaning
);
465 m
= vm_page_lookup(shadow_object
,
466 offset
+ object
->vo_shadow_offset
);
468 if(m
== VM_PAGE_NULL
)
471 /* used as a trigger on upl_commit etc to recognize the */
472 /* pageout daemon's subseqent desire to pageout a cleaning */
473 /* page. When the bit is on the upl commit code will */
474 /* respect the pageout bit in the target page over the */
475 /* caller's page list indication */
476 m
->dump_cleaning
= FALSE
;
478 assert((m
->dirty
) || (m
->precious
) ||
479 (m
->busy
&& m
->cleaning
));
482 * Handle the trusted pager throttle.
483 * Also decrement the burst throttle (if external).
485 vm_page_lock_queues();
487 vm_pageout_throttle_up(m
);
491 * Handle the "target" page(s). These pages are to be freed if
492 * successfully cleaned. Target pages are always busy, and are
493 * wired exactly once. The initial target pages are not mapped,
494 * (so cannot be referenced or modified) but converted target
495 * pages may have been modified between the selection as an
496 * adjacent page and conversion to a target.
500 assert(m
->wire_count
== 1);
502 m
->encrypted_cleaning
= FALSE
;
504 #if MACH_CLUSTER_STATS
505 if (m
->wanted
) vm_pageout_target_collisions
++;
508 * Revoke all access to the page. Since the object is
509 * locked, and the page is busy, this prevents the page
510 * from being dirtied after the pmap_disconnect() call
513 * Since the page is left "dirty" but "not modifed", we
514 * can detect whether the page was redirtied during
515 * pageout by checking the modify state.
517 if (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
)
523 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
524 vm_page_unwire(m
, TRUE
); /* reactivates */
525 VM_STAT_INCR(reactivations
);
528 CLUSTER_STAT(vm_pageout_target_page_freed
++;)
529 vm_page_free(m
);/* clears busy, etc. */
531 vm_page_unlock_queues();
535 * Handle the "adjacent" pages. These pages were cleaned in
536 * place, and should be left alone.
537 * If prep_pin_count is nonzero, then someone is using the
538 * page, so make it active.
540 if (!m
->active
&& !m
->inactive
&& !m
->throttled
&& !m
->private) {
544 vm_page_deactivate(m
);
546 if (m
->overwriting
) {
548 * the (COPY_OUT_FROM == FALSE) request_page_list case
552 * We do not re-set m->dirty !
553 * The page was busy so no extraneous activity
554 * could have occurred. COPY_INTO is a read into the
555 * new pages. CLEAN_IN_PLACE does actually write
556 * out the pages but handling outside of this code
557 * will take care of resetting dirty. We clear the
558 * modify however for the Programmed I/O case.
560 pmap_clear_modify(m
->phys_page
);
566 * alternate (COPY_OUT_FROM == FALSE) request_page_list case
567 * Occurs when the original page was wired
568 * at the time of the list request
570 assert(VM_PAGE_WIRED(m
));
571 vm_page_unwire(m
, TRUE
); /* reactivates */
573 m
->overwriting
= FALSE
;
576 * Set the dirty state according to whether or not the page was
577 * modified during the pageout. Note that we purposefully do
578 * NOT call pmap_clear_modify since the page is still mapped.
579 * If the page were to be dirtied between the 2 calls, this
580 * this fact would be lost. This code is only necessary to
581 * maintain statistics, since the pmap module is always
582 * consulted if m->dirty is false.
584 #if MACH_CLUSTER_STATS
585 m
->dirty
= pmap_is_modified(m
->phys_page
);
587 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
588 else vm_pageout_cluster_cleaned
++;
589 if (m
->wanted
) vm_pageout_cluster_collisions
++;
594 if (m
->encrypted_cleaning
== TRUE
) {
595 m
->encrypted_cleaning
= FALSE
;
601 * Wakeup any thread waiting for the page to be un-cleaning.
604 vm_page_unlock_queues();
607 * Account for the paging reference taken in vm_paging_object_allocate.
609 vm_object_activity_end(shadow_object
);
610 vm_object_unlock(shadow_object
);
612 assert(object
->ref_count
== 0);
613 assert(object
->paging_in_progress
== 0);
614 assert(object
->activity_in_progress
== 0);
615 assert(object
->resident_page_count
== 0);
620 * Routine: vm_pageclean_setup
622 * Purpose: setup a page to be cleaned (made non-dirty), but not
623 * necessarily flushed from the VM page cache.
624 * This is accomplished by cleaning in place.
626 * The page must not be busy, and new_object
634 vm_object_t new_object
,
635 vm_object_offset_t new_offset
)
639 assert(!m
->cleaning
);
643 "vm_pageclean_setup, obj 0x%X off 0x%X page 0x%X new 0x%X new_off 0x%X\n",
644 m
->object
, m
->offset
, m
,
647 pmap_clear_modify(m
->phys_page
);
650 * Mark original page as cleaning in place.
657 * Convert the fictitious page to a private shadow of
660 assert(new_m
->fictitious
);
661 assert(new_m
->phys_page
== vm_page_fictitious_addr
);
662 new_m
->fictitious
= FALSE
;
663 new_m
->private = TRUE
;
664 new_m
->pageout
= TRUE
;
665 new_m
->phys_page
= m
->phys_page
;
667 vm_page_lockspin_queues();
669 vm_page_unlock_queues();
671 vm_page_insert(new_m
, new_object
, new_offset
);
672 assert(!new_m
->wanted
);
677 * Routine: vm_pageout_initialize_page
679 * Causes the specified page to be initialized in
680 * the appropriate memory object. This routine is used to push
681 * pages into a copy-object when they are modified in the
684 * The page is moved to a temporary object and paged out.
687 * The page in question must not be on any pageout queues.
688 * The object to which it belongs must be locked.
689 * The page must be busy, but not hold a paging reference.
692 * Move this page to a completely new object.
695 vm_pageout_initialize_page(
699 vm_object_offset_t paging_offset
;
700 vm_page_t holding_page
;
701 memory_object_t pager
;
704 "vm_pageout_initialize_page, page 0x%X\n",
709 * Verify that we really want to clean this page
716 * Create a paging reference to let us play with the object.
719 paging_offset
= m
->offset
+ object
->paging_offset
;
721 if (m
->absent
|| m
->error
|| m
->restart
|| (!m
->dirty
&& !m
->precious
)) {
723 panic("reservation without pageout?"); /* alan */
724 vm_object_unlock(object
);
730 * If there's no pager, then we can't clean the page. This should
731 * never happen since this should be a copy object and therefore not
732 * an external object, so the pager should always be there.
735 pager
= object
->pager
;
737 if (pager
== MEMORY_OBJECT_NULL
) {
739 panic("missing pager for copy object");
743 /* set the page for future call to vm_fault_list_request */
744 vm_object_paging_begin(object
);
747 pmap_clear_modify(m
->phys_page
);
750 m
->list_req_pending
= TRUE
;
754 vm_page_lockspin_queues();
756 vm_page_unlock_queues();
758 vm_object_unlock(object
);
761 * Write the data to its pager.
762 * Note that the data is passed by naming the new object,
763 * not a virtual address; the pager interface has been
764 * manipulated to use the "internal memory" data type.
765 * [The object reference from its allocation is donated
766 * to the eventual recipient.]
768 memory_object_data_initialize(pager
, paging_offset
, PAGE_SIZE
);
770 vm_object_lock(object
);
771 vm_object_paging_end(object
);
774 #if MACH_CLUSTER_STATS
775 #define MAXCLUSTERPAGES 16
777 unsigned long pages_in_cluster
;
778 unsigned long pages_at_higher_offsets
;
779 unsigned long pages_at_lower_offsets
;
780 } cluster_stats
[MAXCLUSTERPAGES
];
781 #endif /* MACH_CLUSTER_STATS */
785 * vm_pageout_cluster:
787 * Given a page, queue it to the appropriate I/O thread,
788 * which will page it out and attempt to clean adjacent pages
789 * in the same operation.
791 * The page must be busy, and the object and queues locked. We will take a
792 * paging reference to prevent deallocation or collapse when we
793 * release the object lock back at the call site. The I/O thread
794 * is responsible for consuming this reference
796 * The page must not be on any pageout queue.
800 vm_pageout_cluster(vm_page_t m
)
802 vm_object_t object
= m
->object
;
803 struct vm_pageout_queue
*q
;
807 "vm_pageout_cluster, object 0x%X offset 0x%X page 0x%X\n",
808 object
, m
->offset
, m
, 0, 0);
812 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
814 vm_object_lock_assert_exclusive(object
);
817 * Only a certain kind of page is appreciated here.
819 assert(m
->busy
&& (m
->dirty
|| m
->precious
) && (!VM_PAGE_WIRED(m
)));
820 assert(!m
->cleaning
&& !m
->pageout
);
821 #ifndef CONFIG_FREEZE
822 assert(!m
->inactive
&& !m
->active
);
823 assert(!m
->throttled
);
827 * protect the object from collapse -
828 * locking in the object's paging_offset.
830 vm_object_paging_begin(object
);
833 * set the page for future call to vm_fault_list_request
834 * page should already be marked busy
837 m
->list_req_pending
= TRUE
;
841 if (object
->internal
== TRUE
)
842 q
= &vm_pageout_queue_internal
;
844 q
= &vm_pageout_queue_external
;
847 * pgo_laundry count is tied to the laundry bit
852 m
->pageout_queue
= TRUE
;
853 queue_enter(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
855 if (q
->pgo_idle
== TRUE
) {
857 thread_wakeup((event_t
) &q
->pgo_pending
);
864 unsigned long vm_pageout_throttle_up_count
= 0;
867 * A page is back from laundry or we are stealing it back from
868 * the laundering state. See if there are some pages waiting to
869 * go to laundry and if we can let some of them go now.
871 * Object and page queues must be locked.
874 vm_pageout_throttle_up(
877 struct vm_pageout_queue
*q
;
879 assert(m
->object
!= VM_OBJECT_NULL
);
880 assert(m
->object
!= kernel_object
);
882 vm_pageout_throttle_up_count
++;
884 if (m
->object
->internal
== TRUE
)
885 q
= &vm_pageout_queue_internal
;
887 q
= &vm_pageout_queue_external
;
889 if (m
->pageout_queue
== TRUE
) {
891 queue_remove(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
892 m
->pageout_queue
= FALSE
;
894 m
->pageq
.next
= NULL
;
895 m
->pageq
.prev
= NULL
;
897 vm_object_paging_end(m
->object
);
900 if ( m
->laundry
== TRUE
) {
905 if (q
->pgo_throttled
== TRUE
) {
906 q
->pgo_throttled
= FALSE
;
907 thread_wakeup((event_t
) &q
->pgo_laundry
);
909 if (q
->pgo_draining
== TRUE
&& q
->pgo_laundry
== 0) {
910 q
->pgo_draining
= FALSE
;
911 thread_wakeup((event_t
) (&q
->pgo_laundry
+1));
918 * VM memory pressure monitoring.
920 * vm_pageout_scan() keeps track of the number of pages it considers and
921 * reclaims, in the currently active vm_pageout_stat[vm_pageout_stat_now].
923 * compute_memory_pressure() is called every second from compute_averages()
924 * and moves "vm_pageout_stat_now" forward, to start accumulating the number
925 * of recalimed pages in a new vm_pageout_stat[] bucket.
927 * mach_vm_pressure_monitor() collects past statistics about memory pressure.
928 * The caller provides the number of seconds ("nsecs") worth of statistics
929 * it wants, up to 30 seconds.
930 * It computes the number of pages reclaimed in the past "nsecs" seconds and
931 * also returns the number of pages the system still needs to reclaim at this
934 #define VM_PAGEOUT_STAT_SIZE 31
935 struct vm_pageout_stat
{
936 unsigned int considered
;
937 unsigned int reclaimed
;
938 } vm_pageout_stats
[VM_PAGEOUT_STAT_SIZE
] = {{0,0}, };
939 unsigned int vm_pageout_stat_now
= 0;
940 unsigned int vm_memory_pressure
= 0;
942 #define VM_PAGEOUT_STAT_BEFORE(i) \
943 (((i) == 0) ? VM_PAGEOUT_STAT_SIZE - 1 : (i) - 1)
944 #define VM_PAGEOUT_STAT_AFTER(i) \
945 (((i) == VM_PAGEOUT_STAT_SIZE - 1) ? 0 : (i) + 1)
948 * Called from compute_averages().
951 compute_memory_pressure(
954 unsigned int vm_pageout_next
;
957 vm_pageout_stats
[VM_PAGEOUT_STAT_BEFORE(vm_pageout_stat_now
)].reclaimed
;
959 commpage_set_memory_pressure( vm_memory_pressure
);
961 /* move "now" forward */
962 vm_pageout_next
= VM_PAGEOUT_STAT_AFTER(vm_pageout_stat_now
);
963 vm_pageout_stats
[vm_pageout_next
].considered
= 0;
964 vm_pageout_stats
[vm_pageout_next
].reclaimed
= 0;
965 vm_pageout_stat_now
= vm_pageout_next
;
969 mach_vm_ctl_page_free_wanted(void)
971 unsigned int page_free_target
, page_free_count
, page_free_wanted
;
973 page_free_target
= vm_page_free_target
;
974 page_free_count
= vm_page_free_count
;
975 if (page_free_target
> page_free_count
) {
976 page_free_wanted
= page_free_target
- page_free_count
;
978 page_free_wanted
= 0;
981 return page_free_wanted
;
985 mach_vm_pressure_monitor(
986 boolean_t wait_for_pressure
,
987 unsigned int nsecs_monitored
,
988 unsigned int *pages_reclaimed_p
,
989 unsigned int *pages_wanted_p
)
992 unsigned int vm_pageout_then
, vm_pageout_now
;
993 unsigned int pages_reclaimed
;
996 * We don't take the vm_page_queue_lock here because we don't want
997 * vm_pressure_monitor() to get in the way of the vm_pageout_scan()
998 * thread when it's trying to reclaim memory. We don't need fully
999 * accurate monitoring anyway...
1002 if (wait_for_pressure
) {
1003 /* wait until there's memory pressure */
1004 while (vm_page_free_count
>= vm_page_free_target
) {
1005 wr
= assert_wait((event_t
) &vm_page_free_wanted
,
1006 THREAD_INTERRUPTIBLE
);
1007 if (wr
== THREAD_WAITING
) {
1008 wr
= thread_block(THREAD_CONTINUE_NULL
);
1010 if (wr
== THREAD_INTERRUPTED
) {
1011 return KERN_ABORTED
;
1013 if (wr
== THREAD_AWAKENED
) {
1015 * The memory pressure might have already
1016 * been relieved but let's not block again
1017 * and let's report that there was memory
1018 * pressure at some point.
1025 /* provide the number of pages the system wants to reclaim */
1026 if (pages_wanted_p
!= NULL
) {
1027 *pages_wanted_p
= mach_vm_ctl_page_free_wanted();
1030 if (pages_reclaimed_p
== NULL
) {
1031 return KERN_SUCCESS
;
1034 /* provide number of pages reclaimed in the last "nsecs_monitored" */
1036 vm_pageout_now
= vm_pageout_stat_now
;
1037 pages_reclaimed
= 0;
1038 for (vm_pageout_then
=
1039 VM_PAGEOUT_STAT_BEFORE(vm_pageout_now
);
1040 vm_pageout_then
!= vm_pageout_now
&&
1041 nsecs_monitored
-- != 0;
1043 VM_PAGEOUT_STAT_BEFORE(vm_pageout_then
)) {
1044 pages_reclaimed
+= vm_pageout_stats
[vm_pageout_then
].reclaimed
;
1046 } while (vm_pageout_now
!= vm_pageout_stat_now
);
1047 *pages_reclaimed_p
= pages_reclaimed
;
1049 return KERN_SUCCESS
;
1052 /* Page States: Used below to maintain the page state
1053 before it's removed from it's Q. This saved state
1054 helps us do the right accounting in certain cases
1057 #define PAGE_STATE_SPECULATIVE 1
1058 #define PAGE_STATE_ZEROFILL 2
1059 #define PAGE_STATE_INACTIVE 3
1060 #define PAGE_STATE_INACTIVE_FIRST 4
1062 #define VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m) \
1065 * If a "reusable" page somehow made it back into \
1066 * the active queue, it's been re-used and is not \
1067 * quite re-usable. \
1068 * If the VM object was "all_reusable", consider it \
1069 * as "all re-used" instead of converting it to \
1070 * "partially re-used", which could be expensive. \
1072 if ((m)->reusable || \
1073 (m)->object->all_reusable) { \
1074 vm_object_reuse_pages((m)->object, \
1076 (m)->offset + PAGE_SIZE_64, \
1082 #define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT 128
1083 #define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX 1024
1086 #define FCS_DELAYED 1
1087 #define FCS_DEADLOCK_DETECTED 2
1089 struct flow_control
{
1096 * vm_pageout_scan does the dirty work for the pageout daemon.
1097 * It returns with vm_page_queue_free_lock held and
1098 * vm_page_free_wanted == 0.
1101 vm_pageout_scan(void)
1103 unsigned int loop_count
= 0;
1104 unsigned int inactive_burst_count
= 0;
1105 unsigned int active_burst_count
= 0;
1106 unsigned int reactivated_this_call
;
1107 unsigned int reactivate_limit
;
1108 vm_page_t local_freeq
= NULL
;
1109 int local_freed
= 0;
1111 int delayed_unlock_limit
= 0;
1112 int refmod_state
= 0;
1113 int vm_pageout_deadlock_target
= 0;
1114 struct vm_pageout_queue
*iq
;
1115 struct vm_pageout_queue
*eq
;
1116 struct vm_speculative_age_q
*sq
;
1117 struct flow_control flow_control
= { 0, { 0, 0 } };
1118 boolean_t inactive_throttled
= FALSE
;
1119 boolean_t try_failed
;
1121 unsigned int msecs
= 0;
1123 vm_object_t last_object_tried
;
1125 uint64_t zf_run_count
;
1126 uint32_t catch_up_count
= 0;
1127 uint32_t inactive_reclaim_run
;
1128 boolean_t forced_reclaim
;
1129 int page_prev_state
= 0;
1130 int cache_evict_throttle
= 0;
1131 uint32_t vm_pageout_inactive_external_forced_reactivate_limit
= 0;
1133 VM_DEBUG_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_START
,
1134 vm_pageout_speculative_clean
, vm_pageout_inactive_clean
,
1135 vm_pageout_inactive_dirty_internal
, vm_pageout_inactive_dirty_external
);
1137 flow_control
.state
= FCS_IDLE
;
1138 iq
= &vm_pageout_queue_internal
;
1139 eq
= &vm_pageout_queue_external
;
1140 sq
= &vm_page_queue_speculative
[VM_PAGE_SPECULATIVE_AGED_Q
];
1143 XPR(XPR_VM_PAGEOUT
, "vm_pageout_scan\n", 0, 0, 0, 0, 0);
1146 vm_page_lock_queues();
1147 delayed_unlock
= 1; /* must be nonzero if Qs are locked, 0 if unlocked */
1150 * Calculate the max number of referenced pages on the inactive
1151 * queue that we will reactivate.
1153 reactivated_this_call
= 0;
1154 reactivate_limit
= VM_PAGE_REACTIVATE_LIMIT(vm_page_active_count
+
1155 vm_page_inactive_count
);
1156 inactive_reclaim_run
= 0;
1160 * We want to gradually dribble pages from the active queue
1161 * to the inactive queue. If we let the inactive queue get
1162 * very small, and then suddenly dump many pages into it,
1163 * those pages won't get a sufficient chance to be referenced
1164 * before we start taking them from the inactive queue.
1166 * We must limit the rate at which we send pages to the pagers
1167 * so that we don't tie up too many pages in the I/O queues.
1168 * We implement a throttling mechanism using the laundry count
1169 * to limit the number of pages outstanding to the default
1170 * and external pagers. We can bypass the throttles and look
1171 * for clean pages if the pageout queues don't drain in a timely
1172 * fashion since this may indicate that the pageout paths are
1173 * stalled waiting for memory, which only we can provide.
1178 assert(delayed_unlock
!=0);
1181 * A page is "zero-filled" if it was not paged in from somewhere,
1182 * and it belongs to an object at least VM_ZF_OBJECT_SIZE_THRESHOLD big.
1183 * Recalculate the zero-filled page ratio. We use this to apportion
1184 * victimized pages between the normal and zero-filled inactive
1185 * queues according to their relative abundance in memory. Thus if a task
1186 * is flooding memory with zf pages, we begin to hunt them down.
1187 * It would be better to throttle greedy tasks at a higher level,
1188 * but at the moment mach vm cannot do this.
1191 uint64_t total
= vm_page_active_count
+ vm_page_inactive_count
;
1192 uint64_t normal
= total
- vm_zf_count
;
1194 /* zf_ratio is the number of zf pages we victimize per normal page */
1196 if (vm_zf_count
< vm_accellerate_zf_pageout_trigger
)
1198 else if ((vm_zf_count
<= normal
) || (normal
== 0))
1201 zf_ratio
= vm_zf_count
/ normal
;
1207 * Recalculate vm_page_inactivate_target.
1209 vm_page_inactive_target
= VM_PAGE_INACTIVE_TARGET(vm_page_active_count
+
1210 vm_page_inactive_count
+
1211 vm_page_speculative_count
);
1213 * don't want to wake the pageout_scan thread up everytime we fall below
1214 * the targets... set a low water mark at 0.25% below the target
1216 vm_page_inactive_min
= vm_page_inactive_target
- (vm_page_inactive_target
/ 400);
1218 if (vm_page_speculative_percentage
> 50)
1219 vm_page_speculative_percentage
= 50;
1220 else if (vm_page_speculative_percentage
<= 0)
1221 vm_page_speculative_percentage
= 1;
1223 vm_page_speculative_target
= VM_PAGE_SPECULATIVE_TARGET(vm_page_active_count
+
1224 vm_page_inactive_count
);
1226 vm_pageout_inactive_external_forced_reactivate_limit
= vm_page_active_count
+ vm_page_inactive_count
;
1229 last_object_tried
= NULL
;
1232 if ((vm_page_inactive_count
+ vm_page_speculative_count
) < VM_PAGE_INACTIVE_HEALTHY_LIMIT(vm_page_active_count
))
1233 catch_up_count
= vm_page_inactive_count
+ vm_page_speculative_count
;
1240 DTRACE_VM2(rev
, int, 1, (uint64_t *), NULL
);
1242 if (delayed_unlock
== 0) {
1243 vm_page_lock_queues();
1246 if (vm_upl_wait_for_pages
< 0)
1247 vm_upl_wait_for_pages
= 0;
1249 delayed_unlock_limit
= VM_PAGEOUT_DELAYED_UNLOCK_LIMIT
+ vm_upl_wait_for_pages
;
1251 if (delayed_unlock_limit
> VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX
)
1252 delayed_unlock_limit
= VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX
;
1255 * Move pages from active to inactive if we're below the target
1257 if ((vm_page_inactive_count
+ vm_page_speculative_count
) >= vm_page_inactive_target
)
1258 goto done_moving_active_pages
;
1260 if (object
!= NULL
) {
1261 vm_object_unlock(object
);
1263 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1266 * Don't sweep through active queue more than the throttle
1267 * which should be kept relatively low
1269 active_burst_count
= MIN(vm_pageout_burst_active_throttle
,
1270 vm_page_active_count
);
1272 VM_DEBUG_EVENT(vm_pageout_balance
, VM_PAGEOUT_BALANCE
, DBG_FUNC_START
,
1273 vm_pageout_inactive
, vm_pageout_inactive_used
, vm_page_free_count
, local_freed
);
1275 VM_DEBUG_EVENT(vm_pageout_balance
, VM_PAGEOUT_BALANCE
, DBG_FUNC_NONE
,
1276 vm_pageout_speculative_clean
, vm_pageout_inactive_clean
,
1277 vm_pageout_inactive_dirty_internal
, vm_pageout_inactive_dirty_external
);
1279 while (!queue_empty(&vm_page_queue_active
) && active_burst_count
--) {
1281 vm_pageout_active
++;
1283 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
1285 assert(m
->active
&& !m
->inactive
);
1286 assert(!m
->laundry
);
1287 assert(m
->object
!= kernel_object
);
1288 assert(m
->phys_page
!= vm_page_guard_addr
);
1290 DTRACE_VM2(scan
, int, 1, (uint64_t *), NULL
);
1293 * The page might be absent or busy,
1294 * but vm_page_deactivate can handle that.
1296 vm_page_deactivate(m
);
1298 if (delayed_unlock
++ > delayed_unlock_limit
) {
1301 vm_page_unlock_queues();
1303 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
1304 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 1);
1306 vm_page_free_list(local_freeq
, TRUE
);
1308 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
1309 vm_page_free_count
, 0, 0, 1);
1313 vm_page_lock_queues();
1315 lck_mtx_yield(&vm_page_queue_lock
);
1320 * continue the while loop processing
1321 * the active queue... need to hold
1322 * the page queues lock
1327 VM_DEBUG_EVENT(vm_pageout_balance
, VM_PAGEOUT_BALANCE
, DBG_FUNC_END
,
1328 vm_page_active_count
, vm_page_inactive_count
, vm_page_speculative_count
, vm_page_inactive_target
);
1331 /**********************************************************************
1332 * above this point we're playing with the active queue
1333 * below this point we're playing with the throttling mechanisms
1334 * and the inactive queue
1335 **********************************************************************/
1337 done_moving_active_pages
:
1339 if (vm_page_free_count
+ local_freed
>= vm_page_free_target
) {
1340 if (object
!= NULL
) {
1341 vm_object_unlock(object
);
1344 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1347 vm_page_unlock_queues();
1349 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
1350 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 2);
1352 vm_page_free_list(local_freeq
, TRUE
);
1354 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
1355 vm_page_free_count
, local_freed
, 0, 2);
1359 vm_page_lock_queues();
1362 * recalculate vm_page_inactivate_target
1364 vm_page_inactive_target
= VM_PAGE_INACTIVE_TARGET(vm_page_active_count
+
1365 vm_page_inactive_count
+
1366 vm_page_speculative_count
);
1367 #ifndef CONFIG_EMBEDDED
1368 if (((vm_page_inactive_count
+ vm_page_speculative_count
) < vm_page_inactive_target
) &&
1369 !queue_empty(&vm_page_queue_active
)) {
1371 * inactive target still not met... keep going
1372 * until we get the queues balanced...
1377 lck_mtx_lock(&vm_page_queue_free_lock
);
1379 if ((vm_page_free_count
>= vm_page_free_target
) &&
1380 (vm_page_free_wanted
== 0) && (vm_page_free_wanted_privileged
== 0)) {
1382 * done - we have met our target *and*
1383 * there is no one waiting for a page.
1385 vm_page_unlock_queues();
1387 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
1389 assert(vm_pageout_scan_wants_object
== VM_OBJECT_NULL
);
1391 VM_DEBUG_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_NONE
,
1392 vm_pageout_inactive
, vm_pageout_inactive_used
, 0, 0);
1393 VM_DEBUG_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_END
,
1394 vm_pageout_speculative_clean
, vm_pageout_inactive_clean
,
1395 vm_pageout_inactive_dirty_internal
, vm_pageout_inactive_dirty_external
);
1399 lck_mtx_unlock(&vm_page_queue_free_lock
);
1403 * Before anything, we check if we have any ripe volatile
1404 * objects around. If so, try to purge the first object.
1405 * If the purge fails, fall through to reclaim a page instead.
1406 * If the purge succeeds, go back to the top and reevalute
1407 * the new memory situation.
1409 assert (available_for_purge
>=0);
1410 if (available_for_purge
)
1412 if (object
!= NULL
) {
1413 vm_object_unlock(object
);
1417 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_START
, vm_page_free_count
, 0, 0, 0);
1419 if (TRUE
== vm_purgeable_object_purge_one()) {
1421 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
, vm_page_free_count
, 0, 0, 0);
1425 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
, 0, 0, 0, -1);
1427 if (queue_empty(&sq
->age_q
) && vm_page_speculative_count
) {
1429 * try to pull pages from the aging bins...
1430 * see vm_page.h for an explanation of how
1431 * this mechanism works
1433 struct vm_speculative_age_q
*aq
;
1434 mach_timespec_t ts_fully_aged
;
1435 boolean_t can_steal
= FALSE
;
1436 int num_scanned_queues
;
1438 aq
= &vm_page_queue_speculative
[speculative_steal_index
];
1440 num_scanned_queues
= 0;
1441 while (queue_empty(&aq
->age_q
) &&
1442 num_scanned_queues
++ != VM_PAGE_MAX_SPECULATIVE_AGE_Q
) {
1444 speculative_steal_index
++;
1446 if (speculative_steal_index
> VM_PAGE_MAX_SPECULATIVE_AGE_Q
)
1447 speculative_steal_index
= VM_PAGE_MIN_SPECULATIVE_AGE_Q
;
1449 aq
= &vm_page_queue_speculative
[speculative_steal_index
];
1452 if (num_scanned_queues
== VM_PAGE_MAX_SPECULATIVE_AGE_Q
+ 1) {
1454 * XXX We've scanned all the speculative
1455 * queues but still haven't found one
1456 * that is not empty, even though
1457 * vm_page_speculative_count is not 0.
1459 * report the anomaly...
1461 printf("vm_pageout_scan: "
1462 "all speculative queues empty "
1463 "but count=%d. Re-adjusting.\n",
1464 vm_page_speculative_count
);
1465 if (vm_page_speculative_count
> vm_page_speculative_count_drift_max
)
1466 vm_page_speculative_count_drift_max
= vm_page_speculative_count
;
1467 vm_page_speculative_count_drifts
++;
1469 Debugger("vm_pageout_scan: no speculative pages");
1472 vm_page_speculative_count
= 0;
1473 /* ... and continue */
1477 if (vm_page_speculative_count
> vm_page_speculative_target
)
1480 ts_fully_aged
.tv_sec
= (VM_PAGE_MAX_SPECULATIVE_AGE_Q
* vm_page_speculative_q_age_ms
) / 1000;
1481 ts_fully_aged
.tv_nsec
= ((VM_PAGE_MAX_SPECULATIVE_AGE_Q
* vm_page_speculative_q_age_ms
) % 1000)
1482 * 1000 * NSEC_PER_USEC
;
1484 ADD_MACH_TIMESPEC(&ts_fully_aged
, &aq
->age_ts
);
1488 clock_get_system_nanotime(&sec
, &nsec
);
1489 ts
.tv_sec
= (unsigned int) sec
;
1492 if (CMP_MACH_TIMESPEC(&ts
, &ts_fully_aged
) >= 0)
1495 if (can_steal
== TRUE
)
1496 vm_page_speculate_ageit(aq
);
1498 if (queue_empty(&sq
->age_q
) && cache_evict_throttle
== 0) {
1501 if (object
!= NULL
) {
1502 vm_object_unlock(object
);
1505 pages_evicted
= vm_object_cache_evict(100, 10);
1507 if (pages_evicted
) {
1509 vm_pageout_cache_evicted
+= pages_evicted
;
1511 VM_DEBUG_EVENT(vm_pageout_cache_evict
, VM_PAGEOUT_CACHE_EVICT
, DBG_FUNC_NONE
,
1512 vm_page_free_count
, pages_evicted
, vm_pageout_cache_evicted
, 0);
1515 * we just freed up to 100 pages,
1516 * so go back to the top of the main loop
1517 * and re-evaulate the memory situation
1521 cache_evict_throttle
= 100;
1523 if (cache_evict_throttle
)
1524 cache_evict_throttle
--;
1528 * Sometimes we have to pause:
1529 * 1) No inactive pages - nothing to do.
1530 * 2) Flow control - default pageout queue is full
1531 * 3) Loop control - no acceptable pages found on the inactive queue
1532 * within the last vm_pageout_burst_inactive_throttle iterations
1534 if (queue_empty(&vm_page_queue_inactive
) && queue_empty(&vm_page_queue_zf
) && queue_empty(&sq
->age_q
)) {
1535 vm_pageout_scan_empty_throttle
++;
1536 msecs
= vm_pageout_empty_wait
;
1537 goto vm_pageout_scan_delay
;
1539 } else if (inactive_burst_count
>=
1540 MIN(vm_pageout_burst_inactive_throttle
,
1541 (vm_page_inactive_count
+
1542 vm_page_speculative_count
))) {
1543 vm_pageout_scan_burst_throttle
++;
1544 msecs
= vm_pageout_burst_wait
;
1545 goto vm_pageout_scan_delay
;
1547 } else if (VM_PAGE_Q_THROTTLED(iq
) &&
1548 VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
)) {
1552 switch (flow_control
.state
) {
1555 reset_deadlock_timer
:
1556 ts
.tv_sec
= vm_pageout_deadlock_wait
/ 1000;
1557 ts
.tv_nsec
= (vm_pageout_deadlock_wait
% 1000) * 1000 * NSEC_PER_USEC
;
1558 clock_get_system_nanotime(&sec
, &nsec
);
1559 flow_control
.ts
.tv_sec
= (unsigned int) sec
;
1560 flow_control
.ts
.tv_nsec
= nsec
;
1561 ADD_MACH_TIMESPEC(&flow_control
.ts
, &ts
);
1563 flow_control
.state
= FCS_DELAYED
;
1564 msecs
= vm_pageout_deadlock_wait
;
1569 clock_get_system_nanotime(&sec
, &nsec
);
1570 ts
.tv_sec
= (unsigned int) sec
;
1573 if (CMP_MACH_TIMESPEC(&ts
, &flow_control
.ts
) >= 0) {
1575 * the pageout thread for the default pager is potentially
1576 * deadlocked since the
1577 * default pager queue has been throttled for more than the
1578 * allowable time... we need to move some clean pages or dirty
1579 * pages belonging to the external pagers if they aren't throttled
1580 * vm_page_free_wanted represents the number of threads currently
1581 * blocked waiting for pages... we'll move one page for each of
1582 * these plus a fixed amount to break the logjam... once we're done
1583 * moving this number of pages, we'll re-enter the FSC_DELAYED state
1584 * with a new timeout target since we have no way of knowing
1585 * whether we've broken the deadlock except through observation
1586 * of the queue associated with the default pager... we need to
1587 * stop moving pages and allow the system to run to see what
1588 * state it settles into.
1590 vm_pageout_deadlock_target
= vm_pageout_deadlock_relief
+ vm_page_free_wanted
+ vm_page_free_wanted_privileged
;
1591 vm_pageout_scan_deadlock_detected
++;
1592 flow_control
.state
= FCS_DEADLOCK_DETECTED
;
1594 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
1595 goto consider_inactive
;
1598 * just resniff instead of trying
1599 * to compute a new delay time... we're going to be
1600 * awakened immediately upon a laundry completion,
1601 * so we won't wait any longer than necessary
1603 msecs
= vm_pageout_idle_wait
;
1606 case FCS_DEADLOCK_DETECTED
:
1607 if (vm_pageout_deadlock_target
)
1608 goto consider_inactive
;
1609 goto reset_deadlock_timer
;
1612 vm_pageout_scan_throttle
++;
1613 iq
->pgo_throttled
= TRUE
;
1614 vm_pageout_scan_delay
:
1615 if (object
!= NULL
) {
1616 vm_object_unlock(object
);
1619 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1622 vm_page_unlock_queues();
1624 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
1625 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 3);
1627 vm_page_free_list(local_freeq
, TRUE
);
1629 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
1630 vm_page_free_count
, local_freed
, 0, 3);
1634 vm_page_lock_queues();
1636 if (flow_control
.state
== FCS_DELAYED
&&
1637 !VM_PAGE_Q_THROTTLED(iq
)) {
1638 flow_control
.state
= FCS_IDLE
;
1639 vm_pageout_scan_throttle_aborted
++;
1640 goto consider_inactive
;
1644 VM_CHECK_MEMORYSTATUS
;
1646 assert_wait_timeout((event_t
) &iq
->pgo_laundry
, THREAD_INTERRUPTIBLE
, msecs
, 1000*NSEC_PER_USEC
);
1647 counter(c_vm_pageout_scan_block
++);
1649 vm_page_unlock_queues();
1651 assert(vm_pageout_scan_wants_object
== VM_OBJECT_NULL
);
1653 VM_DEBUG_EVENT(vm_pageout_thread_block
, VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_START
,
1654 iq
->pgo_laundry
, iq
->pgo_maxlaundry
, msecs
, 0);
1656 thread_block(THREAD_CONTINUE_NULL
);
1658 VM_DEBUG_EVENT(vm_pageout_thread_block
, VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_END
,
1659 iq
->pgo_laundry
, iq
->pgo_maxlaundry
, msecs
, 0);
1661 vm_page_lock_queues();
1664 iq
->pgo_throttled
= FALSE
;
1666 if (loop_count
>= vm_page_inactive_count
)
1668 inactive_burst_count
= 0;
1675 flow_control
.state
= FCS_IDLE
;
1677 vm_pageout_inactive_external_forced_reactivate_limit
= MIN((vm_page_active_count
+ vm_page_inactive_count
),
1678 vm_pageout_inactive_external_forced_reactivate_limit
);
1680 inactive_burst_count
++;
1681 vm_pageout_inactive
++;
1683 /* Choose a victim. */
1688 if (VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
)) {
1689 assert(vm_page_throttled_count
== 0);
1690 assert(queue_empty(&vm_page_queue_throttled
));
1694 * The most eligible pages are ones we paged in speculatively,
1695 * but which have not yet been touched.
1697 if ( !queue_empty(&sq
->age_q
) ) {
1698 m
= (vm_page_t
) queue_first(&sq
->age_q
);
1700 page_prev_state
= PAGE_STATE_SPECULATIVE
;
1704 * Time for a zero-filled inactive page?
1706 if ( ((zf_run_count
< zf_ratio
) && vm_zf_queue_count
>= zf_queue_min_count
) ||
1707 queue_empty(&vm_page_queue_inactive
)) {
1708 if ( !queue_empty(&vm_page_queue_zf
) ) {
1709 m
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
1711 page_prev_state
= PAGE_STATE_ZEROFILL
;
1717 * It's either a normal inactive page or nothing.
1719 if ( !queue_empty(&vm_page_queue_inactive
) ) {
1720 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
1722 page_prev_state
= PAGE_STATE_INACTIVE
;
1727 panic("vm_pageout: no victim");
1729 VM_PAGE_QUEUES_REMOVE(m
);
1731 assert(!m
->laundry
);
1732 assert(!m
->private);
1733 assert(!m
->fictitious
);
1734 assert(m
->object
!= kernel_object
);
1735 assert(m
->phys_page
!= vm_page_guard_addr
);
1738 if (page_prev_state
!= PAGE_STATE_SPECULATIVE
)
1739 vm_pageout_stats
[vm_pageout_stat_now
].considered
++;
1741 DTRACE_VM2(scan
, int, 1, (uint64_t *), NULL
);
1744 * check to see if we currently are working
1745 * with the same object... if so, we've
1746 * already got the lock
1748 if (m
->object
!= object
) {
1750 * the object associated with candidate page is
1751 * different from the one we were just working
1752 * with... dump the lock if we still own it
1754 if (object
!= NULL
) {
1755 vm_object_unlock(object
);
1757 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1760 * Try to lock object; since we've alread got the
1761 * page queues lock, we can only 'try' for this one.
1762 * if the 'try' fails, we need to do a mutex_pause
1763 * to allow the owner of the object lock a chance to
1764 * run... otherwise, we're likely to trip over this
1765 * object in the same state as we work our way through
1766 * the queue... clumps of pages associated with the same
1767 * object are fairly typical on the inactive and active queues
1769 if (!vm_object_lock_try_scan(m
->object
)) {
1770 vm_page_t m_want
= NULL
;
1772 vm_pageout_inactive_nolock
++;
1774 if (page_prev_state
== PAGE_STATE_SPECULATIVE
)
1775 page_prev_state
= PAGE_STATE_INACTIVE_FIRST
;
1777 pmap_clear_reference(m
->phys_page
);
1778 m
->reference
= FALSE
;
1781 * m->object must be stable since we hold the page queues lock...
1782 * we can update the scan_collisions field sans the object lock
1783 * since it is a separate field and this is the only spot that does
1784 * a read-modify-write operation and it is never executed concurrently...
1785 * we can asynchronously set this field to 0 when creating a UPL, so it
1786 * is possible for the value to be a bit non-determistic, but that's ok
1787 * since it's only used as a hint
1789 m
->object
->scan_collisions
++;
1791 if ( !queue_empty(&sq
->age_q
) )
1792 m_want
= (vm_page_t
) queue_first(&sq
->age_q
);
1793 else if ( ((zf_run_count
< zf_ratio
) && vm_zf_queue_count
>= zf_queue_min_count
) ||
1794 queue_empty(&vm_page_queue_inactive
)) {
1795 if ( !queue_empty(&vm_page_queue_zf
) )
1796 m_want
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
1797 } else if ( !queue_empty(&vm_page_queue_inactive
) ) {
1798 m_want
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
1801 * this is the next object we're going to be interested in
1802 * try to make sure its available after the mutex_yield
1806 vm_pageout_scan_wants_object
= m_want
->object
;
1809 * force us to dump any collected free pages
1810 * and to pause before moving on
1817 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1825 if (m
->encrypted_cleaning
) {
1828 * if this page has already been picked up as
1829 * part of a page-out cluster, it will be busy
1830 * because it is being encrypted (see
1831 * vm_object_upl_request()). But we still
1832 * want to demote it from "clean-in-place"
1833 * (aka "adjacent") to "clean-and-free" (aka
1834 * "target"), so let's ignore its "busy" bit
1835 * here and proceed to check for "cleaning" a
1836 * little bit below...
1839 * A "busy" page should still be left alone for
1840 * most purposes, so we have to be very careful
1841 * not to process that page too much.
1843 assert(m
->cleaning
);
1844 goto consider_inactive_page
;
1848 * Somebody is already playing with this page.
1849 * Put it back on the appropriate queue
1852 vm_pageout_inactive_busy
++;
1854 switch (page_prev_state
) {
1856 case PAGE_STATE_SPECULATIVE
:
1857 vm_page_speculate(m
, FALSE
);
1860 case PAGE_STATE_ZEROFILL
:
1861 m
->zero_fill
= TRUE
;
1863 * fall through to add in the
1866 case PAGE_STATE_INACTIVE
:
1867 VM_PAGE_ENQUEUE_INACTIVE(m
, FALSE
);
1870 case PAGE_STATE_INACTIVE_FIRST
:
1871 VM_PAGE_ENQUEUE_INACTIVE(m
, TRUE
);
1874 goto done_with_inactivepage
;
1879 * If it's absent, in error or the object is no longer alive,
1880 * we can reclaim the page... in the no longer alive case,
1881 * there are 2 states the page can be in that preclude us
1882 * from reclaiming it - busy or cleaning - that we've already
1885 if (m
->absent
|| m
->error
|| !object
->alive
) {
1888 vm_pageout_inactive_absent
++;
1889 else if (!object
->alive
)
1890 vm_pageout_inactive_notalive
++;
1892 vm_pageout_inactive_error
++;
1894 if (vm_pageout_deadlock_target
) {
1895 vm_pageout_scan_inactive_throttle_success
++;
1896 vm_pageout_deadlock_target
--;
1899 DTRACE_VM2(dfree
, int, 1, (uint64_t *), NULL
);
1901 if (object
->internal
) {
1902 DTRACE_VM2(anonfree
, int, 1, (uint64_t *), NULL
);
1904 DTRACE_VM2(fsfree
, int, 1, (uint64_t *), NULL
);
1906 vm_page_free_prepare_queues(m
);
1909 * remove page from object here since we're already
1910 * behind the object lock... defer the rest of the work
1911 * we'd normally do in vm_page_free_prepare_object
1912 * until 'vm_page_free_list' is called
1915 vm_page_remove(m
, TRUE
);
1917 assert(m
->pageq
.next
== NULL
&&
1918 m
->pageq
.prev
== NULL
);
1919 m
->pageq
.next
= (queue_entry_t
)local_freeq
;
1923 inactive_burst_count
= 0;
1925 if (page_prev_state
!= PAGE_STATE_SPECULATIVE
)
1926 vm_pageout_stats
[vm_pageout_stat_now
].reclaimed
++;
1928 goto done_with_inactivepage
;
1931 * If the object is empty, the page must be reclaimed even
1933 * If the page belongs to a volatile object, we stick it back
1936 if (object
->copy
== VM_OBJECT_NULL
) {
1937 if (object
->purgable
== VM_PURGABLE_EMPTY
) {
1939 if (m
->pmapped
== TRUE
) {
1940 /* unmap the page */
1941 refmod_state
= pmap_disconnect(m
->phys_page
);
1942 if (refmod_state
& VM_MEM_MODIFIED
) {
1946 if (m
->dirty
|| m
->precious
) {
1947 /* we saved the cost of cleaning this page ! */
1948 vm_page_purged_count
++;
1952 if (object
->purgable
== VM_PURGABLE_VOLATILE
) {
1953 /* if it's wired, we can't put it on our queue */
1954 assert(!VM_PAGE_WIRED(m
));
1956 /* just stick it back on! */
1957 reactivated_this_call
++;
1958 goto reactivate_page
;
1962 consider_inactive_page
:
1966 * A "busy" page should always be left alone, except...
1968 if (m
->cleaning
&& m
->encrypted_cleaning
) {
1971 * We could get here with a "busy" page
1972 * if it's being encrypted during a
1973 * "clean-in-place" operation. We'll deal
1974 * with it right away by testing if it has been
1975 * referenced and either reactivating it or
1976 * promoting it from "clean-in-place" to
1980 panic("\"busy\" page considered for pageout\n");
1985 * If it's being used, reactivate.
1986 * (Fictitious pages are either busy or absent.)
1987 * First, update the reference and dirty bits
1988 * to make sure the page is unreferenced.
1992 if (m
->reference
== FALSE
&& m
->pmapped
== TRUE
) {
1993 refmod_state
= pmap_get_refmod(m
->phys_page
);
1995 if (refmod_state
& VM_MEM_REFERENCED
)
1996 m
->reference
= TRUE
;
1997 if (refmod_state
& VM_MEM_MODIFIED
)
2002 * If already cleaning this page in place and it hasn't
2003 * been recently referenced, convert from
2004 * "adjacent" to "target". We can leave the page mapped,
2005 * and upl_commit_range will determine whether
2006 * to free or reactivate.
2008 * note: if m->encrypted_cleaning == TRUE, then
2009 * m->cleaning == TRUE
2010 * and we'll handle it here
2014 if (m
->reference
== TRUE
) {
2015 reactivated_this_call
++;
2016 goto reactivate_page
;
2020 m
->dump_cleaning
= TRUE
;
2023 CLUSTER_STAT(vm_pageout_cluster_conversions
++);
2025 inactive_burst_count
= 0;
2027 goto done_with_inactivepage
;
2030 if (m
->reference
|| m
->dirty
) {
2031 /* deal with a rogue "reusable" page */
2032 VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m
);
2035 if (m
->reference
&& !m
->no_cache
) {
2037 * The page we pulled off the inactive list has
2038 * been referenced. It is possible for other
2039 * processors to be touching pages faster than we
2040 * can clear the referenced bit and traverse the
2041 * inactive queue, so we limit the number of
2044 if (++reactivated_this_call
>= reactivate_limit
) {
2045 vm_pageout_reactivation_limit_exceeded
++;
2046 } else if (catch_up_count
) {
2047 vm_pageout_catch_ups
++;
2048 } else if (++inactive_reclaim_run
>= VM_PAGEOUT_INACTIVE_FORCE_RECLAIM
) {
2049 vm_pageout_inactive_force_reclaim
++;
2053 if ( !object
->internal
&& object
->pager
!= MEMORY_OBJECT_NULL
&&
2054 vnode_pager_get_isinuse(object
->pager
, &isinuse
) == KERN_SUCCESS
&& !isinuse
) {
2056 * no explict mappings of this object exist
2057 * and it's not open via the filesystem
2059 vm_page_deactivate(m
);
2060 vm_pageout_inactive_deactivated
++;
2063 * The page was/is being used, so put back on active list.
2065 vm_page_activate(m
);
2066 VM_STAT_INCR(reactivations
);
2068 vm_pageout_inactive_used
++;
2069 inactive_burst_count
= 0;
2071 goto done_with_inactivepage
;
2074 * Make sure we call pmap_get_refmod() if it
2075 * wasn't already called just above, to update
2078 if ((refmod_state
== -1) && !m
->dirty
&& m
->pmapped
) {
2079 refmod_state
= pmap_get_refmod(m
->phys_page
);
2080 if (refmod_state
& VM_MEM_MODIFIED
)
2083 forced_reclaim
= TRUE
;
2085 forced_reclaim
= FALSE
;
2089 "vm_pageout_scan, replace object 0x%X offset 0x%X page 0x%X\n",
2090 object
, m
->offset
, m
, 0,0);
2093 * we've got a candidate page to steal...
2095 * m->dirty is up to date courtesy of the
2096 * preceding check for m->reference... if
2097 * we get here, then m->reference had to be
2098 * FALSE (or possibly "reactivate_limit" was
2099 * exceeded), but in either case we called
2100 * pmap_get_refmod() and updated both
2101 * m->reference and m->dirty
2103 * if it's dirty or precious we need to
2104 * see if the target queue is throtttled
2105 * it if is, we need to skip over it by moving it back
2106 * to the end of the inactive queue
2109 inactive_throttled
= FALSE
;
2111 if (m
->dirty
|| m
->precious
) {
2112 if (object
->internal
) {
2113 if (VM_PAGE_Q_THROTTLED(iq
))
2114 inactive_throttled
= TRUE
;
2115 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
2116 inactive_throttled
= TRUE
;
2120 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) &&
2121 object
->internal
&& m
->dirty
&&
2122 (object
->purgable
== VM_PURGABLE_DENY
||
2123 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
2124 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
2125 queue_enter(&vm_page_queue_throttled
, m
,
2127 m
->throttled
= TRUE
;
2128 vm_page_throttled_count
++;
2130 vm_pageout_scan_reclaimed_throttled
++;
2132 goto done_with_inactivepage
;
2134 if (inactive_throttled
== TRUE
) {
2136 if (object
->internal
)
2137 vm_pageout_scan_inactive_throttled_internal
++;
2139 vm_pageout_scan_inactive_throttled_external
++;
2141 if (page_prev_state
== PAGE_STATE_SPECULATIVE
)
2142 page_prev_state
= PAGE_STATE_INACTIVE
;
2144 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) && object
->internal
== FALSE
) {
2146 * a) The external pageout queue is throttled
2147 * b) We're done with the active queue and moved on to the inactive queue
2148 * c) We start noticing dirty pages and usually we would put them at the end of the inactive queue, but,
2149 * d) We don't have a default pager, and so,
2150 * e) We push these onto the active queue in an effort to cause a re-evaluation of the active queue
2151 * and get back some, possibly clean, pages.
2153 * We also keep a count of the pages of this kind, since, these will be a good indicator of us being in a deadlock
2154 * on systems without a dynamic pager, where:
2155 * a) The external pageout thread is stuck on the truncate lock for a file that is being extended i.e. written.
2156 * b) The thread doing the writing is waiting for pages while holding the truncate lock
2157 * c) Most of the pages in the inactive queue belong to this file.
2160 vm_page_activate(m
);
2161 vm_pageout_inactive_external_forced_reactivate_count
++;
2162 vm_pageout_inactive_external_forced_reactivate_limit
--;
2164 if (vm_pageout_inactive_external_forced_reactivate_limit
<= 0){
2165 vm_pageout_inactive_external_forced_reactivate_limit
= vm_page_active_count
+ vm_page_inactive_count
;
2168 * Possible deadlock scenario so request jetsam action
2171 vm_object_unlock(object
);
2172 object
= VM_OBJECT_NULL
;
2173 vm_page_unlock_queues();
2175 if (jetsam_kill_top_proc(TRUE
, kJetsamFlagsKilledVM
) < 0){
2176 panic("vm_pageout_scan: Jetsam request failed\n");
2179 vm_page_lock_queues();
2183 inactive_burst_count
= 0;
2184 goto done_with_inactivepage
;
2191 * we've got a page that we can steal...
2192 * eliminate all mappings and make sure
2193 * we have the up-to-date modified state
2194 * first take the page BUSY, so that no new
2195 * mappings can be made
2200 * if we need to do a pmap_disconnect then we
2201 * need to re-evaluate m->dirty since the pmap_disconnect
2202 * provides the true state atomically... the
2203 * page was still mapped up to the pmap_disconnect
2204 * and may have been dirtied at the last microsecond
2206 * we also check for the page being referenced 'late'
2207 * if it was, we first need to do a WAKEUP_DONE on it
2208 * since we already set m->busy = TRUE, before
2209 * going off to reactivate it
2211 * Note that if 'pmapped' is FALSE then the page is not
2212 * and has not been in any map, so there is no point calling
2213 * pmap_disconnect(). m->dirty and/or m->reference could
2214 * have been set in anticipation of likely usage of the page.
2216 if (m
->pmapped
== TRUE
) {
2217 refmod_state
= pmap_disconnect(m
->phys_page
);
2219 if (refmod_state
& VM_MEM_MODIFIED
)
2221 if (refmod_state
& VM_MEM_REFERENCED
) {
2223 /* If m->reference is already set, this page must have
2224 * already failed the reactivate_limit test, so don't
2225 * bump the counts twice.
2227 if ( ! m
->reference
) {
2228 m
->reference
= TRUE
;
2229 if (forced_reclaim
||
2230 ++reactivated_this_call
>= reactivate_limit
)
2231 vm_pageout_reactivation_limit_exceeded
++;
2233 PAGE_WAKEUP_DONE(m
);
2234 goto reactivate_page
;
2240 * reset our count of pages that have been reclaimed
2241 * since the last page was 'stolen'
2243 inactive_reclaim_run
= 0;
2246 * If it's clean and not precious, we can free the page.
2248 if (!m
->dirty
&& !m
->precious
) {
2250 if (page_prev_state
== PAGE_STATE_SPECULATIVE
)
2251 vm_pageout_speculative_clean
++;
2253 if (page_prev_state
== PAGE_STATE_ZEROFILL
)
2254 vm_pageout_inactive_zf
++;
2255 vm_pageout_inactive_clean
++;
2261 * The page may have been dirtied since the last check
2262 * for a throttled target queue (which may have been skipped
2263 * if the page was clean then). With the dirty page
2264 * disconnected here, we can make one final check.
2266 if (object
->internal
) {
2267 if (VM_PAGE_Q_THROTTLED(iq
))
2268 inactive_throttled
= TRUE
;
2269 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
2270 inactive_throttled
= TRUE
;
2273 if (inactive_throttled
== TRUE
) {
2275 * we set busy before issuing the pmap_disconnect,
2276 * so clear it and wakeup anyone that happened upon
2279 PAGE_WAKEUP_DONE(m
);
2280 goto throttle_inactive
;
2283 vm_pageout_stats
[vm_pageout_stat_now
].reclaimed
++;
2285 vm_pageout_cluster(m
);
2287 if (page_prev_state
== PAGE_STATE_ZEROFILL
)
2288 vm_pageout_inactive_zf
++;
2289 if (object
->internal
)
2290 vm_pageout_inactive_dirty_internal
++;
2292 vm_pageout_inactive_dirty_external
++;
2294 inactive_burst_count
= 0;
2296 done_with_inactivepage
:
2297 if (delayed_unlock
++ > delayed_unlock_limit
|| try_failed
== TRUE
) {
2299 if (object
!= NULL
) {
2300 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
2301 vm_object_unlock(object
);
2305 vm_page_unlock_queues();
2307 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
2308 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 4);
2310 vm_page_free_list(local_freeq
, TRUE
);
2312 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
2313 vm_page_free_count
, local_freed
, 0, 4);
2317 vm_page_lock_queues();
2319 lck_mtx_yield(&vm_page_queue_lock
);
2324 * back to top of pageout scan loop
2330 int vm_page_free_count_init
;
2333 vm_page_free_reserve(
2336 int free_after_reserve
;
2338 vm_page_free_reserved
+= pages
;
2340 if (vm_page_free_reserved
> VM_PAGE_FREE_RESERVED_LIMIT
)
2341 vm_page_free_reserved
= VM_PAGE_FREE_RESERVED_LIMIT
;
2343 free_after_reserve
= vm_page_free_count_init
- vm_page_free_reserved
;
2345 vm_page_free_min
= vm_page_free_reserved
+
2346 VM_PAGE_FREE_MIN(free_after_reserve
);
2348 if (vm_page_free_min
> VM_PAGE_FREE_MIN_LIMIT
)
2349 vm_page_free_min
= VM_PAGE_FREE_MIN_LIMIT
;
2351 vm_page_free_target
= vm_page_free_reserved
+
2352 VM_PAGE_FREE_TARGET(free_after_reserve
);
2354 if (vm_page_free_target
> VM_PAGE_FREE_TARGET_LIMIT
)
2355 vm_page_free_target
= VM_PAGE_FREE_TARGET_LIMIT
;
2357 if (vm_page_free_target
< vm_page_free_min
+ 5)
2358 vm_page_free_target
= vm_page_free_min
+ 5;
2360 vm_page_throttle_limit
= vm_page_free_target
- (vm_page_free_target
/ 3);
2361 vm_page_creation_throttle
= vm_page_free_target
/ 2;
2365 * vm_pageout is the high level pageout daemon.
2369 vm_pageout_continue(void)
2371 DTRACE_VM2(pgrrun
, int, 1, (uint64_t *), NULL
);
2372 vm_pageout_scan_event_counter
++;
2374 /* we hold vm_page_queue_free_lock now */
2375 assert(vm_page_free_wanted
== 0);
2376 assert(vm_page_free_wanted_privileged
== 0);
2377 assert_wait((event_t
) &vm_page_free_wanted
, THREAD_UNINT
);
2378 lck_mtx_unlock(&vm_page_queue_free_lock
);
2380 counter(c_vm_pageout_block
++);
2381 thread_block((thread_continue_t
)vm_pageout_continue
);
2386 #ifdef FAKE_DEADLOCK
2388 #define FAKE_COUNT 5000
2390 int internal_count
= 0;
2391 int fake_deadlock
= 0;
2396 vm_pageout_iothread_continue(struct vm_pageout_queue
*q
)
2400 memory_object_t pager
;
2401 thread_t self
= current_thread();
2403 if ((vm_pageout_internal_iothread
!= THREAD_NULL
)
2404 && (self
== vm_pageout_external_iothread
)
2405 && (self
->options
& TH_OPT_VMPRIV
))
2406 self
->options
&= ~TH_OPT_VMPRIV
;
2408 vm_page_lockspin_queues();
2410 while ( !queue_empty(&q
->pgo_pending
) ) {
2413 queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
2414 if (m
->object
== slide_info
.slide_object
) {
2415 panic("slid page %p not allowed on this path\n", m
);
2418 m
->pageout_queue
= FALSE
;
2419 m
->pageq
.next
= NULL
;
2420 m
->pageq
.prev
= NULL
;
2421 vm_page_unlock_queues();
2423 #ifdef FAKE_DEADLOCK
2424 if (q
== &vm_pageout_queue_internal
) {
2430 if ((internal_count
== FAKE_COUNT
)) {
2432 pg_count
= vm_page_free_count
+ vm_page_free_reserved
;
2434 if (kmem_alloc(kernel_map
, &addr
, PAGE_SIZE
* pg_count
) == KERN_SUCCESS
) {
2435 kmem_free(kernel_map
, addr
, PAGE_SIZE
* pg_count
);
2444 vm_object_lock(object
);
2446 if (!object
->pager_initialized
) {
2449 * If there is no memory object for the page, create
2450 * one and hand it to the default pager.
2453 if (!object
->pager_initialized
)
2454 vm_object_collapse(object
,
2455 (vm_object_offset_t
) 0,
2457 if (!object
->pager_initialized
)
2458 vm_object_pager_create(object
);
2459 if (!object
->pager_initialized
) {
2461 * Still no pager for the object.
2462 * Reactivate the page.
2464 * Should only happen if there is no
2467 vm_page_lockspin_queues();
2469 vm_pageout_queue_steal(m
, TRUE
);
2470 vm_page_activate(m
);
2471 vm_pageout_dirty_no_pager
++;
2473 vm_page_unlock_queues();
2476 * And we are done with it.
2478 PAGE_WAKEUP_DONE(m
);
2480 vm_object_paging_end(object
);
2481 vm_object_unlock(object
);
2483 vm_page_lockspin_queues();
2487 pager
= object
->pager
;
2488 if (pager
== MEMORY_OBJECT_NULL
) {
2490 * This pager has been destroyed by either
2491 * memory_object_destroy or vm_object_destroy, and
2492 * so there is nowhere for the page to go.
2496 * Just free the page... VM_PAGE_FREE takes
2497 * care of cleaning up all the state...
2498 * including doing the vm_pageout_throttle_up
2502 vm_page_lockspin_queues();
2504 vm_pageout_queue_steal(m
, TRUE
);
2505 vm_page_activate(m
);
2507 vm_page_unlock_queues();
2510 * And we are done with it.
2512 PAGE_WAKEUP_DONE(m
);
2514 vm_object_paging_end(object
);
2515 vm_object_unlock(object
);
2517 vm_page_lockspin_queues();
2521 vm_object_unlock(object
);
2523 * we expect the paging_in_progress reference to have
2524 * already been taken on the object before it was added
2525 * to the appropriate pageout I/O queue... this will
2526 * keep the object from being terminated and/or the
2527 * paging_offset from changing until the I/O has
2528 * completed... therefore no need to lock the object to
2529 * pull the paging_offset from it.
2531 * Send the data to the pager.
2532 * any pageout clustering happens there
2534 memory_object_data_return(pager
,
2535 m
->offset
+ object
->paging_offset
,
2543 vm_object_lock(object
);
2544 vm_object_paging_end(object
);
2545 vm_object_unlock(object
);
2547 vm_page_lockspin_queues();
2549 assert_wait((event_t
) q
, THREAD_UNINT
);
2551 if (q
->pgo_throttled
== TRUE
&& !VM_PAGE_Q_THROTTLED(q
)) {
2552 q
->pgo_throttled
= FALSE
;
2553 thread_wakeup((event_t
) &q
->pgo_laundry
);
2555 if (q
->pgo_draining
== TRUE
&& q
->pgo_laundry
== 0) {
2556 q
->pgo_draining
= FALSE
;
2557 thread_wakeup((event_t
) (&q
->pgo_laundry
+1));
2559 q
->pgo_busy
= FALSE
;
2561 vm_page_unlock_queues();
2563 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_continue
, (void *) &q
->pgo_pending
);
2569 vm_pageout_iothread_external(void)
2571 thread_t self
= current_thread();
2573 self
->options
|= TH_OPT_VMPRIV
;
2575 vm_pageout_iothread_continue(&vm_pageout_queue_external
);
2581 vm_pageout_iothread_internal(void)
2583 thread_t self
= current_thread();
2585 self
->options
|= TH_OPT_VMPRIV
;
2587 vm_pageout_iothread_continue(&vm_pageout_queue_internal
);
2592 vm_set_buffer_cleanup_callout(boolean_t (*func
)(int))
2594 if (OSCompareAndSwapPtr(NULL
, func
, (void * volatile *) &consider_buffer_cache_collect
)) {
2595 return KERN_SUCCESS
;
2597 return KERN_FAILURE
; /* Already set */
2602 vm_pageout_garbage_collect(int collect
)
2605 boolean_t buf_large_zfree
= FALSE
;
2609 * consider_zone_gc should be last, because the other operations
2610 * might return memory to zones.
2612 consider_machine_collect();
2613 if (consider_buffer_cache_collect
!= NULL
) {
2614 buf_large_zfree
= (*consider_buffer_cache_collect
)(0);
2616 consider_zone_gc(buf_large_zfree
);
2618 consider_machine_adjust();
2619 consider_pressure_events();
2623 assert_wait((event_t
) &vm_pageout_garbage_collect
, THREAD_UNINT
);
2625 thread_block_parameter((thread_continue_t
) vm_pageout_garbage_collect
, (void *)1);
2634 thread_t self
= current_thread();
2636 kern_return_t result
;
2640 * Set thread privileges.
2644 self
->priority
= BASEPRI_PREEMPT
- 1;
2645 set_sched_pri(self
, self
->priority
);
2646 thread_unlock(self
);
2648 if (!self
->reserved_stack
)
2649 self
->reserved_stack
= self
->kernel_stack
;
2654 * Initialize some paging parameters.
2657 if (vm_pageout_idle_wait
== 0)
2658 vm_pageout_idle_wait
= VM_PAGEOUT_IDLE_WAIT
;
2660 if (vm_pageout_burst_wait
== 0)
2661 vm_pageout_burst_wait
= VM_PAGEOUT_BURST_WAIT
;
2663 if (vm_pageout_empty_wait
== 0)
2664 vm_pageout_empty_wait
= VM_PAGEOUT_EMPTY_WAIT
;
2666 if (vm_pageout_deadlock_wait
== 0)
2667 vm_pageout_deadlock_wait
= VM_PAGEOUT_DEADLOCK_WAIT
;
2669 if (vm_pageout_deadlock_relief
== 0)
2670 vm_pageout_deadlock_relief
= VM_PAGEOUT_DEADLOCK_RELIEF
;
2672 if (vm_pageout_inactive_relief
== 0)
2673 vm_pageout_inactive_relief
= VM_PAGEOUT_INACTIVE_RELIEF
;
2675 if (vm_pageout_burst_active_throttle
== 0)
2676 vm_pageout_burst_active_throttle
= VM_PAGEOUT_BURST_ACTIVE_THROTTLE
;
2678 if (vm_pageout_burst_inactive_throttle
== 0)
2679 vm_pageout_burst_inactive_throttle
= VM_PAGEOUT_BURST_INACTIVE_THROTTLE
;
2682 * Set kernel task to low backing store privileged
2685 task_lock(kernel_task
);
2686 kernel_task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
2687 task_unlock(kernel_task
);
2689 vm_page_free_count_init
= vm_page_free_count
;
2692 * even if we've already called vm_page_free_reserve
2693 * call it again here to insure that the targets are
2694 * accurately calculated (it uses vm_page_free_count_init)
2695 * calling it with an arg of 0 will not change the reserve
2696 * but will re-calculate free_min and free_target
2698 if (vm_page_free_reserved
< VM_PAGE_FREE_RESERVED(processor_count
)) {
2699 vm_page_free_reserve((VM_PAGE_FREE_RESERVED(processor_count
)) - vm_page_free_reserved
);
2701 vm_page_free_reserve(0);
2704 queue_init(&vm_pageout_queue_external
.pgo_pending
);
2705 vm_pageout_queue_external
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
2706 vm_pageout_queue_external
.pgo_laundry
= 0;
2707 vm_pageout_queue_external
.pgo_idle
= FALSE
;
2708 vm_pageout_queue_external
.pgo_busy
= FALSE
;
2709 vm_pageout_queue_external
.pgo_throttled
= FALSE
;
2710 vm_pageout_queue_external
.pgo_draining
= FALSE
;
2712 queue_init(&vm_pageout_queue_internal
.pgo_pending
);
2713 vm_pageout_queue_internal
.pgo_maxlaundry
= 0;
2714 vm_pageout_queue_internal
.pgo_laundry
= 0;
2715 vm_pageout_queue_internal
.pgo_idle
= FALSE
;
2716 vm_pageout_queue_internal
.pgo_busy
= FALSE
;
2717 vm_pageout_queue_internal
.pgo_throttled
= FALSE
;
2718 vm_pageout_queue_internal
.pgo_draining
= FALSE
;
2721 /* internal pageout thread started when default pager registered first time */
2722 /* external pageout and garbage collection threads started here */
2724 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_external
, NULL
,
2725 BASEPRI_PREEMPT
- 1,
2726 &vm_pageout_external_iothread
);
2727 if (result
!= KERN_SUCCESS
)
2728 panic("vm_pageout_iothread_external: create failed");
2730 thread_deallocate(vm_pageout_external_iothread
);
2732 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_garbage_collect
, NULL
,
2735 if (result
!= KERN_SUCCESS
)
2736 panic("vm_pageout_garbage_collect: create failed");
2738 thread_deallocate(thread
);
2740 vm_object_reaper_init();
2743 vm_pageout_continue();
2748 * The vm_pageout_continue() call above never returns, so the code below is never
2749 * executed. We take advantage of this to declare several DTrace VM related probe
2750 * points that our kernel doesn't have an analog for. These are probe points that
2751 * exist in Solaris and are in the DTrace documentation, so people may have written
2752 * scripts that use them. Declaring the probe points here means their scripts will
2753 * compile and execute which we want for portability of the scripts, but since this
2754 * section of code is never reached, the probe points will simply never fire. Yes,
2755 * this is basically a hack. The problem is the DTrace probe points were chosen with
2756 * Solaris specific VM events in mind, not portability to different VM implementations.
2759 DTRACE_VM2(execfree
, int, 1, (uint64_t *), NULL
);
2760 DTRACE_VM2(execpgin
, int, 1, (uint64_t *), NULL
);
2761 DTRACE_VM2(execpgout
, int, 1, (uint64_t *), NULL
);
2762 DTRACE_VM2(pgswapin
, int, 1, (uint64_t *), NULL
);
2763 DTRACE_VM2(pgswapout
, int, 1, (uint64_t *), NULL
);
2764 DTRACE_VM2(swapin
, int, 1, (uint64_t *), NULL
);
2765 DTRACE_VM2(swapout
, int, 1, (uint64_t *), NULL
);
2770 vm_pageout_internal_start(void)
2772 kern_return_t result
;
2774 vm_pageout_queue_internal
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
2775 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_internal
, NULL
, BASEPRI_PREEMPT
- 1, &vm_pageout_internal_iothread
);
2776 if (result
== KERN_SUCCESS
)
2777 thread_deallocate(vm_pageout_internal_iothread
);
2783 upl_create(int type
, int flags
, upl_size_t size
)
2786 int page_field_size
= 0;
2788 int upl_size
= sizeof(struct upl
);
2790 size
= round_page_32(size
);
2792 if (type
& UPL_CREATE_LITE
) {
2793 page_field_size
= (atop(size
) + 7) >> 3;
2794 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
2796 upl_flags
|= UPL_LITE
;
2798 if (type
& UPL_CREATE_INTERNAL
) {
2799 upl_size
+= (int) sizeof(struct upl_page_info
) * atop(size
);
2801 upl_flags
|= UPL_INTERNAL
;
2803 upl
= (upl_t
)kalloc(upl_size
+ page_field_size
);
2805 if (page_field_size
)
2806 bzero((char *)upl
+ upl_size
, page_field_size
);
2808 upl
->flags
= upl_flags
| flags
;
2809 upl
->src_object
= NULL
;
2810 upl
->kaddr
= (vm_offset_t
)0;
2812 upl
->map_object
= NULL
;
2814 upl
->ext_ref_count
= 0;
2815 upl
->highest_page
= 0;
2817 upl
->vector_upl
= NULL
;
2819 upl
->ubc_alias1
= 0;
2820 upl
->ubc_alias2
= 0;
2822 upl
->upl_creator
= current_thread();
2824 upl
->upl_commit_index
= 0;
2825 bzero(&upl
->upl_commit_records
[0], sizeof(upl
->upl_commit_records
));
2827 (void) OSBacktrace(&upl
->upl_create_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
2828 #endif /* UPL_DEBUG */
2834 upl_destroy(upl_t upl
)
2836 int page_field_size
; /* bit field in word size buf */
2839 if (upl
->ext_ref_count
) {
2840 panic("upl(%p) ext_ref_count", upl
);
2847 if (upl
->flags
& UPL_SHADOWED
) {
2848 object
= upl
->map_object
->shadow
;
2850 object
= upl
->map_object
;
2852 vm_object_lock(object
);
2853 queue_remove(&object
->uplq
, upl
, upl_t
, uplq
);
2854 vm_object_unlock(object
);
2856 #endif /* UPL_DEBUG */
2858 * drop a reference on the map_object whether or
2859 * not a pageout object is inserted
2861 if (upl
->flags
& UPL_SHADOWED
)
2862 vm_object_deallocate(upl
->map_object
);
2864 if (upl
->flags
& UPL_DEVICE_MEMORY
)
2868 page_field_size
= 0;
2870 if (upl
->flags
& UPL_LITE
) {
2871 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
2872 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
2874 upl_lock_destroy(upl
);
2875 upl
->vector_upl
= (vector_upl_t
) 0xfeedbeef;
2876 if (upl
->flags
& UPL_INTERNAL
) {
2878 sizeof(struct upl
) +
2879 (sizeof(struct upl_page_info
) * (size
/PAGE_SIZE
))
2882 kfree(upl
, sizeof(struct upl
) + page_field_size
);
2887 upl_deallocate(upl_t upl
)
2889 if (--upl
->ref_count
== 0) {
2890 if(vector_upl_is_valid(upl
))
2891 vector_upl_deallocate(upl
);
2896 #if DEVELOPMENT || DEBUG
2898 * Statistics about UPL enforcement of copy-on-write obligations.
2900 unsigned long upl_cow
= 0;
2901 unsigned long upl_cow_again
= 0;
2902 unsigned long upl_cow_pages
= 0;
2903 unsigned long upl_cow_again_pages
= 0;
2905 unsigned long iopl_cow
= 0;
2906 unsigned long iopl_cow_pages
= 0;
2910 * Routine: vm_object_upl_request
2912 * Cause the population of a portion of a vm_object.
2913 * Depending on the nature of the request, the pages
2914 * returned may be contain valid data or be uninitialized.
2915 * A page list structure, listing the physical pages
2916 * will be returned upon request.
2917 * This function is called by the file system or any other
2918 * supplier of backing store to a pager.
2919 * IMPORTANT NOTE: The caller must still respect the relationship
2920 * between the vm_object and its backing memory object. The
2921 * caller MUST NOT substitute changes in the backing file
2922 * without first doing a memory_object_lock_request on the
2923 * target range unless it is know that the pages are not
2924 * shared with another entity at the pager level.
2926 * if a page list structure is present
2927 * return the mapped physical pages, where a
2928 * page is not present, return a non-initialized
2929 * one. If the no_sync bit is turned on, don't
2930 * call the pager unlock to synchronize with other
2931 * possible copies of the page. Leave pages busy
2932 * in the original object, if a page list structure
2933 * was specified. When a commit of the page list
2934 * pages is done, the dirty bit will be set for each one.
2936 * If a page list structure is present, return
2937 * all mapped pages. Where a page does not exist
2938 * map a zero filled one. Leave pages busy in
2939 * the original object. If a page list structure
2940 * is not specified, this call is a no-op.
2942 * Note: access of default pager objects has a rather interesting
2943 * twist. The caller of this routine, presumably the file system
2944 * page cache handling code, will never actually make a request
2945 * against a default pager backed object. Only the default
2946 * pager will make requests on backing store related vm_objects
2947 * In this way the default pager can maintain the relationship
2948 * between backing store files (abstract memory objects) and
2949 * the vm_objects (cache objects), they support.
2953 __private_extern__ kern_return_t
2954 vm_object_upl_request(
2956 vm_object_offset_t offset
,
2959 upl_page_info_array_t user_page_list
,
2960 unsigned int *page_list_count
,
2963 vm_page_t dst_page
= VM_PAGE_NULL
;
2964 vm_object_offset_t dst_offset
;
2965 upl_size_t xfer_size
;
2966 unsigned int size_in_pages
;
2971 #if MACH_CLUSTER_STATS
2972 boolean_t encountered_lrp
= FALSE
;
2974 vm_page_t alias_page
= NULL
;
2975 int refmod_state
= 0;
2976 wpl_array_t lite_list
= NULL
;
2977 vm_object_t last_copy_object
;
2978 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
2979 struct vm_page_delayed_work
*dwp
;
2983 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
2985 * For forward compatibility's sake,
2986 * reject any unknown flag.
2988 return KERN_INVALID_VALUE
;
2990 if ( (!object
->internal
) && (object
->paging_offset
!= 0) )
2991 panic("vm_object_upl_request: external object with non-zero paging offset\n");
2992 if (object
->phys_contiguous
)
2993 panic("vm_object_upl_request: contiguous object specified\n");
2996 if ((size
/ PAGE_SIZE
) > MAX_UPL_SIZE
)
2997 size
= MAX_UPL_SIZE
* PAGE_SIZE
;
2999 if ( (cntrl_flags
& UPL_SET_INTERNAL
) && page_list_count
!= NULL
)
3000 *page_list_count
= MAX_UPL_SIZE
;
3002 if (cntrl_flags
& UPL_SET_INTERNAL
) {
3003 if (cntrl_flags
& UPL_SET_LITE
) {
3005 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
, 0, size
);
3007 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
3008 lite_list
= (wpl_array_t
)
3009 (((uintptr_t)user_page_list
) +
3010 ((size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
3012 user_page_list
= NULL
;
3016 upl
= upl_create(UPL_CREATE_INTERNAL
, 0, size
);
3018 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
3020 user_page_list
= NULL
;
3024 if (cntrl_flags
& UPL_SET_LITE
) {
3026 upl
= upl_create(UPL_CREATE_EXTERNAL
| UPL_CREATE_LITE
, 0, size
);
3028 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
3033 upl
= upl_create(UPL_CREATE_EXTERNAL
, 0, size
);
3039 user_page_list
[0].device
= FALSE
;
3041 if (cntrl_flags
& UPL_SET_LITE
) {
3042 upl
->map_object
= object
;
3044 upl
->map_object
= vm_object_allocate(size
);
3046 * No neeed to lock the new object: nobody else knows
3047 * about it yet, so it's all ours so far.
3049 upl
->map_object
->shadow
= object
;
3050 upl
->map_object
->pageout
= TRUE
;
3051 upl
->map_object
->can_persist
= FALSE
;
3052 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
3053 upl
->map_object
->vo_shadow_offset
= offset
;
3054 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
3056 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
3058 upl
->flags
|= UPL_SHADOWED
;
3062 * Just mark the UPL as "encrypted" here.
3063 * We'll actually encrypt the pages later,
3064 * in upl_encrypt(), when the caller has
3065 * selected which pages need to go to swap.
3067 if (cntrl_flags
& UPL_ENCRYPT
)
3068 upl
->flags
|= UPL_ENCRYPTED
;
3070 if (cntrl_flags
& UPL_FOR_PAGEOUT
)
3071 upl
->flags
|= UPL_PAGEOUT
;
3073 vm_object_lock(object
);
3074 vm_object_activity_begin(object
);
3077 * we can lock in the paging_offset once paging_in_progress is set
3080 upl
->offset
= offset
+ object
->paging_offset
;
3083 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
3084 #endif /* UPL_DEBUG */
3086 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= VM_OBJECT_NULL
) {
3088 * Honor copy-on-write obligations
3090 * The caller is gathering these pages and
3091 * might modify their contents. We need to
3092 * make sure that the copy object has its own
3093 * private copies of these pages before we let
3094 * the caller modify them.
3096 vm_object_update(object
,
3101 FALSE
, /* should_return */
3102 MEMORY_OBJECT_COPY_SYNC
,
3104 #if DEVELOPMENT || DEBUG
3106 upl_cow_pages
+= size
>> PAGE_SHIFT
;
3110 * remember which copy object we synchronized with
3112 last_copy_object
= object
->copy
;
3116 dst_offset
= offset
;
3117 size_in_pages
= size
/ PAGE_SIZE
;
3121 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
3123 if (vm_page_free_count
> (vm_page_free_target
+ size_in_pages
) ||
3124 object
->resident_page_count
< (MAX_UPL_SIZE
* 2))
3125 object
->scan_collisions
= 0;
3131 if ((alias_page
== NULL
) && !(cntrl_flags
& UPL_SET_LITE
)) {
3132 vm_object_unlock(object
);
3133 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
3134 vm_object_lock(object
);
3136 if (cntrl_flags
& UPL_COPYOUT_FROM
) {
3137 upl
->flags
|= UPL_PAGE_SYNC_DONE
;
3139 if ( ((dst_page
= vm_page_lookup(object
, dst_offset
)) == VM_PAGE_NULL
) ||
3140 dst_page
->fictitious
||
3143 (VM_PAGE_WIRED(dst_page
) && !dst_page
->pageout
&& !dst_page
->list_req_pending
)) {
3146 user_page_list
[entry
].phys_addr
= 0;
3151 * grab this up front...
3152 * a high percentange of the time we're going to
3153 * need the hardware modification state a bit later
3154 * anyway... so we can eliminate an extra call into
3155 * the pmap layer by grabbing it here and recording it
3157 if (dst_page
->pmapped
)
3158 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
3162 if ( (refmod_state
& VM_MEM_REFERENCED
) && dst_page
->inactive
) {
3164 * page is on inactive list and referenced...
3165 * reactivate it now... this gets it out of the
3166 * way of vm_pageout_scan which would have to
3167 * reactivate it upon tripping over it
3169 dwp
->dw_mask
|= DW_vm_page_activate
;
3171 if (cntrl_flags
& UPL_RET_ONLY_DIRTY
) {
3173 * we're only asking for DIRTY pages to be returned
3175 if (dst_page
->list_req_pending
|| !(cntrl_flags
& UPL_FOR_PAGEOUT
)) {
3177 * if we were the page stolen by vm_pageout_scan to be
3178 * cleaned (as opposed to a buddy being clustered in
3179 * or this request is not being driven by a PAGEOUT cluster
3180 * then we only need to check for the page being dirty or
3181 * precious to decide whether to return it
3183 if (dst_page
->dirty
|| dst_page
->precious
|| (refmod_state
& VM_MEM_MODIFIED
))
3188 * this is a request for a PAGEOUT cluster and this page
3189 * is merely along for the ride as a 'buddy'... not only
3190 * does it have to be dirty to be returned, but it also
3191 * can't have been referenced recently... note that we've
3192 * already filtered above based on whether this page is
3193 * currently on the inactive queue or it meets the page
3194 * ticket (generation count) check
3196 if ( (cntrl_flags
& UPL_CLEAN_IN_PLACE
|| !(refmod_state
& VM_MEM_REFERENCED
) || dst_page
->throttled
) &&
3197 ((refmod_state
& VM_MEM_MODIFIED
) || dst_page
->dirty
|| dst_page
->precious
) ) {
3202 * if we reach here, we're not to return
3203 * the page... go on to the next one
3206 user_page_list
[entry
].phys_addr
= 0;
3211 if (dst_page
->busy
&& (!(dst_page
->list_req_pending
&& (dst_page
->pageout
|| dst_page
->cleaning
)))) {
3212 if (cntrl_flags
& UPL_NOBLOCK
) {
3214 user_page_list
[entry
].phys_addr
= 0;
3219 * someone else is playing with the
3220 * page. We will have to wait.
3222 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
3227 * Someone else already cleaning the page?
3229 if ((dst_page
->cleaning
|| dst_page
->absent
|| VM_PAGE_WIRED(dst_page
)) && !dst_page
->list_req_pending
) {
3231 user_page_list
[entry
].phys_addr
= 0;
3237 * The caller is gathering this page and might
3238 * access its contents later on. Decrypt the
3239 * page before adding it to the UPL, so that
3240 * the caller never sees encrypted data.
3242 if (! (cntrl_flags
& UPL_ENCRYPT
) && dst_page
->encrypted
) {
3246 * save the current state of busy
3247 * mark page as busy while decrypt
3248 * is in progress since it will drop
3249 * the object lock...
3251 was_busy
= dst_page
->busy
;
3252 dst_page
->busy
= TRUE
;
3254 vm_page_decrypt(dst_page
, 0);
3255 vm_page_decrypt_for_upl_counter
++;
3257 * restore to original busy state
3259 dst_page
->busy
= was_busy
;
3261 if (dst_page
->pageout_queue
== TRUE
) {
3263 vm_page_lockspin_queues();
3265 if (dst_page
->pageout_queue
== TRUE
) {
3267 * we've buddied up a page for a clustered pageout
3268 * that has already been moved to the pageout
3269 * queue by pageout_scan... we need to remove
3270 * it from the queue and drop the laundry count
3273 vm_pageout_throttle_up(dst_page
);
3275 vm_page_unlock_queues();
3277 #if MACH_CLUSTER_STATS
3279 * pageout statistics gathering. count
3280 * all the pages we will page out that
3281 * were not counted in the initial
3282 * vm_pageout_scan work
3284 if (dst_page
->list_req_pending
)
3285 encountered_lrp
= TRUE
;
3286 if ((dst_page
->dirty
|| (dst_page
->object
->internal
&& dst_page
->precious
)) && !dst_page
->list_req_pending
) {
3287 if (encountered_lrp
)
3288 CLUSTER_STAT(pages_at_higher_offsets
++;)
3290 CLUSTER_STAT(pages_at_lower_offsets
++;)
3294 * Turn off busy indication on pending
3295 * pageout. Note: we can only get here
3296 * in the request pending case.
3298 dst_page
->list_req_pending
= FALSE
;
3299 dst_page
->busy
= FALSE
;
3301 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
3302 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
3304 if (dst_page
->phys_page
> upl
->highest_page
)
3305 upl
->highest_page
= dst_page
->phys_page
;
3307 if (cntrl_flags
& UPL_SET_LITE
) {
3308 unsigned int pg_num
;
3310 pg_num
= (unsigned int) ((dst_offset
-offset
)/PAGE_SIZE
);
3311 assert(pg_num
== (dst_offset
-offset
)/PAGE_SIZE
);
3312 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
3315 pmap_clear_modify(dst_page
->phys_page
);
3318 * Mark original page as cleaning
3321 dst_page
->cleaning
= TRUE
;
3322 dst_page
->precious
= FALSE
;
3325 * use pageclean setup, it is more
3326 * convenient even for the pageout
3329 vm_object_lock(upl
->map_object
);
3330 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
3331 vm_object_unlock(upl
->map_object
);
3333 alias_page
->absent
= FALSE
;
3338 * Record that this page has been
3341 vm_external_state_set(object
->existence_map
, dst_page
->offset
);
3342 #endif /*MACH_PAGEMAP*/
3343 dst_page
->dirty
= dirty
;
3346 dst_page
->precious
= TRUE
;
3348 if (dst_page
->pageout
)
3349 dst_page
->busy
= TRUE
;
3351 if ( (cntrl_flags
& UPL_ENCRYPT
) ) {
3354 * We want to deny access to the target page
3355 * because its contents are about to be
3356 * encrypted and the user would be very
3357 * confused to see encrypted data instead
3359 * We also set "encrypted_cleaning" to allow
3360 * vm_pageout_scan() to demote that page
3361 * from "adjacent/clean-in-place" to
3362 * "target/clean-and-free" if it bumps into
3363 * this page during its scanning while we're
3364 * still processing this cluster.
3366 dst_page
->busy
= TRUE
;
3367 dst_page
->encrypted_cleaning
= TRUE
;
3369 if ( !(cntrl_flags
& UPL_CLEAN_IN_PLACE
) ) {
3371 * deny access to the target page
3372 * while it is being worked on
3374 if ((!dst_page
->pageout
) && ( !VM_PAGE_WIRED(dst_page
))) {
3375 dst_page
->busy
= TRUE
;
3376 dst_page
->pageout
= TRUE
;
3378 dwp
->dw_mask
|= DW_vm_page_wire
;
3382 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= last_copy_object
) {
3384 * Honor copy-on-write obligations
3386 * The copy object has changed since we
3387 * last synchronized for copy-on-write.
3388 * Another copy object might have been
3389 * inserted while we released the object's
3390 * lock. Since someone could have seen the
3391 * original contents of the remaining pages
3392 * through that new object, we have to
3393 * synchronize with it again for the remaining
3394 * pages only. The previous pages are "busy"
3395 * so they can not be seen through the new
3396 * mapping. The new mapping will see our
3397 * upcoming changes for those previous pages,
3398 * but that's OK since they couldn't see what
3399 * was there before. It's just a race anyway
3400 * and there's no guarantee of consistency or
3401 * atomicity. We just don't want new mappings
3402 * to see both the *before* and *after* pages.
3404 if (object
->copy
!= VM_OBJECT_NULL
) {
3407 dst_offset
,/* current offset */
3408 xfer_size
, /* remaining size */
3411 FALSE
, /* should_return */
3412 MEMORY_OBJECT_COPY_SYNC
,
3415 #if DEVELOPMENT || DEBUG
3417 upl_cow_again_pages
+= xfer_size
>> PAGE_SHIFT
;
3421 * remember the copy object we synced with
3423 last_copy_object
= object
->copy
;
3425 dst_page
= vm_page_lookup(object
, dst_offset
);
3427 if (dst_page
!= VM_PAGE_NULL
) {
3429 if ((cntrl_flags
& UPL_RET_ONLY_ABSENT
)) {
3431 if ( !(dst_page
->absent
&& dst_page
->list_req_pending
) ) {
3433 * skip over pages already present in the cache
3436 user_page_list
[entry
].phys_addr
= 0;
3441 if ( !(dst_page
->list_req_pending
) ) {
3443 if (dst_page
->cleaning
) {
3445 * someone else is writing to the page... wait...
3447 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
3452 if (dst_page
->fictitious
&&
3453 dst_page
->phys_page
== vm_page_fictitious_addr
) {
3454 assert( !dst_page
->speculative
);
3456 * dump the fictitious page
3458 dst_page
->list_req_pending
= FALSE
;
3460 VM_PAGE_FREE(dst_page
);
3464 } else if (dst_page
->absent
) {
3466 * the default_pager case
3468 dst_page
->list_req_pending
= FALSE
;
3469 PAGE_WAKEUP_DONE(dst_page
);
3471 } else if (dst_page
->pageout
|| dst_page
->cleaning
) {
3473 * page was earmarked by vm_pageout_scan
3474 * to be cleaned and stolen... we're going
3475 * to take it back since we are not attempting
3476 * to read that page and we don't want to stall
3477 * waiting for it to be cleaned for 2 reasons...
3478 * 1 - no use paging it out and back in
3479 * 2 - if we stall, we may casue a deadlock in
3480 * the FS trying to acquire the its locks
3481 * on the VNOP_PAGEOUT path presuming that
3482 * those locks are already held on the read
3483 * path before trying to create this UPL
3485 * so undo all of the state that vm_pageout_scan
3488 vm_pageout_queue_steal(dst_page
, FALSE
);
3489 PAGE_WAKEUP_DONE(dst_page
);
3493 if (dst_page
== VM_PAGE_NULL
) {
3494 if (object
->private) {
3496 * This is a nasty wrinkle for users
3497 * of upl who encounter device or
3498 * private memory however, it is
3499 * unavoidable, only a fault can
3500 * resolve the actual backing
3501 * physical page by asking the
3505 user_page_list
[entry
].phys_addr
= 0;
3509 if (object
->scan_collisions
) {
3511 * the pageout_scan thread is trying to steal
3512 * pages from this object, but has run into our
3513 * lock... grab 2 pages from the head of the object...
3514 * the first is freed on behalf of pageout_scan, the
3515 * 2nd is for our own use... we use vm_object_page_grab
3516 * in both cases to avoid taking pages from the free
3517 * list since we are under memory pressure and our
3518 * lock on this object is getting in the way of
3521 dst_page
= vm_object_page_grab(object
);
3523 if (dst_page
!= VM_PAGE_NULL
)
3524 vm_page_release(dst_page
);
3526 dst_page
= vm_object_page_grab(object
);
3528 if (dst_page
== VM_PAGE_NULL
) {
3530 * need to allocate a page
3532 dst_page
= vm_page_grab();
3534 if (dst_page
== VM_PAGE_NULL
) {
3535 if ( (cntrl_flags
& (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) == (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) {
3537 * we don't want to stall waiting for pages to come onto the free list
3538 * while we're already holding absent pages in this UPL
3539 * the caller will deal with the empty slots
3542 user_page_list
[entry
].phys_addr
= 0;
3547 * no pages available... wait
3548 * then try again for the same
3551 vm_object_unlock(object
);
3553 OSAddAtomic(size_in_pages
, &vm_upl_wait_for_pages
);
3555 VM_DEBUG_EVENT(vm_upl_page_wait
, VM_UPL_PAGE_WAIT
, DBG_FUNC_START
, vm_upl_wait_for_pages
, 0, 0, 0);
3558 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
3560 VM_DEBUG_EVENT(vm_upl_page_wait
, VM_UPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, 0);
3562 vm_object_lock(object
);
3566 vm_page_insert(dst_page
, object
, dst_offset
);
3568 dst_page
->absent
= TRUE
;
3569 dst_page
->busy
= FALSE
;
3571 if (cntrl_flags
& UPL_RET_ONLY_ABSENT
) {
3573 * if UPL_RET_ONLY_ABSENT was specified,
3574 * than we're definitely setting up a
3575 * upl for a clustered read/pagein
3576 * operation... mark the pages as clustered
3577 * so upl_commit_range can put them on the
3580 dst_page
->clustered
= TRUE
;
3583 if (dst_page
->fictitious
) {
3584 panic("need corner case for fictitious page");
3586 if (dst_page
->busy
) {
3588 * someone else is playing with the
3589 * page. We will have to wait.
3591 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
3598 if (cntrl_flags
& UPL_ENCRYPT
) {
3600 * The page is going to be encrypted when we
3601 * get it from the pager, so mark it so.
3603 dst_page
->encrypted
= TRUE
;
3606 * Otherwise, the page will not contain
3609 dst_page
->encrypted
= FALSE
;
3611 dst_page
->overwriting
= TRUE
;
3613 if (dst_page
->pmapped
) {
3614 if ( !(cntrl_flags
& UPL_FILE_IO
))
3616 * eliminate all mappings from the
3617 * original object and its prodigy
3619 refmod_state
= pmap_disconnect(dst_page
->phys_page
);
3621 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
3625 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
3626 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
3628 if (cntrl_flags
& UPL_SET_LITE
) {
3629 unsigned int pg_num
;
3631 pg_num
= (unsigned int) ((dst_offset
-offset
)/PAGE_SIZE
);
3632 assert(pg_num
== (dst_offset
-offset
)/PAGE_SIZE
);
3633 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
3636 pmap_clear_modify(dst_page
->phys_page
);
3639 * Mark original page as cleaning
3642 dst_page
->cleaning
= TRUE
;
3643 dst_page
->precious
= FALSE
;
3646 * use pageclean setup, it is more
3647 * convenient even for the pageout
3650 vm_object_lock(upl
->map_object
);
3651 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
3652 vm_object_unlock(upl
->map_object
);
3654 alias_page
->absent
= FALSE
;
3658 if (cntrl_flags
& UPL_REQUEST_SET_DIRTY
) {
3659 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
3660 upl
->flags
|= UPL_SET_DIRTY
;
3662 upl
->flags
|= UPL_SET_DIRTY
;
3663 } else if (cntrl_flags
& UPL_CLEAN_IN_PLACE
) {
3665 * clean in place for read implies
3666 * that a write will be done on all
3667 * the pages that are dirty before
3668 * a upl commit is done. The caller
3669 * is obligated to preserve the
3670 * contents of all pages marked dirty
3672 upl
->flags
|= UPL_CLEAR_DIRTY
;
3674 dst_page
->dirty
= dirty
;
3677 dst_page
->precious
= TRUE
;
3679 if ( !VM_PAGE_WIRED(dst_page
)) {
3681 * deny access to the target page while
3682 * it is being worked on
3684 dst_page
->busy
= TRUE
;
3686 dwp
->dw_mask
|= DW_vm_page_wire
;
3689 * We might be about to satisfy a fault which has been
3690 * requested. So no need for the "restart" bit.
3692 dst_page
->restart
= FALSE
;
3693 if (!dst_page
->absent
&& !(cntrl_flags
& UPL_WILL_MODIFY
)) {
3695 * expect the page to be used
3697 dwp
->dw_mask
|= DW_set_reference
;
3699 if (cntrl_flags
& UPL_PRECIOUS
) {
3700 if (dst_page
->object
->internal
) {
3701 dst_page
->dirty
= TRUE
;
3702 dst_page
->precious
= FALSE
;
3704 dst_page
->precious
= TRUE
;
3707 dst_page
->precious
= FALSE
;
3711 upl
->flags
|= UPL_HAS_BUSY
;
3713 if (dst_page
->phys_page
> upl
->highest_page
)
3714 upl
->highest_page
= dst_page
->phys_page
;
3715 if (user_page_list
) {
3716 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
3717 user_page_list
[entry
].pageout
= dst_page
->pageout
;
3718 user_page_list
[entry
].absent
= dst_page
->absent
;
3719 user_page_list
[entry
].dirty
= dst_page
->dirty
;
3720 user_page_list
[entry
].precious
= dst_page
->precious
;
3721 user_page_list
[entry
].device
= FALSE
;
3722 if (dst_page
->clustered
== TRUE
)
3723 user_page_list
[entry
].speculative
= dst_page
->speculative
;
3725 user_page_list
[entry
].speculative
= FALSE
;
3726 user_page_list
[entry
].cs_validated
= dst_page
->cs_validated
;
3727 user_page_list
[entry
].cs_tainted
= dst_page
->cs_tainted
;
3730 * if UPL_RET_ONLY_ABSENT is set, then
3731 * we are working with a fresh page and we've
3732 * just set the clustered flag on it to
3733 * indicate that it was drug in as part of a
3734 * speculative cluster... so leave it alone
3736 if ( !(cntrl_flags
& UPL_RET_ONLY_ABSENT
)) {
3738 * someone is explicitly grabbing this page...
3739 * update clustered and speculative state
3742 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
3746 if (dwp
->dw_mask
& DW_vm_page_activate
)
3747 VM_STAT_INCR(reactivations
);
3749 VM_PAGE_ADD_DELAYED_WORK(dwp
, dst_page
, dw_count
);
3751 if (dw_count
>= dw_limit
) {
3752 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
3759 dst_offset
+= PAGE_SIZE_64
;
3760 xfer_size
-= PAGE_SIZE
;
3763 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
3765 if (alias_page
!= NULL
) {
3766 VM_PAGE_FREE(alias_page
);
3769 if (page_list_count
!= NULL
) {
3770 if (upl
->flags
& UPL_INTERNAL
)
3771 *page_list_count
= 0;
3772 else if (*page_list_count
> entry
)
3773 *page_list_count
= entry
;
3778 vm_object_unlock(object
);
3780 return KERN_SUCCESS
;
3783 /* JMM - Backward compatability for now */
3785 vm_fault_list_request( /* forward */
3786 memory_object_control_t control
,
3787 vm_object_offset_t offset
,
3790 upl_page_info_t
**user_page_list_ptr
,
3791 unsigned int page_list_count
,
3794 vm_fault_list_request(
3795 memory_object_control_t control
,
3796 vm_object_offset_t offset
,
3799 upl_page_info_t
**user_page_list_ptr
,
3800 unsigned int page_list_count
,
3803 unsigned int local_list_count
;
3804 upl_page_info_t
*user_page_list
;
3807 if((cntrl_flags
& UPL_VECTOR
)==UPL_VECTOR
)
3808 return KERN_INVALID_ARGUMENT
;
3810 if (user_page_list_ptr
!= NULL
) {
3811 local_list_count
= page_list_count
;
3812 user_page_list
= *user_page_list_ptr
;
3814 local_list_count
= 0;
3815 user_page_list
= NULL
;
3817 kr
= memory_object_upl_request(control
,
3825 if(kr
!= KERN_SUCCESS
)
3828 if ((user_page_list_ptr
!= NULL
) && (cntrl_flags
& UPL_INTERNAL
)) {
3829 *user_page_list_ptr
= UPL_GET_INTERNAL_PAGE_LIST(*upl_ptr
);
3832 return KERN_SUCCESS
;
3838 * Routine: vm_object_super_upl_request
3840 * Cause the population of a portion of a vm_object
3841 * in much the same way as memory_object_upl_request.
3842 * Depending on the nature of the request, the pages
3843 * returned may be contain valid data or be uninitialized.
3844 * However, the region may be expanded up to the super
3845 * cluster size provided.
3848 __private_extern__ kern_return_t
3849 vm_object_super_upl_request(
3851 vm_object_offset_t offset
,
3853 upl_size_t super_cluster
,
3855 upl_page_info_t
*user_page_list
,
3856 unsigned int *page_list_count
,
3859 if (object
->paging_offset
> offset
|| ((cntrl_flags
& UPL_VECTOR
)==UPL_VECTOR
))
3860 return KERN_FAILURE
;
3862 assert(object
->paging_in_progress
);
3863 offset
= offset
- object
->paging_offset
;
3865 if (super_cluster
> size
) {
3867 vm_object_offset_t base_offset
;
3868 upl_size_t super_size
;
3869 vm_object_size_t super_size_64
;
3871 base_offset
= (offset
& ~((vm_object_offset_t
) super_cluster
- 1));
3872 super_size
= (offset
+ size
) > (base_offset
+ super_cluster
) ? super_cluster
<<1 : super_cluster
;
3873 super_size_64
= ((base_offset
+ super_size
) > object
->vo_size
) ? (object
->vo_size
- base_offset
) : super_size
;
3874 super_size
= (upl_size_t
) super_size_64
;
3875 assert(super_size
== super_size_64
);
3877 if (offset
> (base_offset
+ super_size
)) {
3878 panic("vm_object_super_upl_request: Missed target pageout"
3879 " %#llx,%#llx, %#x, %#x, %#x, %#llx\n",
3880 offset
, base_offset
, super_size
, super_cluster
,
3881 size
, object
->paging_offset
);
3884 * apparently there is a case where the vm requests a
3885 * page to be written out who's offset is beyond the
3888 if ((offset
+ size
) > (base_offset
+ super_size
)) {
3889 super_size_64
= (offset
+ size
) - base_offset
;
3890 super_size
= (upl_size_t
) super_size_64
;
3891 assert(super_size
== super_size_64
);
3894 offset
= base_offset
;
3897 return vm_object_upl_request(object
, offset
, size
, upl
, user_page_list
, page_list_count
, cntrl_flags
);
3904 vm_map_address_t offset
,
3905 upl_size_t
*upl_size
,
3907 upl_page_info_array_t page_list
,
3908 unsigned int *count
,
3911 vm_map_entry_t entry
;
3913 int force_data_sync
;
3915 vm_object_t local_object
;
3916 vm_map_offset_t local_offset
;
3917 vm_map_offset_t local_start
;
3920 caller_flags
= *flags
;
3922 if (caller_flags
& ~UPL_VALID_FLAGS
) {
3924 * For forward compatibility's sake,
3925 * reject any unknown flag.
3927 return KERN_INVALID_VALUE
;
3929 force_data_sync
= (caller_flags
& UPL_FORCE_DATA_SYNC
);
3930 sync_cow_data
= !(caller_flags
& UPL_COPYOUT_FROM
);
3933 return KERN_INVALID_ARGUMENT
;
3936 vm_map_lock_read(map
);
3938 if (vm_map_lookup_entry(map
, offset
, &entry
)) {
3940 if ((entry
->vme_end
- offset
) < *upl_size
) {
3941 *upl_size
= (upl_size_t
) (entry
->vme_end
- offset
);
3942 assert(*upl_size
== entry
->vme_end
- offset
);
3945 if (caller_flags
& UPL_QUERY_OBJECT_TYPE
) {
3948 if ( !entry
->is_sub_map
&& entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
3949 if (entry
->object
.vm_object
->private)
3950 *flags
= UPL_DEV_MEMORY
;
3952 if (entry
->object
.vm_object
->phys_contiguous
)
3953 *flags
|= UPL_PHYS_CONTIG
;
3955 vm_map_unlock_read(map
);
3957 return KERN_SUCCESS
;
3959 if (entry
->object
.vm_object
== VM_OBJECT_NULL
|| !entry
->object
.vm_object
->phys_contiguous
) {
3960 if ((*upl_size
/PAGE_SIZE
) > MAX_UPL_SIZE
)
3961 *upl_size
= MAX_UPL_SIZE
* PAGE_SIZE
;
3964 * Create an object if necessary.
3966 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
3968 if (vm_map_lock_read_to_write(map
))
3969 goto REDISCOVER_ENTRY
;
3971 entry
->object
.vm_object
= vm_object_allocate((vm_size_t
)(entry
->vme_end
- entry
->vme_start
));
3974 vm_map_lock_write_to_read(map
);
3976 if (!(caller_flags
& UPL_COPYOUT_FROM
)) {
3977 if (!(entry
->protection
& VM_PROT_WRITE
)) {
3978 vm_map_unlock_read(map
);
3979 return KERN_PROTECTION_FAILURE
;
3981 if (entry
->needs_copy
) {
3983 * Honor copy-on-write for COPY_SYMMETRIC
3988 vm_object_offset_t new_offset
;
3991 vm_map_version_t version
;
3996 if (vm_map_lookup_locked(&local_map
,
3997 offset
, VM_PROT_WRITE
,
3998 OBJECT_LOCK_EXCLUSIVE
,
4000 &new_offset
, &prot
, &wired
,
4002 &real_map
) != KERN_SUCCESS
) {
4003 vm_map_unlock_read(local_map
);
4004 return KERN_FAILURE
;
4006 if (real_map
!= map
)
4007 vm_map_unlock(real_map
);
4008 vm_map_unlock_read(local_map
);
4010 vm_object_unlock(object
);
4012 goto REDISCOVER_ENTRY
;
4015 if (entry
->is_sub_map
) {
4018 submap
= entry
->object
.sub_map
;
4019 local_start
= entry
->vme_start
;
4020 local_offset
= entry
->offset
;
4022 vm_map_reference(submap
);
4023 vm_map_unlock_read(map
);
4025 ret
= vm_map_create_upl(submap
,
4026 local_offset
+ (offset
- local_start
),
4027 upl_size
, upl
, page_list
, count
, flags
);
4028 vm_map_deallocate(submap
);
4032 if (sync_cow_data
) {
4033 if (entry
->object
.vm_object
->shadow
|| entry
->object
.vm_object
->copy
) {
4034 local_object
= entry
->object
.vm_object
;
4035 local_start
= entry
->vme_start
;
4036 local_offset
= entry
->offset
;
4038 vm_object_reference(local_object
);
4039 vm_map_unlock_read(map
);
4041 if (local_object
->shadow
&& local_object
->copy
) {
4042 vm_object_lock_request(
4043 local_object
->shadow
,
4044 (vm_object_offset_t
)
4045 ((offset
- local_start
) +
4047 local_object
->vo_shadow_offset
,
4049 MEMORY_OBJECT_DATA_SYNC
,
4052 sync_cow_data
= FALSE
;
4053 vm_object_deallocate(local_object
);
4055 goto REDISCOVER_ENTRY
;
4058 if (force_data_sync
) {
4059 local_object
= entry
->object
.vm_object
;
4060 local_start
= entry
->vme_start
;
4061 local_offset
= entry
->offset
;
4063 vm_object_reference(local_object
);
4064 vm_map_unlock_read(map
);
4066 vm_object_lock_request(
4068 (vm_object_offset_t
)
4069 ((offset
- local_start
) + local_offset
),
4070 (vm_object_size_t
)*upl_size
, FALSE
,
4071 MEMORY_OBJECT_DATA_SYNC
,
4074 force_data_sync
= FALSE
;
4075 vm_object_deallocate(local_object
);
4077 goto REDISCOVER_ENTRY
;
4079 if (entry
->object
.vm_object
->private)
4080 *flags
= UPL_DEV_MEMORY
;
4084 if (entry
->object
.vm_object
->phys_contiguous
)
4085 *flags
|= UPL_PHYS_CONTIG
;
4087 local_object
= entry
->object
.vm_object
;
4088 local_offset
= entry
->offset
;
4089 local_start
= entry
->vme_start
;
4091 vm_object_reference(local_object
);
4092 vm_map_unlock_read(map
);
4094 ret
= vm_object_iopl_request(local_object
,
4095 (vm_object_offset_t
) ((offset
- local_start
) + local_offset
),
4101 vm_object_deallocate(local_object
);
4105 vm_map_unlock_read(map
);
4107 return(KERN_FAILURE
);
4111 * Internal routine to enter a UPL into a VM map.
4113 * JMM - This should just be doable through the standard
4114 * vm_map_enter() API.
4120 vm_map_offset_t
*dst_addr
)
4123 vm_object_offset_t offset
;
4124 vm_map_offset_t addr
;
4127 int isVectorUPL
= 0, curr_upl
=0;
4128 upl_t vector_upl
= NULL
;
4129 vm_offset_t vector_upl_dst_addr
= 0;
4130 vm_map_t vector_upl_submap
= NULL
;
4131 upl_offset_t subupl_offset
= 0;
4132 upl_size_t subupl_size
= 0;
4134 if (upl
== UPL_NULL
)
4135 return KERN_INVALID_ARGUMENT
;
4137 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
4138 int mapped
=0,valid_upls
=0;
4141 upl_lock(vector_upl
);
4142 for(curr_upl
=0; curr_upl
< MAX_VECTOR_UPL_ELEMENTS
; curr_upl
++) {
4143 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
);
4147 if (UPL_PAGE_LIST_MAPPED
& upl
->flags
)
4152 if(mapped
!= valid_upls
)
4153 panic("Only %d of the %d sub-upls within the Vector UPL are alread mapped\n", mapped
, valid_upls
);
4155 upl_unlock(vector_upl
);
4156 return KERN_FAILURE
;
4160 kr
= kmem_suballoc(map
, &vector_upl_dst_addr
, vector_upl
->size
, FALSE
, VM_FLAGS_ANYWHERE
, &vector_upl_submap
);
4161 if( kr
!= KERN_SUCCESS
)
4162 panic("Vector UPL submap allocation failed\n");
4163 map
= vector_upl_submap
;
4164 vector_upl_set_submap(vector_upl
, vector_upl_submap
, vector_upl_dst_addr
);
4170 process_upl_to_enter
:
4172 if(curr_upl
== MAX_VECTOR_UPL_ELEMENTS
) {
4173 *dst_addr
= vector_upl_dst_addr
;
4174 upl_unlock(vector_upl
);
4175 return KERN_SUCCESS
;
4177 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
++ );
4179 goto process_upl_to_enter
;
4181 vector_upl_get_iostate(vector_upl
, upl
, &subupl_offset
, &subupl_size
);
4182 *dst_addr
= (vm_map_offset_t
)(vector_upl_dst_addr
+ (vm_map_offset_t
)subupl_offset
);
4185 * check to see if already mapped
4187 if (UPL_PAGE_LIST_MAPPED
& upl
->flags
) {
4189 return KERN_FAILURE
;
4192 if ((!(upl
->flags
& UPL_SHADOWED
)) &&
4193 ((upl
->flags
& UPL_HAS_BUSY
) ||
4194 !((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) || (upl
->map_object
->phys_contiguous
)))) {
4197 vm_page_t alias_page
;
4198 vm_object_offset_t new_offset
;
4199 unsigned int pg_num
;
4200 wpl_array_t lite_list
;
4202 if (upl
->flags
& UPL_INTERNAL
) {
4203 lite_list
= (wpl_array_t
)
4204 ((((uintptr_t)upl
) + sizeof(struct upl
))
4205 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4207 lite_list
= (wpl_array_t
)(((uintptr_t)upl
) + sizeof(struct upl
));
4209 object
= upl
->map_object
;
4210 upl
->map_object
= vm_object_allocate(upl
->size
);
4212 vm_object_lock(upl
->map_object
);
4214 upl
->map_object
->shadow
= object
;
4215 upl
->map_object
->pageout
= TRUE
;
4216 upl
->map_object
->can_persist
= FALSE
;
4217 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
4218 upl
->map_object
->vo_shadow_offset
= upl
->offset
- object
->paging_offset
;
4219 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
4220 offset
= upl
->map_object
->vo_shadow_offset
;
4224 upl
->flags
|= UPL_SHADOWED
;
4227 pg_num
= (unsigned int) (new_offset
/ PAGE_SIZE
);
4228 assert(pg_num
== new_offset
/ PAGE_SIZE
);
4230 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
4232 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
4234 vm_object_lock(object
);
4236 m
= vm_page_lookup(object
, offset
);
4237 if (m
== VM_PAGE_NULL
) {
4238 panic("vm_upl_map: page missing\n");
4242 * Convert the fictitious page to a private
4243 * shadow of the real page.
4245 assert(alias_page
->fictitious
);
4246 alias_page
->fictitious
= FALSE
;
4247 alias_page
->private = TRUE
;
4248 alias_page
->pageout
= TRUE
;
4250 * since m is a page in the upl it must
4251 * already be wired or BUSY, so it's
4252 * safe to assign the underlying physical
4255 alias_page
->phys_page
= m
->phys_page
;
4257 vm_object_unlock(object
);
4259 vm_page_lockspin_queues();
4260 vm_page_wire(alias_page
);
4261 vm_page_unlock_queues();
4265 * The virtual page ("m") has to be wired in some way
4266 * here or its physical page ("m->phys_page") could
4267 * be recycled at any time.
4268 * Assuming this is enforced by the caller, we can't
4269 * get an encrypted page here. Since the encryption
4270 * key depends on the VM page's "pager" object and
4271 * the "paging_offset", we couldn't handle 2 pageable
4272 * VM pages (with different pagers and paging_offsets)
4273 * sharing the same physical page: we could end up
4274 * encrypting with one key (via one VM page) and
4275 * decrypting with another key (via the alias VM page).
4277 ASSERT_PAGE_DECRYPTED(m
);
4279 vm_page_insert(alias_page
, upl
->map_object
, new_offset
);
4281 assert(!alias_page
->wanted
);
4282 alias_page
->busy
= FALSE
;
4283 alias_page
->absent
= FALSE
;
4286 offset
+= PAGE_SIZE_64
;
4287 new_offset
+= PAGE_SIZE_64
;
4289 vm_object_unlock(upl
->map_object
);
4291 if (upl
->flags
& UPL_SHADOWED
)
4294 offset
= upl
->offset
- upl
->map_object
->paging_offset
;
4298 vm_object_reference(upl
->map_object
);
4303 * NEED A UPL_MAP ALIAS
4305 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
4306 VM_FLAGS_ANYWHERE
, upl
->map_object
, offset
, FALSE
,
4307 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
4309 if (kr
!= KERN_SUCCESS
) {
4315 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
4316 VM_FLAGS_FIXED
, upl
->map_object
, offset
, FALSE
,
4317 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
4319 panic("vm_map_enter failed for a Vector UPL\n");
4321 vm_object_lock(upl
->map_object
);
4323 for (addr
= *dst_addr
; size
> 0; size
-= PAGE_SIZE
, addr
+= PAGE_SIZE
) {
4324 m
= vm_page_lookup(upl
->map_object
, offset
);
4329 /* CODE SIGNING ENFORCEMENT: page has been wpmapped,
4330 * but only in kernel space. If this was on a user map,
4331 * we'd have to set the wpmapped bit. */
4332 /* m->wpmapped = TRUE; */
4333 assert(map
==kernel_map
);
4335 PMAP_ENTER(map
->pmap
, addr
, m
, VM_PROT_ALL
, 0, TRUE
);
4337 offset
+= PAGE_SIZE_64
;
4339 vm_object_unlock(upl
->map_object
);
4342 * hold a reference for the mapping
4345 upl
->flags
|= UPL_PAGE_LIST_MAPPED
;
4346 upl
->kaddr
= (vm_offset_t
) *dst_addr
;
4347 assert(upl
->kaddr
== *dst_addr
);
4350 goto process_upl_to_enter
;
4354 return KERN_SUCCESS
;
4358 * Internal routine to remove a UPL mapping from a VM map.
4360 * XXX - This should just be doable through a standard
4361 * vm_map_remove() operation. Otherwise, implicit clean-up
4362 * of the target map won't be able to correctly remove
4363 * these (and release the reference on the UPL). Having
4364 * to do this means we can't map these into user-space
4374 int isVectorUPL
= 0, curr_upl
= 0;
4375 upl_t vector_upl
= NULL
;
4377 if (upl
== UPL_NULL
)
4378 return KERN_INVALID_ARGUMENT
;
4380 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
4381 int unmapped
=0, valid_upls
=0;
4383 upl_lock(vector_upl
);
4384 for(curr_upl
=0; curr_upl
< MAX_VECTOR_UPL_ELEMENTS
; curr_upl
++) {
4385 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
);
4389 if (!(UPL_PAGE_LIST_MAPPED
& upl
->flags
))
4394 if(unmapped
!= valid_upls
)
4395 panic("%d of the %d sub-upls within the Vector UPL is/are not mapped\n", unmapped
, valid_upls
);
4397 upl_unlock(vector_upl
);
4398 return KERN_FAILURE
;
4406 process_upl_to_remove
:
4408 if(curr_upl
== MAX_VECTOR_UPL_ELEMENTS
) {
4409 vm_map_t v_upl_submap
;
4410 vm_offset_t v_upl_submap_dst_addr
;
4411 vector_upl_get_submap(vector_upl
, &v_upl_submap
, &v_upl_submap_dst_addr
);
4413 vm_map_remove(map
, v_upl_submap_dst_addr
, v_upl_submap_dst_addr
+ vector_upl
->size
, VM_MAP_NO_FLAGS
);
4414 vm_map_deallocate(v_upl_submap
);
4415 upl_unlock(vector_upl
);
4416 return KERN_SUCCESS
;
4419 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
++ );
4421 goto process_upl_to_remove
;
4424 if (upl
->flags
& UPL_PAGE_LIST_MAPPED
) {
4428 assert(upl
->ref_count
> 1);
4429 upl
->ref_count
--; /* removing mapping ref */
4431 upl
->flags
&= ~UPL_PAGE_LIST_MAPPED
;
4432 upl
->kaddr
= (vm_offset_t
) 0;
4438 vm_map_trunc_page(addr
),
4439 vm_map_round_page(addr
+ size
),
4442 return KERN_SUCCESS
;
4446 * If it's a Vectored UPL, we'll be removing the entire
4447 * submap anyways, so no need to remove individual UPL
4448 * element mappings from within the submap
4450 goto process_upl_to_remove
;
4455 return KERN_FAILURE
;
4462 upl_offset_t offset
,
4465 upl_page_info_t
*page_list
,
4466 mach_msg_type_number_t count
,
4469 upl_size_t xfer_size
, subupl_size
= size
;
4470 vm_object_t shadow_object
;
4472 vm_object_offset_t target_offset
;
4473 upl_offset_t subupl_offset
= offset
;
4475 wpl_array_t lite_list
;
4477 int clear_refmod
= 0;
4478 int pgpgout_count
= 0;
4479 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
4480 struct vm_page_delayed_work
*dwp
;
4483 int isVectorUPL
= 0;
4484 upl_t vector_upl
= NULL
;
4485 boolean_t should_be_throttled
= FALSE
;
4489 if (upl
== UPL_NULL
)
4490 return KERN_INVALID_ARGUMENT
;
4495 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
4497 upl_lock(vector_upl
);
4502 process_upl_to_commit
:
4506 offset
= subupl_offset
;
4508 upl_unlock(vector_upl
);
4509 return KERN_SUCCESS
;
4511 upl
= vector_upl_subupl_byoffset(vector_upl
, &offset
, &size
);
4513 upl_unlock(vector_upl
);
4514 return KERN_FAILURE
;
4516 page_list
= UPL_GET_INTERNAL_PAGE_LIST_SIMPLE(upl
);
4517 subupl_size
-= size
;
4518 subupl_offset
+= size
;
4522 if (upl
->upl_commit_index
< UPL_DEBUG_COMMIT_RECORDS
) {
4523 (void) OSBacktrace(&upl
->upl_commit_records
[upl
->upl_commit_index
].c_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
4525 upl
->upl_commit_records
[upl
->upl_commit_index
].c_beg
= offset
;
4526 upl
->upl_commit_records
[upl
->upl_commit_index
].c_end
= (offset
+ size
);
4528 upl
->upl_commit_index
++;
4531 if (upl
->flags
& UPL_DEVICE_MEMORY
)
4533 else if ((offset
+ size
) <= upl
->size
)
4539 upl_unlock(vector_upl
);
4541 return KERN_FAILURE
;
4543 if (upl
->flags
& UPL_SET_DIRTY
)
4544 flags
|= UPL_COMMIT_SET_DIRTY
;
4545 if (upl
->flags
& UPL_CLEAR_DIRTY
)
4546 flags
|= UPL_COMMIT_CLEAR_DIRTY
;
4548 if (upl
->flags
& UPL_INTERNAL
)
4549 lite_list
= (wpl_array_t
) ((((uintptr_t)upl
) + sizeof(struct upl
))
4550 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4552 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
4554 object
= upl
->map_object
;
4556 if (upl
->flags
& UPL_SHADOWED
) {
4557 vm_object_lock(object
);
4558 shadow_object
= object
->shadow
;
4560 shadow_object
= object
;
4562 entry
= offset
/PAGE_SIZE
;
4563 target_offset
= (vm_object_offset_t
)offset
;
4565 if (upl
->flags
& UPL_KERNEL_OBJECT
)
4566 vm_object_lock_shared(shadow_object
);
4568 vm_object_lock(shadow_object
);
4570 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
4571 assert(shadow_object
->blocked_access
);
4572 shadow_object
->blocked_access
= FALSE
;
4573 vm_object_wakeup(object
, VM_OBJECT_EVENT_UNBLOCKED
);
4576 if (shadow_object
->code_signed
) {
4579 * If the object is code-signed, do not let this UPL tell
4580 * us if the pages are valid or not. Let the pages be
4581 * validated by VM the normal way (when they get mapped or
4584 flags
&= ~UPL_COMMIT_CS_VALIDATED
;
4588 * No page list to get the code-signing info from !?
4590 flags
&= ~UPL_COMMIT_CS_VALIDATED
;
4592 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) && shadow_object
->internal
)
4593 should_be_throttled
= TRUE
;
4597 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
4607 if (upl
->flags
& UPL_LITE
) {
4608 unsigned int pg_num
;
4610 pg_num
= (unsigned int) (target_offset
/PAGE_SIZE
);
4611 assert(pg_num
== target_offset
/PAGE_SIZE
);
4613 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
4614 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
4616 if (!(upl
->flags
& UPL_KERNEL_OBJECT
))
4617 m
= vm_page_lookup(shadow_object
, target_offset
+ (upl
->offset
- shadow_object
->paging_offset
));
4620 if (upl
->flags
& UPL_SHADOWED
) {
4621 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
4627 if (m
== VM_PAGE_NULL
)
4628 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->vo_shadow_offset
);
4631 if ((upl
->flags
& UPL_KERNEL_OBJECT
) || m
== VM_PAGE_NULL
)
4632 goto commit_next_page
;
4634 if (flags
& UPL_COMMIT_CS_VALIDATED
) {
4637 * Set the code signing bits according to
4638 * what the UPL says they should be.
4640 m
->cs_validated
= page_list
[entry
].cs_validated
;
4641 m
->cs_tainted
= page_list
[entry
].cs_tainted
;
4643 if (upl
->flags
& UPL_IO_WIRE
) {
4646 page_list
[entry
].phys_addr
= 0;
4648 if (flags
& UPL_COMMIT_SET_DIRTY
) {
4650 } else if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
4653 if (! (flags
& UPL_COMMIT_CS_VALIDATED
) &&
4654 m
->cs_validated
&& !m
->cs_tainted
) {
4657 * This page is no longer dirty
4658 * but could have been modified,
4659 * so it will need to be
4662 m
->cs_validated
= FALSE
;
4663 #if DEVELOPMENT || DEBUG
4664 vm_cs_validated_resets
++;
4666 pmap_disconnect(m
->phys_page
);
4668 clear_refmod
|= VM_MEM_MODIFIED
;
4670 if (flags
& UPL_COMMIT_INACTIVATE
) {
4671 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
4672 clear_refmod
|= VM_MEM_REFERENCED
;
4674 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
4676 * We blocked access to the pages in this UPL.
4677 * Clear the "busy" bit and wake up any waiter
4680 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
4683 if (flags
& UPL_COMMIT_FREE_ABSENT
)
4684 dwp
->dw_mask
|= DW_vm_page_free
;
4687 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
4690 dwp
->dw_mask
|= DW_vm_page_unwire
;
4692 goto commit_next_page
;
4695 * make sure to clear the hardware
4696 * modify or reference bits before
4697 * releasing the BUSY bit on this page
4698 * otherwise we risk losing a legitimate
4701 if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
4704 if (! (flags
& UPL_COMMIT_CS_VALIDATED
) &&
4705 m
->cs_validated
&& !m
->cs_tainted
) {
4708 * This page is no longer dirty
4709 * but could have been modified,
4710 * so it will need to be
4713 m
->cs_validated
= FALSE
;
4714 #if DEVELOPMENT || DEBUG
4715 vm_cs_validated_resets
++;
4717 pmap_disconnect(m
->phys_page
);
4719 clear_refmod
|= VM_MEM_MODIFIED
;
4724 p
= &(page_list
[entry
]);
4726 if (p
->phys_addr
&& p
->pageout
&& !m
->pageout
) {
4730 dwp
->dw_mask
|= DW_vm_page_wire
;
4732 } else if (p
->phys_addr
&&
4733 !p
->pageout
&& m
->pageout
&&
4734 !m
->dump_cleaning
) {
4737 m
->overwriting
= FALSE
;
4739 dwp
->dw_mask
|= (DW_vm_page_unwire
| DW_clear_busy
| DW_PAGE_WAKEUP
);
4741 page_list
[entry
].phys_addr
= 0;
4743 m
->dump_cleaning
= FALSE
;
4746 dwp
->dw_mask
|= DW_vm_pageout_throttle_up
;
4749 m
->cleaning
= FALSE
;
4750 m
->encrypted_cleaning
= FALSE
;
4752 #if MACH_CLUSTER_STATS
4753 if (m
->wanted
) vm_pageout_target_collisions
++;
4757 if (! (flags
& UPL_COMMIT_CS_VALIDATED
) &&
4758 m
->cs_validated
&& !m
->cs_tainted
) {
4761 * This page is no longer dirty
4762 * but could have been modified,
4763 * so it will need to be
4766 m
->cs_validated
= FALSE
;
4767 #if DEVELOPMENT || DEBUG
4768 vm_cs_validated_resets
++;
4770 pmap_disconnect(m
->phys_page
);
4773 if ((flags
& UPL_COMMIT_SET_DIRTY
) ||
4774 (m
->pmapped
&& (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
)))
4779 * page was re-dirtied after we started
4780 * the pageout... reactivate it since
4781 * we don't know whether the on-disk
4782 * copy matches what is now in memory
4784 dwp
->dw_mask
|= (DW_vm_page_unwire
| DW_clear_busy
| DW_PAGE_WAKEUP
);
4786 if (upl
->flags
& UPL_PAGEOUT
) {
4787 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
4788 VM_STAT_INCR(reactivations
);
4789 DTRACE_VM2(pgrec
, int, 1, (uint64_t *), NULL
);
4793 * page has been successfully cleaned
4794 * go ahead and free it for other use
4797 if (m
->object
->internal
) {
4798 DTRACE_VM2(anonpgout
, int, 1, (uint64_t *), NULL
);
4800 DTRACE_VM2(fspgout
, int, 1, (uint64_t *), NULL
);
4802 dwp
->dw_mask
|= DW_vm_page_free
;
4804 if (upl
->flags
& UPL_PAGEOUT
) {
4805 CLUSTER_STAT(vm_pageout_target_page_freed
++;)
4807 if (page_list
[entry
].dirty
) {
4808 VM_STAT_INCR(pageouts
);
4809 DTRACE_VM2(pgout
, int, 1, (uint64_t *), NULL
);
4814 goto commit_next_page
;
4816 #if MACH_CLUSTER_STATS
4818 m
->dirty
= pmap_is_modified(m
->phys_page
);
4820 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
4821 else vm_pageout_cluster_cleaned
++;
4822 if (m
->wanted
) vm_pageout_cluster_collisions
++;
4826 if (! (flags
& UPL_COMMIT_CS_VALIDATED
) &&
4827 m
->cs_validated
&& !m
->cs_tainted
) {
4830 * This page is no longer dirty
4831 * but could have been modified,
4832 * so it will need to be
4835 m
->cs_validated
= FALSE
;
4836 #if DEVELOPMENT || DEBUG
4837 vm_cs_validated_resets
++;
4839 pmap_disconnect(m
->phys_page
);
4842 if (m
->overwriting
) {
4844 * the (COPY_OUT_FROM == FALSE) request_page_list case
4849 dwp
->dw_mask
|= DW_clear_busy
;
4852 * alternate (COPY_OUT_FROM == FALSE) page_list case
4853 * Occurs when the original page was wired
4854 * at the time of the list request
4856 assert(VM_PAGE_WIRED(m
));
4858 dwp
->dw_mask
|= DW_vm_page_unwire
; /* reactivates */
4860 m
->overwriting
= FALSE
;
4862 if (m
->encrypted_cleaning
== TRUE
) {
4863 m
->encrypted_cleaning
= FALSE
;
4865 dwp
->dw_mask
|= DW_clear_busy
;
4867 m
->cleaning
= FALSE
;
4870 * It is a part of the semantic of COPYOUT_FROM
4871 * UPLs that a commit implies cache sync
4872 * between the vm page and the backing store
4873 * this can be used to strip the precious bit
4876 if ((upl
->flags
& UPL_PAGE_SYNC_DONE
) || (flags
& UPL_COMMIT_CLEAR_PRECIOUS
))
4877 m
->precious
= FALSE
;
4879 if (flags
& UPL_COMMIT_SET_DIRTY
)
4882 if (should_be_throttled
== TRUE
&& !m
->active
&& !m
->inactive
&& !m
->speculative
&& !m
->throttled
) {
4884 * page coming back in from being 'frozen'...
4885 * it was dirty before it was frozen, so keep it so
4886 * the vm_page_activate will notice that it really belongs
4887 * on the throttle queue and put it there
4890 dwp
->dw_mask
|= DW_vm_page_activate
;
4893 if ((flags
& UPL_COMMIT_INACTIVATE
) && !m
->clustered
&& !m
->speculative
) {
4894 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
4895 clear_refmod
|= VM_MEM_REFERENCED
;
4896 } else if (!m
->active
&& !m
->inactive
&& !m
->speculative
) {
4898 if (m
->clustered
|| (flags
& UPL_COMMIT_SPECULATE
))
4899 dwp
->dw_mask
|= DW_vm_page_speculate
;
4900 else if (m
->reference
)
4901 dwp
->dw_mask
|= DW_vm_page_activate
;
4903 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
4904 clear_refmod
|= VM_MEM_REFERENCED
;
4908 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
4910 * We blocked access to the pages in this URL.
4911 * Clear the "busy" bit on this page before we
4912 * wake up any waiter.
4914 dwp
->dw_mask
|= DW_clear_busy
;
4917 * Wakeup any thread waiting for the page to be un-cleaning.
4919 dwp
->dw_mask
|= DW_PAGE_WAKEUP
;
4923 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
4925 target_offset
+= PAGE_SIZE_64
;
4926 xfer_size
-= PAGE_SIZE
;
4930 if (dwp
->dw_mask
& ~(DW_clear_busy
| DW_PAGE_WAKEUP
)) {
4931 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
, dw_count
);
4933 if (dw_count
>= dw_limit
) {
4934 vm_page_do_delayed_work(shadow_object
, &dw_array
[0], dw_count
);
4940 if (dwp
->dw_mask
& DW_clear_busy
)
4943 if (dwp
->dw_mask
& DW_PAGE_WAKEUP
)
4949 vm_page_do_delayed_work(shadow_object
, &dw_array
[0], dw_count
);
4953 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4955 } else if (upl
->flags
& UPL_LITE
) {
4959 pg_num
= upl
->size
/PAGE_SIZE
;
4960 pg_num
= (pg_num
+ 31) >> 5;
4963 for (i
= 0; i
< pg_num
; i
++) {
4964 if (lite_list
[i
] != 0) {
4970 if (queue_empty(&upl
->map_object
->memq
))
4973 if (occupied
== 0) {
4975 * If this UPL element belongs to a Vector UPL and is
4976 * empty, then this is the right function to deallocate
4977 * it. So go ahead set the *empty variable. The flag
4978 * UPL_COMMIT_NOTIFY_EMPTY, from the caller's point of view
4979 * should be considered relevant for the Vector UPL and not
4980 * the internal UPLs.
4982 if ((upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
) || isVectorUPL
)
4985 if (object
== shadow_object
&& !(upl
->flags
& UPL_KERNEL_OBJECT
)) {
4987 * this is not a paging object
4988 * so we need to drop the paging reference
4989 * that was taken when we created the UPL
4990 * against this object
4992 vm_object_activity_end(shadow_object
);
4995 * we dontated the paging reference to
4996 * the map object... vm_pageout_object_terminate
4997 * will drop this reference
5001 vm_object_unlock(shadow_object
);
5002 if (object
!= shadow_object
)
5003 vm_object_unlock(object
);
5009 * If we completed our operations on an UPL that is
5010 * part of a Vectored UPL and if empty is TRUE, then
5011 * we should go ahead and deallocate this UPL element.
5012 * Then we check if this was the last of the UPL elements
5013 * within that Vectored UPL. If so, set empty to TRUE
5014 * so that in ubc_upl_commit_range or ubc_upl_commit, we
5015 * can go ahead and deallocate the Vector UPL too.
5018 *empty
= vector_upl_set_subupl(vector_upl
, upl
, 0);
5019 upl_deallocate(upl
);
5021 goto process_upl_to_commit
;
5024 if (pgpgout_count
) {
5025 DTRACE_VM2(pgpgout
, int, pgpgout_count
, (uint64_t *), NULL
);
5028 return KERN_SUCCESS
;
5034 upl_offset_t offset
,
5039 upl_size_t xfer_size
, subupl_size
= size
;
5040 vm_object_t shadow_object
;
5042 vm_object_offset_t target_offset
;
5043 upl_offset_t subupl_offset
= offset
;
5045 wpl_array_t lite_list
;
5047 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
5048 struct vm_page_delayed_work
*dwp
;
5051 int isVectorUPL
= 0;
5052 upl_t vector_upl
= NULL
;
5056 if (upl
== UPL_NULL
)
5057 return KERN_INVALID_ARGUMENT
;
5059 if ( (upl
->flags
& UPL_IO_WIRE
) && !(error
& UPL_ABORT_DUMP_PAGES
) )
5060 return upl_commit_range(upl
, offset
, size
, UPL_COMMIT_FREE_ABSENT
, NULL
, 0, empty
);
5062 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
5064 upl_lock(vector_upl
);
5069 process_upl_to_abort
:
5072 offset
= subupl_offset
;
5074 upl_unlock(vector_upl
);
5075 return KERN_SUCCESS
;
5077 upl
= vector_upl_subupl_byoffset(vector_upl
, &offset
, &size
);
5079 upl_unlock(vector_upl
);
5080 return KERN_FAILURE
;
5082 subupl_size
-= size
;
5083 subupl_offset
+= size
;
5089 if (upl
->upl_commit_index
< UPL_DEBUG_COMMIT_RECORDS
) {
5090 (void) OSBacktrace(&upl
->upl_commit_records
[upl
->upl_commit_index
].c_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
5092 upl
->upl_commit_records
[upl
->upl_commit_index
].c_beg
= offset
;
5093 upl
->upl_commit_records
[upl
->upl_commit_index
].c_end
= (offset
+ size
);
5094 upl
->upl_commit_records
[upl
->upl_commit_index
].c_aborted
= 1;
5096 upl
->upl_commit_index
++;
5099 if (upl
->flags
& UPL_DEVICE_MEMORY
)
5101 else if ((offset
+ size
) <= upl
->size
)
5107 upl_unlock(vector_upl
);
5110 return KERN_FAILURE
;
5112 if (upl
->flags
& UPL_INTERNAL
) {
5113 lite_list
= (wpl_array_t
)
5114 ((((uintptr_t)upl
) + sizeof(struct upl
))
5115 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
5117 lite_list
= (wpl_array_t
)
5118 (((uintptr_t)upl
) + sizeof(struct upl
));
5120 object
= upl
->map_object
;
5122 if (upl
->flags
& UPL_SHADOWED
) {
5123 vm_object_lock(object
);
5124 shadow_object
= object
->shadow
;
5126 shadow_object
= object
;
5128 entry
= offset
/PAGE_SIZE
;
5129 target_offset
= (vm_object_offset_t
)offset
;
5131 if (upl
->flags
& UPL_KERNEL_OBJECT
)
5132 vm_object_lock_shared(shadow_object
);
5134 vm_object_lock(shadow_object
);
5136 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
5137 assert(shadow_object
->blocked_access
);
5138 shadow_object
->blocked_access
= FALSE
;
5139 vm_object_wakeup(object
, VM_OBJECT_EVENT_UNBLOCKED
);
5144 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
5146 if ((error
& UPL_ABORT_DUMP_PAGES
) && (upl
->flags
& UPL_KERNEL_OBJECT
))
5147 panic("upl_abort_range: kernel_object being DUMPED");
5156 if (upl
->flags
& UPL_LITE
) {
5157 unsigned int pg_num
;
5159 pg_num
= (unsigned int) (target_offset
/PAGE_SIZE
);
5160 assert(pg_num
== target_offset
/PAGE_SIZE
);
5163 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
5164 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
5166 if ( !(upl
->flags
& UPL_KERNEL_OBJECT
))
5167 m
= vm_page_lookup(shadow_object
, target_offset
+
5168 (upl
->offset
- shadow_object
->paging_offset
));
5171 if (upl
->flags
& UPL_SHADOWED
) {
5172 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
5177 if (m
== VM_PAGE_NULL
)
5178 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->vo_shadow_offset
);
5181 if ((upl
->flags
& UPL_KERNEL_OBJECT
))
5182 goto abort_next_page
;
5184 if (m
!= VM_PAGE_NULL
) {
5187 boolean_t must_free
= TRUE
;
5190 * COPYOUT = FALSE case
5191 * check for error conditions which must
5192 * be passed back to the pages customer
5194 if (error
& UPL_ABORT_RESTART
) {
5199 } else if (error
& UPL_ABORT_UNAVAILABLE
) {
5203 } else if (error
& UPL_ABORT_ERROR
) {
5212 * This page was a part of a speculative
5213 * read-ahead initiated by the kernel
5214 * itself. No one is expecting this
5215 * page and no one will clean up its
5216 * error state if it ever becomes valid
5218 * We have to free it here.
5225 * If the page was already encrypted,
5226 * we don't really need to decrypt it
5227 * now. It will get decrypted later,
5228 * on demand, as soon as someone needs
5229 * to access its contents.
5232 m
->cleaning
= FALSE
;
5233 m
->encrypted_cleaning
= FALSE
;
5235 if (m
->overwriting
&& !m
->busy
) {
5237 * this shouldn't happen since
5238 * this is an 'absent' page, but
5239 * it doesn't hurt to check for
5240 * the 'alternate' method of
5241 * stabilizing the page...
5242 * we will mark 'busy' to be cleared
5243 * in the following code which will
5244 * take care of the primary stabilzation
5245 * method (i.e. setting 'busy' to TRUE)
5247 dwp
->dw_mask
|= DW_vm_page_unwire
;
5249 m
->overwriting
= FALSE
;
5251 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
5253 if (must_free
== TRUE
)
5254 dwp
->dw_mask
|= DW_vm_page_free
;
5256 dwp
->dw_mask
|= DW_vm_page_activate
;
5259 * Handle the trusted pager throttle.
5262 dwp
->dw_mask
|= DW_vm_pageout_throttle_up
;
5264 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
5266 * We blocked access to the pages in this UPL.
5267 * Clear the "busy" bit and wake up any waiter
5270 dwp
->dw_mask
|= DW_clear_busy
;
5274 assert(m
->wire_count
== 1);
5277 dwp
->dw_mask
|= (DW_vm_page_unwire
| DW_clear_busy
);
5279 if (m
->overwriting
) {
5281 dwp
->dw_mask
|= DW_clear_busy
;
5284 * deal with the 'alternate' method
5285 * of stabilizing the page...
5286 * we will either free the page
5287 * or mark 'busy' to be cleared
5288 * in the following code which will
5289 * take care of the primary stabilzation
5290 * method (i.e. setting 'busy' to TRUE)
5292 dwp
->dw_mask
|= DW_vm_page_unwire
;
5294 m
->overwriting
= FALSE
;
5296 if (m
->encrypted_cleaning
== TRUE
) {
5297 m
->encrypted_cleaning
= FALSE
;
5299 dwp
->dw_mask
|= DW_clear_busy
;
5301 m
->dump_cleaning
= FALSE
;
5302 m
->cleaning
= FALSE
;
5304 vm_external_state_clr(m
->object
->existence_map
, m
->offset
);
5305 #endif /* MACH_PAGEMAP */
5306 if (error
& UPL_ABORT_DUMP_PAGES
) {
5307 pmap_disconnect(m
->phys_page
);
5309 dwp
->dw_mask
|= DW_vm_page_free
;
5311 if (error
& UPL_ABORT_REFERENCE
) {
5313 * we've been told to explictly
5314 * reference this page... for
5315 * file I/O, this is done by
5316 * implementing an LRU on the inactive q
5318 dwp
->dw_mask
|= DW_vm_page_lru
;
5320 dwp
->dw_mask
|= DW_PAGE_WAKEUP
;
5325 target_offset
+= PAGE_SIZE_64
;
5326 xfer_size
-= PAGE_SIZE
;
5330 if (dwp
->dw_mask
& ~(DW_clear_busy
| DW_PAGE_WAKEUP
)) {
5331 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
, dw_count
);
5333 if (dw_count
>= dw_limit
) {
5334 vm_page_do_delayed_work(shadow_object
, &dw_array
[0], dw_count
);
5340 if (dwp
->dw_mask
& DW_clear_busy
)
5343 if (dwp
->dw_mask
& DW_PAGE_WAKEUP
)
5349 vm_page_do_delayed_work(shadow_object
, &dw_array
[0], dw_count
);
5353 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
5355 } else if (upl
->flags
& UPL_LITE
) {
5359 pg_num
= upl
->size
/PAGE_SIZE
;
5360 pg_num
= (pg_num
+ 31) >> 5;
5363 for (i
= 0; i
< pg_num
; i
++) {
5364 if (lite_list
[i
] != 0) {
5370 if (queue_empty(&upl
->map_object
->memq
))
5373 if (occupied
== 0) {
5375 * If this UPL element belongs to a Vector UPL and is
5376 * empty, then this is the right function to deallocate
5377 * it. So go ahead set the *empty variable. The flag
5378 * UPL_COMMIT_NOTIFY_EMPTY, from the caller's point of view
5379 * should be considered relevant for the Vector UPL and
5380 * not the internal UPLs.
5382 if ((upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
) || isVectorUPL
)
5385 if (object
== shadow_object
&& !(upl
->flags
& UPL_KERNEL_OBJECT
)) {
5387 * this is not a paging object
5388 * so we need to drop the paging reference
5389 * that was taken when we created the UPL
5390 * against this object
5392 vm_object_activity_end(shadow_object
);
5395 * we dontated the paging reference to
5396 * the map object... vm_pageout_object_terminate
5397 * will drop this reference
5401 vm_object_unlock(shadow_object
);
5402 if (object
!= shadow_object
)
5403 vm_object_unlock(object
);
5409 * If we completed our operations on an UPL that is
5410 * part of a Vectored UPL and if empty is TRUE, then
5411 * we should go ahead and deallocate this UPL element.
5412 * Then we check if this was the last of the UPL elements
5413 * within that Vectored UPL. If so, set empty to TRUE
5414 * so that in ubc_upl_abort_range or ubc_upl_abort, we
5415 * can go ahead and deallocate the Vector UPL too.
5417 if(*empty
== TRUE
) {
5418 *empty
= vector_upl_set_subupl(vector_upl
, upl
,0);
5419 upl_deallocate(upl
);
5421 goto process_upl_to_abort
;
5424 return KERN_SUCCESS
;
5435 return upl_abort_range(upl
, 0, upl
->size
, error
, &empty
);
5439 /* an option on commit should be wire */
5443 upl_page_info_t
*page_list
,
5444 mach_msg_type_number_t count
)
5448 return upl_commit_range(upl
, 0, upl
->size
, 0, page_list
, count
, &empty
);
5452 unsigned int vm_object_iopl_request_sleep_for_cleaning
= 0;
5455 vm_object_iopl_request(
5457 vm_object_offset_t offset
,
5460 upl_page_info_array_t user_page_list
,
5461 unsigned int *page_list_count
,
5465 vm_object_offset_t dst_offset
;
5466 upl_size_t xfer_size
;
5469 wpl_array_t lite_list
= NULL
;
5470 int no_zero_fill
= FALSE
;
5471 unsigned int size_in_pages
;
5475 struct vm_object_fault_info fault_info
;
5476 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
5477 struct vm_page_delayed_work
*dwp
;
5482 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
5484 * For forward compatibility's sake,
5485 * reject any unknown flag.
5487 return KERN_INVALID_VALUE
;
5489 if (vm_lopage_needed
== FALSE
)
5490 cntrl_flags
&= ~UPL_NEED_32BIT_ADDR
;
5492 if (cntrl_flags
& UPL_NEED_32BIT_ADDR
) {
5493 if ( (cntrl_flags
& (UPL_SET_IO_WIRE
| UPL_SET_LITE
)) != (UPL_SET_IO_WIRE
| UPL_SET_LITE
))
5494 return KERN_INVALID_VALUE
;
5496 if (object
->phys_contiguous
) {
5497 if ((offset
+ object
->vo_shadow_offset
) >= (vm_object_offset_t
)max_valid_dma_address
)
5498 return KERN_INVALID_ADDRESS
;
5500 if (((offset
+ object
->vo_shadow_offset
) + size
) >= (vm_object_offset_t
)max_valid_dma_address
)
5501 return KERN_INVALID_ADDRESS
;
5505 if (cntrl_flags
& UPL_ENCRYPT
) {
5508 * The paging path doesn't use this interface,
5509 * so we don't support the UPL_ENCRYPT flag
5510 * here. We won't encrypt the pages.
5512 assert(! (cntrl_flags
& UPL_ENCRYPT
));
5514 if (cntrl_flags
& UPL_NOZEROFILL
)
5515 no_zero_fill
= TRUE
;
5517 if (cntrl_flags
& UPL_COPYOUT_FROM
)
5518 prot
= VM_PROT_READ
;
5520 prot
= VM_PROT_READ
| VM_PROT_WRITE
;
5522 if (((size
/PAGE_SIZE
) > MAX_UPL_SIZE
) && !object
->phys_contiguous
)
5523 size
= MAX_UPL_SIZE
* PAGE_SIZE
;
5525 if (cntrl_flags
& UPL_SET_INTERNAL
) {
5526 if (page_list_count
!= NULL
)
5527 *page_list_count
= MAX_UPL_SIZE
;
5529 if (((cntrl_flags
& UPL_SET_INTERNAL
) && !(object
->phys_contiguous
)) &&
5530 ((page_list_count
!= NULL
) && (*page_list_count
!= 0) && *page_list_count
< (size
/page_size
)))
5531 return KERN_INVALID_ARGUMENT
;
5533 if ((!object
->internal
) && (object
->paging_offset
!= 0))
5534 panic("vm_object_iopl_request: external object with non-zero paging offset\n");
5537 if (object
->phys_contiguous
)
5542 if (cntrl_flags
& UPL_SET_INTERNAL
) {
5543 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
, UPL_IO_WIRE
, psize
);
5545 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
5546 lite_list
= (wpl_array_t
) (((uintptr_t)user_page_list
) +
5547 ((psize
/ PAGE_SIZE
) * sizeof(upl_page_info_t
)));
5549 user_page_list
= NULL
;
5553 upl
= upl_create(UPL_CREATE_LITE
, UPL_IO_WIRE
, psize
);
5555 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
5561 user_page_list
[0].device
= FALSE
;
5564 upl
->map_object
= object
;
5567 size_in_pages
= size
/ PAGE_SIZE
;
5569 if (object
== kernel_object
&&
5570 !(cntrl_flags
& (UPL_NEED_32BIT_ADDR
| UPL_BLOCK_ACCESS
))) {
5571 upl
->flags
|= UPL_KERNEL_OBJECT
;
5573 vm_object_lock(object
);
5575 vm_object_lock_shared(object
);
5578 vm_object_lock(object
);
5579 vm_object_activity_begin(object
);
5582 * paging in progress also protects the paging_offset
5584 upl
->offset
= offset
+ object
->paging_offset
;
5586 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
5588 * The user requested that access to the pages in this URL
5589 * be blocked until the UPL is commited or aborted.
5591 upl
->flags
|= UPL_ACCESS_BLOCKED
;
5594 if (object
->phys_contiguous
) {
5596 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
5597 #endif /* UPL_DEBUG */
5599 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
5600 assert(!object
->blocked_access
);
5601 object
->blocked_access
= TRUE
;
5604 vm_object_unlock(object
);
5607 * don't need any shadow mappings for this one
5608 * since it is already I/O memory
5610 upl
->flags
|= UPL_DEVICE_MEMORY
;
5612 upl
->highest_page
= (ppnum_t
) ((offset
+ object
->vo_shadow_offset
+ size
- 1)>>PAGE_SHIFT
);
5614 if (user_page_list
) {
5615 user_page_list
[0].phys_addr
= (ppnum_t
) ((offset
+ object
->vo_shadow_offset
)>>PAGE_SHIFT
);
5616 user_page_list
[0].device
= TRUE
;
5618 if (page_list_count
!= NULL
) {
5619 if (upl
->flags
& UPL_INTERNAL
)
5620 *page_list_count
= 0;
5622 *page_list_count
= 1;
5624 return KERN_SUCCESS
;
5626 if (object
!= kernel_object
) {
5628 * Protect user space from future COW operations
5630 object
->true_share
= TRUE
;
5632 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
5633 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
5637 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
5638 #endif /* UPL_DEBUG */
5640 if (!(cntrl_flags
& UPL_COPYOUT_FROM
) &&
5641 object
->copy
!= VM_OBJECT_NULL
) {
5643 * Honor copy-on-write obligations
5645 * The caller is gathering these pages and
5646 * might modify their contents. We need to
5647 * make sure that the copy object has its own
5648 * private copies of these pages before we let
5649 * the caller modify them.
5651 * NOTE: someone else could map the original object
5652 * after we've done this copy-on-write here, and they
5653 * could then see an inconsistent picture of the memory
5654 * while it's being modified via the UPL. To prevent this,
5655 * we would have to block access to these pages until the
5656 * UPL is released. We could use the UPL_BLOCK_ACCESS
5657 * code path for that...
5659 vm_object_update(object
,
5664 FALSE
, /* should_return */
5665 MEMORY_OBJECT_COPY_SYNC
,
5667 #if DEVELOPMENT || DEBUG
5669 iopl_cow_pages
+= size
>> PAGE_SHIFT
;
5677 dst_offset
= offset
;
5679 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
5680 fault_info
.user_tag
= 0;
5681 fault_info
.lo_offset
= offset
;
5682 fault_info
.hi_offset
= offset
+ xfer_size
;
5683 fault_info
.no_cache
= FALSE
;
5684 fault_info
.stealth
= FALSE
;
5685 fault_info
.io_sync
= FALSE
;
5686 fault_info
.cs_bypass
= FALSE
;
5687 fault_info
.mark_zf_absent
= TRUE
;
5691 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
5694 vm_fault_return_t result
;
5695 unsigned int pg_num
;
5699 dst_page
= vm_page_lookup(object
, dst_offset
);
5703 * If the page is encrypted, we need to decrypt it,
5704 * so force a soft page fault.
5706 if (dst_page
== VM_PAGE_NULL
||
5708 dst_page
->encrypted
||
5710 dst_page
->restart
||
5712 dst_page
->fictitious
) {
5714 if (object
== kernel_object
)
5715 panic("vm_object_iopl_request: missing/bad page in kernel object\n");
5719 kern_return_t error_code
;
5722 if (cntrl_flags
& UPL_SET_INTERRUPTIBLE
)
5723 interruptible
= THREAD_ABORTSAFE
;
5725 interruptible
= THREAD_UNINT
;
5727 fault_info
.interruptible
= interruptible
;
5728 fault_info
.cluster_size
= xfer_size
;
5730 vm_object_paging_begin(object
);
5732 result
= vm_fault_page(object
, dst_offset
,
5733 prot
| VM_PROT_WRITE
, FALSE
,
5734 &prot
, &dst_page
, &top_page
,
5736 &error_code
, no_zero_fill
,
5737 FALSE
, &fault_info
);
5741 case VM_FAULT_SUCCESS
:
5743 if ( !dst_page
->absent
) {
5744 PAGE_WAKEUP_DONE(dst_page
);
5747 * we only get back an absent page if we
5748 * requested that it not be zero-filled
5749 * because we are about to fill it via I/O
5751 * absent pages should be left BUSY
5752 * to prevent them from being faulted
5753 * into an address space before we've
5754 * had a chance to complete the I/O on
5755 * them since they may contain info that
5756 * shouldn't be seen by the faulting task
5760 * Release paging references and
5761 * top-level placeholder page, if any.
5763 if (top_page
!= VM_PAGE_NULL
) {
5764 vm_object_t local_object
;
5766 local_object
= top_page
->object
;
5768 if (top_page
->object
!= dst_page
->object
) {
5769 vm_object_lock(local_object
);
5770 VM_PAGE_FREE(top_page
);
5771 vm_object_paging_end(local_object
);
5772 vm_object_unlock(local_object
);
5774 VM_PAGE_FREE(top_page
);
5775 vm_object_paging_end(local_object
);
5778 vm_object_paging_end(object
);
5781 case VM_FAULT_RETRY
:
5782 vm_object_lock(object
);
5785 case VM_FAULT_MEMORY_SHORTAGE
:
5786 OSAddAtomic(size_in_pages
, &vm_upl_wait_for_pages
);
5788 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_START
, vm_upl_wait_for_pages
, 0, 0, 0);
5790 if (vm_page_wait(interruptible
)) {
5791 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
5793 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, 0);
5794 vm_object_lock(object
);
5798 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
5800 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, -1);
5804 case VM_FAULT_INTERRUPTED
:
5805 error_code
= MACH_SEND_INTERRUPTED
;
5806 case VM_FAULT_MEMORY_ERROR
:
5808 ret
= (error_code
? error_code
: KERN_MEMORY_ERROR
);
5810 vm_object_lock(object
);
5813 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
5814 /* success but no page: fail */
5815 vm_object_paging_end(object
);
5816 vm_object_unlock(object
);
5820 panic("vm_object_iopl_request: unexpected error"
5821 " 0x%x from vm_fault_page()\n", result
);
5823 } while (result
!= VM_FAULT_SUCCESS
);
5826 if (upl
->flags
& UPL_KERNEL_OBJECT
)
5827 goto record_phys_addr
;
5829 if (dst_page
->cleaning
) {
5831 * Someone else is cleaning this page in place.as
5832 * In theory, we should be able to proceed and use this
5833 * page but they'll probably end up clearing the "busy"
5834 * bit on it in upl_commit_range() but they didn't set
5835 * it, so they would clear our "busy" bit and open
5836 * us to race conditions.
5837 * We'd better wait for the cleaning to complete and
5840 vm_object_iopl_request_sleep_for_cleaning
++;
5841 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
5844 if ( (cntrl_flags
& UPL_NEED_32BIT_ADDR
) &&
5845 dst_page
->phys_page
>= (max_valid_dma_address
>> PAGE_SHIFT
) ) {
5850 * support devices that can't DMA above 32 bits
5851 * by substituting pages from a pool of low address
5852 * memory for any pages we find above the 4G mark
5853 * can't substitute if the page is already wired because
5854 * we don't know whether that physical address has been
5855 * handed out to some other 64 bit capable DMA device to use
5857 if (VM_PAGE_WIRED(dst_page
)) {
5858 ret
= KERN_PROTECTION_FAILURE
;
5861 low_page
= vm_page_grablo();
5863 if (low_page
== VM_PAGE_NULL
) {
5864 ret
= KERN_RESOURCE_SHORTAGE
;
5868 * from here until the vm_page_replace completes
5869 * we musn't drop the object lock... we don't
5870 * want anyone refaulting this page in and using
5871 * it after we disconnect it... we want the fault
5872 * to find the new page being substituted.
5874 if (dst_page
->pmapped
)
5875 refmod
= pmap_disconnect(dst_page
->phys_page
);
5879 if (!dst_page
->absent
)
5880 vm_page_copy(dst_page
, low_page
);
5882 low_page
->reference
= dst_page
->reference
;
5883 low_page
->dirty
= dst_page
->dirty
;
5884 low_page
->absent
= dst_page
->absent
;
5886 if (refmod
& VM_MEM_REFERENCED
)
5887 low_page
->reference
= TRUE
;
5888 if (refmod
& VM_MEM_MODIFIED
)
5889 low_page
->dirty
= TRUE
;
5891 vm_page_replace(low_page
, object
, dst_offset
);
5893 dst_page
= low_page
;
5895 * vm_page_grablo returned the page marked
5896 * BUSY... we don't need a PAGE_WAKEUP_DONE
5897 * here, because we've never dropped the object lock
5899 if ( !dst_page
->absent
)
5900 dst_page
->busy
= FALSE
;
5902 if ( !dst_page
->busy
)
5903 dwp
->dw_mask
|= DW_vm_page_wire
;
5905 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
5907 * Mark the page "busy" to block any future page fault
5908 * on this page in addition to wiring it.
5909 * We'll also remove the mapping
5910 * of all these pages before leaving this routine.
5912 assert(!dst_page
->fictitious
);
5913 dst_page
->busy
= TRUE
;
5916 * expect the page to be used
5917 * page queues lock must be held to set 'reference'
5919 dwp
->dw_mask
|= DW_set_reference
;
5921 if (!(cntrl_flags
& UPL_COPYOUT_FROM
))
5922 dst_page
->dirty
= TRUE
;
5925 upl
->flags
|= UPL_HAS_BUSY
;
5927 pg_num
= (unsigned int) ((dst_offset
-offset
)/PAGE_SIZE
);
5928 assert(pg_num
== (dst_offset
-offset
)/PAGE_SIZE
);
5929 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
5931 if (dst_page
->phys_page
> upl
->highest_page
)
5932 upl
->highest_page
= dst_page
->phys_page
;
5934 if (user_page_list
) {
5935 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
5936 user_page_list
[entry
].pageout
= dst_page
->pageout
;
5937 user_page_list
[entry
].absent
= dst_page
->absent
;
5938 user_page_list
[entry
].dirty
= dst_page
->dirty
;
5939 user_page_list
[entry
].precious
= dst_page
->precious
;
5940 user_page_list
[entry
].device
= FALSE
;
5941 if (dst_page
->clustered
== TRUE
)
5942 user_page_list
[entry
].speculative
= dst_page
->speculative
;
5944 user_page_list
[entry
].speculative
= FALSE
;
5945 user_page_list
[entry
].cs_validated
= dst_page
->cs_validated
;
5946 user_page_list
[entry
].cs_tainted
= dst_page
->cs_tainted
;
5948 if (object
!= kernel_object
) {
5950 * someone is explicitly grabbing this page...
5951 * update clustered and speculative state
5954 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
5957 dst_offset
+= PAGE_SIZE_64
;
5958 xfer_size
-= PAGE_SIZE
;
5961 VM_PAGE_ADD_DELAYED_WORK(dwp
, dst_page
, dw_count
);
5963 if (dw_count
>= dw_limit
) {
5964 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
5972 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
5974 if (page_list_count
!= NULL
) {
5975 if (upl
->flags
& UPL_INTERNAL
)
5976 *page_list_count
= 0;
5977 else if (*page_list_count
> entry
)
5978 *page_list_count
= entry
;
5980 vm_object_unlock(object
);
5982 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
5984 * We've marked all the pages "busy" so that future
5985 * page faults will block.
5986 * Now remove the mapping for these pages, so that they
5987 * can't be accessed without causing a page fault.
5989 vm_object_pmap_protect(object
, offset
, (vm_object_size_t
)size
,
5990 PMAP_NULL
, 0, VM_PROT_NONE
);
5991 assert(!object
->blocked_access
);
5992 object
->blocked_access
= TRUE
;
5994 return KERN_SUCCESS
;
5999 for (; offset
< dst_offset
; offset
+= PAGE_SIZE
) {
6000 boolean_t need_unwire
;
6002 dst_page
= vm_page_lookup(object
, offset
);
6004 if (dst_page
== VM_PAGE_NULL
)
6005 panic("vm_object_iopl_request: Wired page missing. \n");
6008 * if we've already processed this page in an earlier
6009 * dw_do_work, we need to undo the wiring... we will
6010 * leave the dirty and reference bits on if they
6011 * were set, since we don't have a good way of knowing
6012 * what the previous state was and we won't get here
6013 * under any normal circumstances... we will always
6014 * clear BUSY and wakeup any waiters via vm_page_free
6015 * or PAGE_WAKEUP_DONE
6020 if (dw_array
[dw_index
].dw_m
== dst_page
) {
6022 * still in the deferred work list
6023 * which means we haven't yet called
6024 * vm_page_wire on this page
6026 need_unwire
= FALSE
;
6032 vm_page_lock_queues();
6034 if (dst_page
->absent
) {
6035 vm_page_free(dst_page
);
6037 need_unwire
= FALSE
;
6039 if (need_unwire
== TRUE
)
6040 vm_page_unwire(dst_page
, TRUE
);
6042 PAGE_WAKEUP_DONE(dst_page
);
6044 vm_page_unlock_queues();
6046 if (need_unwire
== TRUE
)
6047 VM_STAT_INCR(reactivations
);
6052 if (! (upl
->flags
& UPL_KERNEL_OBJECT
)) {
6053 vm_object_activity_end(object
);
6055 vm_object_unlock(object
);
6066 kern_return_t retval
;
6067 boolean_t upls_locked
;
6068 vm_object_t object1
, object2
;
6070 if (upl1
== UPL_NULL
|| upl2
== UPL_NULL
|| upl1
== upl2
|| ((upl1
->flags
& UPL_VECTOR
)==UPL_VECTOR
) || ((upl2
->flags
& UPL_VECTOR
)==UPL_VECTOR
)) {
6071 return KERN_INVALID_ARGUMENT
;
6074 upls_locked
= FALSE
;
6077 * Since we need to lock both UPLs at the same time,
6078 * avoid deadlocks by always taking locks in the same order.
6087 upls_locked
= TRUE
; /* the UPLs will need to be unlocked */
6089 object1
= upl1
->map_object
;
6090 object2
= upl2
->map_object
;
6092 if (upl1
->offset
!= 0 || upl2
->offset
!= 0 ||
6093 upl1
->size
!= upl2
->size
) {
6095 * We deal only with full objects, not subsets.
6096 * That's because we exchange the entire backing store info
6097 * for the objects: pager, resident pages, etc... We can't do
6100 retval
= KERN_INVALID_VALUE
;
6105 * Tranpose the VM objects' backing store.
6107 retval
= vm_object_transpose(object1
, object2
,
6108 (vm_object_size_t
) upl1
->size
);
6110 if (retval
== KERN_SUCCESS
) {
6112 * Make each UPL point to the correct VM object, i.e. the
6113 * object holding the pages that the UPL refers to...
6116 queue_remove(&object1
->uplq
, upl1
, upl_t
, uplq
);
6117 queue_remove(&object2
->uplq
, upl2
, upl_t
, uplq
);
6119 upl1
->map_object
= object2
;
6120 upl2
->map_object
= object1
;
6122 queue_enter(&object1
->uplq
, upl2
, upl_t
, uplq
);
6123 queue_enter(&object2
->uplq
, upl1
, upl_t
, uplq
);
6134 upls_locked
= FALSE
;
6143 * Rationale: the user might have some encrypted data on disk (via
6144 * FileVault or any other mechanism). That data is then decrypted in
6145 * memory, which is safe as long as the machine is secure. But that
6146 * decrypted data in memory could be paged out to disk by the default
6147 * pager. The data would then be stored on disk in clear (not encrypted)
6148 * and it could be accessed by anyone who gets physical access to the
6149 * disk (if the laptop or the disk gets stolen for example). This weakens
6150 * the security offered by FileVault.
6152 * Solution: the default pager will optionally request that all the
6153 * pages it gathers for pageout be encrypted, via the UPL interfaces,
6154 * before it sends this UPL to disk via the vnode_pageout() path.
6158 * To avoid disrupting the VM LRU algorithms, we want to keep the
6159 * clean-in-place mechanisms, which allow us to send some extra pages to
6160 * swap (clustering) without actually removing them from the user's
6161 * address space. We don't want the user to unknowingly access encrypted
6162 * data, so we have to actually remove the encrypted pages from the page
6163 * table. When the user accesses the data, the hardware will fail to
6164 * locate the virtual page in its page table and will trigger a page
6165 * fault. We can then decrypt the page and enter it in the page table
6166 * again. Whenever we allow the user to access the contents of a page,
6167 * we have to make sure it's not encrypted.
6173 * Reserve of virtual addresses in the kernel address space.
6174 * We need to map the physical pages in the kernel, so that we
6175 * can call the encryption/decryption routines with a kernel
6176 * virtual address. We keep this pool of pre-allocated kernel
6177 * virtual addresses so that we don't have to scan the kernel's
6178 * virtual address space each time we need to encrypt or decrypt
6180 * It would be nice to be able to encrypt and decrypt in physical
6181 * mode but that might not always be more efficient...
6183 decl_simple_lock_data(,vm_paging_lock
)
6184 #define VM_PAGING_NUM_PAGES 64
6185 vm_map_offset_t vm_paging_base_address
= 0;
6186 boolean_t vm_paging_page_inuse
[VM_PAGING_NUM_PAGES
] = { FALSE
, };
6187 int vm_paging_max_index
= 0;
6188 int vm_paging_page_waiter
= 0;
6189 int vm_paging_page_waiter_total
= 0;
6190 unsigned long vm_paging_no_kernel_page
= 0;
6191 unsigned long vm_paging_objects_mapped
= 0;
6192 unsigned long vm_paging_pages_mapped
= 0;
6193 unsigned long vm_paging_objects_mapped_slow
= 0;
6194 unsigned long vm_paging_pages_mapped_slow
= 0;
6197 vm_paging_map_init(void)
6200 vm_map_offset_t page_map_offset
;
6201 vm_map_entry_t map_entry
;
6203 assert(vm_paging_base_address
== 0);
6206 * Initialize our pool of pre-allocated kernel
6207 * virtual addresses.
6209 page_map_offset
= 0;
6210 kr
= vm_map_find_space(kernel_map
,
6212 VM_PAGING_NUM_PAGES
* PAGE_SIZE
,
6216 if (kr
!= KERN_SUCCESS
) {
6217 panic("vm_paging_map_init: kernel_map full\n");
6219 map_entry
->object
.vm_object
= kernel_object
;
6220 map_entry
->offset
= page_map_offset
;
6221 map_entry
->protection
= VM_PROT_NONE
;
6222 map_entry
->max_protection
= VM_PROT_NONE
;
6223 map_entry
->permanent
= TRUE
;
6224 vm_object_reference(kernel_object
);
6225 vm_map_unlock(kernel_map
);
6227 assert(vm_paging_base_address
== 0);
6228 vm_paging_base_address
= page_map_offset
;
6233 * vm_paging_map_object:
6234 * Maps part of a VM object's pages in the kernel
6235 * virtual address space, using the pre-allocated
6236 * kernel virtual addresses, if possible.
6238 * The VM object is locked. This lock will get
6239 * dropped and re-acquired though, so the caller
6240 * must make sure the VM object is kept alive
6241 * (by holding a VM map that has a reference
6242 * on it, for example, or taking an extra reference).
6243 * The page should also be kept busy to prevent
6244 * it from being reclaimed.
6247 vm_paging_map_object(
6248 vm_map_offset_t
*address
,
6251 vm_object_offset_t offset
,
6252 vm_map_size_t
*size
,
6253 vm_prot_t protection
,
6254 boolean_t can_unlock_object
)
6257 vm_map_offset_t page_map_offset
;
6258 vm_map_size_t map_size
;
6259 vm_object_offset_t object_offset
;
6263 if (page
!= VM_PAGE_NULL
&& *size
== PAGE_SIZE
) {
6266 * Use one of the pre-allocated kernel virtual addresses
6267 * and just enter the VM page in the kernel address space
6268 * at that virtual address.
6270 simple_lock(&vm_paging_lock
);
6273 * Try and find an available kernel virtual address
6274 * from our pre-allocated pool.
6276 page_map_offset
= 0;
6278 for (i
= 0; i
< VM_PAGING_NUM_PAGES
; i
++) {
6279 if (vm_paging_page_inuse
[i
] == FALSE
) {
6281 vm_paging_base_address
+
6286 if (page_map_offset
!= 0) {
6287 /* found a space to map our page ! */
6291 if (can_unlock_object
) {
6293 * If we can afford to unlock the VM object,
6294 * let's take the slow path now...
6299 * We can't afford to unlock the VM object, so
6300 * let's wait for a space to become available...
6302 vm_paging_page_waiter_total
++;
6303 vm_paging_page_waiter
++;
6304 thread_sleep_fast_usimple_lock(&vm_paging_page_waiter
,
6307 vm_paging_page_waiter
--;
6308 /* ... and try again */
6311 if (page_map_offset
!= 0) {
6313 * We found a kernel virtual address;
6314 * map the physical page to that virtual address.
6316 if (i
> vm_paging_max_index
) {
6317 vm_paging_max_index
= i
;
6319 vm_paging_page_inuse
[i
] = TRUE
;
6320 simple_unlock(&vm_paging_lock
);
6322 page
->pmapped
= TRUE
;
6325 * Keep the VM object locked over the PMAP_ENTER
6326 * and the actual use of the page by the kernel,
6327 * or this pmap mapping might get undone by a
6328 * vm_object_pmap_protect() call...
6330 PMAP_ENTER(kernel_pmap
,
6336 vm_paging_objects_mapped
++;
6337 vm_paging_pages_mapped
++;
6338 *address
= page_map_offset
;
6340 /* all done and mapped, ready to use ! */
6341 return KERN_SUCCESS
;
6345 * We ran out of pre-allocated kernel virtual
6346 * addresses. Just map the page in the kernel
6347 * the slow and regular way.
6349 vm_paging_no_kernel_page
++;
6350 simple_unlock(&vm_paging_lock
);
6353 if (! can_unlock_object
) {
6354 return KERN_NOT_SUPPORTED
;
6357 object_offset
= vm_object_trunc_page(offset
);
6358 map_size
= vm_map_round_page(*size
);
6361 * Try and map the required range of the object
6365 vm_object_reference_locked(object
); /* for the map entry */
6366 vm_object_unlock(object
);
6368 kr
= vm_map_enter(kernel_map
,
6379 if (kr
!= KERN_SUCCESS
) {
6382 vm_object_deallocate(object
); /* for the map entry */
6383 vm_object_lock(object
);
6390 * Enter the mapped pages in the page table now.
6392 vm_object_lock(object
);
6394 * VM object must be kept locked from before PMAP_ENTER()
6395 * until after the kernel is done accessing the page(s).
6396 * Otherwise, the pmap mappings in the kernel could be
6397 * undone by a call to vm_object_pmap_protect().
6400 for (page_map_offset
= 0;
6402 map_size
-= PAGE_SIZE_64
, page_map_offset
+= PAGE_SIZE_64
) {
6404 page
= vm_page_lookup(object
, offset
+ page_map_offset
);
6405 if (page
== VM_PAGE_NULL
) {
6406 printf("vm_paging_map_object: no page !?");
6407 vm_object_unlock(object
);
6408 kr
= vm_map_remove(kernel_map
, *address
, *size
,
6410 assert(kr
== KERN_SUCCESS
);
6413 vm_object_lock(object
);
6414 return KERN_MEMORY_ERROR
;
6416 page
->pmapped
= TRUE
;
6418 //assert(pmap_verify_free(page->phys_page));
6419 PMAP_ENTER(kernel_pmap
,
6420 *address
+ page_map_offset
,
6427 vm_paging_objects_mapped_slow
++;
6428 vm_paging_pages_mapped_slow
+= (unsigned long) (map_size
/ PAGE_SIZE_64
);
6430 return KERN_SUCCESS
;
6435 * vm_paging_unmap_object:
6436 * Unmaps part of a VM object's pages from the kernel
6437 * virtual address space.
6439 * The VM object is locked. This lock will get
6440 * dropped and re-acquired though.
6443 vm_paging_unmap_object(
6445 vm_map_offset_t start
,
6446 vm_map_offset_t end
)
6451 if ((vm_paging_base_address
== 0) ||
6452 (start
< vm_paging_base_address
) ||
6453 (end
> (vm_paging_base_address
6454 + (VM_PAGING_NUM_PAGES
* PAGE_SIZE
)))) {
6456 * We didn't use our pre-allocated pool of
6457 * kernel virtual address. Deallocate the
6460 if (object
!= VM_OBJECT_NULL
) {
6461 vm_object_unlock(object
);
6463 kr
= vm_map_remove(kernel_map
, start
, end
, VM_MAP_NO_FLAGS
);
6464 if (object
!= VM_OBJECT_NULL
) {
6465 vm_object_lock(object
);
6467 assert(kr
== KERN_SUCCESS
);
6470 * We used a kernel virtual address from our
6471 * pre-allocated pool. Put it back in the pool
6474 assert(end
- start
== PAGE_SIZE
);
6475 i
= (int) ((start
- vm_paging_base_address
) >> PAGE_SHIFT
);
6476 assert(i
>= 0 && i
< VM_PAGING_NUM_PAGES
);
6478 /* undo the pmap mapping */
6479 pmap_remove(kernel_pmap
, start
, end
);
6481 simple_lock(&vm_paging_lock
);
6482 vm_paging_page_inuse
[i
] = FALSE
;
6483 if (vm_paging_page_waiter
) {
6484 thread_wakeup(&vm_paging_page_waiter
);
6486 simple_unlock(&vm_paging_lock
);
6493 * "iv" is the "initial vector". Ideally, we want to
6494 * have a different one for each page we encrypt, so that
6495 * crackers can't find encryption patterns too easily.
6497 #define SWAP_CRYPT_AES_KEY_SIZE 128 /* XXX 192 and 256 don't work ! */
6498 boolean_t swap_crypt_ctx_initialized
= FALSE
;
6499 aes_32t swap_crypt_key
[8]; /* big enough for a 256 key */
6500 aes_ctx swap_crypt_ctx
;
6501 const unsigned char swap_crypt_null_iv
[AES_BLOCK_SIZE
] = {0xa, };
6504 boolean_t swap_crypt_ctx_tested
= FALSE
;
6505 unsigned char swap_crypt_test_page_ref
[4096] __attribute__((aligned(4096)));
6506 unsigned char swap_crypt_test_page_encrypt
[4096] __attribute__((aligned(4096)));
6507 unsigned char swap_crypt_test_page_decrypt
[4096] __attribute__((aligned(4096)));
6511 * Initialize the encryption context: key and key size.
6513 void swap_crypt_ctx_initialize(void); /* forward */
6515 swap_crypt_ctx_initialize(void)
6520 * No need for locking to protect swap_crypt_ctx_initialized
6521 * because the first use of encryption will come from the
6522 * pageout thread (we won't pagein before there's been a pageout)
6523 * and there's only one pageout thread.
6525 if (swap_crypt_ctx_initialized
== FALSE
) {
6527 i
< (sizeof (swap_crypt_key
) /
6528 sizeof (swap_crypt_key
[0]));
6530 swap_crypt_key
[i
] = random();
6532 aes_encrypt_key((const unsigned char *) swap_crypt_key
,
6533 SWAP_CRYPT_AES_KEY_SIZE
,
6534 &swap_crypt_ctx
.encrypt
);
6535 aes_decrypt_key((const unsigned char *) swap_crypt_key
,
6536 SWAP_CRYPT_AES_KEY_SIZE
,
6537 &swap_crypt_ctx
.decrypt
);
6538 swap_crypt_ctx_initialized
= TRUE
;
6543 * Validate the encryption algorithms.
6545 if (swap_crypt_ctx_tested
== FALSE
) {
6547 for (i
= 0; i
< 4096; i
++) {
6548 swap_crypt_test_page_ref
[i
] = (char) i
;
6551 aes_encrypt_cbc(swap_crypt_test_page_ref
,
6553 PAGE_SIZE
/ AES_BLOCK_SIZE
,
6554 swap_crypt_test_page_encrypt
,
6555 &swap_crypt_ctx
.encrypt
);
6557 aes_decrypt_cbc(swap_crypt_test_page_encrypt
,
6559 PAGE_SIZE
/ AES_BLOCK_SIZE
,
6560 swap_crypt_test_page_decrypt
,
6561 &swap_crypt_ctx
.decrypt
);
6562 /* compare result with original */
6563 for (i
= 0; i
< 4096; i
++) {
6564 if (swap_crypt_test_page_decrypt
[i
] !=
6565 swap_crypt_test_page_ref
[i
]) {
6566 panic("encryption test failed");
6571 aes_encrypt_cbc(swap_crypt_test_page_decrypt
,
6573 PAGE_SIZE
/ AES_BLOCK_SIZE
,
6574 swap_crypt_test_page_decrypt
,
6575 &swap_crypt_ctx
.encrypt
);
6576 /* decrypt in place */
6577 aes_decrypt_cbc(swap_crypt_test_page_decrypt
,
6579 PAGE_SIZE
/ AES_BLOCK_SIZE
,
6580 swap_crypt_test_page_decrypt
,
6581 &swap_crypt_ctx
.decrypt
);
6582 for (i
= 0; i
< 4096; i
++) {
6583 if (swap_crypt_test_page_decrypt
[i
] !=
6584 swap_crypt_test_page_ref
[i
]) {
6585 panic("in place encryption test failed");
6589 swap_crypt_ctx_tested
= TRUE
;
6597 * Encrypt the given page, for secure paging.
6598 * The page might already be mapped at kernel virtual
6599 * address "kernel_mapping_offset". Otherwise, we need
6603 * The page's object is locked, but this lock will be released
6605 * The page is busy and not accessible by users (not entered in any pmap).
6610 vm_map_offset_t kernel_mapping_offset
)
6613 vm_map_size_t kernel_mapping_size
;
6614 vm_offset_t kernel_vaddr
;
6616 unsigned char aes_iv
[AES_BLOCK_SIZE
];
6618 memory_object_t pager_object
;
6619 vm_object_offset_t paging_offset
;
6623 if (! vm_pages_encrypted
) {
6624 vm_pages_encrypted
= TRUE
;
6628 assert(page
->dirty
|| page
->precious
);
6630 if (page
->encrypted
) {
6632 * Already encrypted: no need to do it again.
6634 vm_page_encrypt_already_encrypted_counter
++;
6637 ASSERT_PAGE_DECRYPTED(page
);
6640 * Take a paging-in-progress reference to keep the object
6641 * alive even if we have to unlock it (in vm_paging_map_object()
6644 vm_object_paging_begin(page
->object
);
6646 if (kernel_mapping_offset
== 0) {
6648 * The page hasn't already been mapped in kernel space
6649 * by the caller. Map it now, so that we can access
6650 * its contents and encrypt them.
6652 kernel_mapping_size
= PAGE_SIZE
;
6653 kr
= vm_paging_map_object(&kernel_mapping_offset
,
6657 &kernel_mapping_size
,
6658 VM_PROT_READ
| VM_PROT_WRITE
,
6660 if (kr
!= KERN_SUCCESS
) {
6661 panic("vm_page_encrypt: "
6662 "could not map page in kernel: 0x%x\n",
6666 kernel_mapping_size
= 0;
6668 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
6670 if (swap_crypt_ctx_initialized
== FALSE
) {
6671 swap_crypt_ctx_initialize();
6673 assert(swap_crypt_ctx_initialized
);
6676 * Prepare an "initial vector" for the encryption.
6677 * We use the "pager" and the "paging_offset" for that
6678 * page to obfuscate the encrypted data a bit more and
6679 * prevent crackers from finding patterns that they could
6680 * use to break the key.
6682 bzero(&encrypt_iv
.aes_iv
[0], sizeof (encrypt_iv
.aes_iv
));
6683 encrypt_iv
.vm
.pager_object
= page
->object
->pager
;
6684 encrypt_iv
.vm
.paging_offset
=
6685 page
->object
->paging_offset
+ page
->offset
;
6687 /* encrypt the "initial vector" */
6688 aes_encrypt_cbc((const unsigned char *) &encrypt_iv
.aes_iv
[0],
6691 &encrypt_iv
.aes_iv
[0],
6692 &swap_crypt_ctx
.encrypt
);
6697 aes_encrypt_cbc((const unsigned char *) kernel_vaddr
,
6698 &encrypt_iv
.aes_iv
[0],
6699 PAGE_SIZE
/ AES_BLOCK_SIZE
,
6700 (unsigned char *) kernel_vaddr
,
6701 &swap_crypt_ctx
.encrypt
);
6703 vm_page_encrypt_counter
++;
6706 * Unmap the page from the kernel's address space,
6707 * if we had to map it ourselves. Otherwise, let
6708 * the caller undo the mapping if needed.
6710 if (kernel_mapping_size
!= 0) {
6711 vm_paging_unmap_object(page
->object
,
6712 kernel_mapping_offset
,
6713 kernel_mapping_offset
+ kernel_mapping_size
);
6717 * Clear the "reference" and "modified" bits.
6718 * This should clean up any impact the encryption had
6720 * The page was kept busy and disconnected from all pmaps,
6721 * so it can't have been referenced or modified from user
6723 * The software bits will be reset later after the I/O
6724 * has completed (in upl_commit_range()).
6726 pmap_clear_refmod(page
->phys_page
, VM_MEM_REFERENCED
| VM_MEM_MODIFIED
);
6728 page
->encrypted
= TRUE
;
6730 vm_object_paging_end(page
->object
);
6736 * Decrypt the given page.
6737 * The page might already be mapped at kernel virtual
6738 * address "kernel_mapping_offset". Otherwise, we need
6742 * The page's VM object is locked but will be unlocked and relocked.
6743 * The page is busy and not accessible by users (not entered in any pmap).
6748 vm_map_offset_t kernel_mapping_offset
)
6751 vm_map_size_t kernel_mapping_size
;
6752 vm_offset_t kernel_vaddr
;
6754 unsigned char aes_iv
[AES_BLOCK_SIZE
];
6756 memory_object_t pager_object
;
6757 vm_object_offset_t paging_offset
;
6760 boolean_t was_dirty
;
6763 assert(page
->encrypted
);
6765 was_dirty
= page
->dirty
;
6768 * Take a paging-in-progress reference to keep the object
6769 * alive even if we have to unlock it (in vm_paging_map_object()
6772 vm_object_paging_begin(page
->object
);
6774 if (kernel_mapping_offset
== 0) {
6776 * The page hasn't already been mapped in kernel space
6777 * by the caller. Map it now, so that we can access
6778 * its contents and decrypt them.
6780 kernel_mapping_size
= PAGE_SIZE
;
6781 kr
= vm_paging_map_object(&kernel_mapping_offset
,
6785 &kernel_mapping_size
,
6786 VM_PROT_READ
| VM_PROT_WRITE
,
6788 if (kr
!= KERN_SUCCESS
) {
6789 panic("vm_page_decrypt: "
6790 "could not map page in kernel: 0x%x\n",
6794 kernel_mapping_size
= 0;
6796 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
6798 assert(swap_crypt_ctx_initialized
);
6801 * Prepare an "initial vector" for the decryption.
6802 * It has to be the same as the "initial vector" we
6803 * used to encrypt that page.
6805 bzero(&decrypt_iv
.aes_iv
[0], sizeof (decrypt_iv
.aes_iv
));
6806 decrypt_iv
.vm
.pager_object
= page
->object
->pager
;
6807 decrypt_iv
.vm
.paging_offset
=
6808 page
->object
->paging_offset
+ page
->offset
;
6810 /* encrypt the "initial vector" */
6811 aes_encrypt_cbc((const unsigned char *) &decrypt_iv
.aes_iv
[0],
6814 &decrypt_iv
.aes_iv
[0],
6815 &swap_crypt_ctx
.encrypt
);
6820 aes_decrypt_cbc((const unsigned char *) kernel_vaddr
,
6821 &decrypt_iv
.aes_iv
[0],
6822 PAGE_SIZE
/ AES_BLOCK_SIZE
,
6823 (unsigned char *) kernel_vaddr
,
6824 &swap_crypt_ctx
.decrypt
);
6825 vm_page_decrypt_counter
++;
6828 * Unmap the page from the kernel's address space,
6829 * if we had to map it ourselves. Otherwise, let
6830 * the caller undo the mapping if needed.
6832 if (kernel_mapping_size
!= 0) {
6833 vm_paging_unmap_object(page
->object
,
6835 kernel_vaddr
+ PAGE_SIZE
);
6840 * The pager did not specify that the page would be
6841 * clean when it got paged in, so let's not clean it here
6846 * After decryption, the page is actually still clean.
6847 * It was encrypted as part of paging, which "cleans"
6848 * the "dirty" pages.
6849 * Noone could access it after it was encrypted
6850 * and the decryption doesn't count.
6852 page
->dirty
= FALSE
;
6853 assert (page
->cs_validated
== FALSE
);
6854 pmap_clear_refmod(page
->phys_page
, VM_MEM_MODIFIED
| VM_MEM_REFERENCED
);
6856 page
->encrypted
= FALSE
;
6859 * We've just modified the page's contents via the data cache and part
6860 * of the new contents might still be in the cache and not yet in RAM.
6861 * Since the page is now available and might get gathered in a UPL to
6862 * be part of a DMA transfer from a driver that expects the memory to
6863 * be coherent at this point, we have to flush the data cache.
6865 pmap_sync_page_attributes_phys(page
->phys_page
);
6867 * Since the page is not mapped yet, some code might assume that it
6868 * doesn't need to invalidate the instruction cache when writing to
6869 * that page. That code relies on "pmapped" being FALSE, so that the
6870 * caches get synchronized when the page is first mapped.
6872 assert(pmap_verify_free(page
->phys_page
));
6873 page
->pmapped
= FALSE
;
6874 page
->wpmapped
= FALSE
;
6876 vm_object_paging_end(page
->object
);
6879 #if DEVELOPMENT || DEBUG
6880 unsigned long upl_encrypt_upls
= 0;
6881 unsigned long upl_encrypt_pages
= 0;
6888 * Encrypts all the pages in the UPL, within the specified range.
6894 upl_offset_t crypt_offset
,
6895 upl_size_t crypt_size
)
6897 upl_size_t upl_size
, subupl_size
=crypt_size
;
6898 upl_offset_t offset_in_upl
, subupl_offset
=crypt_offset
;
6899 vm_object_t upl_object
;
6900 vm_object_offset_t upl_offset
;
6902 vm_object_t shadow_object
;
6903 vm_object_offset_t shadow_offset
;
6904 vm_object_offset_t paging_offset
;
6905 vm_object_offset_t base_offset
;
6906 int isVectorUPL
= 0;
6907 upl_t vector_upl
= NULL
;
6909 if((isVectorUPL
= vector_upl_is_valid(upl
)))
6912 process_upl_to_encrypt
:
6914 crypt_size
= subupl_size
;
6915 crypt_offset
= subupl_offset
;
6916 upl
= vector_upl_subupl_byoffset(vector_upl
, &crypt_offset
, &crypt_size
);
6918 panic("upl_encrypt: Accessing a sub-upl that doesn't exist\n");
6919 subupl_size
-= crypt_size
;
6920 subupl_offset
+= crypt_size
;
6923 #if DEVELOPMENT || DEBUG
6925 upl_encrypt_pages
+= crypt_size
/ PAGE_SIZE
;
6927 upl_object
= upl
->map_object
;
6928 upl_offset
= upl
->offset
;
6929 upl_size
= upl
->size
;
6931 vm_object_lock(upl_object
);
6934 * Find the VM object that contains the actual pages.
6936 if (upl_object
->pageout
) {
6937 shadow_object
= upl_object
->shadow
;
6939 * The offset in the shadow object is actually also
6940 * accounted for in upl->offset. It possibly shouldn't be
6941 * this way, but for now don't account for it twice.
6944 assert(upl_object
->paging_offset
== 0); /* XXX ? */
6945 vm_object_lock(shadow_object
);
6947 shadow_object
= upl_object
;
6951 paging_offset
= shadow_object
->paging_offset
;
6952 vm_object_paging_begin(shadow_object
);
6954 if (shadow_object
!= upl_object
)
6955 vm_object_unlock(upl_object
);
6958 base_offset
= shadow_offset
;
6959 base_offset
+= upl_offset
;
6960 base_offset
+= crypt_offset
;
6961 base_offset
-= paging_offset
;
6963 assert(crypt_offset
+ crypt_size
<= upl_size
);
6965 for (offset_in_upl
= 0;
6966 offset_in_upl
< crypt_size
;
6967 offset_in_upl
+= PAGE_SIZE
) {
6968 page
= vm_page_lookup(shadow_object
,
6969 base_offset
+ offset_in_upl
);
6970 if (page
== VM_PAGE_NULL
) {
6971 panic("upl_encrypt: "
6972 "no page for (obj=%p,off=0x%llx+0x%x)!\n",
6978 * Disconnect the page from all pmaps, so that nobody can
6979 * access it while it's encrypted. After that point, all
6980 * accesses to this page will cause a page fault and block
6981 * while the page is busy being encrypted. After the
6982 * encryption completes, any access will cause a
6983 * page fault and the page gets decrypted at that time.
6985 pmap_disconnect(page
->phys_page
);
6986 vm_page_encrypt(page
, 0);
6988 if (vm_object_lock_avoid(shadow_object
)) {
6990 * Give vm_pageout_scan() a chance to convert more
6991 * pages from "clean-in-place" to "clean-and-free",
6992 * if it's interested in the same pages we selected
6995 vm_object_unlock(shadow_object
);
6997 vm_object_lock(shadow_object
);
7001 vm_object_paging_end(shadow_object
);
7002 vm_object_unlock(shadow_object
);
7004 if(isVectorUPL
&& subupl_size
)
7005 goto process_upl_to_encrypt
;
7012 __unused upl_offset_t crypt_offset
,
7013 __unused upl_size_t crypt_size
)
7019 __unused vm_page_t page
,
7020 __unused vm_map_offset_t kernel_mapping_offset
)
7026 __unused vm_page_t page
,
7027 __unused vm_map_offset_t kernel_mapping_offset
)
7034 vm_pageout_queue_steal(vm_page_t page
, boolean_t queues_locked
)
7038 pageout
= page
->pageout
;
7040 page
->list_req_pending
= FALSE
;
7041 page
->cleaning
= FALSE
;
7042 page
->pageout
= FALSE
;
7044 if (!queues_locked
) {
7045 vm_page_lockspin_queues();
7049 * need to drop the laundry count...
7050 * we may also need to remove it
7051 * from the I/O paging queue...
7052 * vm_pageout_throttle_up handles both cases
7054 * the laundry and pageout_queue flags are cleared...
7056 vm_pageout_throttle_up(page
);
7058 if (pageout
== TRUE
) {
7060 * toss the wire count we picked up
7061 * when we intially set this page up
7064 vm_page_unwire(page
, TRUE
);
7066 vm_page_steal_pageout_page
++;
7068 if (!queues_locked
) {
7069 vm_page_unlock_queues();
7074 vector_upl_create(vm_offset_t upl_offset
)
7076 int vector_upl_size
= sizeof(struct _vector_upl
);
7079 vector_upl_t vector_upl
= (vector_upl_t
)kalloc(vector_upl_size
);
7081 upl
= upl_create(0,UPL_VECTOR
,0);
7082 upl
->vector_upl
= vector_upl
;
7083 upl
->offset
= upl_offset
;
7084 vector_upl
->size
= 0;
7085 vector_upl
->offset
= upl_offset
;
7086 vector_upl
->invalid_upls
=0;
7087 vector_upl
->num_upls
=0;
7088 vector_upl
->pagelist
= NULL
;
7090 for(i
=0; i
< MAX_VECTOR_UPL_ELEMENTS
; i
++) {
7091 vector_upl
->upl_iostates
[i
].size
= 0;
7092 vector_upl
->upl_iostates
[i
].offset
= 0;
7099 vector_upl_deallocate(upl_t upl
)
7102 vector_upl_t vector_upl
= upl
->vector_upl
;
7104 if(vector_upl
->invalid_upls
!= vector_upl
->num_upls
)
7105 panic("Deallocating non-empty Vectored UPL\n");
7106 kfree(vector_upl
->pagelist
,(sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
)));
7107 vector_upl
->invalid_upls
=0;
7108 vector_upl
->num_upls
= 0;
7109 vector_upl
->pagelist
= NULL
;
7110 vector_upl
->size
= 0;
7111 vector_upl
->offset
= 0;
7112 kfree(vector_upl
, sizeof(struct _vector_upl
));
7113 vector_upl
= (vector_upl_t
)0xdeadbeef;
7116 panic("vector_upl_deallocate was passed a non-vectored upl\n");
7119 panic("vector_upl_deallocate was passed a NULL upl\n");
7123 vector_upl_is_valid(upl_t upl
)
7125 if(upl
&& ((upl
->flags
& UPL_VECTOR
)==UPL_VECTOR
)) {
7126 vector_upl_t vector_upl
= upl
->vector_upl
;
7127 if(vector_upl
== NULL
|| vector_upl
== (vector_upl_t
)0xdeadbeef || vector_upl
== (vector_upl_t
)0xfeedbeef)
7136 vector_upl_set_subupl(upl_t upl
,upl_t subupl
, uint32_t io_size
)
7138 if(vector_upl_is_valid(upl
)) {
7139 vector_upl_t vector_upl
= upl
->vector_upl
;
7144 if(io_size
< PAGE_SIZE
)
7145 io_size
= PAGE_SIZE
;
7146 subupl
->vector_upl
= (void*)vector_upl
;
7147 vector_upl
->upl_elems
[vector_upl
->num_upls
++] = subupl
;
7148 vector_upl
->size
+= io_size
;
7149 upl
->size
+= io_size
;
7152 uint32_t i
=0,invalid_upls
=0;
7153 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
7154 if(vector_upl
->upl_elems
[i
] == subupl
)
7157 if(i
== vector_upl
->num_upls
)
7158 panic("Trying to remove sub-upl when none exists");
7160 vector_upl
->upl_elems
[i
] = NULL
;
7161 invalid_upls
= hw_atomic_add(&(vector_upl
)->invalid_upls
, 1);
7162 if(invalid_upls
== vector_upl
->num_upls
)
7169 panic("vector_upl_set_subupl was passed a NULL upl element\n");
7172 panic("vector_upl_set_subupl was passed a non-vectored upl\n");
7175 panic("vector_upl_set_subupl was passed a NULL upl\n");
7181 vector_upl_set_pagelist(upl_t upl
)
7183 if(vector_upl_is_valid(upl
)) {
7185 vector_upl_t vector_upl
= upl
->vector_upl
;
7188 vm_offset_t pagelist_size
=0, cur_upl_pagelist_size
=0;
7190 vector_upl
->pagelist
= (upl_page_info_array_t
)kalloc(sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
));
7192 for(i
=0; i
< vector_upl
->num_upls
; i
++) {
7193 cur_upl_pagelist_size
= sizeof(struct upl_page_info
) * vector_upl
->upl_elems
[i
]->size
/PAGE_SIZE
;
7194 bcopy(UPL_GET_INTERNAL_PAGE_LIST_SIMPLE(vector_upl
->upl_elems
[i
]), (char*)vector_upl
->pagelist
+ pagelist_size
, cur_upl_pagelist_size
);
7195 pagelist_size
+= cur_upl_pagelist_size
;
7196 if(vector_upl
->upl_elems
[i
]->highest_page
> upl
->highest_page
)
7197 upl
->highest_page
= vector_upl
->upl_elems
[i
]->highest_page
;
7199 assert( pagelist_size
== (sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
)) );
7202 panic("vector_upl_set_pagelist was passed a non-vectored upl\n");
7205 panic("vector_upl_set_pagelist was passed a NULL upl\n");
7210 vector_upl_subupl_byindex(upl_t upl
, uint32_t index
)
7212 if(vector_upl_is_valid(upl
)) {
7213 vector_upl_t vector_upl
= upl
->vector_upl
;
7215 if(index
< vector_upl
->num_upls
)
7216 return vector_upl
->upl_elems
[index
];
7219 panic("vector_upl_subupl_byindex was passed a non-vectored upl\n");
7225 vector_upl_subupl_byoffset(upl_t upl
, upl_offset_t
*upl_offset
, upl_size_t
*upl_size
)
7227 if(vector_upl_is_valid(upl
)) {
7229 vector_upl_t vector_upl
= upl
->vector_upl
;
7232 upl_t subupl
= NULL
;
7233 vector_upl_iostates_t subupl_state
;
7235 for(i
=0; i
< vector_upl
->num_upls
; i
++) {
7236 subupl
= vector_upl
->upl_elems
[i
];
7237 subupl_state
= vector_upl
->upl_iostates
[i
];
7238 if( *upl_offset
<= (subupl_state
.offset
+ subupl_state
.size
- 1)) {
7239 /* We could have been passed an offset/size pair that belongs
7240 * to an UPL element that has already been committed/aborted.
7241 * If so, return NULL.
7245 if((subupl_state
.offset
+ subupl_state
.size
) < (*upl_offset
+ *upl_size
)) {
7246 *upl_size
= (subupl_state
.offset
+ subupl_state
.size
) - *upl_offset
;
7247 if(*upl_size
> subupl_state
.size
)
7248 *upl_size
= subupl_state
.size
;
7250 if(*upl_offset
>= subupl_state
.offset
)
7251 *upl_offset
-= subupl_state
.offset
;
7253 panic("Vector UPL offset miscalculation\n");
7259 panic("vector_upl_subupl_byoffset was passed a non-vectored UPL\n");
7265 vector_upl_get_submap(upl_t upl
, vm_map_t
*v_upl_submap
, vm_offset_t
*submap_dst_addr
)
7267 *v_upl_submap
= NULL
;
7269 if(vector_upl_is_valid(upl
)) {
7270 vector_upl_t vector_upl
= upl
->vector_upl
;
7272 *v_upl_submap
= vector_upl
->submap
;
7273 *submap_dst_addr
= vector_upl
->submap_dst_addr
;
7276 panic("vector_upl_get_submap was passed a non-vectored UPL\n");
7279 panic("vector_upl_get_submap was passed a null UPL\n");
7283 vector_upl_set_submap(upl_t upl
, vm_map_t submap
, vm_offset_t submap_dst_addr
)
7285 if(vector_upl_is_valid(upl
)) {
7286 vector_upl_t vector_upl
= upl
->vector_upl
;
7288 vector_upl
->submap
= submap
;
7289 vector_upl
->submap_dst_addr
= submap_dst_addr
;
7292 panic("vector_upl_get_submap was passed a non-vectored UPL\n");
7295 panic("vector_upl_get_submap was passed a NULL UPL\n");
7299 vector_upl_set_iostate(upl_t upl
, upl_t subupl
, upl_offset_t offset
, upl_size_t size
)
7301 if(vector_upl_is_valid(upl
)) {
7303 vector_upl_t vector_upl
= upl
->vector_upl
;
7306 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
7307 if(vector_upl
->upl_elems
[i
] == subupl
)
7311 if(i
== vector_upl
->num_upls
)
7312 panic("setting sub-upl iostate when none exists");
7314 vector_upl
->upl_iostates
[i
].offset
= offset
;
7315 if(size
< PAGE_SIZE
)
7317 vector_upl
->upl_iostates
[i
].size
= size
;
7320 panic("vector_upl_set_iostate was passed a non-vectored UPL\n");
7323 panic("vector_upl_set_iostate was passed a NULL UPL\n");
7327 vector_upl_get_iostate(upl_t upl
, upl_t subupl
, upl_offset_t
*offset
, upl_size_t
*size
)
7329 if(vector_upl_is_valid(upl
)) {
7331 vector_upl_t vector_upl
= upl
->vector_upl
;
7334 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
7335 if(vector_upl
->upl_elems
[i
] == subupl
)
7339 if(i
== vector_upl
->num_upls
)
7340 panic("getting sub-upl iostate when none exists");
7342 *offset
= vector_upl
->upl_iostates
[i
].offset
;
7343 *size
= vector_upl
->upl_iostates
[i
].size
;
7346 panic("vector_upl_get_iostate was passed a non-vectored UPL\n");
7349 panic("vector_upl_get_iostate was passed a NULL UPL\n");
7353 vector_upl_get_iostate_byindex(upl_t upl
, uint32_t index
, upl_offset_t
*offset
, upl_size_t
*size
)
7355 if(vector_upl_is_valid(upl
)) {
7356 vector_upl_t vector_upl
= upl
->vector_upl
;
7358 if(index
< vector_upl
->num_upls
) {
7359 *offset
= vector_upl
->upl_iostates
[index
].offset
;
7360 *size
= vector_upl
->upl_iostates
[index
].size
;
7363 *offset
= *size
= 0;
7366 panic("vector_upl_get_iostate_byindex was passed a non-vectored UPL\n");
7369 panic("vector_upl_get_iostate_byindex was passed a NULL UPL\n");
7373 upl_get_internal_vectorupl_pagelist(upl_t upl
)
7375 return ((vector_upl_t
)(upl
->vector_upl
))->pagelist
;
7379 upl_get_internal_vectorupl(upl_t upl
)
7381 return upl
->vector_upl
;
7385 upl_get_internal_pagelist_offset(void)
7387 return sizeof(struct upl
);
7396 upl
->flags
|= UPL_CLEAR_DIRTY
;
7398 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
7409 upl
->ext_ref_count
++;
7411 if (!upl
->ext_ref_count
) {
7412 panic("upl_set_referenced not %p\n", upl
);
7414 upl
->ext_ref_count
--;
7420 vm_page_is_slideable(vm_page_t m
)
7422 boolean_t result
= FALSE
;
7423 vm_object_t slide_object
= slide_info
.slide_object
;
7424 mach_vm_offset_t start
= slide_info
.start
;
7425 mach_vm_offset_t end
= slide_info
.end
;
7427 /* make sure our page belongs to the one object allowed to do this */
7428 if (slide_object
== VM_OBJECT_NULL
) {
7432 /*Should we traverse down the chain?*/
7433 if (m
->object
!= slide_object
) {
7437 if(!m
->slid
&& (start
<= m
->offset
&& end
> m
->offset
)) {
7443 int vm_page_slide_counter
= 0;
7444 int vm_page_slide_errors
= 0;
7448 vm_map_offset_t kernel_mapping_offset
)
7451 vm_map_size_t kernel_mapping_size
;
7452 vm_offset_t kernel_vaddr
;
7453 uint32_t pageIndex
= 0;
7455 assert(!page
->slid
);
7458 * Take a paging-in-progress reference to keep the object
7459 * alive even if we have to unlock it (in vm_paging_map_object()
7462 vm_object_paging_begin(page
->object
);
7464 if (kernel_mapping_offset
== 0) {
7466 * The page hasn't already been mapped in kernel space
7467 * by the caller. Map it now, so that we can access
7468 * its contents and decrypt them.
7470 kernel_mapping_size
= PAGE_SIZE
;
7471 kr
= vm_paging_map_object(&kernel_mapping_offset
,
7475 &kernel_mapping_size
,
7476 VM_PROT_READ
| VM_PROT_WRITE
,
7478 if (kr
!= KERN_SUCCESS
) {
7479 panic("vm_page_slide: "
7480 "could not map page in kernel: 0x%x\n",
7484 kernel_mapping_size
= 0;
7486 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
7489 * Slide the pointers on the page.
7492 /*assert that slide_file_info.start/end are page-aligned?*/
7494 pageIndex
= (uint32_t)((page
->offset
- slide_info
.start
)/PAGE_SIZE
);
7495 kr
= vm_shared_region_slide(kernel_vaddr
, pageIndex
);
7496 vm_page_slide_counter
++;
7499 * Unmap the page from the kernel's address space,
7501 if (kernel_mapping_size
!= 0) {
7502 vm_paging_unmap_object(page
->object
,
7504 kernel_vaddr
+ PAGE_SIZE
);
7507 page
->dirty
= FALSE
;
7508 pmap_clear_refmod(page
->phys_page
, VM_MEM_MODIFIED
| VM_MEM_REFERENCED
);
7510 if (kr
== KERN_SUCCESS
) {
7514 vm_page_slide_errors
++;
7517 vm_object_paging_end(page
->object
);
7525 boolean_t
upl_device_page(upl_page_info_t
*upl
)
7527 return(UPL_DEVICE_PAGE(upl
));
7529 boolean_t
upl_page_present(upl_page_info_t
*upl
, int index
)
7531 return(UPL_PAGE_PRESENT(upl
, index
));
7533 boolean_t
upl_speculative_page(upl_page_info_t
*upl
, int index
)
7535 return(UPL_SPECULATIVE_PAGE(upl
, index
));
7537 boolean_t
upl_dirty_page(upl_page_info_t
*upl
, int index
)
7539 return(UPL_DIRTY_PAGE(upl
, index
));
7541 boolean_t
upl_valid_page(upl_page_info_t
*upl
, int index
)
7543 return(UPL_VALID_PAGE(upl
, index
));
7545 ppnum_t
upl_phys_page(upl_page_info_t
*upl
, int index
)
7547 return(UPL_PHYS_PAGE(upl
, index
));
7552 vm_countdirtypages(void)
7564 vm_page_lock_queues();
7565 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
7567 if (m
==(vm_page_t
)0) break;
7569 if(m
->dirty
) dpages
++;
7570 if(m
->pageout
) pgopages
++;
7571 if(m
->precious
) precpages
++;
7573 assert(m
->object
!= kernel_object
);
7574 m
= (vm_page_t
) queue_next(&m
->pageq
);
7575 if (m
==(vm_page_t
)0) break;
7577 } while (!queue_end(&vm_page_queue_inactive
,(queue_entry_t
) m
));
7578 vm_page_unlock_queues();
7580 vm_page_lock_queues();
7581 m
= (vm_page_t
) queue_first(&vm_page_queue_throttled
);
7583 if (m
==(vm_page_t
)0) break;
7587 assert(!m
->pageout
);
7588 assert(m
->object
!= kernel_object
);
7589 m
= (vm_page_t
) queue_next(&m
->pageq
);
7590 if (m
==(vm_page_t
)0) break;
7592 } while (!queue_end(&vm_page_queue_throttled
,(queue_entry_t
) m
));
7593 vm_page_unlock_queues();
7595 vm_page_lock_queues();
7596 m
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
7598 if (m
==(vm_page_t
)0) break;
7600 if(m
->dirty
) dpages
++;
7601 if(m
->pageout
) pgopages
++;
7602 if(m
->precious
) precpages
++;
7604 assert(m
->object
!= kernel_object
);
7605 m
= (vm_page_t
) queue_next(&m
->pageq
);
7606 if (m
==(vm_page_t
)0) break;
7608 } while (!queue_end(&vm_page_queue_zf
,(queue_entry_t
) m
));
7609 vm_page_unlock_queues();
7611 printf("IN Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
7617 vm_page_lock_queues();
7618 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
7621 if(m
== (vm_page_t
)0) break;
7622 if(m
->dirty
) dpages
++;
7623 if(m
->pageout
) pgopages
++;
7624 if(m
->precious
) precpages
++;
7626 assert(m
->object
!= kernel_object
);
7627 m
= (vm_page_t
) queue_next(&m
->pageq
);
7628 if(m
== (vm_page_t
)0) break;
7630 } while (!queue_end(&vm_page_queue_active
,(queue_entry_t
) m
));
7631 vm_page_unlock_queues();
7633 printf("AC Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
7636 #endif /* MACH_BSD */
7638 ppnum_t
upl_get_highest_page(
7641 return upl
->highest_page
;
7644 upl_size_t
upl_get_size(
7651 kern_return_t
upl_ubc_alias_set(upl_t upl
, uintptr_t alias1
, uintptr_t alias2
)
7653 upl
->ubc_alias1
= alias1
;
7654 upl
->ubc_alias2
= alias2
;
7655 return KERN_SUCCESS
;
7657 int upl_ubc_alias_get(upl_t upl
, uintptr_t * al
, uintptr_t * al2
)
7660 *al
= upl
->ubc_alias1
;
7662 *al2
= upl
->ubc_alias2
;
7663 return KERN_SUCCESS
;
7665 #endif /* UPL_DEBUG */
7670 #include <ddb/db_output.h>
7671 #include <ddb/db_print.h>
7672 #include <vm/vm_print.h>
7674 #define printf kdbprintf
7675 void db_pageout(void);
7681 iprintf("VM Statistics:\n");
7683 iprintf("pages:\n");
7685 iprintf("activ %5d inact %5d free %5d",
7686 vm_page_active_count
, vm_page_inactive_count
,
7687 vm_page_free_count
);
7688 printf(" wire %5d gobbl %5d\n",
7689 vm_page_wire_count
, vm_page_gobble_count
);
7691 iprintf("target:\n");
7693 iprintf("min %5d inact %5d free %5d",
7694 vm_page_free_min
, vm_page_inactive_target
,
7695 vm_page_free_target
);
7696 printf(" resrv %5d\n", vm_page_free_reserved
);
7698 iprintf("pause:\n");
7704 extern int c_laundry_pages_freed
;
7705 #endif /* MACH_COUNTERS */
7710 iprintf("Pageout Statistics:\n");
7712 iprintf("active %5d inactv %5d\n",
7713 vm_pageout_active
, vm_pageout_inactive
);
7714 iprintf("nolock %5d avoid %5d busy %5d absent %5d\n",
7715 vm_pageout_inactive_nolock
, vm_pageout_inactive_avoid
,
7716 vm_pageout_inactive_busy
, vm_pageout_inactive_absent
);
7717 iprintf("used %5d clean %5d dirty(internal) %5d dirty(external) %5d\n",
7718 vm_pageout_inactive_used
, vm_pageout_inactive_clean
,
7719 vm_pageout_inactive_dirty_internal
, vm_pageout_inactive_dirty_external
);
7721 iprintf("laundry_pages_freed %d\n", c_laundry_pages_freed
);
7722 #endif /* MACH_COUNTERS */
7723 #if MACH_CLUSTER_STATS
7724 iprintf("Cluster Statistics:\n");
7726 iprintf("dirtied %5d cleaned %5d collisions %5d\n",
7727 vm_pageout_cluster_dirtied
, vm_pageout_cluster_cleaned
,
7728 vm_pageout_cluster_collisions
);
7729 iprintf("clusters %5d conversions %5d\n",
7730 vm_pageout_cluster_clusters
, vm_pageout_cluster_conversions
);
7732 iprintf("Target Statistics:\n");
7734 iprintf("collisions %5d page_dirtied %5d page_freed %5d\n",
7735 vm_pageout_target_collisions
, vm_pageout_target_page_dirtied
,
7736 vm_pageout_target_page_freed
);
7738 #endif /* MACH_CLUSTER_STATS */
7742 #endif /* MACH_KDB */