2 * Copyright (c) 2000-2007 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/thread.h>
92 #include <kern/kalloc.h>
94 #include <machine/vm_tuning.h>
97 #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>
113 #include <../bsd/crypto/aes/aes.h>
116 #ifndef VM_PAGEOUT_BURST_ACTIVE_THROTTLE /* maximum iterations of the active queue to move pages to inactive */
117 #ifdef CONFIG_EMBEDDED
118 #define VM_PAGEOUT_BURST_ACTIVE_THROTTLE 2048
120 #define VM_PAGEOUT_BURST_ACTIVE_THROTTLE 100
124 #ifndef VM_PAGEOUT_BURST_INACTIVE_THROTTLE /* maximum iterations of the inactive queue w/o stealing/cleaning a page */
125 #ifdef CONFIG_EMBEDDED
126 #define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 1024
128 #define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 4096
132 #ifndef VM_PAGEOUT_DEADLOCK_RELIEF
133 #define VM_PAGEOUT_DEADLOCK_RELIEF 100 /* number of pages to move to break deadlock */
136 #ifndef VM_PAGEOUT_INACTIVE_RELIEF
137 #define VM_PAGEOUT_INACTIVE_RELIEF 50 /* minimum number of pages to move to the inactive q */
140 #ifndef VM_PAGE_LAUNDRY_MAX
141 #define VM_PAGE_LAUNDRY_MAX 16UL /* maximum pageouts on a given pageout queue */
142 #endif /* VM_PAGEOUT_LAUNDRY_MAX */
144 #ifndef VM_PAGEOUT_BURST_WAIT
145 #define VM_PAGEOUT_BURST_WAIT 30 /* milliseconds per page */
146 #endif /* VM_PAGEOUT_BURST_WAIT */
148 #ifndef VM_PAGEOUT_EMPTY_WAIT
149 #define VM_PAGEOUT_EMPTY_WAIT 200 /* milliseconds */
150 #endif /* VM_PAGEOUT_EMPTY_WAIT */
152 #ifndef VM_PAGEOUT_DEADLOCK_WAIT
153 #define VM_PAGEOUT_DEADLOCK_WAIT 300 /* milliseconds */
154 #endif /* VM_PAGEOUT_DEADLOCK_WAIT */
156 #ifndef VM_PAGEOUT_IDLE_WAIT
157 #define VM_PAGEOUT_IDLE_WAIT 10 /* milliseconds */
158 #endif /* VM_PAGEOUT_IDLE_WAIT */
160 #ifndef VM_PAGE_SPECULATIVE_TARGET
161 #define VM_PAGE_SPECULATIVE_TARGET(total) ((total) * 1 / 20)
162 #endif /* VM_PAGE_SPECULATIVE_TARGET */
164 #ifndef VM_PAGE_INACTIVE_HEALTHY_LIMIT
165 #define VM_PAGE_INACTIVE_HEALTHY_LIMIT(total) ((total) * 1 / 200)
166 #endif /* VM_PAGE_INACTIVE_HEALTHY_LIMIT */
170 * To obtain a reasonable LRU approximation, the inactive queue
171 * needs to be large enough to give pages on it a chance to be
172 * referenced a second time. This macro defines the fraction
173 * of active+inactive pages that should be inactive.
174 * The pageout daemon uses it to update vm_page_inactive_target.
176 * If vm_page_free_count falls below vm_page_free_target and
177 * vm_page_inactive_count is below vm_page_inactive_target,
178 * then the pageout daemon starts running.
181 #ifndef VM_PAGE_INACTIVE_TARGET
182 #define VM_PAGE_INACTIVE_TARGET(avail) ((avail) * 1 / 3)
183 #endif /* VM_PAGE_INACTIVE_TARGET */
186 * Once the pageout daemon starts running, it keeps going
187 * until vm_page_free_count meets or exceeds vm_page_free_target.
190 #ifndef VM_PAGE_FREE_TARGET
191 #ifdef CONFIG_EMBEDDED
192 #define VM_PAGE_FREE_TARGET(free) (15 + (free) / 100)
194 #define VM_PAGE_FREE_TARGET(free) (15 + (free) / 80)
196 #endif /* VM_PAGE_FREE_TARGET */
199 * The pageout daemon always starts running once vm_page_free_count
200 * falls below vm_page_free_min.
203 #ifndef VM_PAGE_FREE_MIN
204 #ifdef CONFIG_EMBEDDED
205 #define VM_PAGE_FREE_MIN(free) (10 + (free) / 200)
207 #define VM_PAGE_FREE_MIN(free) (10 + (free) / 100)
209 #endif /* VM_PAGE_FREE_MIN */
211 #define VM_PAGE_FREE_MIN_LIMIT 1500
212 #define VM_PAGE_FREE_TARGET_LIMIT 2000
216 * When vm_page_free_count falls below vm_page_free_reserved,
217 * only vm-privileged threads can allocate pages. vm-privilege
218 * allows the pageout daemon and default pager (and any other
219 * associated threads needed for default pageout) to continue
220 * operation by dipping into the reserved pool of pages.
223 #ifndef VM_PAGE_FREE_RESERVED
224 #define VM_PAGE_FREE_RESERVED(n) \
225 ((6 * VM_PAGE_LAUNDRY_MAX) + (n))
226 #endif /* VM_PAGE_FREE_RESERVED */
229 * When we dequeue pages from the inactive list, they are
230 * reactivated (ie, put back on the active queue) if referenced.
231 * However, it is possible to starve the free list if other
232 * processors are referencing pages faster than we can turn off
233 * the referenced bit. So we limit the number of reactivations
234 * we will make per call of vm_pageout_scan().
236 #define VM_PAGE_REACTIVATE_LIMIT_MAX 20000
237 #ifndef VM_PAGE_REACTIVATE_LIMIT
238 #ifdef CONFIG_EMBEDDED
239 #define VM_PAGE_REACTIVATE_LIMIT(avail) (VM_PAGE_INACTIVE_TARGET(avail) / 2)
241 #define VM_PAGE_REACTIVATE_LIMIT(avail) (MAX((avail) * 1 / 20,VM_PAGE_REACTIVATE_LIMIT_MAX))
243 #endif /* VM_PAGE_REACTIVATE_LIMIT */
244 #define VM_PAGEOUT_INACTIVE_FORCE_RECLAIM 100
248 * must hold the page queues lock to
249 * manipulate this structure
251 struct vm_pageout_queue
{
252 queue_head_t pgo_pending
; /* laundry pages to be processed by pager's iothread */
253 unsigned int pgo_laundry
; /* current count of laundry pages on queue or in flight */
254 unsigned int pgo_maxlaundry
;
256 unsigned int pgo_idle
:1, /* iothread is blocked waiting for work to do */
257 pgo_busy
:1, /* iothread is currently processing request from pgo_pending */
258 pgo_throttled
:1,/* vm_pageout_scan thread needs a wakeup when pgo_laundry drops */
262 #define VM_PAGE_Q_THROTTLED(q) \
263 ((q)->pgo_laundry >= (q)->pgo_maxlaundry)
267 * Exported variable used to broadcast the activation of the pageout scan
268 * Working Set uses this to throttle its use of pmap removes. In this
269 * way, code which runs within memory in an uncontested context does
270 * not keep encountering soft faults.
273 unsigned int vm_pageout_scan_event_counter
= 0;
276 * Forward declarations for internal routines.
279 static void vm_pageout_garbage_collect(int);
280 static void vm_pageout_iothread_continue(struct vm_pageout_queue
*);
281 static void vm_pageout_iothread_external(void);
282 static void vm_pageout_iothread_internal(void);
283 static void vm_pageout_queue_steal(vm_page_t
);
285 extern void vm_pageout_continue(void);
286 extern void vm_pageout_scan(void);
288 static thread_t vm_pageout_external_iothread
= THREAD_NULL
;
289 static thread_t vm_pageout_internal_iothread
= THREAD_NULL
;
291 unsigned int vm_pageout_reserved_internal
= 0;
292 unsigned int vm_pageout_reserved_really
= 0;
294 unsigned int vm_pageout_idle_wait
= 0; /* milliseconds */
295 unsigned int vm_pageout_empty_wait
= 0; /* milliseconds */
296 unsigned int vm_pageout_burst_wait
= 0; /* milliseconds */
297 unsigned int vm_pageout_deadlock_wait
= 0; /* milliseconds */
298 unsigned int vm_pageout_deadlock_relief
= 0;
299 unsigned int vm_pageout_inactive_relief
= 0;
300 unsigned int vm_pageout_burst_active_throttle
= 0;
301 unsigned int vm_pageout_burst_inactive_throttle
= 0;
304 * Protection against zero fill flushing live working sets derived
305 * from existing backing store and files
307 unsigned int vm_accellerate_zf_pageout_trigger
= 400;
308 unsigned int zf_queue_min_count
= 100;
309 unsigned int vm_zf_count
= 0;
310 unsigned int vm_zf_queue_count
= 0;
313 * These variables record the pageout daemon's actions:
314 * how many pages it looks at and what happens to those pages.
315 * No locking needed because only one thread modifies the variables.
318 unsigned int vm_pageout_active
= 0; /* debugging */
319 unsigned int vm_pageout_inactive
= 0; /* debugging */
320 unsigned int vm_pageout_inactive_throttled
= 0; /* debugging */
321 unsigned int vm_pageout_inactive_forced
= 0; /* debugging */
322 unsigned int vm_pageout_inactive_nolock
= 0; /* debugging */
323 unsigned int vm_pageout_inactive_avoid
= 0; /* debugging */
324 unsigned int vm_pageout_inactive_busy
= 0; /* debugging */
325 unsigned int vm_pageout_inactive_absent
= 0; /* debugging */
326 unsigned int vm_pageout_inactive_used
= 0; /* debugging */
327 unsigned int vm_pageout_inactive_clean
= 0; /* debugging */
328 unsigned int vm_pageout_inactive_dirty
= 0; /* debugging */
329 unsigned int vm_pageout_dirty_no_pager
= 0; /* debugging */
330 unsigned int vm_pageout_purged_objects
= 0; /* debugging */
331 unsigned int vm_stat_discard
= 0; /* debugging */
332 unsigned int vm_stat_discard_sent
= 0; /* debugging */
333 unsigned int vm_stat_discard_failure
= 0; /* debugging */
334 unsigned int vm_stat_discard_throttle
= 0; /* debugging */
335 unsigned int vm_pageout_reactivation_limit_exceeded
= 0; /* debugging */
336 unsigned int vm_pageout_catch_ups
= 0; /* debugging */
337 unsigned int vm_pageout_inactive_force_reclaim
= 0; /* debugging */
339 unsigned int vm_pageout_scan_active_throttled
= 0;
340 unsigned int vm_pageout_scan_inactive_throttled
= 0;
341 unsigned int vm_pageout_scan_throttle
= 0; /* debugging */
342 unsigned int vm_pageout_scan_burst_throttle
= 0; /* debugging */
343 unsigned int vm_pageout_scan_empty_throttle
= 0; /* debugging */
344 unsigned int vm_pageout_scan_deadlock_detected
= 0; /* debugging */
345 unsigned int vm_pageout_scan_active_throttle_success
= 0; /* debugging */
346 unsigned int vm_pageout_scan_inactive_throttle_success
= 0; /* debugging */
348 * Backing store throttle when BS is exhausted
350 unsigned int vm_backing_store_low
= 0;
352 unsigned int vm_pageout_out_of_line
= 0;
353 unsigned int vm_pageout_in_place
= 0;
357 * counters and statistics...
359 unsigned long vm_page_decrypt_counter
= 0;
360 unsigned long vm_page_decrypt_for_upl_counter
= 0;
361 unsigned long vm_page_encrypt_counter
= 0;
362 unsigned long vm_page_encrypt_abort_counter
= 0;
363 unsigned long vm_page_encrypt_already_encrypted_counter
= 0;
364 boolean_t vm_pages_encrypted
= FALSE
; /* are there encrypted pages ? */
366 struct vm_pageout_queue vm_pageout_queue_internal
;
367 struct vm_pageout_queue vm_pageout_queue_external
;
369 unsigned int vm_page_speculative_target
= 0;
371 vm_object_t vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
373 unsigned long vm_cs_validated_resets
= 0;
376 * Routine: vm_backing_store_disable
378 * Suspend non-privileged threads wishing to extend
379 * backing store when we are low on backing store
380 * (Synchronized by caller)
383 vm_backing_store_disable(
387 vm_backing_store_low
= 1;
389 if(vm_backing_store_low
) {
390 vm_backing_store_low
= 0;
391 thread_wakeup((event_t
) &vm_backing_store_low
);
397 #if MACH_CLUSTER_STATS
398 unsigned long vm_pageout_cluster_dirtied
= 0;
399 unsigned long vm_pageout_cluster_cleaned
= 0;
400 unsigned long vm_pageout_cluster_collisions
= 0;
401 unsigned long vm_pageout_cluster_clusters
= 0;
402 unsigned long vm_pageout_cluster_conversions
= 0;
403 unsigned long vm_pageout_target_collisions
= 0;
404 unsigned long vm_pageout_target_page_dirtied
= 0;
405 unsigned long vm_pageout_target_page_freed
= 0;
406 #define CLUSTER_STAT(clause) clause
407 #else /* MACH_CLUSTER_STATS */
408 #define CLUSTER_STAT(clause)
409 #endif /* MACH_CLUSTER_STATS */
412 * Routine: vm_pageout_object_terminate
414 * Destroy the pageout_object, and perform all of the
415 * required cleanup actions.
418 * The object must be locked, and will be returned locked.
421 vm_pageout_object_terminate(
424 vm_object_t shadow_object
;
427 * Deal with the deallocation (last reference) of a pageout object
428 * (used for cleaning-in-place) by dropping the paging references/
429 * freeing pages in the original object.
432 assert(object
->pageout
);
433 shadow_object
= object
->shadow
;
434 vm_object_lock(shadow_object
);
436 while (!queue_empty(&object
->memq
)) {
438 vm_object_offset_t offset
;
440 p
= (vm_page_t
) queue_first(&object
->memq
);
445 assert(!p
->cleaning
);
451 m
= vm_page_lookup(shadow_object
,
452 offset
+ object
->shadow_offset
);
454 if(m
== VM_PAGE_NULL
)
457 /* used as a trigger on upl_commit etc to recognize the */
458 /* pageout daemon's subseqent desire to pageout a cleaning */
459 /* page. When the bit is on the upl commit code will */
460 /* respect the pageout bit in the target page over the */
461 /* caller's page list indication */
462 m
->dump_cleaning
= FALSE
;
464 assert((m
->dirty
) || (m
->precious
) ||
465 (m
->busy
&& m
->cleaning
));
468 * Handle the trusted pager throttle.
469 * Also decrement the burst throttle (if external).
471 vm_page_lock_queues();
473 vm_pageout_throttle_up(m
);
477 * Handle the "target" page(s). These pages are to be freed if
478 * successfully cleaned. Target pages are always busy, and are
479 * wired exactly once. The initial target pages are not mapped,
480 * (so cannot be referenced or modified) but converted target
481 * pages may have been modified between the selection as an
482 * adjacent page and conversion to a target.
486 assert(m
->wire_count
== 1);
488 m
->encrypted_cleaning
= FALSE
;
490 #if MACH_CLUSTER_STATS
491 if (m
->wanted
) vm_pageout_target_collisions
++;
494 * Revoke all access to the page. Since the object is
495 * locked, and the page is busy, this prevents the page
496 * from being dirtied after the pmap_disconnect() call
499 * Since the page is left "dirty" but "not modifed", we
500 * can detect whether the page was redirtied during
501 * pageout by checking the modify state.
503 if (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
)
509 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
510 vm_page_unwire(m
);/* reactivates */
511 VM_STAT_INCR(reactivations
);
514 CLUSTER_STAT(vm_pageout_target_page_freed
++;)
515 vm_page_free(m
);/* clears busy, etc. */
517 vm_page_unlock_queues();
521 * Handle the "adjacent" pages. These pages were cleaned in
522 * place, and should be left alone.
523 * If prep_pin_count is nonzero, then someone is using the
524 * page, so make it active.
526 if (!m
->active
&& !m
->inactive
&& !m
->throttled
&& !m
->private) {
530 vm_page_deactivate(m
);
532 if((m
->busy
) && (m
->cleaning
)) {
534 /* the request_page_list case, (COPY_OUT_FROM FALSE) */
537 /* We do not re-set m->dirty ! */
538 /* The page was busy so no extraneous activity */
539 /* could have occurred. COPY_INTO is a read into the */
540 /* new pages. CLEAN_IN_PLACE does actually write */
541 /* out the pages but handling outside of this code */
542 /* will take care of resetting dirty. We clear the */
543 /* modify however for the Programmed I/O case. */
544 pmap_clear_modify(m
->phys_page
);
547 m
->overwriting
= FALSE
;
548 } else if (m
->overwriting
) {
549 /* alternate request page list, write to page_list */
550 /* case. Occurs when the original page was wired */
551 /* at the time of the list request */
552 assert(m
->wire_count
!= 0);
553 vm_page_unwire(m
);/* reactivates */
554 m
->overwriting
= FALSE
;
557 * Set the dirty state according to whether or not the page was
558 * modified during the pageout. Note that we purposefully do
559 * NOT call pmap_clear_modify since the page is still mapped.
560 * If the page were to be dirtied between the 2 calls, this
561 * this fact would be lost. This code is only necessary to
562 * maintain statistics, since the pmap module is always
563 * consulted if m->dirty is false.
565 #if MACH_CLUSTER_STATS
566 m
->dirty
= pmap_is_modified(m
->phys_page
);
568 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
569 else vm_pageout_cluster_cleaned
++;
570 if (m
->wanted
) vm_pageout_cluster_collisions
++;
576 m
->encrypted_cleaning
= FALSE
;
579 * Wakeup any thread waiting for the page to be un-cleaning.
582 vm_page_unlock_queues();
585 * Account for the paging reference taken in vm_paging_object_allocate.
587 vm_object_paging_end(shadow_object
);
588 vm_object_unlock(shadow_object
);
590 assert(object
->ref_count
== 0);
591 assert(object
->paging_in_progress
== 0);
592 assert(object
->resident_page_count
== 0);
597 * Routine: vm_pageclean_setup
599 * Purpose: setup a page to be cleaned (made non-dirty), but not
600 * necessarily flushed from the VM page cache.
601 * This is accomplished by cleaning in place.
603 * The page must not be busy, and the object and page
604 * queues must be locked.
611 vm_object_t new_object
,
612 vm_object_offset_t new_offset
)
616 assert(!m
->cleaning
);
620 "vm_pageclean_setup, obj 0x%X off 0x%X page 0x%X new 0x%X new_off 0x%X\n",
621 (integer_t
)m
->object
, m
->offset
, (integer_t
)m
,
622 (integer_t
)new_m
, new_offset
);
624 pmap_clear_modify(m
->phys_page
);
627 * Mark original page as cleaning in place.
634 * Convert the fictitious page to a private shadow of
637 assert(new_m
->fictitious
);
638 assert(new_m
->phys_page
== vm_page_fictitious_addr
);
639 new_m
->fictitious
= FALSE
;
640 new_m
->private = TRUE
;
641 new_m
->pageout
= TRUE
;
642 new_m
->phys_page
= m
->phys_page
;
645 vm_page_insert(new_m
, new_object
, new_offset
);
646 assert(!new_m
->wanted
);
651 * Routine: vm_pageout_initialize_page
653 * Causes the specified page to be initialized in
654 * the appropriate memory object. This routine is used to push
655 * pages into a copy-object when they are modified in the
658 * The page is moved to a temporary object and paged out.
661 * The page in question must not be on any pageout queues.
662 * The object to which it belongs must be locked.
663 * The page must be busy, but not hold a paging reference.
666 * Move this page to a completely new object.
669 vm_pageout_initialize_page(
673 vm_object_offset_t paging_offset
;
674 vm_page_t holding_page
;
675 memory_object_t pager
;
678 "vm_pageout_initialize_page, page 0x%X\n",
679 (integer_t
)m
, 0, 0, 0, 0);
683 * Verify that we really want to clean this page
690 * Create a paging reference to let us play with the object.
693 paging_offset
= m
->offset
+ object
->paging_offset
;
695 if (m
->absent
|| m
->error
|| m
->restart
|| (!m
->dirty
&& !m
->precious
)) {
697 panic("reservation without pageout?"); /* alan */
698 vm_object_unlock(object
);
704 * If there's no pager, then we can't clean the page. This should
705 * never happen since this should be a copy object and therefore not
706 * an external object, so the pager should always be there.
709 pager
= object
->pager
;
711 if (pager
== MEMORY_OBJECT_NULL
) {
713 panic("missing pager for copy object");
717 /* set the page for future call to vm_fault_list_request */
718 vm_object_paging_begin(object
);
720 vm_page_lock_queues();
721 pmap_clear_modify(m
->phys_page
);
724 m
->list_req_pending
= TRUE
;
728 vm_page_unlock_queues();
729 vm_object_unlock(object
);
732 * Write the data to its pager.
733 * Note that the data is passed by naming the new object,
734 * not a virtual address; the pager interface has been
735 * manipulated to use the "internal memory" data type.
736 * [The object reference from its allocation is donated
737 * to the eventual recipient.]
739 memory_object_data_initialize(pager
, paging_offset
, PAGE_SIZE
);
741 vm_object_lock(object
);
742 vm_object_paging_end(object
);
745 #if MACH_CLUSTER_STATS
746 #define MAXCLUSTERPAGES 16
748 unsigned long pages_in_cluster
;
749 unsigned long pages_at_higher_offsets
;
750 unsigned long pages_at_lower_offsets
;
751 } cluster_stats
[MAXCLUSTERPAGES
];
752 #endif /* MACH_CLUSTER_STATS */
756 * vm_pageout_cluster:
758 * Given a page, queue it to the appropriate I/O thread,
759 * which will page it out and attempt to clean adjacent pages
760 * in the same operation.
762 * The page must be busy, and the object and queues locked. We will take a
763 * paging reference to prevent deallocation or collapse when we
764 * release the object lock back at the call site. The I/O thread
765 * is responsible for consuming this reference
767 * The page must not be on any pageout queue.
771 vm_pageout_cluster(vm_page_t m
)
773 vm_object_t object
= m
->object
;
774 struct vm_pageout_queue
*q
;
778 "vm_pageout_cluster, object 0x%X offset 0x%X page 0x%X\n",
779 (integer_t
)object
, m
->offset
, (integer_t
)m
, 0, 0);
782 * Only a certain kind of page is appreciated here.
784 assert(m
->busy
&& (m
->dirty
|| m
->precious
) && (m
->wire_count
== 0));
785 assert(!m
->cleaning
&& !m
->pageout
&& !m
->inactive
&& !m
->active
);
786 assert(!m
->throttled
);
789 * protect the object from collapse -
790 * locking in the object's paging_offset.
792 vm_object_paging_begin(object
);
795 * set the page for future call to vm_fault_list_request
796 * page should already be marked busy
799 m
->list_req_pending
= TRUE
;
804 if (object
->internal
== TRUE
)
805 q
= &vm_pageout_queue_internal
;
807 q
= &vm_pageout_queue_external
;
810 m
->pageout_queue
= TRUE
;
811 queue_enter(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
813 if (q
->pgo_idle
== TRUE
) {
815 thread_wakeup((event_t
) &q
->pgo_pending
);
820 unsigned long vm_pageout_throttle_up_count
= 0;
823 * A page is back from laundry. See if there are some pages waiting to
824 * go to laundry and if we can let some of them go now.
826 * Object and page queues must be locked.
829 vm_pageout_throttle_up(
832 struct vm_pageout_queue
*q
;
834 vm_pageout_throttle_up_count
++;
837 assert(m
->object
!= VM_OBJECT_NULL
);
838 assert(m
->object
!= kernel_object
);
840 if (m
->object
->internal
== TRUE
)
841 q
= &vm_pageout_queue_internal
;
843 q
= &vm_pageout_queue_external
;
848 if (q
->pgo_throttled
== TRUE
) {
849 q
->pgo_throttled
= FALSE
;
850 thread_wakeup((event_t
) &q
->pgo_laundry
);
856 * vm_pageout_scan does the dirty work for the pageout daemon.
857 * It returns with vm_page_queue_free_lock held and
858 * vm_page_free_wanted == 0.
861 #define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT (3 * MAX_UPL_TRANSFER)
864 #define FCS_DELAYED 1
865 #define FCS_DEADLOCK_DETECTED 2
867 struct flow_control
{
873 vm_pageout_scan(void)
875 unsigned int loop_count
= 0;
876 unsigned int inactive_burst_count
= 0;
877 unsigned int active_burst_count
= 0;
878 unsigned int reactivated_this_call
;
879 unsigned int reactivate_limit
;
880 vm_page_t local_freeq
= NULL
;
883 int need_internal_inactive
= 0;
884 int refmod_state
= 0;
885 int vm_pageout_deadlock_target
= 0;
886 struct vm_pageout_queue
*iq
;
887 struct vm_pageout_queue
*eq
;
888 struct vm_speculative_age_q
*sq
;
889 struct flow_control flow_control
;
890 boolean_t inactive_throttled
= FALSE
;
891 boolean_t try_failed
;
893 unsigned int msecs
= 0;
895 vm_object_t last_object_tried
;
898 uint32_t catch_up_count
= 0;
899 uint32_t inactive_reclaim_run
;
900 boolean_t forced_reclaim
;
902 flow_control
.state
= FCS_IDLE
;
903 iq
= &vm_pageout_queue_internal
;
904 eq
= &vm_pageout_queue_external
;
905 sq
= &vm_page_queue_speculative
[VM_PAGE_SPECULATIVE_AGED_Q
];
908 XPR(XPR_VM_PAGEOUT
, "vm_pageout_scan\n", 0, 0, 0, 0, 0);
911 vm_page_lock_queues();
912 delayed_unlock
= 1; /* must be nonzero if Qs are locked, 0 if unlocked */
915 * Calculate the max number of referenced pages on the inactive
916 * queue that we will reactivate.
918 reactivated_this_call
= 0;
919 reactivate_limit
= VM_PAGE_REACTIVATE_LIMIT(vm_page_active_count
+
920 vm_page_inactive_count
);
921 inactive_reclaim_run
= 0;
925 * We want to gradually dribble pages from the active queue
926 * to the inactive queue. If we let the inactive queue get
927 * very small, and then suddenly dump many pages into it,
928 * those pages won't get a sufficient chance to be referenced
929 * before we start taking them from the inactive queue.
931 * We must limit the rate at which we send pages to the pagers.
932 * data_write messages consume memory, for message buffers and
933 * for map-copy objects. If we get too far ahead of the pagers,
934 * we can potentially run out of memory.
936 * We can use the laundry count to limit directly the number
937 * of pages outstanding to the default pager. A similar
938 * strategy for external pagers doesn't work, because
939 * external pagers don't have to deallocate the pages sent them,
940 * and because we might have to send pages to external pagers
941 * even if they aren't processing writes. So we also
942 * use a burst count to limit writes to external pagers.
944 * When memory is very tight, we can't rely on external pagers to
945 * clean pages. They probably aren't running, because they
946 * aren't vm-privileged. If we kept sending dirty pages to them,
947 * we could exhaust the free list.
952 assert(delayed_unlock
!=0);
955 * A page is "zero-filled" if it was not paged in from somewhere,
956 * and it belongs to an object at least VM_ZF_OBJECT_SIZE_THRESHOLD big.
957 * Recalculate the zero-filled page ratio. We use this to apportion
958 * victimized pages between the normal and zero-filled inactive
959 * queues according to their relative abundance in memory. Thus if a task
960 * is flooding memory with zf pages, we begin to hunt them down.
961 * It would be better to throttle greedy tasks at a higher level,
962 * but at the moment mach vm cannot do this.
965 uint32_t total
= vm_page_active_count
+ vm_page_inactive_count
;
966 uint32_t normal
= total
- vm_zf_count
;
968 /* zf_ratio is the number of zf pages we victimize per normal page */
970 if (vm_zf_count
< vm_accellerate_zf_pageout_trigger
)
972 else if ((vm_zf_count
<= normal
) || (normal
== 0))
975 zf_ratio
= vm_zf_count
/ normal
;
981 * Recalculate vm_page_inactivate_target.
983 vm_page_inactive_target
= VM_PAGE_INACTIVE_TARGET(vm_page_active_count
+
984 vm_page_inactive_count
+
985 vm_page_speculative_count
);
987 * don't want to wake the pageout_scan thread up everytime we fall below
988 * the targets... set a low water mark at 0.25% below the target
990 vm_page_inactive_min
= vm_page_inactive_target
- (vm_page_inactive_target
/ 400);
992 vm_page_speculative_target
= VM_PAGE_SPECULATIVE_TARGET(vm_page_active_count
+
993 vm_page_inactive_count
);
995 last_object_tried
= NULL
;
998 if ((vm_page_inactive_count
+ vm_page_speculative_count
) < VM_PAGE_INACTIVE_HEALTHY_LIMIT(vm_page_active_count
))
999 catch_up_count
= vm_page_inactive_count
+ vm_page_speculative_count
;
1006 DTRACE_VM2(rev
, int, 1, (uint64_t *), NULL
);
1008 if (delayed_unlock
== 0) {
1009 vm_page_lock_queues();
1014 * Don't sweep through active queue more than the throttle
1015 * which should be kept relatively low
1017 active_burst_count
= vm_pageout_burst_active_throttle
;
1020 * Move pages from active to inactive.
1022 if (need_internal_inactive
== 0 && (vm_page_inactive_count
+ vm_page_speculative_count
) >= vm_page_inactive_target
)
1023 goto done_moving_active_pages
;
1025 while (!queue_empty(&vm_page_queue_active
) &&
1026 (need_internal_inactive
|| active_burst_count
)) {
1028 if (active_burst_count
)
1029 active_burst_count
--;
1031 vm_pageout_active
++;
1033 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
1035 assert(m
->active
&& !m
->inactive
);
1036 assert(!m
->laundry
);
1037 assert(m
->object
!= kernel_object
);
1038 assert(m
->phys_page
!= vm_page_guard_addr
);
1040 DTRACE_VM2(scan
, int, 1, (uint64_t *), NULL
);
1043 * Try to lock object; since we've already got the
1044 * page queues lock, we can only 'try' for this one.
1045 * if the 'try' fails, we need to do a mutex_pause
1046 * to allow the owner of the object lock a chance to
1047 * run... otherwise, we're likely to trip over this
1048 * object in the same state as we work our way through
1049 * the queue... clumps of pages associated with the same
1050 * object are fairly typical on the inactive and active queues
1052 if (m
->object
!= object
) {
1053 if (object
!= NULL
) {
1054 vm_object_unlock(object
);
1056 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1058 if (!vm_object_lock_try_scan(m
->object
)) {
1060 * move page to end of active queue and continue
1062 queue_remove(&vm_page_queue_active
, m
,
1064 queue_enter(&vm_page_queue_active
, m
,
1069 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
1071 * this is the next object we're going to be interested in
1072 * try to make sure its available after the mutex_yield
1075 vm_pageout_scan_wants_object
= m
->object
;
1077 goto done_with_activepage
;
1085 * if the page is BUSY, then we pull it
1086 * off the active queue and leave it alone.
1087 * when BUSY is cleared, it will get stuck
1088 * back on the appropriate queue
1091 queue_remove(&vm_page_queue_active
, m
,
1093 m
->pageq
.next
= NULL
;
1094 m
->pageq
.prev
= NULL
;
1097 vm_page_active_count
--;
1100 goto done_with_activepage
;
1104 * Deactivate the page while holding the object
1105 * locked, so we know the page is still not busy.
1106 * This should prevent races between pmap_enter
1107 * and pmap_clear_reference. The page might be
1108 * absent or fictitious, but vm_page_deactivate
1111 vm_page_deactivate(m
);
1113 if (need_internal_inactive
) {
1114 vm_pageout_scan_active_throttle_success
++;
1115 need_internal_inactive
--;
1117 done_with_activepage
:
1118 if (delayed_unlock
++ > VM_PAGEOUT_DELAYED_UNLOCK_LIMIT
|| try_failed
== TRUE
) {
1120 if (object
!= NULL
) {
1121 vm_object_unlock(object
);
1123 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1126 vm_page_free_list(local_freeq
);
1131 mutex_yield(&vm_page_queue_lock
);
1136 * continue the while loop processing
1137 * the active queue... need to hold
1138 * the page queues lock
1145 /**********************************************************************
1146 * above this point we're playing with the active queue
1147 * below this point we're playing with the throttling mechanisms
1148 * and the inactive queue
1149 **********************************************************************/
1151 done_moving_active_pages
:
1154 * We are done if we have met our target *and*
1155 * nobody is still waiting for a page.
1157 if (vm_page_free_count
+ local_freed
>= vm_page_free_target
) {
1158 if (object
!= NULL
) {
1159 vm_object_unlock(object
);
1162 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1165 vm_page_free_list(local_freeq
);
1171 * inactive target still not met... keep going
1172 * until we get the queues balanced
1174 if (((vm_page_inactive_count
+ vm_page_speculative_count
) < vm_page_inactive_target
) &&
1175 !queue_empty(&vm_page_queue_active
))
1178 mutex_lock(&vm_page_queue_free_lock
);
1180 if ((vm_page_free_count
>= vm_page_free_target
) &&
1181 (vm_page_free_wanted
== 0) && (vm_page_free_wanted_privileged
== 0)) {
1183 vm_page_unlock_queues();
1185 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
1187 assert(vm_pageout_scan_wants_object
== VM_OBJECT_NULL
);
1191 mutex_unlock(&vm_page_queue_free_lock
);
1194 * Before anything, we check if we have any ripe volatile objects around.
1195 * If so, purge the first and see what it gives us.
1197 assert (available_for_purge
>=0);
1198 if (available_for_purge
)
1200 if (object
!= NULL
) {
1201 vm_object_unlock(object
);
1204 vm_purgeable_object_purge_one();
1208 if (queue_empty(&sq
->age_q
) && vm_page_speculative_count
) {
1210 * try to pull pages from the aging bins
1211 * see vm_page.h for an explanation of how
1212 * this mechanism works
1214 struct vm_speculative_age_q
*aq
;
1215 mach_timespec_t ts_fully_aged
;
1216 boolean_t can_steal
= FALSE
;
1218 aq
= &vm_page_queue_speculative
[speculative_steal_index
];
1220 while (queue_empty(&aq
->age_q
)) {
1222 speculative_steal_index
++;
1224 if (speculative_steal_index
> VM_PAGE_MAX_SPECULATIVE_AGE_Q
)
1225 speculative_steal_index
= VM_PAGE_MIN_SPECULATIVE_AGE_Q
;
1227 aq
= &vm_page_queue_speculative
[speculative_steal_index
];
1229 if (vm_page_speculative_count
> vm_page_speculative_target
)
1232 ts_fully_aged
.tv_sec
= (VM_PAGE_MAX_SPECULATIVE_AGE_Q
* VM_PAGE_SPECULATIVE_Q_AGE_MS
) / 1000;
1233 ts_fully_aged
.tv_nsec
= ((VM_PAGE_MAX_SPECULATIVE_AGE_Q
* VM_PAGE_SPECULATIVE_Q_AGE_MS
) % 1000)
1234 * 1000 * NSEC_PER_USEC
;
1236 ADD_MACH_TIMESPEC(&ts_fully_aged
, &aq
->age_ts
);
1238 clock_get_system_nanotime(&ts
.tv_sec
, (unsigned *)&ts
.tv_nsec
);
1240 if (CMP_MACH_TIMESPEC(&ts
, &ts_fully_aged
) >= 0)
1243 if (can_steal
== TRUE
)
1244 vm_page_speculate_ageit(aq
);
1248 * Sometimes we have to pause:
1249 * 1) No inactive pages - nothing to do.
1250 * 2) Flow control - default pageout queue is full
1251 * 3) Loop control - no acceptable pages found on the inactive queue
1252 * within the last vm_pageout_burst_inactive_throttle iterations
1254 if (queue_empty(&vm_page_queue_inactive
) && queue_empty(&vm_page_queue_zf
) && queue_empty(&sq
->age_q
) &&
1255 (VM_PAGE_Q_THROTTLED(iq
) || queue_empty(&vm_page_queue_throttled
))) {
1256 vm_pageout_scan_empty_throttle
++;
1257 msecs
= vm_pageout_empty_wait
;
1258 goto vm_pageout_scan_delay
;
1260 } else if (inactive_burst_count
>= vm_pageout_burst_inactive_throttle
) {
1261 vm_pageout_scan_burst_throttle
++;
1262 msecs
= vm_pageout_burst_wait
;
1263 goto vm_pageout_scan_delay
;
1265 } else if (VM_PAGE_Q_THROTTLED(iq
) && IP_VALID(memory_manager_default
)) {
1267 switch (flow_control
.state
) {
1270 reset_deadlock_timer
:
1271 ts
.tv_sec
= vm_pageout_deadlock_wait
/ 1000;
1272 ts
.tv_nsec
= (vm_pageout_deadlock_wait
% 1000) * 1000 * NSEC_PER_USEC
;
1273 clock_get_system_nanotime(&flow_control
.ts
.tv_sec
,
1274 (unsigned *)&flow_control
.ts
.tv_nsec
);
1275 ADD_MACH_TIMESPEC(&flow_control
.ts
, &ts
);
1277 flow_control
.state
= FCS_DELAYED
;
1278 msecs
= vm_pageout_deadlock_wait
;
1283 clock_get_system_nanotime(&ts
.tv_sec
,
1284 (unsigned *)&ts
.tv_nsec
);
1286 if (CMP_MACH_TIMESPEC(&ts
, &flow_control
.ts
) >= 0) {
1288 * the pageout thread for the default pager is potentially
1289 * deadlocked since the
1290 * default pager queue has been throttled for more than the
1291 * allowable time... we need to move some clean pages or dirty
1292 * pages belonging to the external pagers if they aren't throttled
1293 * vm_page_free_wanted represents the number of threads currently
1294 * blocked waiting for pages... we'll move one page for each of
1295 * these plus a fixed amount to break the logjam... once we're done
1296 * moving this number of pages, we'll re-enter the FSC_DELAYED state
1297 * with a new timeout target since we have no way of knowing
1298 * whether we've broken the deadlock except through observation
1299 * of the queue associated with the default pager... we need to
1300 * stop moving pages and allow the system to run to see what
1301 * state it settles into.
1303 vm_pageout_deadlock_target
= vm_pageout_deadlock_relief
+ vm_page_free_wanted
+ vm_page_free_wanted_privileged
;
1304 vm_pageout_scan_deadlock_detected
++;
1305 flow_control
.state
= FCS_DEADLOCK_DETECTED
;
1307 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
1308 goto consider_inactive
;
1311 * just resniff instead of trying
1312 * to compute a new delay time... we're going to be
1313 * awakened immediately upon a laundry completion,
1314 * so we won't wait any longer than necessary
1316 msecs
= vm_pageout_idle_wait
;
1319 case FCS_DEADLOCK_DETECTED
:
1320 if (vm_pageout_deadlock_target
)
1321 goto consider_inactive
;
1322 goto reset_deadlock_timer
;
1325 vm_pageout_scan_throttle
++;
1326 iq
->pgo_throttled
= TRUE
;
1327 vm_pageout_scan_delay
:
1328 if (object
!= NULL
) {
1329 vm_object_unlock(object
);
1332 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1335 vm_page_free_list(local_freeq
);
1345 * Decide if we need to send a memory status notification.
1348 (vm_page_active_count
+ vm_page_inactive_count
+
1349 vm_page_speculative_count
+ vm_page_free_count
+
1350 (IP_VALID(memory_manager_default
)?0:vm_page_purgeable_count
) ) * 100 /
1352 if (percent_avail
>= (kern_memorystatus_level
+ 5) ||
1353 percent_avail
<= (kern_memorystatus_level
- 5)) {
1354 kern_memorystatus_level
= percent_avail
;
1355 thread_wakeup((event_t
)&kern_memorystatus_wakeup
);
1359 assert_wait_timeout((event_t
) &iq
->pgo_laundry
, THREAD_INTERRUPTIBLE
, msecs
, 1000*NSEC_PER_USEC
);
1361 counter(c_vm_pageout_scan_block
++);
1363 vm_page_unlock_queues();
1365 assert(vm_pageout_scan_wants_object
== VM_OBJECT_NULL
);
1367 thread_block(THREAD_CONTINUE_NULL
);
1369 vm_page_lock_queues();
1372 iq
->pgo_throttled
= FALSE
;
1374 if (loop_count
>= vm_page_inactive_count
)
1376 inactive_burst_count
= 0;
1383 flow_control
.state
= FCS_IDLE
;
1386 inactive_burst_count
++;
1387 vm_pageout_inactive
++;
1389 /* Choose a victim. */
1395 * the most eligible pages are ones that were throttled because the
1396 * pager wasn't ready at the time. If a pager is ready now,
1397 * see if one of these is useful.
1399 if (!VM_PAGE_Q_THROTTLED(iq
) && !queue_empty(&vm_page_queue_throttled
)) {
1400 m
= (vm_page_t
) queue_first(&vm_page_queue_throttled
);
1405 * The second most eligible pages are ones we paged in speculatively,
1406 * but which have not yet been touched.
1408 if ( !queue_empty(&sq
->age_q
) ) {
1409 m
= (vm_page_t
) queue_first(&sq
->age_q
);
1413 * Time for a zero-filled inactive page?
1415 if ( ((zf_run_count
< zf_ratio
) && vm_zf_queue_count
>= zf_queue_min_count
) ||
1416 queue_empty(&vm_page_queue_inactive
)) {
1417 if ( !queue_empty(&vm_page_queue_zf
) ) {
1418 m
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
1424 * It's either a normal inactive page or nothing.
1426 if ( !queue_empty(&vm_page_queue_inactive
) ) {
1427 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
1432 panic("vm_pageout: no victim");
1435 assert(!m
->active
&& (m
->inactive
|| m
->speculative
|| m
->throttled
));
1436 assert(!m
->laundry
);
1437 assert(m
->object
!= kernel_object
);
1438 assert(m
->phys_page
!= vm_page_guard_addr
);
1440 DTRACE_VM2(scan
, int, 1, (uint64_t *), NULL
);
1443 * check to see if we currently are working
1444 * with the same object... if so, we've
1445 * already got the lock
1447 if (m
->object
!= object
) {
1449 * the object associated with candidate page is
1450 * different from the one we were just working
1451 * with... dump the lock if we still own it
1453 if (object
!= NULL
) {
1454 vm_object_unlock(object
);
1456 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1459 * Try to lock object; since we've alread got the
1460 * page queues lock, we can only 'try' for this one.
1461 * if the 'try' fails, we need to do a mutex_pause
1462 * to allow the owner of the object lock a chance to
1463 * run... otherwise, we're likely to trip over this
1464 * object in the same state as we work our way through
1465 * the queue... clumps of pages associated with the same
1466 * object are fairly typical on the inactive and active queues
1468 if (!vm_object_lock_try_scan(m
->object
)) {
1470 * Move page to end and continue.
1471 * Don't re-issue ticket
1474 queue_remove(&vm_page_queue_zf
, m
,
1476 queue_enter(&vm_page_queue_zf
, m
,
1478 } else if (m
->speculative
) {
1480 m
->speculative
= FALSE
;
1481 vm_page_speculative_count
--;
1484 * move to the tail of the inactive queue
1485 * to get it out of the way... the speculative
1486 * queue is generally too small to depend
1487 * on there being enough pages from other
1488 * objects to make cycling it back on the
1489 * same queue a winning proposition
1491 queue_enter(&vm_page_queue_inactive
, m
,
1494 vm_page_inactive_count
++;
1495 token_new_pagecount
++;
1496 } else if (m
->throttled
) {
1497 queue_remove(&vm_page_queue_throttled
, m
,
1499 m
->throttled
= FALSE
;
1500 vm_page_throttled_count
--;
1503 * not throttled any more, so can stick
1504 * it on the inactive queue.
1506 queue_enter(&vm_page_queue_inactive
, m
,
1509 vm_page_inactive_count
++;
1510 token_new_pagecount
++;
1512 queue_remove(&vm_page_queue_inactive
, m
,
1515 vm_page_inactive_count
--; /* balance for purgeable queue asserts */
1517 vm_purgeable_q_advance_all();
1519 queue_enter(&vm_page_queue_inactive
, m
,
1522 vm_page_inactive_count
++; /* balance for purgeable queue asserts */
1524 token_new_pagecount
++;
1526 pmap_clear_reference(m
->phys_page
);
1527 m
->reference
= FALSE
;
1529 vm_pageout_inactive_nolock
++;
1531 if ( !queue_empty(&sq
->age_q
) )
1532 m
= (vm_page_t
) queue_first(&sq
->age_q
);
1533 else if ( ((zf_run_count
< zf_ratio
) && vm_zf_queue_count
>= zf_queue_min_count
) ||
1534 queue_empty(&vm_page_queue_inactive
)) {
1535 if ( !queue_empty(&vm_page_queue_zf
) )
1536 m
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
1537 } else if ( !queue_empty(&vm_page_queue_inactive
) ) {
1538 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
1541 * this is the next object we're going to be interested in
1542 * try to make sure its available after the mutex_yield
1545 vm_pageout_scan_wants_object
= m
->object
;
1548 * force us to dump any collected free pages
1549 * and to pause before moving on
1553 goto done_with_inactivepage
;
1556 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1562 * Paging out pages of external objects which
1563 * are currently being created must be avoided.
1564 * The pager may claim for memory, thus leading to a
1565 * possible dead lock between it and the pageout thread,
1566 * if such pages are finally chosen. The remaining assumption
1567 * is that there will finally be enough available pages in the
1568 * inactive pool to page out in order to satisfy all memory
1569 * claimed by the thread which concurrently creates the pager.
1571 if (!object
->pager_initialized
&& object
->pager_created
) {
1573 * Move page to end and continue, hoping that
1574 * there will be enough other inactive pages to
1575 * page out so that the thread which currently
1576 * initializes the pager will succeed.
1577 * Don't re-grant the ticket, the page should
1578 * pulled from the queue and paged out whenever
1579 * one of its logically adjacent fellows is
1582 * Pages found on the speculative list can never be
1583 * in this state... they always have a pager associated
1586 assert(!m
->speculative
);
1589 queue_remove(&vm_page_queue_zf
, m
,
1591 queue_enter(&vm_page_queue_zf
, m
,
1594 queue_remove(&vm_page_queue_inactive
, m
,
1597 vm_page_inactive_count
--; /* balance for purgeable queue asserts */
1599 vm_purgeable_q_advance_all();
1601 queue_enter(&vm_page_queue_inactive
, m
,
1604 vm_page_inactive_count
++; /* balance for purgeable queue asserts */
1606 token_new_pagecount
++;
1608 vm_pageout_inactive_avoid
++;
1610 goto done_with_inactivepage
;
1613 * Remove the page from its list.
1615 if (m
->speculative
) {
1617 m
->speculative
= FALSE
;
1618 vm_page_speculative_count
--;
1619 } else if (m
->throttled
) {
1620 queue_remove(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
1621 m
->throttled
= FALSE
;
1622 vm_page_throttled_count
--;
1625 queue_remove(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
1626 vm_zf_queue_count
--;
1628 queue_remove(&vm_page_queue_inactive
, m
, vm_page_t
, pageq
);
1630 m
->inactive
= FALSE
;
1632 vm_page_inactive_count
--;
1633 vm_purgeable_q_advance_all();
1636 /* If the object is empty, the page must be reclaimed even if dirty or used. */
1637 /* If the page belongs to a volatile object, we stick it back on. */
1638 if (object
->copy
== VM_OBJECT_NULL
) {
1639 if(object
->purgable
== VM_PURGABLE_EMPTY
&& !m
->cleaning
) {
1641 if (m
->pmapped
== TRUE
) {
1642 /* unmap the page */
1643 refmod_state
= pmap_disconnect(m
->phys_page
);
1644 if (refmod_state
& VM_MEM_MODIFIED
) {
1648 if (m
->dirty
|| m
->precious
) {
1649 /* we saved the cost of cleaning this page ! */
1650 vm_page_purged_count
++;
1654 if (object
->purgable
== VM_PURGABLE_VOLATILE
) {
1655 /* if it's wired, we can't put it on our queue */
1656 assert(m
->wire_count
== 0);
1657 /* just stick it back on! */
1658 goto reactivate_page
;
1661 m
->pageq
.next
= NULL
;
1662 m
->pageq
.prev
= NULL
;
1664 if ( !m
->fictitious
&& catch_up_count
)
1669 * if this page has already been picked up as part of a
1670 * page-out cluster, it will be busy because it is being
1671 * encrypted (see vm_object_upl_request()). But we still
1672 * want to demote it from "clean-in-place" (aka "adjacent")
1673 * to "clean-and-free" (aka "target"), so let's ignore its
1674 * "busy" bit here and proceed to check for "cleaning" a
1675 * little bit below...
1677 if ( !m
->encrypted_cleaning
&& (m
->busy
|| !object
->alive
)) {
1679 * Somebody is already playing with this page.
1680 * Leave it off the pageout queues.
1683 vm_pageout_inactive_busy
++;
1685 goto done_with_inactivepage
;
1689 * If it's absent or in error, we can reclaim the page.
1692 if (m
->absent
|| m
->error
) {
1693 vm_pageout_inactive_absent
++;
1695 if (vm_pageout_deadlock_target
) {
1696 vm_pageout_scan_inactive_throttle_success
++;
1697 vm_pageout_deadlock_target
--;
1700 DTRACE_VM2(dfree
, int, 1, (uint64_t *), NULL
);
1702 if (m
->object
->internal
) {
1703 DTRACE_VM2(anonfree
, int, 1, (uint64_t *), NULL
);
1705 DTRACE_VM2(fsfree
, int, 1, (uint64_t *), NULL
);
1708 vm_page_free_prepare(m
);
1710 assert(m
->pageq
.next
== NULL
&&
1711 m
->pageq
.prev
== NULL
);
1712 m
->pageq
.next
= (queue_entry_t
)local_freeq
;
1716 inactive_burst_count
= 0;
1718 goto done_with_inactivepage
;
1721 assert(!m
->private);
1722 assert(!m
->fictitious
);
1725 * If already cleaning this page in place, convert from
1726 * "adjacent" to "target". We can leave the page mapped,
1727 * and vm_pageout_object_terminate will determine whether
1728 * to free or reactivate.
1734 m
->dump_cleaning
= TRUE
;
1737 CLUSTER_STAT(vm_pageout_cluster_conversions
++);
1739 inactive_burst_count
= 0;
1741 goto done_with_inactivepage
;
1745 * If it's being used, reactivate.
1746 * (Fictitious pages are either busy or absent.)
1747 * First, update the reference and dirty bits
1748 * to make sure the page is unreferenced.
1752 if (m
->reference
== FALSE
&& m
->pmapped
== TRUE
) {
1753 refmod_state
= pmap_get_refmod(m
->phys_page
);
1755 if (refmod_state
& VM_MEM_REFERENCED
)
1756 m
->reference
= TRUE
;
1757 if (refmod_state
& VM_MEM_MODIFIED
)
1760 if (m
->reference
&& !m
->no_cache
) {
1762 * The page we pulled off the inactive list has
1763 * been referenced. It is possible for other
1764 * processors to be touching pages faster than we
1765 * can clear the referenced bit and traverse the
1766 * inactive queue, so we limit the number of
1769 if (++reactivated_this_call
>= reactivate_limit
) {
1770 vm_pageout_reactivation_limit_exceeded
++;
1771 } else if (catch_up_count
) {
1772 vm_pageout_catch_ups
++;
1773 } else if (++inactive_reclaim_run
>= VM_PAGEOUT_INACTIVE_FORCE_RECLAIM
) {
1774 vm_pageout_inactive_force_reclaim
++;
1777 * The page was being used, so put back on active list.
1780 vm_page_activate(m
);
1781 VM_STAT_INCR(reactivations
);
1783 vm_pageout_inactive_used
++;
1784 inactive_burst_count
= 0;
1786 goto done_with_inactivepage
;
1789 * Make sure we call pmap_get_refmod() if it
1790 * wasn't already called just above, to update
1793 if ((refmod_state
== -1) && !m
->dirty
&& m
->pmapped
) {
1794 refmod_state
= pmap_get_refmod(m
->phys_page
);
1795 if (refmod_state
& VM_MEM_MODIFIED
)
1798 forced_reclaim
= TRUE
;
1800 forced_reclaim
= FALSE
;
1804 "vm_pageout_scan, replace object 0x%X offset 0x%X page 0x%X\n",
1805 (integer_t
)object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
1808 * we've got a candidate page to steal...
1810 * m->dirty is up to date courtesy of the
1811 * preceding check for m->reference... if
1812 * we get here, then m->reference had to be
1813 * FALSE (or possibly "reactivate_limit" was
1814 * exceeded), but in either case we called
1815 * pmap_get_refmod() and updated both
1816 * m->reference and m->dirty
1818 * if it's dirty or precious we need to
1819 * see if the target queue is throtttled
1820 * it if is, we need to skip over it by moving it back
1821 * to the end of the inactive queue
1823 inactive_throttled
= FALSE
;
1825 if (m
->dirty
|| m
->precious
) {
1826 if (object
->internal
) {
1827 if (VM_PAGE_Q_THROTTLED(iq
))
1828 inactive_throttled
= TRUE
;
1829 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
1830 inactive_throttled
= TRUE
;
1833 if (inactive_throttled
== TRUE
) {
1835 if (!IP_VALID(memory_manager_default
) &&
1837 (object
->purgable
== VM_PURGABLE_DENY
||
1838 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
1839 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
1840 queue_enter(&vm_page_queue_throttled
, m
,
1842 m
->throttled
= TRUE
;
1843 vm_page_throttled_count
++;
1846 queue_enter(&vm_page_queue_zf
, m
,
1848 vm_zf_queue_count
++;
1850 queue_enter(&vm_page_queue_inactive
, m
,
1853 if (!m
->fictitious
) {
1854 vm_page_inactive_count
++;
1855 token_new_pagecount
++;
1858 vm_pageout_scan_inactive_throttled
++;
1859 goto done_with_inactivepage
;
1863 * we've got a page that we can steal...
1864 * eliminate all mappings and make sure
1865 * we have the up-to-date modified state
1866 * first take the page BUSY, so that no new
1867 * mappings can be made
1872 * if we need to do a pmap_disconnect then we
1873 * need to re-evaluate m->dirty since the pmap_disconnect
1874 * provides the true state atomically... the
1875 * page was still mapped up to the pmap_disconnect
1876 * and may have been dirtied at the last microsecond
1878 * we also check for the page being referenced 'late'
1879 * if it was, we first need to do a WAKEUP_DONE on it
1880 * since we already set m->busy = TRUE, before
1881 * going off to reactivate it
1883 * Note that if 'pmapped' is FALSE then the page is not
1884 * and has not been in any map, so there is no point calling
1885 * pmap_disconnect(). m->dirty and/or m->reference could
1886 * have been set in anticipation of likely usage of the page.
1888 if (m
->pmapped
== TRUE
) {
1889 refmod_state
= pmap_disconnect(m
->phys_page
);
1891 if (refmod_state
& VM_MEM_MODIFIED
)
1893 if (refmod_state
& VM_MEM_REFERENCED
) {
1895 /* If m->reference is already set, this page must have
1896 * already failed the reactivate_limit test, so don't
1897 * bump the counts twice.
1899 if ( ! m
->reference
) {
1900 m
->reference
= TRUE
;
1901 if (forced_reclaim
||
1902 ++reactivated_this_call
>= reactivate_limit
)
1903 vm_pageout_reactivation_limit_exceeded
++;
1905 PAGE_WAKEUP_DONE(m
);
1906 goto reactivate_page
;
1912 * reset our count of pages that have been reclaimed
1913 * since the last page was 'stolen'
1915 inactive_reclaim_run
= 0;
1918 * If it's clean and not precious, we can free the page.
1920 if (!m
->dirty
&& !m
->precious
) {
1921 vm_pageout_inactive_clean
++;
1926 * The page may have been dirtied since the last check
1927 * for a throttled target queue (which may have been skipped
1928 * if the page was clean then). With the dirty page
1929 * disconnected here, we can make one final check.
1932 boolean_t disconnect_throttled
= FALSE
;
1933 if (object
->internal
) {
1934 if (VM_PAGE_Q_THROTTLED(iq
))
1935 disconnect_throttled
= TRUE
;
1936 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
1937 disconnect_throttled
= TRUE
;
1940 if (disconnect_throttled
== TRUE
) {
1941 PAGE_WAKEUP_DONE(m
);
1942 goto throttle_inactive
;
1946 vm_pageout_cluster(m
);
1948 vm_pageout_inactive_dirty
++;
1950 inactive_burst_count
= 0;
1952 done_with_inactivepage
:
1953 if (delayed_unlock
++ > VM_PAGEOUT_DELAYED_UNLOCK_LIMIT
|| try_failed
== TRUE
) {
1955 if (object
!= NULL
) {
1956 vm_object_unlock(object
);
1958 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1961 vm_page_free_list(local_freeq
);
1966 mutex_yield(&vm_page_queue_lock
);
1971 * back to top of pageout scan loop
1977 int vm_page_free_count_init
;
1980 vm_page_free_reserve(
1983 int free_after_reserve
;
1985 vm_page_free_reserved
+= pages
;
1987 free_after_reserve
= vm_page_free_count_init
- vm_page_free_reserved
;
1989 vm_page_free_min
= vm_page_free_reserved
+
1990 VM_PAGE_FREE_MIN(free_after_reserve
);
1992 if (vm_page_free_min
> VM_PAGE_FREE_MIN_LIMIT
)
1993 vm_page_free_min
= VM_PAGE_FREE_MIN_LIMIT
;
1995 vm_page_free_target
= vm_page_free_reserved
+
1996 VM_PAGE_FREE_TARGET(free_after_reserve
);
1998 if (vm_page_free_target
> VM_PAGE_FREE_TARGET_LIMIT
)
1999 vm_page_free_target
= VM_PAGE_FREE_TARGET_LIMIT
;
2001 if (vm_page_free_target
< vm_page_free_min
+ 5)
2002 vm_page_free_target
= vm_page_free_min
+ 5;
2007 * vm_pageout is the high level pageout daemon.
2011 vm_pageout_continue(void)
2013 DTRACE_VM2(pgrrun
, int, 1, (uint64_t *), NULL
);
2014 vm_pageout_scan_event_counter
++;
2016 /* we hold vm_page_queue_free_lock now */
2017 assert(vm_page_free_wanted
== 0);
2018 assert(vm_page_free_wanted_privileged
== 0);
2019 assert_wait((event_t
) &vm_page_free_wanted
, THREAD_UNINT
);
2020 mutex_unlock(&vm_page_queue_free_lock
);
2022 counter(c_vm_pageout_block
++);
2023 thread_block((thread_continue_t
)vm_pageout_continue
);
2029 * must be called with the
2030 * queues and object locks held
2033 vm_pageout_queue_steal(vm_page_t m
)
2035 struct vm_pageout_queue
*q
;
2037 if (m
->object
->internal
== TRUE
)
2038 q
= &vm_pageout_queue_internal
;
2040 q
= &vm_pageout_queue_external
;
2043 m
->pageout_queue
= FALSE
;
2044 queue_remove(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
2046 m
->pageq
.next
= NULL
;
2047 m
->pageq
.prev
= NULL
;
2049 vm_object_paging_end(m
->object
);
2055 #ifdef FAKE_DEADLOCK
2057 #define FAKE_COUNT 5000
2059 int internal_count
= 0;
2060 int fake_deadlock
= 0;
2065 vm_pageout_iothread_continue(struct vm_pageout_queue
*q
)
2069 boolean_t need_wakeup
;
2070 memory_object_t pager
;
2071 thread_t self
= current_thread();
2073 if ((vm_pageout_internal_iothread
!= THREAD_NULL
)
2074 && (self
== vm_pageout_external_iothread
)
2075 && (self
->options
& TH_OPT_VMPRIV
))
2076 self
->options
&= ~TH_OPT_VMPRIV
;
2078 vm_page_lockspin_queues();
2080 while ( !queue_empty(&q
->pgo_pending
) ) {
2083 queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
2084 m
->pageout_queue
= FALSE
;
2085 vm_page_unlock_queues();
2087 m
->pageq
.next
= NULL
;
2088 m
->pageq
.prev
= NULL
;
2089 #ifdef FAKE_DEADLOCK
2090 if (q
== &vm_pageout_queue_internal
) {
2096 if ((internal_count
== FAKE_COUNT
)) {
2098 pg_count
= vm_page_free_count
+ vm_page_free_reserved
;
2100 if (kmem_alloc(kernel_map
, &addr
, PAGE_SIZE
* pg_count
) == KERN_SUCCESS
) {
2101 kmem_free(kernel_map
, addr
, PAGE_SIZE
* pg_count
);
2110 vm_object_lock(object
);
2112 if (!object
->pager_initialized
) {
2115 * If there is no memory object for the page, create
2116 * one and hand it to the default pager.
2119 if (!object
->pager_initialized
)
2120 vm_object_collapse(object
,
2121 (vm_object_offset_t
) 0,
2123 if (!object
->pager_initialized
)
2124 vm_object_pager_create(object
);
2125 if (!object
->pager_initialized
) {
2127 * Still no pager for the object.
2128 * Reactivate the page.
2130 * Should only happen if there is no
2133 m
->list_req_pending
= FALSE
;
2134 m
->cleaning
= FALSE
;
2137 vm_page_lockspin_queues();
2139 vm_pageout_throttle_up(m
);
2140 vm_pageout_dirty_no_pager
++;
2141 vm_page_activate(m
);
2142 vm_page_unlock_queues();
2145 * And we are done with it.
2147 PAGE_WAKEUP_DONE(m
);
2149 vm_object_paging_end(object
);
2150 vm_object_unlock(object
);
2152 vm_page_lockspin_queues();
2156 pager
= object
->pager
;
2157 if (pager
== MEMORY_OBJECT_NULL
) {
2159 * This pager has been destroyed by either
2160 * memory_object_destroy or vm_object_destroy, and
2161 * so there is nowhere for the page to go.
2162 * Just free the page... VM_PAGE_FREE takes
2163 * care of cleaning up all the state...
2164 * including doing the vm_pageout_throttle_up
2169 vm_object_paging_end(object
);
2170 vm_object_unlock(object
);
2172 vm_page_lockspin_queues();
2175 vm_object_unlock(object
);
2177 * we expect the paging_in_progress reference to have
2178 * already been taken on the object before it was added
2179 * to the appropriate pageout I/O queue... this will
2180 * keep the object from being terminated and/or the
2181 * paging_offset from changing until the I/O has
2182 * completed... therefore no need to lock the object to
2183 * pull the paging_offset from it.
2185 * Send the data to the pager.
2186 * any pageout clustering happens there
2188 memory_object_data_return(pager
,
2189 m
->offset
+ object
->paging_offset
,
2197 vm_object_lock(object
);
2198 vm_object_paging_end(object
);
2199 vm_object_unlock(object
);
2201 vm_page_lockspin_queues();
2203 assert_wait((event_t
) q
, THREAD_UNINT
);
2206 if (q
->pgo_throttled
== TRUE
&& !VM_PAGE_Q_THROTTLED(q
)) {
2207 q
->pgo_throttled
= FALSE
;
2210 need_wakeup
= FALSE
;
2212 q
->pgo_busy
= FALSE
;
2214 vm_page_unlock_queues();
2216 if (need_wakeup
== TRUE
)
2217 thread_wakeup((event_t
) &q
->pgo_laundry
);
2219 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_continue
, (void *) &q
->pgo_pending
);
2225 vm_pageout_iothread_external(void)
2227 thread_t self
= current_thread();
2229 self
->options
|= TH_OPT_VMPRIV
;
2231 vm_pageout_iothread_continue(&vm_pageout_queue_external
);
2237 vm_pageout_iothread_internal(void)
2239 thread_t self
= current_thread();
2241 self
->options
|= TH_OPT_VMPRIV
;
2243 vm_pageout_iothread_continue(&vm_pageout_queue_internal
);
2248 vm_pageout_garbage_collect(int collect
)
2254 * consider_zone_gc should be last, because the other operations
2255 * might return memory to zones.
2257 consider_machine_collect();
2260 consider_machine_adjust();
2263 assert_wait((event_t
) &vm_pageout_garbage_collect
, THREAD_UNINT
);
2265 thread_block_parameter((thread_continue_t
) vm_pageout_garbage_collect
, (void *)1);
2274 thread_t self
= current_thread();
2276 kern_return_t result
;
2280 * Set thread privileges.
2284 self
->priority
= BASEPRI_PREEMPT
- 1;
2285 set_sched_pri(self
, self
->priority
);
2286 thread_unlock(self
);
2288 if (!self
->reserved_stack
)
2289 self
->reserved_stack
= self
->kernel_stack
;
2294 * Initialize some paging parameters.
2297 if (vm_pageout_idle_wait
== 0)
2298 vm_pageout_idle_wait
= VM_PAGEOUT_IDLE_WAIT
;
2300 if (vm_pageout_burst_wait
== 0)
2301 vm_pageout_burst_wait
= VM_PAGEOUT_BURST_WAIT
;
2303 if (vm_pageout_empty_wait
== 0)
2304 vm_pageout_empty_wait
= VM_PAGEOUT_EMPTY_WAIT
;
2306 if (vm_pageout_deadlock_wait
== 0)
2307 vm_pageout_deadlock_wait
= VM_PAGEOUT_DEADLOCK_WAIT
;
2309 if (vm_pageout_deadlock_relief
== 0)
2310 vm_pageout_deadlock_relief
= VM_PAGEOUT_DEADLOCK_RELIEF
;
2312 if (vm_pageout_inactive_relief
== 0)
2313 vm_pageout_inactive_relief
= VM_PAGEOUT_INACTIVE_RELIEF
;
2315 if (vm_pageout_burst_active_throttle
== 0)
2316 vm_pageout_burst_active_throttle
= VM_PAGEOUT_BURST_ACTIVE_THROTTLE
;
2318 if (vm_pageout_burst_inactive_throttle
== 0)
2319 vm_pageout_burst_inactive_throttle
= VM_PAGEOUT_BURST_INACTIVE_THROTTLE
;
2322 * Set kernel task to low backing store privileged
2325 task_lock(kernel_task
);
2326 kernel_task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
2327 task_unlock(kernel_task
);
2329 vm_page_free_count_init
= vm_page_free_count
;
2332 * even if we've already called vm_page_free_reserve
2333 * call it again here to insure that the targets are
2334 * accurately calculated (it uses vm_page_free_count_init)
2335 * calling it with an arg of 0 will not change the reserve
2336 * but will re-calculate free_min and free_target
2338 if (vm_page_free_reserved
< VM_PAGE_FREE_RESERVED(processor_count
)) {
2339 vm_page_free_reserve((VM_PAGE_FREE_RESERVED(processor_count
)) - vm_page_free_reserved
);
2341 vm_page_free_reserve(0);
2344 queue_init(&vm_pageout_queue_external
.pgo_pending
);
2345 vm_pageout_queue_external
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
2346 vm_pageout_queue_external
.pgo_laundry
= 0;
2347 vm_pageout_queue_external
.pgo_idle
= FALSE
;
2348 vm_pageout_queue_external
.pgo_busy
= FALSE
;
2349 vm_pageout_queue_external
.pgo_throttled
= FALSE
;
2351 queue_init(&vm_pageout_queue_internal
.pgo_pending
);
2352 vm_pageout_queue_internal
.pgo_maxlaundry
= 0;
2353 vm_pageout_queue_internal
.pgo_laundry
= 0;
2354 vm_pageout_queue_internal
.pgo_idle
= FALSE
;
2355 vm_pageout_queue_internal
.pgo_busy
= FALSE
;
2356 vm_pageout_queue_internal
.pgo_throttled
= FALSE
;
2359 /* internal pageout thread started when default pager registered first time */
2360 /* external pageout and garbage collection threads started here */
2362 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_external
, NULL
,
2363 BASEPRI_PREEMPT
- 1,
2364 &vm_pageout_external_iothread
);
2365 if (result
!= KERN_SUCCESS
)
2366 panic("vm_pageout_iothread_external: create failed");
2368 thread_deallocate(vm_pageout_external_iothread
);
2370 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_garbage_collect
, NULL
,
2373 if (result
!= KERN_SUCCESS
)
2374 panic("vm_pageout_garbage_collect: create failed");
2376 thread_deallocate(thread
);
2378 vm_object_reaper_init();
2381 vm_pageout_continue();
2386 * The vm_pageout_continue() call above never returns, so the code below is never
2387 * executed. We take advantage of this to declare several DTrace VM related probe
2388 * points that our kernel doesn't have an analog for. These are probe points that
2389 * exist in Solaris and are in the DTrace documentation, so people may have written
2390 * scripts that use them. Declaring the probe points here means their scripts will
2391 * compile and execute which we want for portability of the scripts, but since this
2392 * section of code is never reached, the probe points will simply never fire. Yes,
2393 * this is basically a hack. The problem is the DTrace probe points were chosen with
2394 * Solaris specific VM events in mind, not portability to different VM implementations.
2397 DTRACE_VM2(execfree
, int, 1, (uint64_t *), NULL
);
2398 DTRACE_VM2(execpgin
, int, 1, (uint64_t *), NULL
);
2399 DTRACE_VM2(execpgout
, int, 1, (uint64_t *), NULL
);
2400 DTRACE_VM2(pgswapin
, int, 1, (uint64_t *), NULL
);
2401 DTRACE_VM2(pgswapout
, int, 1, (uint64_t *), NULL
);
2402 DTRACE_VM2(swapin
, int, 1, (uint64_t *), NULL
);
2403 DTRACE_VM2(swapout
, int, 1, (uint64_t *), NULL
);
2408 vm_pageout_internal_start(void)
2410 kern_return_t result
;
2412 vm_pageout_queue_internal
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
2413 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_internal
, NULL
, BASEPRI_PREEMPT
- 1, &vm_pageout_internal_iothread
);
2414 if (result
== KERN_SUCCESS
)
2415 thread_deallocate(vm_pageout_internal_iothread
);
2419 #define UPL_DELAYED_UNLOCK_LIMIT (MAX_UPL_TRANSFER / 2)
2422 upl_create(int type
, int flags
, upl_size_t size
)
2425 int page_field_size
= 0;
2427 int upl_size
= sizeof(struct upl
);
2429 if (type
& UPL_CREATE_LITE
) {
2430 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
2431 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
2433 upl_flags
|= UPL_LITE
;
2435 if (type
& UPL_CREATE_INTERNAL
) {
2436 upl_size
+= sizeof(struct upl_page_info
) * (size
/PAGE_SIZE
);
2438 upl_flags
|= UPL_INTERNAL
;
2440 upl
= (upl_t
)kalloc(upl_size
+ page_field_size
);
2442 if (page_field_size
)
2443 bzero((char *)upl
+ upl_size
, page_field_size
);
2445 upl
->flags
= upl_flags
| flags
;
2446 upl
->src_object
= NULL
;
2447 upl
->kaddr
= (vm_offset_t
)0;
2449 upl
->map_object
= NULL
;
2451 upl
->highest_page
= 0;
2454 upl
->ubc_alias1
= 0;
2455 upl
->ubc_alias2
= 0;
2456 #endif /* UPL_DEBUG */
2461 upl_destroy(upl_t upl
)
2463 int page_field_size
; /* bit field in word size buf */
2470 if (upl
->flags
& UPL_SHADOWED
) {
2471 object
= upl
->map_object
->shadow
;
2473 object
= upl
->map_object
;
2475 vm_object_lock(object
);
2476 queue_remove(&object
->uplq
, upl
, upl_t
, uplq
);
2477 vm_object_unlock(object
);
2479 #endif /* UPL_DEBUG */
2481 * drop a reference on the map_object whether or
2482 * not a pageout object is inserted
2484 if (upl
->flags
& UPL_SHADOWED
)
2485 vm_object_deallocate(upl
->map_object
);
2487 if (upl
->flags
& UPL_DEVICE_MEMORY
)
2491 page_field_size
= 0;
2493 if (upl
->flags
& UPL_LITE
) {
2494 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
2495 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
2497 if (upl
->flags
& UPL_INTERNAL
) {
2499 sizeof(struct upl
) +
2500 (sizeof(struct upl_page_info
) * (size
/PAGE_SIZE
))
2503 kfree(upl
, sizeof(struct upl
) + page_field_size
);
2507 void uc_upl_dealloc(upl_t upl
);
2508 __private_extern__
void
2509 uc_upl_dealloc(upl_t upl
)
2511 if (--upl
->ref_count
== 0)
2516 upl_deallocate(upl_t upl
)
2518 if (--upl
->ref_count
== 0)
2523 * Statistics about UPL enforcement of copy-on-write obligations.
2525 unsigned long upl_cow
= 0;
2526 unsigned long upl_cow_again
= 0;
2527 unsigned long upl_cow_contiguous
= 0;
2528 unsigned long upl_cow_pages
= 0;
2529 unsigned long upl_cow_again_pages
= 0;
2530 unsigned long upl_cow_contiguous_pages
= 0;
2533 * Routine: vm_object_upl_request
2535 * Cause the population of a portion of a vm_object.
2536 * Depending on the nature of the request, the pages
2537 * returned may be contain valid data or be uninitialized.
2538 * A page list structure, listing the physical pages
2539 * will be returned upon request.
2540 * This function is called by the file system or any other
2541 * supplier of backing store to a pager.
2542 * IMPORTANT NOTE: The caller must still respect the relationship
2543 * between the vm_object and its backing memory object. The
2544 * caller MUST NOT substitute changes in the backing file
2545 * without first doing a memory_object_lock_request on the
2546 * target range unless it is know that the pages are not
2547 * shared with another entity at the pager level.
2549 * if a page list structure is present
2550 * return the mapped physical pages, where a
2551 * page is not present, return a non-initialized
2552 * one. If the no_sync bit is turned on, don't
2553 * call the pager unlock to synchronize with other
2554 * possible copies of the page. Leave pages busy
2555 * in the original object, if a page list structure
2556 * was specified. When a commit of the page list
2557 * pages is done, the dirty bit will be set for each one.
2559 * If a page list structure is present, return
2560 * all mapped pages. Where a page does not exist
2561 * map a zero filled one. Leave pages busy in
2562 * the original object. If a page list structure
2563 * is not specified, this call is a no-op.
2565 * Note: access of default pager objects has a rather interesting
2566 * twist. The caller of this routine, presumably the file system
2567 * page cache handling code, will never actually make a request
2568 * against a default pager backed object. Only the default
2569 * pager will make requests on backing store related vm_objects
2570 * In this way the default pager can maintain the relationship
2571 * between backing store files (abstract memory objects) and
2572 * the vm_objects (cache objects), they support.
2576 __private_extern__ kern_return_t
2577 vm_object_upl_request(
2579 vm_object_offset_t offset
,
2582 upl_page_info_array_t user_page_list
,
2583 unsigned int *page_list_count
,
2586 vm_page_t dst_page
= VM_PAGE_NULL
;
2587 vm_object_offset_t dst_offset
;
2588 upl_size_t xfer_size
;
2593 #if MACH_CLUSTER_STATS
2594 boolean_t encountered_lrp
= FALSE
;
2596 vm_page_t alias_page
= NULL
;
2597 int refmod_state
= 0;
2598 wpl_array_t lite_list
= NULL
;
2599 vm_object_t last_copy_object
;
2600 int delayed_unlock
= 0;
2603 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
2605 * For forward compatibility's sake,
2606 * reject any unknown flag.
2608 return KERN_INVALID_VALUE
;
2610 if ( (!object
->internal
) && (object
->paging_offset
!= 0) )
2611 panic("vm_object_upl_request: external object with non-zero paging offset\n");
2612 if (object
->phys_contiguous
)
2613 panic("vm_object_upl_request: contiguous object specified\n");
2616 if ((size
/ PAGE_SIZE
) > MAX_UPL_SIZE
)
2617 size
= MAX_UPL_SIZE
* PAGE_SIZE
;
2619 if ( (cntrl_flags
& UPL_SET_INTERNAL
) && page_list_count
!= NULL
)
2620 *page_list_count
= MAX_UPL_SIZE
;
2622 if (cntrl_flags
& UPL_SET_INTERNAL
) {
2623 if (cntrl_flags
& UPL_SET_LITE
) {
2625 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
, 0, size
);
2627 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
2628 lite_list
= (wpl_array_t
)
2629 (((uintptr_t)user_page_list
) +
2630 ((size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
2632 upl
= upl_create(UPL_CREATE_INTERNAL
, 0, size
);
2634 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
2637 if (cntrl_flags
& UPL_SET_LITE
) {
2639 upl
= upl_create(UPL_CREATE_EXTERNAL
| UPL_CREATE_LITE
, 0, size
);
2641 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
2643 upl
= upl_create(UPL_CREATE_EXTERNAL
, 0, size
);
2649 user_page_list
[0].device
= FALSE
;
2651 if (cntrl_flags
& UPL_SET_LITE
) {
2652 upl
->map_object
= object
;
2654 upl
->map_object
= vm_object_allocate(size
);
2656 * No neeed to lock the new object: nobody else knows
2657 * about it yet, so it's all ours so far.
2659 upl
->map_object
->shadow
= object
;
2660 upl
->map_object
->pageout
= TRUE
;
2661 upl
->map_object
->can_persist
= FALSE
;
2662 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
2663 upl
->map_object
->shadow_offset
= offset
;
2664 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
2666 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
2668 upl
->flags
|= UPL_SHADOWED
;
2672 * Just mark the UPL as "encrypted" here.
2673 * We'll actually encrypt the pages later,
2674 * in upl_encrypt(), when the caller has
2675 * selected which pages need to go to swap.
2677 if (cntrl_flags
& UPL_ENCRYPT
)
2678 upl
->flags
|= UPL_ENCRYPTED
;
2680 if (cntrl_flags
& UPL_FOR_PAGEOUT
)
2681 upl
->flags
|= UPL_PAGEOUT
;
2683 vm_object_lock(object
);
2684 vm_object_paging_begin(object
);
2687 * we can lock in the paging_offset once paging_in_progress is set
2690 upl
->offset
= offset
+ object
->paging_offset
;
2693 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
2694 #endif /* UPL_DEBUG */
2696 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= VM_OBJECT_NULL
) {
2698 * Honor copy-on-write obligations
2700 * The caller is gathering these pages and
2701 * might modify their contents. We need to
2702 * make sure that the copy object has its own
2703 * private copies of these pages before we let
2704 * the caller modify them.
2706 vm_object_update(object
,
2711 FALSE
, /* should_return */
2712 MEMORY_OBJECT_COPY_SYNC
,
2715 upl_cow_pages
+= size
>> PAGE_SHIFT
;
2718 * remember which copy object we synchronized with
2720 last_copy_object
= object
->copy
;
2724 dst_offset
= offset
;
2728 if ((alias_page
== NULL
) && !(cntrl_flags
& UPL_SET_LITE
)) {
2729 if (delayed_unlock
) {
2731 vm_page_unlock_queues();
2733 vm_object_unlock(object
);
2734 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
2737 if (delayed_unlock
== 0) {
2739 * pageout_scan takes the vm_page_lock_queues first
2740 * then tries for the object lock... to avoid what
2741 * is effectively a lock inversion, we'll go to the
2742 * trouble of taking them in that same order... otherwise
2743 * if this object contains the majority of the pages resident
2744 * in the UBC (or a small set of large objects actively being
2745 * worked on contain the majority of the pages), we could
2746 * cause the pageout_scan thread to 'starve' in its attempt
2747 * to find pages to move to the free queue, since it has to
2748 * successfully acquire the object lock of any candidate page
2749 * before it can steal/clean it.
2751 vm_object_unlock(object
);
2753 for (j
= 0; ; j
++) {
2754 vm_page_lock_queues();
2756 if (vm_object_lock_try(object
))
2758 vm_page_unlock_queues();
2763 if (cntrl_flags
& UPL_COPYOUT_FROM
) {
2764 upl
->flags
|= UPL_PAGE_SYNC_DONE
;
2766 if ( ((dst_page
= vm_page_lookup(object
, dst_offset
)) == VM_PAGE_NULL
) ||
2767 dst_page
->fictitious
||
2770 (dst_page
->wire_count
&& !dst_page
->pageout
&& !dst_page
->list_req_pending
)) {
2773 user_page_list
[entry
].phys_addr
= 0;
2775 goto delay_unlock_queues
;
2778 * grab this up front...
2779 * a high percentange of the time we're going to
2780 * need the hardware modification state a bit later
2781 * anyway... so we can eliminate an extra call into
2782 * the pmap layer by grabbing it here and recording it
2784 if (dst_page
->pmapped
)
2785 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
2789 if ( (refmod_state
& VM_MEM_REFERENCED
) && dst_page
->inactive
) {
2791 * page is on inactive list and referenced...
2792 * reactivate it now... this gets it out of the
2793 * way of vm_pageout_scan which would have to
2794 * reactivate it upon tripping over it
2796 vm_page_activate(dst_page
);
2797 VM_STAT_INCR(reactivations
);
2799 if (cntrl_flags
& UPL_RET_ONLY_DIRTY
) {
2801 * we're only asking for DIRTY pages to be returned
2803 if (dst_page
->list_req_pending
|| !(cntrl_flags
& UPL_FOR_PAGEOUT
)) {
2805 * if we were the page stolen by vm_pageout_scan to be
2806 * cleaned (as opposed to a buddy being clustered in
2807 * or this request is not being driven by a PAGEOUT cluster
2808 * then we only need to check for the page being dirty or
2809 * precious to decide whether to return it
2811 if (dst_page
->dirty
|| dst_page
->precious
|| (refmod_state
& VM_MEM_MODIFIED
))
2816 * this is a request for a PAGEOUT cluster and this page
2817 * is merely along for the ride as a 'buddy'... not only
2818 * does it have to be dirty to be returned, but it also
2819 * can't have been referenced recently... note that we've
2820 * already filtered above based on whether this page is
2821 * currently on the inactive queue or it meets the page
2822 * ticket (generation count) check
2824 if ( !(refmod_state
& VM_MEM_REFERENCED
) &&
2825 ((refmod_state
& VM_MEM_MODIFIED
) || dst_page
->dirty
|| dst_page
->precious
) ) {
2830 * if we reach here, we're not to return
2831 * the page... go on to the next one
2834 user_page_list
[entry
].phys_addr
= 0;
2836 goto delay_unlock_queues
;
2839 if (dst_page
->busy
&& (!(dst_page
->list_req_pending
&& dst_page
->pageout
))) {
2840 if (cntrl_flags
& UPL_NOBLOCK
) {
2842 user_page_list
[entry
].phys_addr
= 0;
2844 goto delay_unlock_queues
;
2847 * someone else is playing with the
2848 * page. We will have to wait.
2851 vm_page_unlock_queues();
2853 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
2858 * Someone else already cleaning the page?
2860 if ((dst_page
->cleaning
|| dst_page
->absent
|| dst_page
->wire_count
!= 0) && !dst_page
->list_req_pending
) {
2862 user_page_list
[entry
].phys_addr
= 0;
2864 goto delay_unlock_queues
;
2868 * The caller is gathering this page and might
2869 * access its contents later on. Decrypt the
2870 * page before adding it to the UPL, so that
2871 * the caller never sees encrypted data.
2873 if (! (cntrl_flags
& UPL_ENCRYPT
) && dst_page
->encrypted
) {
2877 vm_page_unlock_queues();
2879 * save the current state of busy
2880 * mark page as busy while decrypt
2881 * is in progress since it will drop
2882 * the object lock...
2884 was_busy
= dst_page
->busy
;
2885 dst_page
->busy
= TRUE
;
2887 vm_page_decrypt(dst_page
, 0);
2888 vm_page_decrypt_for_upl_counter
++;
2890 * restore to original busy state
2892 dst_page
->busy
= was_busy
;
2894 vm_page_lock_queues();
2897 if (dst_page
->pageout_queue
== TRUE
)
2899 * we've buddied up a page for a clustered pageout
2900 * that has already been moved to the pageout
2901 * queue by pageout_scan... we need to remove
2902 * it from the queue and drop the laundry count
2905 vm_pageout_queue_steal(dst_page
);
2906 #if MACH_CLUSTER_STATS
2908 * pageout statistics gathering. count
2909 * all the pages we will page out that
2910 * were not counted in the initial
2911 * vm_pageout_scan work
2913 if (dst_page
->list_req_pending
)
2914 encountered_lrp
= TRUE
;
2915 if ((dst_page
->dirty
|| (dst_page
->object
->internal
&& dst_page
->precious
)) && !dst_page
->list_req_pending
) {
2916 if (encountered_lrp
)
2917 CLUSTER_STAT(pages_at_higher_offsets
++;)
2919 CLUSTER_STAT(pages_at_lower_offsets
++;)
2923 * Turn off busy indication on pending
2924 * pageout. Note: we can only get here
2925 * in the request pending case.
2927 dst_page
->list_req_pending
= FALSE
;
2928 dst_page
->busy
= FALSE
;
2930 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
2931 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
2933 if (dst_page
->phys_page
> upl
->highest_page
)
2934 upl
->highest_page
= dst_page
->phys_page
;
2936 if (cntrl_flags
& UPL_SET_LITE
) {
2939 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
2940 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
2943 pmap_clear_modify(dst_page
->phys_page
);
2946 * Mark original page as cleaning
2949 dst_page
->cleaning
= TRUE
;
2950 dst_page
->precious
= FALSE
;
2953 * use pageclean setup, it is more
2954 * convenient even for the pageout
2957 vm_object_lock(upl
->map_object
);
2958 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
2959 vm_object_unlock(upl
->map_object
);
2961 alias_page
->absent
= FALSE
;
2966 * Record that this page has been
2969 vm_external_state_set(object
->existence_map
, dst_page
->offset
);
2970 #endif /*MACH_PAGEMAP*/
2971 dst_page
->dirty
= dirty
;
2974 dst_page
->precious
= TRUE
;
2976 if (dst_page
->pageout
)
2977 dst_page
->busy
= TRUE
;
2979 if ( (cntrl_flags
& UPL_ENCRYPT
) ) {
2982 * We want to deny access to the target page
2983 * because its contents are about to be
2984 * encrypted and the user would be very
2985 * confused to see encrypted data instead
2987 * We also set "encrypted_cleaning" to allow
2988 * vm_pageout_scan() to demote that page
2989 * from "adjacent/clean-in-place" to
2990 * "target/clean-and-free" if it bumps into
2991 * this page during its scanning while we're
2992 * still processing this cluster.
2994 dst_page
->busy
= TRUE
;
2995 dst_page
->encrypted_cleaning
= TRUE
;
2997 if ( !(cntrl_flags
& UPL_CLEAN_IN_PLACE
) ) {
2999 * deny access to the target page
3000 * while it is being worked on
3002 if ((!dst_page
->pageout
) && (dst_page
->wire_count
== 0)) {
3003 dst_page
->busy
= TRUE
;
3004 dst_page
->pageout
= TRUE
;
3005 vm_page_wire(dst_page
);
3009 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= last_copy_object
) {
3011 * Honor copy-on-write obligations
3013 * The copy object has changed since we
3014 * last synchronized for copy-on-write.
3015 * Another copy object might have been
3016 * inserted while we released the object's
3017 * lock. Since someone could have seen the
3018 * original contents of the remaining pages
3019 * through that new object, we have to
3020 * synchronize with it again for the remaining
3021 * pages only. The previous pages are "busy"
3022 * so they can not be seen through the new
3023 * mapping. The new mapping will see our
3024 * upcoming changes for those previous pages,
3025 * but that's OK since they couldn't see what
3026 * was there before. It's just a race anyway
3027 * and there's no guarantee of consistency or
3028 * atomicity. We just don't want new mappings
3029 * to see both the *before* and *after* pages.
3031 if (object
->copy
!= VM_OBJECT_NULL
) {
3033 vm_page_unlock_queues();
3037 dst_offset
,/* current offset */
3038 xfer_size
, /* remaining size */
3041 FALSE
, /* should_return */
3042 MEMORY_OBJECT_COPY_SYNC
,
3046 upl_cow_again_pages
+= xfer_size
>> PAGE_SHIFT
;
3048 vm_page_lock_queues();
3052 * remember the copy object we synced with
3054 last_copy_object
= object
->copy
;
3056 dst_page
= vm_page_lookup(object
, dst_offset
);
3058 if (dst_page
!= VM_PAGE_NULL
) {
3059 if ( !(dst_page
->list_req_pending
) ) {
3060 if ((cntrl_flags
& UPL_RET_ONLY_ABSENT
) && !dst_page
->absent
) {
3062 * skip over pages already present in the cache
3065 user_page_list
[entry
].phys_addr
= 0;
3067 goto delay_unlock_queues
;
3069 if (dst_page
->cleaning
) {
3071 * someone else is writing to the page... wait...
3074 vm_page_unlock_queues();
3076 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
3081 if (dst_page
->fictitious
&&
3082 dst_page
->phys_page
== vm_page_fictitious_addr
) {
3083 assert( !dst_page
->speculative
);
3085 * dump the fictitious page
3087 dst_page
->list_req_pending
= FALSE
;
3089 vm_page_free(dst_page
);
3092 } else if (dst_page
->absent
) {
3094 * the default_pager case
3096 dst_page
->list_req_pending
= FALSE
;
3097 dst_page
->busy
= FALSE
;
3101 if (dst_page
== VM_PAGE_NULL
) {
3102 if (object
->private) {
3104 * This is a nasty wrinkle for users
3105 * of upl who encounter device or
3106 * private memory however, it is
3107 * unavoidable, only a fault can
3108 * resolve the actual backing
3109 * physical page by asking the
3113 user_page_list
[entry
].phys_addr
= 0;
3115 goto delay_unlock_queues
;
3118 * need to allocate a page
3120 dst_page
= vm_page_grab();
3122 if (dst_page
== VM_PAGE_NULL
) {
3123 if ( (cntrl_flags
& (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) == (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) {
3125 * we don't want to stall waiting for pages to come onto the free list
3126 * while we're already holding absent pages in this UPL
3127 * the caller will deal with the empty slots
3130 user_page_list
[entry
].phys_addr
= 0;
3135 * no pages available... wait
3136 * then try again for the same
3140 vm_page_unlock_queues();
3142 vm_object_unlock(object
);
3146 * pageout_scan takes the vm_page_lock_queues first
3147 * then tries for the object lock... to avoid what
3148 * is effectively a lock inversion, we'll go to the
3149 * trouble of taking them in that same order... otherwise
3150 * if this object contains the majority of the pages resident
3151 * in the UBC (or a small set of large objects actively being
3152 * worked on contain the majority of the pages), we could
3153 * cause the pageout_scan thread to 'starve' in its attempt
3154 * to find pages to move to the free queue, since it has to
3155 * successfully acquire the object lock of any candidate page
3156 * before it can steal/clean it.
3158 for (j
= 0; ; j
++) {
3159 vm_page_lock_queues();
3161 if (vm_object_lock_try(object
))
3163 vm_page_unlock_queues();
3170 vm_page_insert_internal(dst_page
, object
, dst_offset
, TRUE
);
3172 dst_page
->absent
= TRUE
;
3173 dst_page
->busy
= FALSE
;
3175 if (cntrl_flags
& UPL_RET_ONLY_ABSENT
) {
3177 * if UPL_RET_ONLY_ABSENT was specified,
3178 * than we're definitely setting up a
3179 * upl for a clustered read/pagein
3180 * operation... mark the pages as clustered
3181 * so upl_commit_range can put them on the
3184 dst_page
->clustered
= TRUE
;
3190 if (cntrl_flags
& UPL_ENCRYPT
) {
3192 * The page is going to be encrypted when we
3193 * get it from the pager, so mark it so.
3195 dst_page
->encrypted
= TRUE
;
3198 * Otherwise, the page will not contain
3201 dst_page
->encrypted
= FALSE
;
3203 dst_page
->overwriting
= TRUE
;
3205 if (dst_page
->fictitious
) {
3206 panic("need corner case for fictitious page");
3208 if (dst_page
->busy
) {
3210 * someone else is playing with the
3211 * page. We will have to wait.
3214 vm_page_unlock_queues();
3216 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
3220 if (dst_page
->pmapped
) {
3221 if ( !(cntrl_flags
& UPL_FILE_IO
))
3223 * eliminate all mappings from the
3224 * original object and its prodigy
3226 refmod_state
= pmap_disconnect(dst_page
->phys_page
);
3228 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
3232 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
3233 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
3235 if (cntrl_flags
& UPL_SET_LITE
) {
3238 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
3239 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
3242 pmap_clear_modify(dst_page
->phys_page
);
3245 * Mark original page as cleaning
3248 dst_page
->cleaning
= TRUE
;
3249 dst_page
->precious
= FALSE
;
3252 * use pageclean setup, it is more
3253 * convenient even for the pageout
3256 vm_object_lock(upl
->map_object
);
3257 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
3258 vm_object_unlock(upl
->map_object
);
3260 alias_page
->absent
= FALSE
;
3264 if (cntrl_flags
& UPL_CLEAN_IN_PLACE
) {
3266 * clean in place for read implies
3267 * that a write will be done on all
3268 * the pages that are dirty before
3269 * a upl commit is done. The caller
3270 * is obligated to preserve the
3271 * contents of all pages marked dirty
3273 upl
->flags
|= UPL_CLEAR_DIRTY
;
3275 dst_page
->dirty
= dirty
;
3278 dst_page
->precious
= TRUE
;
3280 if (dst_page
->wire_count
== 0) {
3282 * deny access to the target page while
3283 * it is being worked on
3285 dst_page
->busy
= TRUE
;
3287 vm_page_wire(dst_page
);
3289 if (dst_page
->clustered
) {
3291 * expect the page not to be used
3292 * since it's coming in as part
3293 * of a speculative cluster...
3294 * pages that are 'consumed' will
3295 * get a hardware reference
3297 dst_page
->reference
= FALSE
;
3300 * expect the page to be used
3302 dst_page
->reference
= TRUE
;
3304 dst_page
->precious
= (cntrl_flags
& UPL_PRECIOUS
) ? TRUE
: FALSE
;
3306 if (dst_page
->phys_page
> upl
->highest_page
)
3307 upl
->highest_page
= dst_page
->phys_page
;
3308 if (user_page_list
) {
3309 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
3310 user_page_list
[entry
].dirty
= dst_page
->dirty
;
3311 user_page_list
[entry
].pageout
= dst_page
->pageout
;
3312 user_page_list
[entry
].absent
= dst_page
->absent
;
3313 user_page_list
[entry
].precious
= dst_page
->precious
;
3315 if (dst_page
->clustered
== TRUE
)
3316 user_page_list
[entry
].speculative
= dst_page
->speculative
;
3318 user_page_list
[entry
].speculative
= FALSE
;
3321 * if UPL_RET_ONLY_ABSENT is set, then
3322 * we are working with a fresh page and we've
3323 * just set the clustered flag on it to
3324 * indicate that it was drug in as part of a
3325 * speculative cluster... so leave it alone
3327 if ( !(cntrl_flags
& UPL_RET_ONLY_ABSENT
)) {
3329 * someone is explicitly grabbing this page...
3330 * update clustered and speculative state
3333 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
3335 delay_unlock_queues
:
3336 if (delayed_unlock
++ > UPL_DELAYED_UNLOCK_LIMIT
) {
3338 * pageout_scan takes the vm_page_lock_queues first
3339 * then tries for the object lock... to avoid what
3340 * is effectively a lock inversion, we'll go to the
3341 * trouble of taking them in that same order... otherwise
3342 * if this object contains the majority of the pages resident
3343 * in the UBC (or a small set of large objects actively being
3344 * worked on contain the majority of the pages), we could
3345 * cause the pageout_scan thread to 'starve' in its attempt
3346 * to find pages to move to the free queue, since it has to
3347 * successfully acquire the object lock of any candidate page
3348 * before it can steal/clean it.
3350 vm_object_unlock(object
);
3351 mutex_yield(&vm_page_queue_lock
);
3353 for (j
= 0; ; j
++) {
3354 if (vm_object_lock_try(object
))
3356 vm_page_unlock_queues();
3358 vm_page_lock_queues();
3364 dst_offset
+= PAGE_SIZE_64
;
3365 xfer_size
-= PAGE_SIZE
;
3367 if (alias_page
!= NULL
) {
3368 if (delayed_unlock
== 0) {
3369 vm_page_lock_queues();
3372 vm_page_free(alias_page
);
3375 vm_page_unlock_queues();
3377 if (page_list_count
!= NULL
) {
3378 if (upl
->flags
& UPL_INTERNAL
)
3379 *page_list_count
= 0;
3380 else if (*page_list_count
> entry
)
3381 *page_list_count
= entry
;
3383 vm_object_unlock(object
);
3385 return KERN_SUCCESS
;
3388 /* JMM - Backward compatability for now */
3390 vm_fault_list_request( /* forward */
3391 memory_object_control_t control
,
3392 vm_object_offset_t offset
,
3395 upl_page_info_t
**user_page_list_ptr
,
3396 unsigned int page_list_count
,
3399 vm_fault_list_request(
3400 memory_object_control_t control
,
3401 vm_object_offset_t offset
,
3404 upl_page_info_t
**user_page_list_ptr
,
3405 unsigned int page_list_count
,
3408 unsigned int local_list_count
;
3409 upl_page_info_t
*user_page_list
;
3412 if (user_page_list_ptr
!= NULL
) {
3413 local_list_count
= page_list_count
;
3414 user_page_list
= *user_page_list_ptr
;
3416 local_list_count
= 0;
3417 user_page_list
= NULL
;
3419 kr
= memory_object_upl_request(control
,
3427 if(kr
!= KERN_SUCCESS
)
3430 if ((user_page_list_ptr
!= NULL
) && (cntrl_flags
& UPL_INTERNAL
)) {
3431 *user_page_list_ptr
= UPL_GET_INTERNAL_PAGE_LIST(*upl_ptr
);
3434 return KERN_SUCCESS
;
3440 * Routine: vm_object_super_upl_request
3442 * Cause the population of a portion of a vm_object
3443 * in much the same way as memory_object_upl_request.
3444 * Depending on the nature of the request, the pages
3445 * returned may be contain valid data or be uninitialized.
3446 * However, the region may be expanded up to the super
3447 * cluster size provided.
3450 __private_extern__ kern_return_t
3451 vm_object_super_upl_request(
3453 vm_object_offset_t offset
,
3455 upl_size_t super_cluster
,
3457 upl_page_info_t
*user_page_list
,
3458 unsigned int *page_list_count
,
3461 if (object
->paging_offset
> offset
)
3462 return KERN_FAILURE
;
3464 assert(object
->paging_in_progress
);
3465 offset
= offset
- object
->paging_offset
;
3467 if (super_cluster
> size
) {
3469 vm_object_offset_t base_offset
;
3470 upl_size_t super_size
;
3472 base_offset
= (offset
& ~((vm_object_offset_t
) super_cluster
- 1));
3473 super_size
= (offset
+ size
) > (base_offset
+ super_cluster
) ? super_cluster
<<1 : super_cluster
;
3474 super_size
= ((base_offset
+ super_size
) > object
->size
) ? (object
->size
- base_offset
) : super_size
;
3476 if (offset
> (base_offset
+ super_size
)) {
3477 panic("vm_object_super_upl_request: Missed target pageout"
3478 " %#llx,%#llx, %#x, %#x, %#x, %#llx\n",
3479 offset
, base_offset
, super_size
, super_cluster
,
3480 size
, object
->paging_offset
);
3483 * apparently there is a case where the vm requests a
3484 * page to be written out who's offset is beyond the
3487 if ((offset
+ size
) > (base_offset
+ super_size
))
3488 super_size
= (offset
+ size
) - base_offset
;
3490 offset
= base_offset
;
3493 return vm_object_upl_request(object
, offset
, size
, upl
, user_page_list
, page_list_count
, cntrl_flags
);
3500 vm_map_address_t offset
,
3501 upl_size_t
*upl_size
,
3503 upl_page_info_array_t page_list
,
3504 unsigned int *count
,
3507 vm_map_entry_t entry
;
3509 int force_data_sync
;
3511 vm_object_t local_object
;
3512 vm_map_offset_t local_offset
;
3513 vm_map_offset_t local_start
;
3516 caller_flags
= *flags
;
3518 if (caller_flags
& ~UPL_VALID_FLAGS
) {
3520 * For forward compatibility's sake,
3521 * reject any unknown flag.
3523 return KERN_INVALID_VALUE
;
3525 force_data_sync
= (caller_flags
& UPL_FORCE_DATA_SYNC
);
3526 sync_cow_data
= !(caller_flags
& UPL_COPYOUT_FROM
);
3529 return KERN_INVALID_ARGUMENT
;
3534 if (vm_map_lookup_entry(map
, offset
, &entry
)) {
3536 if ((entry
->vme_end
- offset
) < *upl_size
)
3537 *upl_size
= entry
->vme_end
- offset
;
3539 if (caller_flags
& UPL_QUERY_OBJECT_TYPE
) {
3542 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
3543 if (entry
->object
.vm_object
->private)
3544 *flags
= UPL_DEV_MEMORY
;
3546 if (entry
->object
.vm_object
->phys_contiguous
)
3547 *flags
|= UPL_PHYS_CONTIG
;
3551 return KERN_SUCCESS
;
3553 if (entry
->object
.vm_object
== VM_OBJECT_NULL
|| !entry
->object
.vm_object
->phys_contiguous
) {
3554 if ((*upl_size
/page_size
) > MAX_UPL_SIZE
)
3555 *upl_size
= MAX_UPL_SIZE
* page_size
;
3558 * Create an object if necessary.
3560 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
3561 entry
->object
.vm_object
= vm_object_allocate((vm_size_t
)(entry
->vme_end
- entry
->vme_start
));
3564 if (!(caller_flags
& UPL_COPYOUT_FROM
)) {
3565 if (!(entry
->protection
& VM_PROT_WRITE
)) {
3567 return KERN_PROTECTION_FAILURE
;
3569 if (entry
->needs_copy
) {
3572 vm_object_offset_t new_offset
;
3575 vm_map_version_t version
;
3579 vm_map_lock_write_to_read(map
);
3581 if (vm_map_lookup_locked(&local_map
,
3582 offset
, VM_PROT_WRITE
,
3583 OBJECT_LOCK_EXCLUSIVE
,
3585 &new_offset
, &prot
, &wired
,
3588 vm_map_unlock(local_map
);
3589 return KERN_FAILURE
;
3591 if (real_map
!= map
)
3592 vm_map_unlock(real_map
);
3593 vm_object_unlock(object
);
3594 vm_map_unlock(local_map
);
3596 goto REDISCOVER_ENTRY
;
3599 if (entry
->is_sub_map
) {
3602 submap
= entry
->object
.sub_map
;
3603 local_start
= entry
->vme_start
;
3604 local_offset
= entry
->offset
;
3606 vm_map_reference(submap
);
3609 ret
= vm_map_create_upl(submap
,
3610 local_offset
+ (offset
- local_start
),
3611 upl_size
, upl
, page_list
, count
, flags
);
3612 vm_map_deallocate(submap
);
3616 if (sync_cow_data
) {
3617 if (entry
->object
.vm_object
->shadow
|| entry
->object
.vm_object
->copy
) {
3618 local_object
= entry
->object
.vm_object
;
3619 local_start
= entry
->vme_start
;
3620 local_offset
= entry
->offset
;
3622 vm_object_reference(local_object
);
3625 if (entry
->object
.vm_object
->shadow
&& entry
->object
.vm_object
->copy
) {
3626 vm_object_lock_request(
3627 local_object
->shadow
,
3628 (vm_object_offset_t
)
3629 ((offset
- local_start
) +
3631 local_object
->shadow_offset
,
3633 MEMORY_OBJECT_DATA_SYNC
,
3636 sync_cow_data
= FALSE
;
3637 vm_object_deallocate(local_object
);
3639 goto REDISCOVER_ENTRY
;
3642 if (force_data_sync
) {
3643 local_object
= entry
->object
.vm_object
;
3644 local_start
= entry
->vme_start
;
3645 local_offset
= entry
->offset
;
3647 vm_object_reference(local_object
);
3650 vm_object_lock_request(
3652 (vm_object_offset_t
)
3653 ((offset
- local_start
) + local_offset
),
3654 (vm_object_size_t
)*upl_size
, FALSE
,
3655 MEMORY_OBJECT_DATA_SYNC
,
3658 force_data_sync
= FALSE
;
3659 vm_object_deallocate(local_object
);
3661 goto REDISCOVER_ENTRY
;
3663 if (entry
->object
.vm_object
->private)
3664 *flags
= UPL_DEV_MEMORY
;
3668 if (entry
->object
.vm_object
->phys_contiguous
)
3669 *flags
|= UPL_PHYS_CONTIG
;
3671 local_object
= entry
->object
.vm_object
;
3672 local_offset
= entry
->offset
;
3673 local_start
= entry
->vme_start
;
3675 vm_object_reference(local_object
);
3678 ret
= vm_object_iopl_request(local_object
,
3679 (vm_object_offset_t
) ((offset
- local_start
) + local_offset
),
3685 vm_object_deallocate(local_object
);
3691 return(KERN_FAILURE
);
3695 * Internal routine to enter a UPL into a VM map.
3697 * JMM - This should just be doable through the standard
3698 * vm_map_enter() API.
3704 vm_map_offset_t
*dst_addr
)
3707 vm_object_offset_t offset
;
3708 vm_map_offset_t addr
;
3712 if (upl
== UPL_NULL
)
3713 return KERN_INVALID_ARGUMENT
;
3718 * check to see if already mapped
3720 if (UPL_PAGE_LIST_MAPPED
& upl
->flags
) {
3722 return KERN_FAILURE
;
3725 if ((!(upl
->flags
& UPL_SHADOWED
)) && !((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) ||
3726 (upl
->map_object
->phys_contiguous
))) {
3728 vm_page_t alias_page
;
3729 vm_object_offset_t new_offset
;
3731 wpl_array_t lite_list
;
3733 if (upl
->flags
& UPL_INTERNAL
) {
3734 lite_list
= (wpl_array_t
)
3735 ((((uintptr_t)upl
) + sizeof(struct upl
))
3736 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
3738 lite_list
= (wpl_array_t
)(((uintptr_t)upl
) + sizeof(struct upl
));
3740 object
= upl
->map_object
;
3741 upl
->map_object
= vm_object_allocate(upl
->size
);
3743 vm_object_lock(upl
->map_object
);
3745 upl
->map_object
->shadow
= object
;
3746 upl
->map_object
->pageout
= TRUE
;
3747 upl
->map_object
->can_persist
= FALSE
;
3748 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
3749 upl
->map_object
->shadow_offset
= upl
->offset
- object
->paging_offset
;
3750 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
3751 offset
= upl
->map_object
->shadow_offset
;
3755 upl
->flags
|= UPL_SHADOWED
;
3758 pg_num
= (new_offset
)/PAGE_SIZE
;
3760 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
3762 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
3764 vm_object_lock(object
);
3766 m
= vm_page_lookup(object
, offset
);
3767 if (m
== VM_PAGE_NULL
) {
3768 panic("vm_upl_map: page missing\n");
3772 * Convert the fictitious page to a private
3773 * shadow of the real page.
3775 assert(alias_page
->fictitious
);
3776 alias_page
->fictitious
= FALSE
;
3777 alias_page
->private = TRUE
;
3778 alias_page
->pageout
= TRUE
;
3780 * since m is a page in the upl it must
3781 * already be wired or BUSY, so it's
3782 * safe to assign the underlying physical
3785 alias_page
->phys_page
= m
->phys_page
;
3787 vm_object_unlock(object
);
3789 vm_page_lockspin_queues();
3790 vm_page_wire(alias_page
);
3791 vm_page_unlock_queues();
3795 * The virtual page ("m") has to be wired in some way
3796 * here or its physical page ("m->phys_page") could
3797 * be recycled at any time.
3798 * Assuming this is enforced by the caller, we can't
3799 * get an encrypted page here. Since the encryption
3800 * key depends on the VM page's "pager" object and
3801 * the "paging_offset", we couldn't handle 2 pageable
3802 * VM pages (with different pagers and paging_offsets)
3803 * sharing the same physical page: we could end up
3804 * encrypting with one key (via one VM page) and
3805 * decrypting with another key (via the alias VM page).
3807 ASSERT_PAGE_DECRYPTED(m
);
3809 vm_page_insert(alias_page
, upl
->map_object
, new_offset
);
3811 assert(!alias_page
->wanted
);
3812 alias_page
->busy
= FALSE
;
3813 alias_page
->absent
= FALSE
;
3816 offset
+= PAGE_SIZE_64
;
3817 new_offset
+= PAGE_SIZE_64
;
3819 vm_object_unlock(upl
->map_object
);
3821 if ((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) || upl
->map_object
->phys_contiguous
)
3822 offset
= upl
->offset
- upl
->map_object
->paging_offset
;
3827 vm_object_reference(upl
->map_object
);
3831 * NEED A UPL_MAP ALIAS
3833 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
3834 VM_FLAGS_ANYWHERE
, upl
->map_object
, offset
, FALSE
,
3835 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
3837 if (kr
!= KERN_SUCCESS
) {
3841 vm_object_lock(upl
->map_object
);
3843 for (addr
= *dst_addr
; size
> 0; size
-= PAGE_SIZE
, addr
+= PAGE_SIZE
) {
3844 m
= vm_page_lookup(upl
->map_object
, offset
);
3847 unsigned int cache_attr
;
3848 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
3853 PMAP_ENTER(map
->pmap
, addr
, m
, VM_PROT_ALL
, cache_attr
, TRUE
);
3855 offset
+= PAGE_SIZE_64
;
3857 vm_object_unlock(upl
->map_object
);
3860 * hold a reference for the mapping
3863 upl
->flags
|= UPL_PAGE_LIST_MAPPED
;
3864 upl
->kaddr
= *dst_addr
;
3867 return KERN_SUCCESS
;
3871 * Internal routine to remove a UPL mapping from a VM map.
3873 * XXX - This should just be doable through a standard
3874 * vm_map_remove() operation. Otherwise, implicit clean-up
3875 * of the target map won't be able to correctly remove
3876 * these (and release the reference on the UPL). Having
3877 * to do this means we can't map these into user-space
3888 if (upl
== UPL_NULL
)
3889 return KERN_INVALID_ARGUMENT
;
3893 if (upl
->flags
& UPL_PAGE_LIST_MAPPED
) {
3897 assert(upl
->ref_count
> 1);
3898 upl
->ref_count
--; /* removing mapping ref */
3900 upl
->flags
&= ~UPL_PAGE_LIST_MAPPED
;
3901 upl
->kaddr
= (vm_offset_t
) 0;
3905 vm_map_trunc_page(addr
),
3906 vm_map_round_page(addr
+ size
),
3909 return KERN_SUCCESS
;
3913 return KERN_FAILURE
;
3919 upl_offset_t offset
,
3922 upl_page_info_t
*page_list
,
3923 mach_msg_type_number_t count
,
3926 upl_size_t xfer_size
;
3927 vm_object_t shadow_object
;
3929 vm_object_offset_t target_offset
;
3931 wpl_array_t lite_list
;
3933 int delayed_unlock
= 0;
3934 int clear_refmod
= 0;
3935 int pgpgout_count
= 0;
3940 if (upl
== UPL_NULL
)
3941 return KERN_INVALID_ARGUMENT
;
3946 if (upl
->flags
& UPL_DEVICE_MEMORY
)
3948 else if ((offset
+ size
) <= upl
->size
)
3951 return KERN_FAILURE
;
3955 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
3957 * We used this UPL to block access to the pages by marking
3958 * them "busy". Now we need to clear the "busy" bit to allow
3959 * access to these pages again.
3961 flags
|= UPL_COMMIT_ALLOW_ACCESS
;
3963 if (upl
->flags
& UPL_CLEAR_DIRTY
)
3964 flags
|= UPL_COMMIT_CLEAR_DIRTY
;
3966 if (upl
->flags
& UPL_INTERNAL
)
3967 lite_list
= (wpl_array_t
) ((((uintptr_t)upl
) + sizeof(struct upl
))
3968 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
3970 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
3972 object
= upl
->map_object
;
3974 if (upl
->flags
& UPL_SHADOWED
) {
3975 vm_object_lock(object
);
3976 shadow_object
= object
->shadow
;
3978 shadow_object
= object
;
3980 entry
= offset
/PAGE_SIZE
;
3981 target_offset
= (vm_object_offset_t
)offset
;
3984 * pageout_scan takes the vm_page_lock_queues first
3985 * then tries for the object lock... to avoid what
3986 * is effectively a lock inversion, we'll go to the
3987 * trouble of taking them in that same order... otherwise
3988 * if this object contains the majority of the pages resident
3989 * in the UBC (or a small set of large objects actively being
3990 * worked on contain the majority of the pages), we could
3991 * cause the pageout_scan thread to 'starve' in its attempt
3992 * to find pages to move to the free queue, since it has to
3993 * successfully acquire the object lock of any candidate page
3994 * before it can steal/clean it.
3996 for (j
= 0; ; j
++) {
3997 vm_page_lock_queues();
3999 if (vm_object_lock_try(shadow_object
))
4001 vm_page_unlock_queues();
4011 if (upl
->flags
& UPL_LITE
) {
4014 pg_num
= target_offset
/PAGE_SIZE
;
4016 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
4017 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
4019 m
= vm_page_lookup(shadow_object
, target_offset
+ (upl
->offset
- shadow_object
->paging_offset
));
4022 if (upl
->flags
& UPL_SHADOWED
) {
4023 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
4029 if (m
== VM_PAGE_NULL
)
4030 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->shadow_offset
);
4033 if (m
!= VM_PAGE_NULL
) {
4037 if (upl
->flags
& UPL_IO_WIRE
) {
4042 page_list
[entry
].phys_addr
= 0;
4044 if (flags
& UPL_COMMIT_SET_DIRTY
)
4046 else if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
4048 if (m
->cs_validated
&& !m
->cs_tainted
) {
4051 * This page is no longer dirty
4052 * but could have been modified,
4053 * so it will need to be
4056 m
->cs_validated
= FALSE
;
4057 vm_cs_validated_resets
++;
4059 clear_refmod
|= VM_MEM_MODIFIED
;
4061 if (flags
& UPL_COMMIT_INACTIVATE
)
4062 vm_page_deactivate(m
);
4065 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
4067 if (flags
& UPL_COMMIT_ALLOW_ACCESS
) {
4069 * We blocked access to the pages in this UPL.
4070 * Clear the "busy" bit and wake up any waiter
4073 PAGE_WAKEUP_DONE(m
);
4075 goto commit_next_page
;
4078 * make sure to clear the hardware
4079 * modify or reference bits before
4080 * releasing the BUSY bit on this page
4081 * otherwise we risk losing a legitimate
4084 if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
4086 if (m
->cs_validated
&& !m
->cs_tainted
) {
4089 * This page is no longer dirty
4090 * but could have been modified,
4091 * so it will need to be
4094 m
->cs_validated
= FALSE
;
4095 vm_cs_validated_resets
++;
4097 clear_refmod
|= VM_MEM_MODIFIED
;
4100 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
4105 p
= &(page_list
[entry
]);
4107 if (p
->phys_addr
&& p
->pageout
&& !m
->pageout
) {
4111 } else if (p
->phys_addr
&&
4112 !p
->pageout
&& m
->pageout
&&
4113 !m
->dump_cleaning
) {
4116 m
->overwriting
= FALSE
;
4119 PAGE_WAKEUP_DONE(m
);
4121 page_list
[entry
].phys_addr
= 0;
4123 m
->dump_cleaning
= FALSE
;
4126 vm_pageout_throttle_up(m
);
4129 m
->cleaning
= FALSE
;
4130 m
->encrypted_cleaning
= FALSE
;
4132 #if MACH_CLUSTER_STATS
4133 if (m
->wanted
) vm_pageout_target_collisions
++;
4136 if (m
->cs_validated
&& !m
->cs_tainted
) {
4139 * This page is no longer dirty
4140 * but could have been modified,
4141 * so it will need to be
4144 m
->cs_validated
= FALSE
;
4145 vm_cs_validated_resets
++;
4148 if (m
->pmapped
&& (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
))
4153 * page was re-dirtied after we started
4154 * the pageout... reactivate it since
4155 * we don't know whether the on-disk
4156 * copy matches what is now in memory
4160 if (upl
->flags
& UPL_PAGEOUT
) {
4161 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
4162 VM_STAT_INCR(reactivations
);
4163 DTRACE_VM2(pgrec
, int, 1, (uint64_t *), NULL
);
4165 PAGE_WAKEUP_DONE(m
);
4168 * page has been successfully cleaned
4169 * go ahead and free it for other use
4172 if (m
->object
->internal
) {
4173 DTRACE_VM2(anonpgout
, int, 1, (uint64_t *), NULL
);
4175 DTRACE_VM2(fspgout
, int, 1, (uint64_t *), NULL
);
4180 if (upl
->flags
& UPL_PAGEOUT
) {
4181 CLUSTER_STAT(vm_pageout_target_page_freed
++;)
4183 if (page_list
[entry
].dirty
) {
4184 VM_STAT_INCR(pageouts
);
4185 DTRACE_VM2(pgout
, int, 1, (uint64_t *), NULL
);
4190 goto commit_next_page
;
4192 #if MACH_CLUSTER_STATS
4194 m
->dirty
= pmap_is_modified(m
->phys_page
);
4196 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
4197 else vm_pageout_cluster_cleaned
++;
4198 if (m
->wanted
) vm_pageout_cluster_collisions
++;
4201 if (m
->cs_validated
&& !m
->cs_tainted
) {
4204 * This page is no longer dirty
4205 * but could have been modified,
4206 * so it will need to be
4209 m
->cs_validated
= FALSE
;
4210 vm_cs_validated_resets
++;
4213 if ((m
->busy
) && (m
->cleaning
)) {
4215 * the request_page_list case
4218 m
->overwriting
= FALSE
;
4220 } else if (m
->overwriting
) {
4222 * alternate request page list, write to
4223 * page_list case. Occurs when the original
4224 * page was wired at the time of the list
4227 assert(m
->wire_count
!= 0);
4228 vm_page_unwire(m
);/* reactivates */
4229 m
->overwriting
= FALSE
;
4231 m
->cleaning
= FALSE
;
4232 m
->encrypted_cleaning
= FALSE
;
4235 * It is a part of the semantic of COPYOUT_FROM
4236 * UPLs that a commit implies cache sync
4237 * between the vm page and the backing store
4238 * this can be used to strip the precious bit
4241 if (upl
->flags
& UPL_PAGE_SYNC_DONE
)
4242 m
->precious
= FALSE
;
4244 if (flags
& UPL_COMMIT_SET_DIRTY
)
4247 if ((flags
& UPL_COMMIT_INACTIVATE
) && !m
->clustered
&& !m
->speculative
) {
4248 vm_page_deactivate(m
);
4249 } else if (!m
->active
&& !m
->inactive
&& !m
->speculative
) {
4252 vm_page_speculate(m
, TRUE
);
4253 else if (m
->reference
)
4254 vm_page_activate(m
);
4256 vm_page_deactivate(m
);
4258 if (flags
& UPL_COMMIT_ALLOW_ACCESS
) {
4260 * We blocked access to the pages in this URL.
4261 * Clear the "busy" bit on this page before we
4262 * wake up any waiter.
4267 * Wakeup any thread waiting for the page to be un-cleaning.
4272 target_offset
+= PAGE_SIZE_64
;
4273 xfer_size
-= PAGE_SIZE
;
4276 if (delayed_unlock
++ > UPL_DELAYED_UNLOCK_LIMIT
) {
4278 * pageout_scan takes the vm_page_lock_queues first
4279 * then tries for the object lock... to avoid what
4280 * is effectively a lock inversion, we'll go to the
4281 * trouble of taking them in that same order... otherwise
4282 * if this object contains the majority of the pages resident
4283 * in the UBC (or a small set of large objects actively being
4284 * worked on contain the majority of the pages), we could
4285 * cause the pageout_scan thread to 'starve' in its attempt
4286 * to find pages to move to the free queue, since it has to
4287 * successfully acquire the object lock of any candidate page
4288 * before it can steal/clean it.
4290 vm_object_unlock(shadow_object
);
4291 mutex_yield(&vm_page_queue_lock
);
4293 for (j
= 0; ; j
++) {
4294 if (vm_object_lock_try(shadow_object
))
4296 vm_page_unlock_queues();
4298 vm_page_lock_queues();
4304 vm_page_unlock_queues();
4308 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4310 } else if (upl
->flags
& UPL_LITE
) {
4314 pg_num
= upl
->size
/PAGE_SIZE
;
4315 pg_num
= (pg_num
+ 31) >> 5;
4318 for (i
= 0; i
< pg_num
; i
++) {
4319 if (lite_list
[i
] != 0) {
4325 if (queue_empty(&upl
->map_object
->memq
))
4328 if (occupied
== 0) {
4329 if (upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
)
4332 if (object
== shadow_object
) {
4334 * this is not a paging object
4335 * so we need to drop the paging reference
4336 * that was taken when we created the UPL
4337 * against this object
4339 vm_object_paging_end(shadow_object
);
4342 * we dontated the paging reference to
4343 * the map object... vm_pageout_object_terminate
4344 * will drop this reference
4348 vm_object_unlock(shadow_object
);
4349 if (object
!= shadow_object
)
4350 vm_object_unlock(object
);
4353 if (pgpgout_count
) {
4354 DTRACE_VM2(pgpgout
, int, pgpgout_count
, (uint64_t *), NULL
);
4357 return KERN_SUCCESS
;
4363 upl_offset_t offset
,
4368 upl_size_t xfer_size
;
4369 vm_object_t shadow_object
;
4371 vm_object_offset_t target_offset
;
4373 wpl_array_t lite_list
;
4375 int delayed_unlock
= 0;
4380 if (upl
== UPL_NULL
)
4381 return KERN_INVALID_ARGUMENT
;
4383 if ( (upl
->flags
& UPL_IO_WIRE
) && !(error
& UPL_ABORT_DUMP_PAGES
) )
4384 return upl_commit_range(upl
, offset
, size
, 0, NULL
, 0, empty
);
4386 if (upl
->flags
& UPL_DEVICE_MEMORY
)
4388 else if ((offset
+ size
) <= upl
->size
)
4391 return KERN_FAILURE
;
4395 if (upl
->flags
& UPL_INTERNAL
) {
4396 lite_list
= (wpl_array_t
)
4397 ((((uintptr_t)upl
) + sizeof(struct upl
))
4398 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4400 lite_list
= (wpl_array_t
)
4401 (((uintptr_t)upl
) + sizeof(struct upl
));
4403 object
= upl
->map_object
;
4405 if (upl
->flags
& UPL_SHADOWED
) {
4406 vm_object_lock(object
);
4407 shadow_object
= object
->shadow
;
4409 shadow_object
= object
;
4411 entry
= offset
/PAGE_SIZE
;
4412 target_offset
= (vm_object_offset_t
)offset
;
4415 * pageout_scan takes the vm_page_lock_queues first
4416 * then tries for the object lock... to avoid what
4417 * is effectively a lock inversion, we'll go to the
4418 * trouble of taking them in that same order... otherwise
4419 * if this object contains the majority of the pages resident
4420 * in the UBC (or a small set of large objects actively being
4421 * worked on contain the majority of the pages), we could
4422 * cause the pageout_scan thread to 'starve' in its attempt
4423 * to find pages to move to the free queue, since it has to
4424 * successfully acquire the object lock of any candidate page
4425 * before it can steal/clean it.
4427 for (j
= 0; ; j
++) {
4428 vm_page_lock_queues();
4430 if (vm_object_lock_try(shadow_object
))
4432 vm_page_unlock_queues();
4442 if (upl
->flags
& UPL_LITE
) {
4444 pg_num
= target_offset
/PAGE_SIZE
;
4446 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
4447 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
4449 m
= vm_page_lookup(shadow_object
, target_offset
+
4450 (upl
->offset
- shadow_object
->paging_offset
));
4453 if (upl
->flags
& UPL_SHADOWED
) {
4454 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
4459 if (m
== VM_PAGE_NULL
)
4460 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->shadow_offset
);
4463 if (m
!= VM_PAGE_NULL
) {
4466 boolean_t must_free
= TRUE
;
4468 m
->clustered
= FALSE
;
4470 * COPYOUT = FALSE case
4471 * check for error conditions which must
4472 * be passed back to the pages customer
4474 if (error
& UPL_ABORT_RESTART
) {
4480 } else if (error
& UPL_ABORT_UNAVAILABLE
) {
4484 } else if (error
& UPL_ABORT_ERROR
) {
4494 * If the page was already encrypted,
4495 * we don't really need to decrypt it
4496 * now. It will get decrypted later,
4497 * on demand, as soon as someone needs
4498 * to access its contents.
4501 m
->cleaning
= FALSE
;
4502 m
->encrypted_cleaning
= FALSE
;
4503 m
->overwriting
= FALSE
;
4504 PAGE_WAKEUP_DONE(m
);
4506 if (must_free
== TRUE
)
4509 vm_page_activate(m
);
4512 * Handle the trusted pager throttle.
4515 vm_pageout_throttle_up(m
);
4519 assert(m
->wire_count
== 1);
4523 m
->dump_cleaning
= FALSE
;
4524 m
->cleaning
= FALSE
;
4525 m
->encrypted_cleaning
= FALSE
;
4526 m
->overwriting
= FALSE
;
4528 vm_external_state_clr(m
->object
->existence_map
, m
->offset
);
4529 #endif /* MACH_PAGEMAP */
4530 if (error
& UPL_ABORT_DUMP_PAGES
) {
4531 pmap_disconnect(m
->phys_page
);
4534 if (error
& UPL_ABORT_REFERENCE
) {
4536 * we've been told to explictly
4537 * reference this page... for
4538 * file I/O, this is done by
4539 * implementing an LRU on the inactive q
4543 PAGE_WAKEUP_DONE(m
);
4547 if (delayed_unlock
++ > UPL_DELAYED_UNLOCK_LIMIT
) {
4549 * pageout_scan takes the vm_page_lock_queues first
4550 * then tries for the object lock... to avoid what
4551 * is effectively a lock inversion, we'll go to the
4552 * trouble of taking them in that same order... otherwise
4553 * if this object contains the majority of the pages resident
4554 * in the UBC (or a small set of large objects actively being
4555 * worked on contain the majority of the pages), we could
4556 * cause the pageout_scan thread to 'starve' in its attempt
4557 * to find pages to move to the free queue, since it has to
4558 * successfully acquire the object lock of any candidate page
4559 * before it can steal/clean it.
4561 vm_object_unlock(shadow_object
);
4562 mutex_yield(&vm_page_queue_lock
);
4564 for (j
= 0; ; j
++) {
4565 if (vm_object_lock_try(shadow_object
))
4567 vm_page_unlock_queues();
4569 vm_page_lock_queues();
4573 target_offset
+= PAGE_SIZE_64
;
4574 xfer_size
-= PAGE_SIZE
;
4578 vm_page_unlock_queues();
4582 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4584 } else if (upl
->flags
& UPL_LITE
) {
4588 pg_num
= upl
->size
/PAGE_SIZE
;
4589 pg_num
= (pg_num
+ 31) >> 5;
4592 for (i
= 0; i
< pg_num
; i
++) {
4593 if (lite_list
[i
] != 0) {
4599 if (queue_empty(&upl
->map_object
->memq
))
4602 if (occupied
== 0) {
4603 if (upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
)
4606 if (object
== shadow_object
) {
4608 * this is not a paging object
4609 * so we need to drop the paging reference
4610 * that was taken when we created the UPL
4611 * against this object
4613 vm_object_paging_end(shadow_object
);
4616 * we dontated the paging reference to
4617 * the map object... vm_pageout_object_terminate
4618 * will drop this reference
4622 vm_object_unlock(shadow_object
);
4623 if (object
!= shadow_object
)
4624 vm_object_unlock(object
);
4627 return KERN_SUCCESS
;
4638 return upl_abort_range(upl
, 0, upl
->size
, error
, &empty
);
4642 /* an option on commit should be wire */
4646 upl_page_info_t
*page_list
,
4647 mach_msg_type_number_t count
)
4651 return upl_commit_range(upl
, 0, upl
->size
, 0, page_list
, count
, &empty
);
4656 vm_object_iopl_request(
4658 vm_object_offset_t offset
,
4661 upl_page_info_array_t user_page_list
,
4662 unsigned int *page_list_count
,
4666 vm_object_offset_t dst_offset
;
4667 upl_size_t xfer_size
;
4670 wpl_array_t lite_list
= NULL
;
4671 int delayed_unlock
= 0;
4672 int no_zero_fill
= FALSE
;
4676 struct vm_object_fault_info fault_info
;
4679 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
4681 * For forward compatibility's sake,
4682 * reject any unknown flag.
4684 return KERN_INVALID_VALUE
;
4686 if (vm_lopage_poolsize
== 0)
4687 cntrl_flags
&= ~UPL_NEED_32BIT_ADDR
;
4689 if (cntrl_flags
& UPL_NEED_32BIT_ADDR
) {
4690 if ( (cntrl_flags
& (UPL_SET_IO_WIRE
| UPL_SET_LITE
)) != (UPL_SET_IO_WIRE
| UPL_SET_LITE
))
4691 return KERN_INVALID_VALUE
;
4693 if (object
->phys_contiguous
) {
4694 if ((offset
+ object
->shadow_offset
) >= (vm_object_offset_t
)max_valid_dma_address
)
4695 return KERN_INVALID_ADDRESS
;
4697 if (((offset
+ object
->shadow_offset
) + size
) >= (vm_object_offset_t
)max_valid_dma_address
)
4698 return KERN_INVALID_ADDRESS
;
4702 if (cntrl_flags
& UPL_ENCRYPT
) {
4705 * The paging path doesn't use this interface,
4706 * so we don't support the UPL_ENCRYPT flag
4707 * here. We won't encrypt the pages.
4709 assert(! (cntrl_flags
& UPL_ENCRYPT
));
4711 if (cntrl_flags
& UPL_NOZEROFILL
)
4712 no_zero_fill
= TRUE
;
4714 if (cntrl_flags
& UPL_COPYOUT_FROM
)
4715 prot
= VM_PROT_READ
;
4717 prot
= VM_PROT_READ
| VM_PROT_WRITE
;
4719 if (((size
/page_size
) > MAX_UPL_SIZE
) && !object
->phys_contiguous
)
4720 size
= MAX_UPL_SIZE
* page_size
;
4722 if (cntrl_flags
& UPL_SET_INTERNAL
) {
4723 if (page_list_count
!= NULL
)
4724 *page_list_count
= MAX_UPL_SIZE
;
4726 if (((cntrl_flags
& UPL_SET_INTERNAL
) && !(object
->phys_contiguous
)) &&
4727 ((page_list_count
!= NULL
) && (*page_list_count
!= 0) && *page_list_count
< (size
/page_size
)))
4728 return KERN_INVALID_ARGUMENT
;
4730 if ((!object
->internal
) && (object
->paging_offset
!= 0))
4731 panic("vm_object_iopl_request: external object with non-zero paging offset\n");
4734 if (object
->phys_contiguous
)
4739 if (cntrl_flags
& UPL_SET_INTERNAL
) {
4740 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
, UPL_IO_WIRE
, psize
);
4742 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
4743 lite_list
= (wpl_array_t
) (((uintptr_t)user_page_list
) +
4744 ((psize
/ PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4746 upl
= upl_create(UPL_CREATE_LITE
, UPL_IO_WIRE
, psize
);
4748 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
4751 user_page_list
[0].device
= FALSE
;
4754 upl
->map_object
= object
;
4757 vm_object_lock(object
);
4758 vm_object_paging_begin(object
);
4760 * paging in progress also protects the paging_offset
4762 upl
->offset
= offset
+ object
->paging_offset
;
4764 if (object
->phys_contiguous
) {
4766 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
4767 #endif /* UPL_DEBUG */
4769 vm_object_unlock(object
);
4772 * don't need any shadow mappings for this one
4773 * since it is already I/O memory
4775 upl
->flags
|= UPL_DEVICE_MEMORY
;
4777 upl
->highest_page
= (offset
+ object
->shadow_offset
+ size
- 1)>>PAGE_SHIFT
;
4779 if (user_page_list
) {
4780 user_page_list
[0].phys_addr
= (offset
+ object
->shadow_offset
)>>PAGE_SHIFT
;
4781 user_page_list
[0].device
= TRUE
;
4783 if (page_list_count
!= NULL
) {
4784 if (upl
->flags
& UPL_INTERNAL
)
4785 *page_list_count
= 0;
4787 *page_list_count
= 1;
4789 return KERN_SUCCESS
;
4792 * Protect user space from future COW operations
4794 object
->true_share
= TRUE
;
4796 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
4797 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
4800 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
4801 #endif /* UPL_DEBUG */
4803 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
4805 * The user requested that access to the pages in this URL
4806 * be blocked until the UPL is commited or aborted.
4808 upl
->flags
|= UPL_ACCESS_BLOCKED
;
4813 dst_offset
= offset
;
4815 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
4816 fault_info
.user_tag
= 0;
4817 fault_info
.lo_offset
= offset
;
4818 fault_info
.hi_offset
= offset
+ xfer_size
;
4819 fault_info
.no_cache
= FALSE
;
4822 vm_fault_return_t result
;
4825 dst_page
= vm_page_lookup(object
, dst_offset
);
4829 * If the page is encrypted, we need to decrypt it,
4830 * so force a soft page fault.
4832 if ((dst_page
== VM_PAGE_NULL
) || (dst_page
->busy
) ||
4833 (dst_page
->encrypted
) ||
4834 (dst_page
->unusual
&& (dst_page
->error
||
4835 dst_page
->restart
||
4837 dst_page
->fictitious
))) {
4841 kern_return_t error_code
;
4844 if (delayed_unlock
) {
4846 vm_page_unlock_queues();
4848 if (cntrl_flags
& UPL_SET_INTERRUPTIBLE
)
4849 interruptible
= THREAD_ABORTSAFE
;
4851 interruptible
= THREAD_UNINT
;
4853 fault_info
.interruptible
= interruptible
;
4854 fault_info
.cluster_size
= xfer_size
;
4856 result
= vm_fault_page(object
, dst_offset
,
4857 prot
| VM_PROT_WRITE
, FALSE
,
4858 &prot
, &dst_page
, &top_page
,
4860 &error_code
, no_zero_fill
,
4861 FALSE
, &fault_info
);
4865 case VM_FAULT_SUCCESS
:
4867 PAGE_WAKEUP_DONE(dst_page
);
4869 * Release paging references and
4870 * top-level placeholder page, if any.
4872 if (top_page
!= VM_PAGE_NULL
) {
4873 vm_object_t local_object
;
4875 local_object
= top_page
->object
;
4877 if (top_page
->object
!= dst_page
->object
) {
4878 vm_object_lock(local_object
);
4879 VM_PAGE_FREE(top_page
);
4880 vm_object_paging_end(local_object
);
4881 vm_object_unlock(local_object
);
4883 VM_PAGE_FREE(top_page
);
4884 vm_object_paging_end(local_object
);
4889 case VM_FAULT_RETRY
:
4890 vm_object_lock(object
);
4891 vm_object_paging_begin(object
);
4894 case VM_FAULT_FICTITIOUS_SHORTAGE
:
4895 vm_page_more_fictitious();
4897 vm_object_lock(object
);
4898 vm_object_paging_begin(object
);
4901 case VM_FAULT_MEMORY_SHORTAGE
:
4902 if (vm_page_wait(interruptible
)) {
4903 vm_object_lock(object
);
4904 vm_object_paging_begin(object
);
4909 case VM_FAULT_INTERRUPTED
:
4910 error_code
= MACH_SEND_INTERRUPTED
;
4911 case VM_FAULT_MEMORY_ERROR
:
4912 ret
= (error_code
? error_code
: KERN_MEMORY_ERROR
);
4914 vm_object_lock(object
);
4915 vm_object_paging_begin(object
);
4918 } while (result
!= VM_FAULT_SUCCESS
);
4921 if ( (cntrl_flags
& UPL_NEED_32BIT_ADDR
) &&
4922 dst_page
->phys_page
>= (max_valid_dma_address
>> PAGE_SHIFT
) ) {
4927 * support devices that can't DMA above 32 bits
4928 * by substituting pages from a pool of low address
4929 * memory for any pages we find above the 4G mark
4930 * can't substitute if the page is already wired because
4931 * we don't know whether that physical address has been
4932 * handed out to some other 64 bit capable DMA device to use
4934 if (dst_page
->wire_count
) {
4935 ret
= KERN_PROTECTION_FAILURE
;
4938 if (delayed_unlock
) {
4940 vm_page_unlock_queues();
4942 low_page
= vm_page_grablo();
4944 if (low_page
== VM_PAGE_NULL
) {
4945 ret
= KERN_RESOURCE_SHORTAGE
;
4949 * from here until the vm_page_replace completes
4950 * we musn't drop the object lock... we don't
4951 * want anyone refaulting this page in and using
4952 * it after we disconnect it... we want the fault
4953 * to find the new page being substituted.
4955 if (dst_page
->pmapped
)
4956 refmod
= pmap_disconnect(dst_page
->phys_page
);
4959 vm_page_copy(dst_page
, low_page
);
4961 low_page
->reference
= dst_page
->reference
;
4962 low_page
->dirty
= dst_page
->dirty
;
4964 if (refmod
& VM_MEM_REFERENCED
)
4965 low_page
->reference
= TRUE
;
4966 if (refmod
& VM_MEM_MODIFIED
)
4967 low_page
->dirty
= TRUE
;
4969 vm_page_lock_queues();
4970 vm_page_replace(low_page
, object
, dst_offset
);
4972 * keep the queue lock since we're going to
4973 * need it immediately
4977 dst_page
= low_page
;
4979 * vm_page_grablo returned the page marked
4980 * BUSY... we don't need a PAGE_WAKEUP_DONE
4981 * here, because we've never dropped the object lock
4983 dst_page
->busy
= FALSE
;
4985 if (delayed_unlock
== 0)
4986 vm_page_lock_queues();
4988 vm_page_wire(dst_page
);
4990 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
4992 * Mark the page "busy" to block any future page fault
4993 * on this page. We'll also remove the mapping
4994 * of all these pages before leaving this routine.
4996 assert(!dst_page
->fictitious
);
4997 dst_page
->busy
= TRUE
;
4999 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
5000 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
5003 * expect the page to be used
5004 * page queues lock must be held to set 'reference'
5006 dst_page
->reference
= TRUE
;
5008 if (!(cntrl_flags
& UPL_COPYOUT_FROM
))
5009 dst_page
->dirty
= TRUE
;
5011 if (dst_page
->phys_page
> upl
->highest_page
)
5012 upl
->highest_page
= dst_page
->phys_page
;
5014 if (user_page_list
) {
5015 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
5016 user_page_list
[entry
].dirty
= dst_page
->dirty
;
5017 user_page_list
[entry
].pageout
= dst_page
->pageout
;
5018 user_page_list
[entry
].absent
= dst_page
->absent
;
5019 user_page_list
[entry
].precious
= dst_page
->precious
;
5021 if (dst_page
->clustered
== TRUE
)
5022 user_page_list
[entry
].speculative
= dst_page
->speculative
;
5024 user_page_list
[entry
].speculative
= FALSE
;
5027 * someone is explicitly grabbing this page...
5028 * update clustered and speculative state
5031 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
5033 if (delayed_unlock
++ > UPL_DELAYED_UNLOCK_LIMIT
) {
5034 mutex_yield(&vm_page_queue_lock
);
5038 dst_offset
+= PAGE_SIZE_64
;
5039 xfer_size
-= PAGE_SIZE
;
5042 vm_page_unlock_queues();
5044 if (page_list_count
!= NULL
) {
5045 if (upl
->flags
& UPL_INTERNAL
)
5046 *page_list_count
= 0;
5047 else if (*page_list_count
> entry
)
5048 *page_list_count
= entry
;
5050 vm_object_unlock(object
);
5052 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
5054 * We've marked all the pages "busy" so that future
5055 * page faults will block.
5056 * Now remove the mapping for these pages, so that they
5057 * can't be accessed without causing a page fault.
5059 vm_object_pmap_protect(object
, offset
, (vm_object_size_t
)size
,
5060 PMAP_NULL
, 0, VM_PROT_NONE
);
5062 return KERN_SUCCESS
;
5066 vm_page_unlock_queues();
5068 for (; offset
< dst_offset
; offset
+= PAGE_SIZE
) {
5069 dst_page
= vm_page_lookup(object
, offset
);
5071 if (dst_page
== VM_PAGE_NULL
)
5072 panic("vm_object_iopl_request: Wired pages missing. \n");
5074 vm_page_lockspin_queues();
5075 vm_page_unwire(dst_page
);
5076 vm_page_unlock_queues();
5078 VM_STAT_INCR(reactivations
);
5080 vm_object_paging_end(object
);
5081 vm_object_unlock(object
);
5092 kern_return_t retval
;
5093 boolean_t upls_locked
;
5094 vm_object_t object1
, object2
;
5096 if (upl1
== UPL_NULL
|| upl2
== UPL_NULL
|| upl1
== upl2
) {
5097 return KERN_INVALID_ARGUMENT
;
5100 upls_locked
= FALSE
;
5103 * Since we need to lock both UPLs at the same time,
5104 * avoid deadlocks by always taking locks in the same order.
5113 upls_locked
= TRUE
; /* the UPLs will need to be unlocked */
5115 object1
= upl1
->map_object
;
5116 object2
= upl2
->map_object
;
5118 if (upl1
->offset
!= 0 || upl2
->offset
!= 0 ||
5119 upl1
->size
!= upl2
->size
) {
5121 * We deal only with full objects, not subsets.
5122 * That's because we exchange the entire backing store info
5123 * for the objects: pager, resident pages, etc... We can't do
5126 retval
= KERN_INVALID_VALUE
;
5131 * Tranpose the VM objects' backing store.
5133 retval
= vm_object_transpose(object1
, object2
,
5134 (vm_object_size_t
) upl1
->size
);
5136 if (retval
== KERN_SUCCESS
) {
5138 * Make each UPL point to the correct VM object, i.e. the
5139 * object holding the pages that the UPL refers to...
5142 queue_remove(&object1
->uplq
, upl1
, upl_t
, uplq
);
5143 queue_remove(&object2
->uplq
, upl2
, upl_t
, uplq
);
5145 upl1
->map_object
= object2
;
5146 upl2
->map_object
= object1
;
5148 queue_enter(&object1
->uplq
, upl2
, upl_t
, uplq
);
5149 queue_enter(&object2
->uplq
, upl1
, upl_t
, uplq
);
5160 upls_locked
= FALSE
;
5169 * Rationale: the user might have some encrypted data on disk (via
5170 * FileVault or any other mechanism). That data is then decrypted in
5171 * memory, which is safe as long as the machine is secure. But that
5172 * decrypted data in memory could be paged out to disk by the default
5173 * pager. The data would then be stored on disk in clear (not encrypted)
5174 * and it could be accessed by anyone who gets physical access to the
5175 * disk (if the laptop or the disk gets stolen for example). This weakens
5176 * the security offered by FileVault.
5178 * Solution: the default pager will optionally request that all the
5179 * pages it gathers for pageout be encrypted, via the UPL interfaces,
5180 * before it sends this UPL to disk via the vnode_pageout() path.
5184 * To avoid disrupting the VM LRU algorithms, we want to keep the
5185 * clean-in-place mechanisms, which allow us to send some extra pages to
5186 * swap (clustering) without actually removing them from the user's
5187 * address space. We don't want the user to unknowingly access encrypted
5188 * data, so we have to actually remove the encrypted pages from the page
5189 * table. When the user accesses the data, the hardware will fail to
5190 * locate the virtual page in its page table and will trigger a page
5191 * fault. We can then decrypt the page and enter it in the page table
5192 * again. Whenever we allow the user to access the contents of a page,
5193 * we have to make sure it's not encrypted.
5199 * Reserve of virtual addresses in the kernel address space.
5200 * We need to map the physical pages in the kernel, so that we
5201 * can call the encryption/decryption routines with a kernel
5202 * virtual address. We keep this pool of pre-allocated kernel
5203 * virtual addresses so that we don't have to scan the kernel's
5204 * virtaul address space each time we need to encrypt or decrypt
5206 * It would be nice to be able to encrypt and decrypt in physical
5207 * mode but that might not always be more efficient...
5209 decl_simple_lock_data(,vm_paging_lock
)
5210 #define VM_PAGING_NUM_PAGES 64
5211 vm_map_offset_t vm_paging_base_address
= 0;
5212 boolean_t vm_paging_page_inuse
[VM_PAGING_NUM_PAGES
] = { FALSE
, };
5213 int vm_paging_max_index
= 0;
5214 int vm_paging_page_waiter
= 0;
5215 int vm_paging_page_waiter_total
= 0;
5216 unsigned long vm_paging_no_kernel_page
= 0;
5217 unsigned long vm_paging_objects_mapped
= 0;
5218 unsigned long vm_paging_pages_mapped
= 0;
5219 unsigned long vm_paging_objects_mapped_slow
= 0;
5220 unsigned long vm_paging_pages_mapped_slow
= 0;
5223 vm_paging_map_init(void)
5226 vm_map_offset_t page_map_offset
;
5227 vm_map_entry_t map_entry
;
5229 assert(vm_paging_base_address
== 0);
5232 * Initialize our pool of pre-allocated kernel
5233 * virtual addresses.
5235 page_map_offset
= 0;
5236 kr
= vm_map_find_space(kernel_map
,
5238 VM_PAGING_NUM_PAGES
* PAGE_SIZE
,
5242 if (kr
!= KERN_SUCCESS
) {
5243 panic("vm_paging_map_init: kernel_map full\n");
5245 map_entry
->object
.vm_object
= kernel_object
;
5247 page_map_offset
- VM_MIN_KERNEL_ADDRESS
;
5248 vm_object_reference(kernel_object
);
5249 vm_map_unlock(kernel_map
);
5251 assert(vm_paging_base_address
== 0);
5252 vm_paging_base_address
= page_map_offset
;
5257 * vm_paging_map_object:
5258 * Maps part of a VM object's pages in the kernel
5259 * virtual address space, using the pre-allocated
5260 * kernel virtual addresses, if possible.
5262 * The VM object is locked. This lock will get
5263 * dropped and re-acquired though, so the caller
5264 * must make sure the VM object is kept alive
5265 * (by holding a VM map that has a reference
5266 * on it, for example, or taking an extra reference).
5267 * The page should also be kept busy to prevent
5268 * it from being reclaimed.
5271 vm_paging_map_object(
5272 vm_map_offset_t
*address
,
5275 vm_object_offset_t offset
,
5276 vm_map_size_t
*size
,
5277 boolean_t can_unlock_object
)
5280 vm_map_offset_t page_map_offset
;
5281 vm_map_size_t map_size
;
5282 vm_object_offset_t object_offset
;
5286 if (page
!= VM_PAGE_NULL
&& *size
== PAGE_SIZE
) {
5289 * Use one of the pre-allocated kernel virtual addresses
5290 * and just enter the VM page in the kernel address space
5291 * at that virtual address.
5293 simple_lock(&vm_paging_lock
);
5296 * Try and find an available kernel virtual address
5297 * from our pre-allocated pool.
5299 page_map_offset
= 0;
5301 for (i
= 0; i
< VM_PAGING_NUM_PAGES
; i
++) {
5302 if (vm_paging_page_inuse
[i
] == FALSE
) {
5304 vm_paging_base_address
+
5309 if (page_map_offset
!= 0) {
5310 /* found a space to map our page ! */
5314 if (can_unlock_object
) {
5316 * If we can afford to unlock the VM object,
5317 * let's take the slow path now...
5322 * We can't afford to unlock the VM object, so
5323 * let's wait for a space to become available...
5325 vm_paging_page_waiter_total
++;
5326 vm_paging_page_waiter
++;
5327 thread_sleep_fast_usimple_lock(&vm_paging_page_waiter
,
5330 vm_paging_page_waiter
--;
5331 /* ... and try again */
5334 if (page_map_offset
!= 0) {
5336 * We found a kernel virtual address;
5337 * map the physical page to that virtual address.
5339 if (i
> vm_paging_max_index
) {
5340 vm_paging_max_index
= i
;
5342 vm_paging_page_inuse
[i
] = TRUE
;
5343 simple_unlock(&vm_paging_lock
);
5345 if (page
->pmapped
== FALSE
) {
5346 pmap_sync_page_data_phys(page
->phys_page
);
5348 page
->pmapped
= TRUE
;
5351 * Keep the VM object locked over the PMAP_ENTER
5352 * and the actual use of the page by the kernel,
5353 * or this pmap mapping might get undone by a
5354 * vm_object_pmap_protect() call...
5356 PMAP_ENTER(kernel_pmap
,
5360 ((int) page
->object
->wimg_bits
&
5363 vm_paging_objects_mapped
++;
5364 vm_paging_pages_mapped
++;
5365 *address
= page_map_offset
;
5367 /* all done and mapped, ready to use ! */
5368 return KERN_SUCCESS
;
5372 * We ran out of pre-allocated kernel virtual
5373 * addresses. Just map the page in the kernel
5374 * the slow and regular way.
5376 vm_paging_no_kernel_page
++;
5377 simple_unlock(&vm_paging_lock
);
5380 if (! can_unlock_object
) {
5381 return KERN_NOT_SUPPORTED
;
5384 object_offset
= vm_object_trunc_page(offset
);
5385 map_size
= vm_map_round_page(*size
);
5388 * Try and map the required range of the object
5392 vm_object_reference_locked(object
); /* for the map entry */
5393 vm_object_unlock(object
);
5395 kr
= vm_map_enter(kernel_map
,
5406 if (kr
!= KERN_SUCCESS
) {
5409 vm_object_deallocate(object
); /* for the map entry */
5410 vm_object_lock(object
);
5417 * Enter the mapped pages in the page table now.
5419 vm_object_lock(object
);
5421 * VM object must be kept locked from before PMAP_ENTER()
5422 * until after the kernel is done accessing the page(s).
5423 * Otherwise, the pmap mappings in the kernel could be
5424 * undone by a call to vm_object_pmap_protect().
5427 for (page_map_offset
= 0;
5429 map_size
-= PAGE_SIZE_64
, page_map_offset
+= PAGE_SIZE_64
) {
5430 unsigned int cache_attr
;
5432 page
= vm_page_lookup(object
, offset
+ page_map_offset
);
5433 if (page
== VM_PAGE_NULL
) {
5434 printf("vm_paging_map_object: no page !?");
5435 vm_object_unlock(object
);
5436 kr
= vm_map_remove(kernel_map
, *address
, *size
,
5438 assert(kr
== KERN_SUCCESS
);
5441 vm_object_lock(object
);
5442 return KERN_MEMORY_ERROR
;
5444 if (page
->pmapped
== FALSE
) {
5445 pmap_sync_page_data_phys(page
->phys_page
);
5447 page
->pmapped
= TRUE
;
5448 page
->wpmapped
= TRUE
;
5449 cache_attr
= ((unsigned int) object
->wimg_bits
) & VM_WIMG_MASK
;
5451 //assert(pmap_verify_free(page->phys_page));
5452 PMAP_ENTER(kernel_pmap
,
5453 *address
+ page_map_offset
,
5460 vm_paging_objects_mapped_slow
++;
5461 vm_paging_pages_mapped_slow
+= map_size
/ PAGE_SIZE_64
;
5463 return KERN_SUCCESS
;
5468 * vm_paging_unmap_object:
5469 * Unmaps part of a VM object's pages from the kernel
5470 * virtual address space.
5472 * The VM object is locked. This lock will get
5473 * dropped and re-acquired though.
5476 vm_paging_unmap_object(
5478 vm_map_offset_t start
,
5479 vm_map_offset_t end
)
5484 if ((vm_paging_base_address
== 0) ||
5485 (start
< vm_paging_base_address
) ||
5486 (end
> (vm_paging_base_address
5487 + (VM_PAGING_NUM_PAGES
* PAGE_SIZE
)))) {
5489 * We didn't use our pre-allocated pool of
5490 * kernel virtual address. Deallocate the
5493 if (object
!= VM_OBJECT_NULL
) {
5494 vm_object_unlock(object
);
5496 kr
= vm_map_remove(kernel_map
, start
, end
, VM_MAP_NO_FLAGS
);
5497 if (object
!= VM_OBJECT_NULL
) {
5498 vm_object_lock(object
);
5500 assert(kr
== KERN_SUCCESS
);
5503 * We used a kernel virtual address from our
5504 * pre-allocated pool. Put it back in the pool
5507 assert(end
- start
== PAGE_SIZE
);
5508 i
= (start
- vm_paging_base_address
) >> PAGE_SHIFT
;
5510 /* undo the pmap mapping */
5511 pmap_remove(kernel_pmap
, start
, end
);
5513 simple_lock(&vm_paging_lock
);
5514 vm_paging_page_inuse
[i
] = FALSE
;
5515 if (vm_paging_page_waiter
) {
5516 thread_wakeup(&vm_paging_page_waiter
);
5518 simple_unlock(&vm_paging_lock
);
5525 * "iv" is the "initial vector". Ideally, we want to
5526 * have a different one for each page we encrypt, so that
5527 * crackers can't find encryption patterns too easily.
5529 #define SWAP_CRYPT_AES_KEY_SIZE 128 /* XXX 192 and 256 don't work ! */
5530 boolean_t swap_crypt_ctx_initialized
= FALSE
;
5531 aes_32t swap_crypt_key
[8]; /* big enough for a 256 key */
5532 aes_ctx swap_crypt_ctx
;
5533 const unsigned char swap_crypt_null_iv
[AES_BLOCK_SIZE
] = {0xa, };
5536 boolean_t swap_crypt_ctx_tested
= FALSE
;
5537 unsigned char swap_crypt_test_page_ref
[4096] __attribute__((aligned(4096)));
5538 unsigned char swap_crypt_test_page_encrypt
[4096] __attribute__((aligned(4096)));
5539 unsigned char swap_crypt_test_page_decrypt
[4096] __attribute__((aligned(4096)));
5542 extern u_long
random(void);
5545 * Initialize the encryption context: key and key size.
5547 void swap_crypt_ctx_initialize(void); /* forward */
5549 swap_crypt_ctx_initialize(void)
5554 * No need for locking to protect swap_crypt_ctx_initialized
5555 * because the first use of encryption will come from the
5556 * pageout thread (we won't pagein before there's been a pageout)
5557 * and there's only one pageout thread.
5559 if (swap_crypt_ctx_initialized
== FALSE
) {
5561 i
< (sizeof (swap_crypt_key
) /
5562 sizeof (swap_crypt_key
[0]));
5564 swap_crypt_key
[i
] = random();
5566 aes_encrypt_key((const unsigned char *) swap_crypt_key
,
5567 SWAP_CRYPT_AES_KEY_SIZE
,
5568 &swap_crypt_ctx
.encrypt
);
5569 aes_decrypt_key((const unsigned char *) swap_crypt_key
,
5570 SWAP_CRYPT_AES_KEY_SIZE
,
5571 &swap_crypt_ctx
.decrypt
);
5572 swap_crypt_ctx_initialized
= TRUE
;
5577 * Validate the encryption algorithms.
5579 if (swap_crypt_ctx_tested
== FALSE
) {
5581 for (i
= 0; i
< 4096; i
++) {
5582 swap_crypt_test_page_ref
[i
] = (char) i
;
5585 aes_encrypt_cbc(swap_crypt_test_page_ref
,
5587 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5588 swap_crypt_test_page_encrypt
,
5589 &swap_crypt_ctx
.encrypt
);
5591 aes_decrypt_cbc(swap_crypt_test_page_encrypt
,
5593 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5594 swap_crypt_test_page_decrypt
,
5595 &swap_crypt_ctx
.decrypt
);
5596 /* compare result with original */
5597 for (i
= 0; i
< 4096; i
++) {
5598 if (swap_crypt_test_page_decrypt
[i
] !=
5599 swap_crypt_test_page_ref
[i
]) {
5600 panic("encryption test failed");
5605 aes_encrypt_cbc(swap_crypt_test_page_decrypt
,
5607 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5608 swap_crypt_test_page_decrypt
,
5609 &swap_crypt_ctx
.encrypt
);
5610 /* decrypt in place */
5611 aes_decrypt_cbc(swap_crypt_test_page_decrypt
,
5613 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5614 swap_crypt_test_page_decrypt
,
5615 &swap_crypt_ctx
.decrypt
);
5616 for (i
= 0; i
< 4096; i
++) {
5617 if (swap_crypt_test_page_decrypt
[i
] !=
5618 swap_crypt_test_page_ref
[i
]) {
5619 panic("in place encryption test failed");
5623 swap_crypt_ctx_tested
= TRUE
;
5631 * Encrypt the given page, for secure paging.
5632 * The page might already be mapped at kernel virtual
5633 * address "kernel_mapping_offset". Otherwise, we need
5637 * The page's object is locked, but this lock will be released
5639 * The page is busy and not accessible by users (not entered in any pmap).
5644 vm_map_offset_t kernel_mapping_offset
)
5647 vm_map_size_t kernel_mapping_size
;
5648 vm_offset_t kernel_vaddr
;
5650 unsigned char aes_iv
[AES_BLOCK_SIZE
];
5652 memory_object_t pager_object
;
5653 vm_object_offset_t paging_offset
;
5657 if (! vm_pages_encrypted
) {
5658 vm_pages_encrypted
= TRUE
;
5662 assert(page
->dirty
|| page
->precious
);
5664 if (page
->encrypted
) {
5666 * Already encrypted: no need to do it again.
5668 vm_page_encrypt_already_encrypted_counter
++;
5671 ASSERT_PAGE_DECRYPTED(page
);
5674 * Take a paging-in-progress reference to keep the object
5675 * alive even if we have to unlock it (in vm_paging_map_object()
5678 vm_object_paging_begin(page
->object
);
5680 if (kernel_mapping_offset
== 0) {
5682 * The page hasn't already been mapped in kernel space
5683 * by the caller. Map it now, so that we can access
5684 * its contents and encrypt them.
5686 kernel_mapping_size
= PAGE_SIZE
;
5687 kr
= vm_paging_map_object(&kernel_mapping_offset
,
5691 &kernel_mapping_size
,
5693 if (kr
!= KERN_SUCCESS
) {
5694 panic("vm_page_encrypt: "
5695 "could not map page in kernel: 0x%x\n",
5699 kernel_mapping_size
= 0;
5701 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
5703 if (swap_crypt_ctx_initialized
== FALSE
) {
5704 swap_crypt_ctx_initialize();
5706 assert(swap_crypt_ctx_initialized
);
5709 * Prepare an "initial vector" for the encryption.
5710 * We use the "pager" and the "paging_offset" for that
5711 * page to obfuscate the encrypted data a bit more and
5712 * prevent crackers from finding patterns that they could
5713 * use to break the key.
5715 bzero(&encrypt_iv
.aes_iv
[0], sizeof (encrypt_iv
.aes_iv
));
5716 encrypt_iv
.vm
.pager_object
= page
->object
->pager
;
5717 encrypt_iv
.vm
.paging_offset
=
5718 page
->object
->paging_offset
+ page
->offset
;
5720 /* encrypt the "initial vector" */
5721 aes_encrypt_cbc((const unsigned char *) &encrypt_iv
.aes_iv
[0],
5724 &encrypt_iv
.aes_iv
[0],
5725 &swap_crypt_ctx
.encrypt
);
5730 aes_encrypt_cbc((const unsigned char *) kernel_vaddr
,
5731 &encrypt_iv
.aes_iv
[0],
5732 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5733 (unsigned char *) kernel_vaddr
,
5734 &swap_crypt_ctx
.encrypt
);
5736 vm_page_encrypt_counter
++;
5739 * Unmap the page from the kernel's address space,
5740 * if we had to map it ourselves. Otherwise, let
5741 * the caller undo the mapping if needed.
5743 if (kernel_mapping_size
!= 0) {
5744 vm_paging_unmap_object(page
->object
,
5745 kernel_mapping_offset
,
5746 kernel_mapping_offset
+ kernel_mapping_size
);
5750 * Clear the "reference" and "modified" bits.
5751 * This should clean up any impact the encryption had
5753 * The page was kept busy and disconnected from all pmaps,
5754 * so it can't have been referenced or modified from user
5756 * The software bits will be reset later after the I/O
5757 * has completed (in upl_commit_range()).
5759 pmap_clear_refmod(page
->phys_page
, VM_MEM_REFERENCED
| VM_MEM_MODIFIED
);
5761 page
->encrypted
= TRUE
;
5763 vm_object_paging_end(page
->object
);
5769 * Decrypt the given page.
5770 * The page might already be mapped at kernel virtual
5771 * address "kernel_mapping_offset". Otherwise, we need
5775 * The page's VM object is locked but will be unlocked and relocked.
5776 * The page is busy and not accessible by users (not entered in any pmap).
5781 vm_map_offset_t kernel_mapping_offset
)
5784 vm_map_size_t kernel_mapping_size
;
5785 vm_offset_t kernel_vaddr
;
5787 unsigned char aes_iv
[AES_BLOCK_SIZE
];
5789 memory_object_t pager_object
;
5790 vm_object_offset_t paging_offset
;
5795 assert(page
->encrypted
);
5798 * Take a paging-in-progress reference to keep the object
5799 * alive even if we have to unlock it (in vm_paging_map_object()
5802 vm_object_paging_begin(page
->object
);
5804 if (kernel_mapping_offset
== 0) {
5806 * The page hasn't already been mapped in kernel space
5807 * by the caller. Map it now, so that we can access
5808 * its contents and decrypt them.
5810 kernel_mapping_size
= PAGE_SIZE
;
5811 kr
= vm_paging_map_object(&kernel_mapping_offset
,
5815 &kernel_mapping_size
,
5817 if (kr
!= KERN_SUCCESS
) {
5818 panic("vm_page_decrypt: "
5819 "could not map page in kernel: 0x%x\n",
5823 kernel_mapping_size
= 0;
5825 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
5827 assert(swap_crypt_ctx_initialized
);
5830 * Prepare an "initial vector" for the decryption.
5831 * It has to be the same as the "initial vector" we
5832 * used to encrypt that page.
5834 bzero(&decrypt_iv
.aes_iv
[0], sizeof (decrypt_iv
.aes_iv
));
5835 decrypt_iv
.vm
.pager_object
= page
->object
->pager
;
5836 decrypt_iv
.vm
.paging_offset
=
5837 page
->object
->paging_offset
+ page
->offset
;
5839 /* encrypt the "initial vector" */
5840 aes_encrypt_cbc((const unsigned char *) &decrypt_iv
.aes_iv
[0],
5843 &decrypt_iv
.aes_iv
[0],
5844 &swap_crypt_ctx
.encrypt
);
5849 aes_decrypt_cbc((const unsigned char *) kernel_vaddr
,
5850 &decrypt_iv
.aes_iv
[0],
5851 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5852 (unsigned char *) kernel_vaddr
,
5853 &swap_crypt_ctx
.decrypt
);
5854 vm_page_decrypt_counter
++;
5857 * Unmap the page from the kernel's address space,
5858 * if we had to map it ourselves. Otherwise, let
5859 * the caller undo the mapping if needed.
5861 if (kernel_mapping_size
!= 0) {
5862 vm_paging_unmap_object(page
->object
,
5864 kernel_vaddr
+ PAGE_SIZE
);
5868 * After decryption, the page is actually clean.
5869 * It was encrypted as part of paging, which "cleans"
5870 * the "dirty" pages.
5871 * Noone could access it after it was encrypted
5872 * and the decryption doesn't count.
5874 page
->dirty
= FALSE
;
5875 if (page
->cs_validated
&& !page
->cs_tainted
) {
5878 * This page is no longer dirty
5879 * but could have been modified,
5880 * so it will need to be
5883 page
->cs_validated
= FALSE
;
5884 vm_cs_validated_resets
++;
5886 pmap_clear_refmod(page
->phys_page
, VM_MEM_MODIFIED
| VM_MEM_REFERENCED
);
5888 page
->encrypted
= FALSE
;
5891 * We've just modified the page's contents via the data cache and part
5892 * of the new contents might still be in the cache and not yet in RAM.
5893 * Since the page is now available and might get gathered in a UPL to
5894 * be part of a DMA transfer from a driver that expects the memory to
5895 * be coherent at this point, we have to flush the data cache.
5897 pmap_sync_page_attributes_phys(page
->phys_page
);
5899 * Since the page is not mapped yet, some code might assume that it
5900 * doesn't need to invalidate the instruction cache when writing to
5901 * that page. That code relies on "pmapped" being FALSE, so that the
5902 * caches get synchronized when the page is first mapped.
5904 assert(pmap_verify_free(page
->phys_page
));
5905 page
->pmapped
= FALSE
;
5906 page
->wpmapped
= FALSE
;
5908 vm_object_paging_end(page
->object
);
5911 unsigned long upl_encrypt_upls
= 0;
5912 unsigned long upl_encrypt_pages
= 0;
5918 * Encrypts all the pages in the UPL, within the specified range.
5924 upl_offset_t crypt_offset
,
5925 upl_size_t crypt_size
)
5927 upl_size_t upl_size
;
5928 upl_offset_t upl_offset
;
5929 vm_object_t upl_object
;
5931 vm_object_t shadow_object
;
5932 vm_object_offset_t shadow_offset
;
5933 vm_object_offset_t paging_offset
;
5934 vm_object_offset_t base_offset
;
5937 upl_encrypt_pages
+= crypt_size
/ PAGE_SIZE
;
5939 upl_object
= upl
->map_object
;
5940 upl_offset
= upl
->offset
;
5941 upl_size
= upl
->size
;
5943 vm_object_lock(upl_object
);
5946 * Find the VM object that contains the actual pages.
5948 if (upl_object
->pageout
) {
5949 shadow_object
= upl_object
->shadow
;
5951 * The offset in the shadow object is actually also
5952 * accounted for in upl->offset. It possibly shouldn't be
5953 * this way, but for now don't account for it twice.
5956 assert(upl_object
->paging_offset
== 0); /* XXX ? */
5957 vm_object_lock(shadow_object
);
5959 shadow_object
= upl_object
;
5963 paging_offset
= shadow_object
->paging_offset
;
5964 vm_object_paging_begin(shadow_object
);
5966 if (shadow_object
!= upl_object
)
5967 vm_object_unlock(upl_object
);
5970 base_offset
= shadow_offset
;
5971 base_offset
+= upl_offset
;
5972 base_offset
+= crypt_offset
;
5973 base_offset
-= paging_offset
;
5975 assert(crypt_offset
+ crypt_size
<= upl_size
);
5977 for (upl_offset
= 0;
5978 upl_offset
< crypt_size
;
5979 upl_offset
+= PAGE_SIZE
) {
5980 page
= vm_page_lookup(shadow_object
,
5981 base_offset
+ upl_offset
);
5982 if (page
== VM_PAGE_NULL
) {
5983 panic("upl_encrypt: "
5984 "no page for (obj=%p,off=%lld+%d)!\n",
5990 * Disconnect the page from all pmaps, so that nobody can
5991 * access it while it's encrypted. After that point, all
5992 * accesses to this page will cause a page fault and block
5993 * while the page is busy being encrypted. After the
5994 * encryption completes, any access will cause a
5995 * page fault and the page gets decrypted at that time.
5997 pmap_disconnect(page
->phys_page
);
5998 vm_page_encrypt(page
, 0);
6000 if (shadow_object
== vm_pageout_scan_wants_object
) {
6002 * Give vm_pageout_scan() a chance to convert more
6003 * pages from "clean-in-place" to "clean-and-free",
6004 * if it's interested in the same pages we selected
6007 vm_object_unlock(shadow_object
);
6008 vm_object_lock(shadow_object
);
6012 vm_object_paging_end(shadow_object
);
6013 vm_object_unlock(shadow_object
);
6020 __unused upl_offset_t crypt_offset
,
6021 __unused upl_size_t crypt_size
)
6027 __unused vm_page_t page
,
6028 __unused vm_map_offset_t kernel_mapping_offset
)
6034 __unused vm_page_t page
,
6035 __unused vm_map_offset_t kernel_mapping_offset
)
6042 upl_get_internal_pagelist_offset(void)
6044 return sizeof(struct upl
);
6053 upl
->flags
|= UPL_CLEAR_DIRTY
;
6055 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
6062 boolean_t
upl_device_page(upl_page_info_t
*upl
)
6064 return(UPL_DEVICE_PAGE(upl
));
6066 boolean_t
upl_page_present(upl_page_info_t
*upl
, int index
)
6068 return(UPL_PAGE_PRESENT(upl
, index
));
6070 boolean_t
upl_speculative_page(upl_page_info_t
*upl
, int index
)
6072 return(UPL_SPECULATIVE_PAGE(upl
, index
));
6074 boolean_t
upl_dirty_page(upl_page_info_t
*upl
, int index
)
6076 return(UPL_DIRTY_PAGE(upl
, index
));
6078 boolean_t
upl_valid_page(upl_page_info_t
*upl
, int index
)
6080 return(UPL_VALID_PAGE(upl
, index
));
6082 ppnum_t
upl_phys_page(upl_page_info_t
*upl
, int index
)
6084 return(UPL_PHYS_PAGE(upl
, index
));
6089 vm_countdirtypages(void)
6101 vm_page_lock_queues();
6102 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
6104 if (m
==(vm_page_t
)0) break;
6106 if(m
->dirty
) dpages
++;
6107 if(m
->pageout
) pgopages
++;
6108 if(m
->precious
) precpages
++;
6110 assert(m
->object
!= kernel_object
);
6111 m
= (vm_page_t
) queue_next(&m
->pageq
);
6112 if (m
==(vm_page_t
)0) break;
6114 } while (!queue_end(&vm_page_queue_inactive
,(queue_entry_t
) m
));
6115 vm_page_unlock_queues();
6117 vm_page_lock_queues();
6118 m
= (vm_page_t
) queue_first(&vm_page_queue_throttled
);
6120 if (m
==(vm_page_t
)0) break;
6124 assert(!m
->pageout
);
6125 assert(m
->object
!= kernel_object
);
6126 m
= (vm_page_t
) queue_next(&m
->pageq
);
6127 if (m
==(vm_page_t
)0) break;
6129 } while (!queue_end(&vm_page_queue_throttled
,(queue_entry_t
) m
));
6130 vm_page_unlock_queues();
6132 vm_page_lock_queues();
6133 m
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
6135 if (m
==(vm_page_t
)0) break;
6137 if(m
->dirty
) dpages
++;
6138 if(m
->pageout
) pgopages
++;
6139 if(m
->precious
) precpages
++;
6141 assert(m
->object
!= kernel_object
);
6142 m
= (vm_page_t
) queue_next(&m
->pageq
);
6143 if (m
==(vm_page_t
)0) break;
6145 } while (!queue_end(&vm_page_queue_zf
,(queue_entry_t
) m
));
6146 vm_page_unlock_queues();
6148 printf("IN Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
6154 vm_page_lock_queues();
6155 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
6158 if(m
== (vm_page_t
)0) break;
6159 if(m
->dirty
) dpages
++;
6160 if(m
->pageout
) pgopages
++;
6161 if(m
->precious
) precpages
++;
6163 assert(m
->object
!= kernel_object
);
6164 m
= (vm_page_t
) queue_next(&m
->pageq
);
6165 if(m
== (vm_page_t
)0) break;
6167 } while (!queue_end(&vm_page_queue_active
,(queue_entry_t
) m
));
6168 vm_page_unlock_queues();
6170 printf("AC Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
6173 #endif /* MACH_BSD */
6175 ppnum_t
upl_get_highest_page(
6178 return upl
->highest_page
;
6182 kern_return_t
upl_ubc_alias_set(upl_t upl
, unsigned int alias1
, unsigned int alias2
)
6184 upl
->ubc_alias1
= alias1
;
6185 upl
->ubc_alias2
= alias2
;
6186 return KERN_SUCCESS
;
6188 int upl_ubc_alias_get(upl_t upl
, unsigned int * al
, unsigned int * al2
)
6191 *al
= upl
->ubc_alias1
;
6193 *al2
= upl
->ubc_alias2
;
6194 return KERN_SUCCESS
;
6196 #endif /* UPL_DEBUG */
6201 #include <ddb/db_output.h>
6202 #include <ddb/db_print.h>
6203 #include <vm/vm_print.h>
6205 #define printf kdbprintf
6206 void db_pageout(void);
6212 iprintf("VM Statistics:\n");
6214 iprintf("pages:\n");
6216 iprintf("activ %5d inact %5d free %5d",
6217 vm_page_active_count
, vm_page_inactive_count
,
6218 vm_page_free_count
);
6219 printf(" wire %5d gobbl %5d\n",
6220 vm_page_wire_count
, vm_page_gobble_count
);
6222 iprintf("target:\n");
6224 iprintf("min %5d inact %5d free %5d",
6225 vm_page_free_min
, vm_page_inactive_target
,
6226 vm_page_free_target
);
6227 printf(" resrv %5d\n", vm_page_free_reserved
);
6229 iprintf("pause:\n");
6235 extern int c_laundry_pages_freed
;
6236 #endif /* MACH_COUNTERS */
6241 iprintf("Pageout Statistics:\n");
6243 iprintf("active %5d inactv %5d\n",
6244 vm_pageout_active
, vm_pageout_inactive
);
6245 iprintf("nolock %5d avoid %5d busy %5d absent %5d\n",
6246 vm_pageout_inactive_nolock
, vm_pageout_inactive_avoid
,
6247 vm_pageout_inactive_busy
, vm_pageout_inactive_absent
);
6248 iprintf("used %5d clean %5d dirty %5d\n",
6249 vm_pageout_inactive_used
, vm_pageout_inactive_clean
,
6250 vm_pageout_inactive_dirty
);
6252 iprintf("laundry_pages_freed %d\n", c_laundry_pages_freed
);
6253 #endif /* MACH_COUNTERS */
6254 #if MACH_CLUSTER_STATS
6255 iprintf("Cluster Statistics:\n");
6257 iprintf("dirtied %5d cleaned %5d collisions %5d\n",
6258 vm_pageout_cluster_dirtied
, vm_pageout_cluster_cleaned
,
6259 vm_pageout_cluster_collisions
);
6260 iprintf("clusters %5d conversions %5d\n",
6261 vm_pageout_cluster_clusters
, vm_pageout_cluster_conversions
);
6263 iprintf("Target Statistics:\n");
6265 iprintf("collisions %5d page_dirtied %5d page_freed %5d\n",
6266 vm_pageout_target_collisions
, vm_pageout_target_page_dirtied
,
6267 vm_pageout_target_page_freed
);
6269 #endif /* MACH_CLUSTER_STATS */
6273 #endif /* MACH_KDB */