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 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(1);
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(1);
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(1);
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 queue_enter(&vm_page_queue_throttled
, m
,
1841 m
->throttled
= TRUE
;
1842 vm_page_throttled_count
++;
1845 queue_enter(&vm_page_queue_zf
, m
,
1847 vm_zf_queue_count
++;
1849 queue_enter(&vm_page_queue_inactive
, m
,
1852 if (!m
->fictitious
) {
1853 vm_page_inactive_count
++;
1854 token_new_pagecount
++;
1857 vm_pageout_scan_inactive_throttled
++;
1858 goto done_with_inactivepage
;
1862 * we've got a page that we can steal...
1863 * eliminate all mappings and make sure
1864 * we have the up-to-date modified state
1865 * first take the page BUSY, so that no new
1866 * mappings can be made
1871 * if we need to do a pmap_disconnect then we
1872 * need to re-evaluate m->dirty since the pmap_disconnect
1873 * provides the true state atomically... the
1874 * page was still mapped up to the pmap_disconnect
1875 * and may have been dirtied at the last microsecond
1877 * we also check for the page being referenced 'late'
1878 * if it was, we first need to do a WAKEUP_DONE on it
1879 * since we already set m->busy = TRUE, before
1880 * going off to reactivate it
1882 * Note that if 'pmapped' is FALSE then the page is not
1883 * and has not been in any map, so there is no point calling
1884 * pmap_disconnect(). m->dirty and/or m->reference could
1885 * have been set in anticipation of likely usage of the page.
1887 if (m
->pmapped
== TRUE
) {
1888 refmod_state
= pmap_disconnect(m
->phys_page
);
1890 if (refmod_state
& VM_MEM_MODIFIED
)
1892 if (refmod_state
& VM_MEM_REFERENCED
) {
1894 /* If m->reference is already set, this page must have
1895 * already failed the reactivate_limit test, so don't
1896 * bump the counts twice.
1898 if ( ! m
->reference
) {
1899 m
->reference
= TRUE
;
1900 if (forced_reclaim
||
1901 ++reactivated_this_call
>= reactivate_limit
)
1902 vm_pageout_reactivation_limit_exceeded
++;
1904 PAGE_WAKEUP_DONE(m
);
1905 goto reactivate_page
;
1911 * reset our count of pages that have been reclaimed
1912 * since the last page was 'stolen'
1914 inactive_reclaim_run
= 0;
1917 * If it's clean and not precious, we can free the page.
1919 if (!m
->dirty
&& !m
->precious
) {
1920 vm_pageout_inactive_clean
++;
1925 * The page may have been dirtied since the last check
1926 * for a throttled target queue (which may have been skipped
1927 * if the page was clean then). With the dirty page
1928 * disconnected here, we can make one final check.
1931 boolean_t disconnect_throttled
= FALSE
;
1932 if (object
->internal
) {
1933 if (VM_PAGE_Q_THROTTLED(iq
))
1934 disconnect_throttled
= TRUE
;
1935 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
1936 disconnect_throttled
= TRUE
;
1939 if (disconnect_throttled
== TRUE
) {
1940 PAGE_WAKEUP_DONE(m
);
1941 goto throttle_inactive
;
1945 vm_pageout_cluster(m
);
1947 vm_pageout_inactive_dirty
++;
1949 inactive_burst_count
= 0;
1951 done_with_inactivepage
:
1952 if (delayed_unlock
++ > VM_PAGEOUT_DELAYED_UNLOCK_LIMIT
|| try_failed
== TRUE
) {
1954 if (object
!= NULL
) {
1955 vm_object_unlock(object
);
1957 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1960 vm_page_free_list(local_freeq
);
1965 mutex_yield(&vm_page_queue_lock
);
1970 * back to top of pageout scan loop
1976 int vm_page_free_count_init
;
1979 vm_page_free_reserve(
1982 int free_after_reserve
;
1984 vm_page_free_reserved
+= pages
;
1986 free_after_reserve
= vm_page_free_count_init
- vm_page_free_reserved
;
1988 vm_page_free_min
= vm_page_free_reserved
+
1989 VM_PAGE_FREE_MIN(free_after_reserve
);
1991 if (vm_page_free_min
> VM_PAGE_FREE_MIN_LIMIT
)
1992 vm_page_free_min
= VM_PAGE_FREE_MIN_LIMIT
;
1994 vm_page_free_target
= vm_page_free_reserved
+
1995 VM_PAGE_FREE_TARGET(free_after_reserve
);
1997 if (vm_page_free_target
> VM_PAGE_FREE_TARGET_LIMIT
)
1998 vm_page_free_target
= VM_PAGE_FREE_TARGET_LIMIT
;
2000 if (vm_page_free_target
< vm_page_free_min
+ 5)
2001 vm_page_free_target
= vm_page_free_min
+ 5;
2006 * vm_pageout is the high level pageout daemon.
2010 vm_pageout_continue(void)
2012 DTRACE_VM2(pgrrun
, int, 1, (uint64_t *), NULL
);
2013 vm_pageout_scan_event_counter
++;
2015 /* we hold vm_page_queue_free_lock now */
2016 assert(vm_page_free_wanted
== 0);
2017 assert(vm_page_free_wanted_privileged
== 0);
2018 assert_wait((event_t
) &vm_page_free_wanted
, THREAD_UNINT
);
2019 mutex_unlock(&vm_page_queue_free_lock
);
2021 counter(c_vm_pageout_block
++);
2022 thread_block((thread_continue_t
)vm_pageout_continue
);
2028 * must be called with the
2029 * queues and object locks held
2032 vm_pageout_queue_steal(vm_page_t m
)
2034 struct vm_pageout_queue
*q
;
2036 if (m
->object
->internal
== TRUE
)
2037 q
= &vm_pageout_queue_internal
;
2039 q
= &vm_pageout_queue_external
;
2042 m
->pageout_queue
= FALSE
;
2043 queue_remove(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
2045 m
->pageq
.next
= NULL
;
2046 m
->pageq
.prev
= NULL
;
2048 vm_object_paging_end(m
->object
);
2054 #ifdef FAKE_DEADLOCK
2056 #define FAKE_COUNT 5000
2058 int internal_count
= 0;
2059 int fake_deadlock
= 0;
2064 vm_pageout_iothread_continue(struct vm_pageout_queue
*q
)
2068 boolean_t need_wakeup
;
2069 memory_object_t pager
;
2070 thread_t self
= current_thread();
2072 if ((vm_pageout_internal_iothread
!= THREAD_NULL
)
2073 && (self
== vm_pageout_external_iothread
)
2074 && (self
->options
& TH_OPT_VMPRIV
))
2075 self
->options
&= ~TH_OPT_VMPRIV
;
2077 vm_page_lockspin_queues();
2079 while ( !queue_empty(&q
->pgo_pending
) ) {
2082 queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
2083 m
->pageout_queue
= FALSE
;
2084 vm_page_unlock_queues();
2086 m
->pageq
.next
= NULL
;
2087 m
->pageq
.prev
= NULL
;
2088 #ifdef FAKE_DEADLOCK
2089 if (q
== &vm_pageout_queue_internal
) {
2095 if ((internal_count
== FAKE_COUNT
)) {
2097 pg_count
= vm_page_free_count
+ vm_page_free_reserved
;
2099 if (kmem_alloc(kernel_map
, &addr
, PAGE_SIZE
* pg_count
) == KERN_SUCCESS
) {
2100 kmem_free(kernel_map
, addr
, PAGE_SIZE
* pg_count
);
2109 vm_object_lock(object
);
2111 if (!object
->pager_initialized
) {
2114 * If there is no memory object for the page, create
2115 * one and hand it to the default pager.
2118 if (!object
->pager_initialized
)
2119 vm_object_collapse(object
,
2120 (vm_object_offset_t
) 0,
2122 if (!object
->pager_initialized
)
2123 vm_object_pager_create(object
);
2124 if (!object
->pager_initialized
) {
2126 * Still no pager for the object.
2127 * Reactivate the page.
2129 * Should only happen if there is no
2132 m
->list_req_pending
= FALSE
;
2133 m
->cleaning
= FALSE
;
2136 vm_page_lockspin_queues();
2138 vm_pageout_throttle_up(m
);
2139 vm_pageout_dirty_no_pager
++;
2140 vm_page_activate(m
);
2141 vm_page_unlock_queues();
2144 * And we are done with it.
2146 PAGE_WAKEUP_DONE(m
);
2148 vm_object_paging_end(object
);
2149 vm_object_unlock(object
);
2151 vm_page_lockspin_queues();
2155 pager
= object
->pager
;
2156 if (pager
== MEMORY_OBJECT_NULL
) {
2158 * This pager has been destroyed by either
2159 * memory_object_destroy or vm_object_destroy, and
2160 * so there is nowhere for the page to go.
2161 * Just free the page... VM_PAGE_FREE takes
2162 * care of cleaning up all the state...
2163 * including doing the vm_pageout_throttle_up
2168 vm_object_paging_end(object
);
2169 vm_object_unlock(object
);
2171 vm_page_lockspin_queues();
2174 vm_object_unlock(object
);
2176 * we expect the paging_in_progress reference to have
2177 * already been taken on the object before it was added
2178 * to the appropriate pageout I/O queue... this will
2179 * keep the object from being terminated and/or the
2180 * paging_offset from changing until the I/O has
2181 * completed... therefore no need to lock the object to
2182 * pull the paging_offset from it.
2184 * Send the data to the pager.
2185 * any pageout clustering happens there
2187 memory_object_data_return(pager
,
2188 m
->offset
+ object
->paging_offset
,
2196 vm_object_lock(object
);
2197 vm_object_paging_end(object
);
2198 vm_object_unlock(object
);
2200 vm_page_lockspin_queues();
2202 assert_wait((event_t
) q
, THREAD_UNINT
);
2205 if (q
->pgo_throttled
== TRUE
&& !VM_PAGE_Q_THROTTLED(q
)) {
2206 q
->pgo_throttled
= FALSE
;
2209 need_wakeup
= FALSE
;
2211 q
->pgo_busy
= FALSE
;
2213 vm_page_unlock_queues();
2215 if (need_wakeup
== TRUE
)
2216 thread_wakeup((event_t
) &q
->pgo_laundry
);
2218 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_continue
, (void *) &q
->pgo_pending
);
2224 vm_pageout_iothread_external(void)
2226 thread_t self
= current_thread();
2228 self
->options
|= TH_OPT_VMPRIV
;
2230 vm_pageout_iothread_continue(&vm_pageout_queue_external
);
2236 vm_pageout_iothread_internal(void)
2238 thread_t self
= current_thread();
2240 self
->options
|= TH_OPT_VMPRIV
;
2242 vm_pageout_iothread_continue(&vm_pageout_queue_internal
);
2247 vm_pageout_garbage_collect(int collect
)
2253 * consider_zone_gc should be last, because the other operations
2254 * might return memory to zones.
2256 consider_machine_collect();
2259 consider_machine_adjust();
2262 assert_wait((event_t
) &vm_pageout_garbage_collect
, THREAD_UNINT
);
2264 thread_block_parameter((thread_continue_t
) vm_pageout_garbage_collect
, (void *)1);
2273 thread_t self
= current_thread();
2275 kern_return_t result
;
2279 * Set thread privileges.
2283 self
->priority
= BASEPRI_PREEMPT
- 1;
2284 set_sched_pri(self
, self
->priority
);
2285 thread_unlock(self
);
2287 if (!self
->reserved_stack
)
2288 self
->reserved_stack
= self
->kernel_stack
;
2293 * Initialize some paging parameters.
2296 if (vm_pageout_idle_wait
== 0)
2297 vm_pageout_idle_wait
= VM_PAGEOUT_IDLE_WAIT
;
2299 if (vm_pageout_burst_wait
== 0)
2300 vm_pageout_burst_wait
= VM_PAGEOUT_BURST_WAIT
;
2302 if (vm_pageout_empty_wait
== 0)
2303 vm_pageout_empty_wait
= VM_PAGEOUT_EMPTY_WAIT
;
2305 if (vm_pageout_deadlock_wait
== 0)
2306 vm_pageout_deadlock_wait
= VM_PAGEOUT_DEADLOCK_WAIT
;
2308 if (vm_pageout_deadlock_relief
== 0)
2309 vm_pageout_deadlock_relief
= VM_PAGEOUT_DEADLOCK_RELIEF
;
2311 if (vm_pageout_inactive_relief
== 0)
2312 vm_pageout_inactive_relief
= VM_PAGEOUT_INACTIVE_RELIEF
;
2314 if (vm_pageout_burst_active_throttle
== 0)
2315 vm_pageout_burst_active_throttle
= VM_PAGEOUT_BURST_ACTIVE_THROTTLE
;
2317 if (vm_pageout_burst_inactive_throttle
== 0)
2318 vm_pageout_burst_inactive_throttle
= VM_PAGEOUT_BURST_INACTIVE_THROTTLE
;
2321 * Set kernel task to low backing store privileged
2324 task_lock(kernel_task
);
2325 kernel_task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
2326 task_unlock(kernel_task
);
2328 vm_page_free_count_init
= vm_page_free_count
;
2331 * even if we've already called vm_page_free_reserve
2332 * call it again here to insure that the targets are
2333 * accurately calculated (it uses vm_page_free_count_init)
2334 * calling it with an arg of 0 will not change the reserve
2335 * but will re-calculate free_min and free_target
2337 if (vm_page_free_reserved
< VM_PAGE_FREE_RESERVED(processor_count
)) {
2338 vm_page_free_reserve((VM_PAGE_FREE_RESERVED(processor_count
)) - vm_page_free_reserved
);
2340 vm_page_free_reserve(0);
2343 queue_init(&vm_pageout_queue_external
.pgo_pending
);
2344 vm_pageout_queue_external
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
2345 vm_pageout_queue_external
.pgo_laundry
= 0;
2346 vm_pageout_queue_external
.pgo_idle
= FALSE
;
2347 vm_pageout_queue_external
.pgo_busy
= FALSE
;
2348 vm_pageout_queue_external
.pgo_throttled
= FALSE
;
2350 queue_init(&vm_pageout_queue_internal
.pgo_pending
);
2351 vm_pageout_queue_internal
.pgo_maxlaundry
= 0;
2352 vm_pageout_queue_internal
.pgo_laundry
= 0;
2353 vm_pageout_queue_internal
.pgo_idle
= FALSE
;
2354 vm_pageout_queue_internal
.pgo_busy
= FALSE
;
2355 vm_pageout_queue_internal
.pgo_throttled
= FALSE
;
2358 /* internal pageout thread started when default pager registered first time */
2359 /* external pageout and garbage collection threads started here */
2361 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_external
, NULL
,
2362 BASEPRI_PREEMPT
- 1,
2363 &vm_pageout_external_iothread
);
2364 if (result
!= KERN_SUCCESS
)
2365 panic("vm_pageout_iothread_external: create failed");
2367 thread_deallocate(vm_pageout_external_iothread
);
2369 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_garbage_collect
, NULL
,
2372 if (result
!= KERN_SUCCESS
)
2373 panic("vm_pageout_garbage_collect: create failed");
2375 thread_deallocate(thread
);
2377 vm_object_reaper_init();
2380 vm_pageout_continue();
2385 * The vm_pageout_continue() call above never returns, so the code below is never
2386 * executed. We take advantage of this to declare several DTrace VM related probe
2387 * points that our kernel doesn't have an analog for. These are probe points that
2388 * exist in Solaris and are in the DTrace documentation, so people may have written
2389 * scripts that use them. Declaring the probe points here means their scripts will
2390 * compile and execute which we want for portability of the scripts, but since this
2391 * section of code is never reached, the probe points will simply never fire. Yes,
2392 * this is basically a hack. The problem is the DTrace probe points were chosen with
2393 * Solaris specific VM events in mind, not portability to different VM implementations.
2396 DTRACE_VM2(execfree
, int, 1, (uint64_t *), NULL
);
2397 DTRACE_VM2(execpgin
, int, 1, (uint64_t *), NULL
);
2398 DTRACE_VM2(execpgout
, int, 1, (uint64_t *), NULL
);
2399 DTRACE_VM2(pgswapin
, int, 1, (uint64_t *), NULL
);
2400 DTRACE_VM2(pgswapout
, int, 1, (uint64_t *), NULL
);
2401 DTRACE_VM2(swapin
, int, 1, (uint64_t *), NULL
);
2402 DTRACE_VM2(swapout
, int, 1, (uint64_t *), NULL
);
2407 vm_pageout_internal_start(void)
2409 kern_return_t result
;
2411 vm_pageout_queue_internal
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
2412 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_internal
, NULL
, BASEPRI_PREEMPT
- 1, &vm_pageout_internal_iothread
);
2413 if (result
== KERN_SUCCESS
)
2414 thread_deallocate(vm_pageout_internal_iothread
);
2418 #define UPL_DELAYED_UNLOCK_LIMIT (MAX_UPL_TRANSFER / 2)
2421 upl_create(int type
, int flags
, upl_size_t size
)
2424 int page_field_size
= 0;
2426 int upl_size
= sizeof(struct upl
);
2428 if (type
& UPL_CREATE_LITE
) {
2429 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
2430 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
2432 upl_flags
|= UPL_LITE
;
2434 if (type
& UPL_CREATE_INTERNAL
) {
2435 upl_size
+= sizeof(struct upl_page_info
) * (size
/PAGE_SIZE
);
2437 upl_flags
|= UPL_INTERNAL
;
2439 upl
= (upl_t
)kalloc(upl_size
+ page_field_size
);
2441 if (page_field_size
)
2442 bzero((char *)upl
+ upl_size
, page_field_size
);
2444 upl
->flags
= upl_flags
| flags
;
2445 upl
->src_object
= NULL
;
2446 upl
->kaddr
= (vm_offset_t
)0;
2448 upl
->map_object
= NULL
;
2450 upl
->highest_page
= 0;
2453 upl
->ubc_alias1
= 0;
2454 upl
->ubc_alias2
= 0;
2455 #endif /* UPL_DEBUG */
2460 upl_destroy(upl_t upl
)
2462 int page_field_size
; /* bit field in word size buf */
2469 if (upl
->flags
& UPL_SHADOWED
) {
2470 object
= upl
->map_object
->shadow
;
2472 object
= upl
->map_object
;
2474 vm_object_lock(object
);
2475 queue_remove(&object
->uplq
, upl
, upl_t
, uplq
);
2476 vm_object_unlock(object
);
2478 #endif /* UPL_DEBUG */
2480 * drop a reference on the map_object whether or
2481 * not a pageout object is inserted
2483 if (upl
->flags
& UPL_SHADOWED
)
2484 vm_object_deallocate(upl
->map_object
);
2486 if (upl
->flags
& UPL_DEVICE_MEMORY
)
2490 page_field_size
= 0;
2492 if (upl
->flags
& UPL_LITE
) {
2493 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
2494 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
2496 if (upl
->flags
& UPL_INTERNAL
) {
2498 sizeof(struct upl
) +
2499 (sizeof(struct upl_page_info
) * (size
/PAGE_SIZE
))
2502 kfree(upl
, sizeof(struct upl
) + page_field_size
);
2506 void uc_upl_dealloc(upl_t upl
);
2507 __private_extern__
void
2508 uc_upl_dealloc(upl_t upl
)
2510 if (--upl
->ref_count
== 0)
2515 upl_deallocate(upl_t upl
)
2517 if (--upl
->ref_count
== 0)
2522 * Statistics about UPL enforcement of copy-on-write obligations.
2524 unsigned long upl_cow
= 0;
2525 unsigned long upl_cow_again
= 0;
2526 unsigned long upl_cow_contiguous
= 0;
2527 unsigned long upl_cow_pages
= 0;
2528 unsigned long upl_cow_again_pages
= 0;
2529 unsigned long upl_cow_contiguous_pages
= 0;
2532 * Routine: vm_object_upl_request
2534 * Cause the population of a portion of a vm_object.
2535 * Depending on the nature of the request, the pages
2536 * returned may be contain valid data or be uninitialized.
2537 * A page list structure, listing the physical pages
2538 * will be returned upon request.
2539 * This function is called by the file system or any other
2540 * supplier of backing store to a pager.
2541 * IMPORTANT NOTE: The caller must still respect the relationship
2542 * between the vm_object and its backing memory object. The
2543 * caller MUST NOT substitute changes in the backing file
2544 * without first doing a memory_object_lock_request on the
2545 * target range unless it is know that the pages are not
2546 * shared with another entity at the pager level.
2548 * if a page list structure is present
2549 * return the mapped physical pages, where a
2550 * page is not present, return a non-initialized
2551 * one. If the no_sync bit is turned on, don't
2552 * call the pager unlock to synchronize with other
2553 * possible copies of the page. Leave pages busy
2554 * in the original object, if a page list structure
2555 * was specified. When a commit of the page list
2556 * pages is done, the dirty bit will be set for each one.
2558 * If a page list structure is present, return
2559 * all mapped pages. Where a page does not exist
2560 * map a zero filled one. Leave pages busy in
2561 * the original object. If a page list structure
2562 * is not specified, this call is a no-op.
2564 * Note: access of default pager objects has a rather interesting
2565 * twist. The caller of this routine, presumably the file system
2566 * page cache handling code, will never actually make a request
2567 * against a default pager backed object. Only the default
2568 * pager will make requests on backing store related vm_objects
2569 * In this way the default pager can maintain the relationship
2570 * between backing store files (abstract memory objects) and
2571 * the vm_objects (cache objects), they support.
2575 __private_extern__ kern_return_t
2576 vm_object_upl_request(
2578 vm_object_offset_t offset
,
2581 upl_page_info_array_t user_page_list
,
2582 unsigned int *page_list_count
,
2585 vm_page_t dst_page
= VM_PAGE_NULL
;
2586 vm_object_offset_t dst_offset
;
2587 upl_size_t xfer_size
;
2592 #if MACH_CLUSTER_STATS
2593 boolean_t encountered_lrp
= FALSE
;
2595 vm_page_t alias_page
= NULL
;
2596 int refmod_state
= 0;
2597 wpl_array_t lite_list
= NULL
;
2598 vm_object_t last_copy_object
;
2599 int delayed_unlock
= 0;
2602 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
2604 * For forward compatibility's sake,
2605 * reject any unknown flag.
2607 return KERN_INVALID_VALUE
;
2609 if ( (!object
->internal
) && (object
->paging_offset
!= 0) )
2610 panic("vm_object_upl_request: external object with non-zero paging offset\n");
2611 if (object
->phys_contiguous
)
2612 panic("vm_object_upl_request: contiguous object specified\n");
2615 if ((size
/ PAGE_SIZE
) > MAX_UPL_TRANSFER
)
2616 size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2618 if ( (cntrl_flags
& UPL_SET_INTERNAL
) && page_list_count
!= NULL
)
2619 *page_list_count
= MAX_UPL_TRANSFER
;
2621 if (cntrl_flags
& UPL_SET_INTERNAL
) {
2622 if (cntrl_flags
& UPL_SET_LITE
) {
2624 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
, 0, size
);
2626 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
2627 lite_list
= (wpl_array_t
)
2628 (((uintptr_t)user_page_list
) +
2629 ((size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
2631 upl
= upl_create(UPL_CREATE_INTERNAL
, 0, size
);
2633 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
2636 if (cntrl_flags
& UPL_SET_LITE
) {
2638 upl
= upl_create(UPL_CREATE_EXTERNAL
| UPL_CREATE_LITE
, 0, size
);
2640 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
2642 upl
= upl_create(UPL_CREATE_EXTERNAL
, 0, size
);
2648 user_page_list
[0].device
= FALSE
;
2650 if (cntrl_flags
& UPL_SET_LITE
) {
2651 upl
->map_object
= object
;
2653 upl
->map_object
= vm_object_allocate(size
);
2655 * No neeed to lock the new object: nobody else knows
2656 * about it yet, so it's all ours so far.
2658 upl
->map_object
->shadow
= object
;
2659 upl
->map_object
->pageout
= TRUE
;
2660 upl
->map_object
->can_persist
= FALSE
;
2661 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
2662 upl
->map_object
->shadow_offset
= offset
;
2663 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
2665 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
2667 upl
->flags
|= UPL_SHADOWED
;
2671 * Just mark the UPL as "encrypted" here.
2672 * We'll actually encrypt the pages later,
2673 * in upl_encrypt(), when the caller has
2674 * selected which pages need to go to swap.
2676 if (cntrl_flags
& UPL_ENCRYPT
)
2677 upl
->flags
|= UPL_ENCRYPTED
;
2679 if (cntrl_flags
& UPL_FOR_PAGEOUT
)
2680 upl
->flags
|= UPL_PAGEOUT
;
2682 vm_object_lock(object
);
2683 vm_object_paging_begin(object
);
2686 * we can lock in the paging_offset once paging_in_progress is set
2689 upl
->offset
= offset
+ object
->paging_offset
;
2692 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
2693 #endif /* UPL_DEBUG */
2695 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= VM_OBJECT_NULL
) {
2697 * Honor copy-on-write obligations
2699 * The caller is gathering these pages and
2700 * might modify their contents. We need to
2701 * make sure that the copy object has its own
2702 * private copies of these pages before we let
2703 * the caller modify them.
2705 vm_object_update(object
,
2710 FALSE
, /* should_return */
2711 MEMORY_OBJECT_COPY_SYNC
,
2714 upl_cow_pages
+= size
>> PAGE_SHIFT
;
2717 * remember which copy object we synchronized with
2719 last_copy_object
= object
->copy
;
2723 dst_offset
= offset
;
2727 if ((alias_page
== NULL
) && !(cntrl_flags
& UPL_SET_LITE
)) {
2728 if (delayed_unlock
) {
2730 vm_page_unlock_queues();
2732 vm_object_unlock(object
);
2733 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
2736 if (delayed_unlock
== 0) {
2738 * pageout_scan takes the vm_page_lock_queues first
2739 * then tries for the object lock... to avoid what
2740 * is effectively a lock inversion, we'll go to the
2741 * trouble of taking them in that same order... otherwise
2742 * if this object contains the majority of the pages resident
2743 * in the UBC (or a small set of large objects actively being
2744 * worked on contain the majority of the pages), we could
2745 * cause the pageout_scan thread to 'starve' in its attempt
2746 * to find pages to move to the free queue, since it has to
2747 * successfully acquire the object lock of any candidate page
2748 * before it can steal/clean it.
2750 vm_object_unlock(object
);
2752 for (j
= 0; ; j
++) {
2753 vm_page_lock_queues();
2755 if (vm_object_lock_try(object
))
2757 vm_page_unlock_queues();
2762 if (cntrl_flags
& UPL_COPYOUT_FROM
) {
2763 upl
->flags
|= UPL_PAGE_SYNC_DONE
;
2765 if ( ((dst_page
= vm_page_lookup(object
, dst_offset
)) == VM_PAGE_NULL
) ||
2766 dst_page
->fictitious
||
2769 (dst_page
->wire_count
&& !dst_page
->pageout
&& !dst_page
->list_req_pending
)) {
2772 user_page_list
[entry
].phys_addr
= 0;
2774 goto delay_unlock_queues
;
2777 * grab this up front...
2778 * a high percentange of the time we're going to
2779 * need the hardware modification state a bit later
2780 * anyway... so we can eliminate an extra call into
2781 * the pmap layer by grabbing it here and recording it
2783 if (dst_page
->pmapped
)
2784 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
2788 if ( (refmod_state
& VM_MEM_REFERENCED
) && dst_page
->inactive
) {
2790 * page is on inactive list and referenced...
2791 * reactivate it now... this gets it out of the
2792 * way of vm_pageout_scan which would have to
2793 * reactivate it upon tripping over it
2795 vm_page_activate(dst_page
);
2796 VM_STAT_INCR(reactivations
);
2798 if (cntrl_flags
& UPL_RET_ONLY_DIRTY
) {
2800 * we're only asking for DIRTY pages to be returned
2802 if (dst_page
->list_req_pending
|| !(cntrl_flags
& UPL_FOR_PAGEOUT
)) {
2804 * if we were the page stolen by vm_pageout_scan to be
2805 * cleaned (as opposed to a buddy being clustered in
2806 * or this request is not being driven by a PAGEOUT cluster
2807 * then we only need to check for the page being dirty or
2808 * precious to decide whether to return it
2810 if (dst_page
->dirty
|| dst_page
->precious
|| (refmod_state
& VM_MEM_MODIFIED
))
2815 * this is a request for a PAGEOUT cluster and this page
2816 * is merely along for the ride as a 'buddy'... not only
2817 * does it have to be dirty to be returned, but it also
2818 * can't have been referenced recently... note that we've
2819 * already filtered above based on whether this page is
2820 * currently on the inactive queue or it meets the page
2821 * ticket (generation count) check
2823 if ( !(refmod_state
& VM_MEM_REFERENCED
) &&
2824 ((refmod_state
& VM_MEM_MODIFIED
) || dst_page
->dirty
|| dst_page
->precious
) ) {
2829 * if we reach here, we're not to return
2830 * the page... go on to the next one
2833 user_page_list
[entry
].phys_addr
= 0;
2835 goto delay_unlock_queues
;
2838 if (dst_page
->busy
&& (!(dst_page
->list_req_pending
&& dst_page
->pageout
))) {
2839 if (cntrl_flags
& UPL_NOBLOCK
) {
2841 user_page_list
[entry
].phys_addr
= 0;
2843 goto delay_unlock_queues
;
2846 * someone else is playing with the
2847 * page. We will have to wait.
2850 vm_page_unlock_queues();
2852 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
2857 * Someone else already cleaning the page?
2859 if ((dst_page
->cleaning
|| dst_page
->absent
|| dst_page
->wire_count
!= 0) && !dst_page
->list_req_pending
) {
2861 user_page_list
[entry
].phys_addr
= 0;
2863 goto delay_unlock_queues
;
2867 * The caller is gathering this page and might
2868 * access its contents later on. Decrypt the
2869 * page before adding it to the UPL, so that
2870 * the caller never sees encrypted data.
2872 if (! (cntrl_flags
& UPL_ENCRYPT
) && dst_page
->encrypted
) {
2876 vm_page_unlock_queues();
2878 * save the current state of busy
2879 * mark page as busy while decrypt
2880 * is in progress since it will drop
2881 * the object lock...
2883 was_busy
= dst_page
->busy
;
2884 dst_page
->busy
= TRUE
;
2886 vm_page_decrypt(dst_page
, 0);
2887 vm_page_decrypt_for_upl_counter
++;
2889 * restore to original busy state
2891 dst_page
->busy
= was_busy
;
2893 vm_page_lock_queues();
2896 if (dst_page
->pageout_queue
== TRUE
)
2898 * we've buddied up a page for a clustered pageout
2899 * that has already been moved to the pageout
2900 * queue by pageout_scan... we need to remove
2901 * it from the queue and drop the laundry count
2904 vm_pageout_queue_steal(dst_page
);
2905 #if MACH_CLUSTER_STATS
2907 * pageout statistics gathering. count
2908 * all the pages we will page out that
2909 * were not counted in the initial
2910 * vm_pageout_scan work
2912 if (dst_page
->list_req_pending
)
2913 encountered_lrp
= TRUE
;
2914 if ((dst_page
->dirty
|| (dst_page
->object
->internal
&& dst_page
->precious
)) && !dst_page
->list_req_pending
) {
2915 if (encountered_lrp
)
2916 CLUSTER_STAT(pages_at_higher_offsets
++;)
2918 CLUSTER_STAT(pages_at_lower_offsets
++;)
2922 * Turn off busy indication on pending
2923 * pageout. Note: we can only get here
2924 * in the request pending case.
2926 dst_page
->list_req_pending
= FALSE
;
2927 dst_page
->busy
= FALSE
;
2929 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
2930 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
2932 if (dst_page
->phys_page
> upl
->highest_page
)
2933 upl
->highest_page
= dst_page
->phys_page
;
2935 if (cntrl_flags
& UPL_SET_LITE
) {
2938 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
2939 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
2942 pmap_clear_modify(dst_page
->phys_page
);
2945 * Mark original page as cleaning
2948 dst_page
->cleaning
= TRUE
;
2949 dst_page
->precious
= FALSE
;
2952 * use pageclean setup, it is more
2953 * convenient even for the pageout
2956 vm_object_lock(upl
->map_object
);
2957 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
2958 vm_object_unlock(upl
->map_object
);
2960 alias_page
->absent
= FALSE
;
2965 * Record that this page has been
2968 vm_external_state_set(object
->existence_map
, dst_page
->offset
);
2969 #endif /*MACH_PAGEMAP*/
2970 dst_page
->dirty
= dirty
;
2973 dst_page
->precious
= TRUE
;
2975 if (dst_page
->pageout
)
2976 dst_page
->busy
= TRUE
;
2978 if ( (cntrl_flags
& UPL_ENCRYPT
) ) {
2981 * We want to deny access to the target page
2982 * because its contents are about to be
2983 * encrypted and the user would be very
2984 * confused to see encrypted data instead
2986 * We also set "encrypted_cleaning" to allow
2987 * vm_pageout_scan() to demote that page
2988 * from "adjacent/clean-in-place" to
2989 * "target/clean-and-free" if it bumps into
2990 * this page during its scanning while we're
2991 * still processing this cluster.
2993 dst_page
->busy
= TRUE
;
2994 dst_page
->encrypted_cleaning
= TRUE
;
2996 if ( !(cntrl_flags
& UPL_CLEAN_IN_PLACE
) ) {
2998 * deny access to the target page
2999 * while it is being worked on
3001 if ((!dst_page
->pageout
) && (dst_page
->wire_count
== 0)) {
3002 dst_page
->busy
= TRUE
;
3003 dst_page
->pageout
= TRUE
;
3004 vm_page_wire(dst_page
);
3008 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= last_copy_object
) {
3010 * Honor copy-on-write obligations
3012 * The copy object has changed since we
3013 * last synchronized for copy-on-write.
3014 * Another copy object might have been
3015 * inserted while we released the object's
3016 * lock. Since someone could have seen the
3017 * original contents of the remaining pages
3018 * through that new object, we have to
3019 * synchronize with it again for the remaining
3020 * pages only. The previous pages are "busy"
3021 * so they can not be seen through the new
3022 * mapping. The new mapping will see our
3023 * upcoming changes for those previous pages,
3024 * but that's OK since they couldn't see what
3025 * was there before. It's just a race anyway
3026 * and there's no guarantee of consistency or
3027 * atomicity. We just don't want new mappings
3028 * to see both the *before* and *after* pages.
3030 if (object
->copy
!= VM_OBJECT_NULL
) {
3032 vm_page_unlock_queues();
3036 dst_offset
,/* current offset */
3037 xfer_size
, /* remaining size */
3040 FALSE
, /* should_return */
3041 MEMORY_OBJECT_COPY_SYNC
,
3045 upl_cow_again_pages
+= xfer_size
>> PAGE_SHIFT
;
3047 vm_page_lock_queues();
3051 * remember the copy object we synced with
3053 last_copy_object
= object
->copy
;
3055 dst_page
= vm_page_lookup(object
, dst_offset
);
3057 if (dst_page
!= VM_PAGE_NULL
) {
3058 if ( !(dst_page
->list_req_pending
) ) {
3059 if ((cntrl_flags
& UPL_RET_ONLY_ABSENT
) && !dst_page
->absent
) {
3061 * skip over pages already present in the cache
3064 user_page_list
[entry
].phys_addr
= 0;
3066 goto delay_unlock_queues
;
3068 if (dst_page
->cleaning
) {
3070 * someone else is writing to the page... wait...
3073 vm_page_unlock_queues();
3075 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
3080 if (dst_page
->fictitious
&&
3081 dst_page
->phys_page
== vm_page_fictitious_addr
) {
3082 assert( !dst_page
->speculative
);
3084 * dump the fictitious page
3086 dst_page
->list_req_pending
= FALSE
;
3088 vm_page_free(dst_page
);
3091 } else if (dst_page
->absent
) {
3093 * the default_pager case
3095 dst_page
->list_req_pending
= FALSE
;
3096 dst_page
->busy
= FALSE
;
3100 if (dst_page
== VM_PAGE_NULL
) {
3101 if (object
->private) {
3103 * This is a nasty wrinkle for users
3104 * of upl who encounter device or
3105 * private memory however, it is
3106 * unavoidable, only a fault can
3107 * resolve the actual backing
3108 * physical page by asking the
3112 user_page_list
[entry
].phys_addr
= 0;
3114 goto delay_unlock_queues
;
3117 * need to allocate a page
3119 dst_page
= vm_page_grab();
3121 if (dst_page
== VM_PAGE_NULL
) {
3122 if ( (cntrl_flags
& (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) == (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) {
3124 * we don't want to stall waiting for pages to come onto the free list
3125 * while we're already holding absent pages in this UPL
3126 * the caller will deal with the empty slots
3129 user_page_list
[entry
].phys_addr
= 0;
3134 * no pages available... wait
3135 * then try again for the same
3139 vm_page_unlock_queues();
3141 vm_object_unlock(object
);
3145 * pageout_scan takes the vm_page_lock_queues first
3146 * then tries for the object lock... to avoid what
3147 * is effectively a lock inversion, we'll go to the
3148 * trouble of taking them in that same order... otherwise
3149 * if this object contains the majority of the pages resident
3150 * in the UBC (or a small set of large objects actively being
3151 * worked on contain the majority of the pages), we could
3152 * cause the pageout_scan thread to 'starve' in its attempt
3153 * to find pages to move to the free queue, since it has to
3154 * successfully acquire the object lock of any candidate page
3155 * before it can steal/clean it.
3157 for (j
= 0; ; j
++) {
3158 vm_page_lock_queues();
3160 if (vm_object_lock_try(object
))
3162 vm_page_unlock_queues();
3169 vm_page_insert_internal(dst_page
, object
, dst_offset
, TRUE
);
3171 dst_page
->absent
= TRUE
;
3172 dst_page
->busy
= FALSE
;
3174 if (cntrl_flags
& UPL_RET_ONLY_ABSENT
) {
3176 * if UPL_RET_ONLY_ABSENT was specified,
3177 * than we're definitely setting up a
3178 * upl for a clustered read/pagein
3179 * operation... mark the pages as clustered
3180 * so upl_commit_range can put them on the
3183 dst_page
->clustered
= TRUE
;
3189 if (cntrl_flags
& UPL_ENCRYPT
) {
3191 * The page is going to be encrypted when we
3192 * get it from the pager, so mark it so.
3194 dst_page
->encrypted
= TRUE
;
3197 * Otherwise, the page will not contain
3200 dst_page
->encrypted
= FALSE
;
3202 dst_page
->overwriting
= TRUE
;
3204 if (dst_page
->fictitious
) {
3205 panic("need corner case for fictitious page");
3207 if (dst_page
->busy
) {
3209 * someone else is playing with the
3210 * page. We will have to wait.
3213 vm_page_unlock_queues();
3215 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
3219 if (dst_page
->pmapped
) {
3220 if ( !(cntrl_flags
& UPL_FILE_IO
))
3222 * eliminate all mappings from the
3223 * original object and its prodigy
3225 refmod_state
= pmap_disconnect(dst_page
->phys_page
);
3227 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
3231 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
3232 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
3234 if (cntrl_flags
& UPL_SET_LITE
) {
3237 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
3238 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
3241 pmap_clear_modify(dst_page
->phys_page
);
3244 * Mark original page as cleaning
3247 dst_page
->cleaning
= TRUE
;
3248 dst_page
->precious
= FALSE
;
3251 * use pageclean setup, it is more
3252 * convenient even for the pageout
3255 vm_object_lock(upl
->map_object
);
3256 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
3257 vm_object_unlock(upl
->map_object
);
3259 alias_page
->absent
= FALSE
;
3263 if (cntrl_flags
& UPL_CLEAN_IN_PLACE
) {
3265 * clean in place for read implies
3266 * that a write will be done on all
3267 * the pages that are dirty before
3268 * a upl commit is done. The caller
3269 * is obligated to preserve the
3270 * contents of all pages marked dirty
3272 upl
->flags
|= UPL_CLEAR_DIRTY
;
3274 dst_page
->dirty
= dirty
;
3277 dst_page
->precious
= TRUE
;
3279 if (dst_page
->wire_count
== 0) {
3281 * deny access to the target page while
3282 * it is being worked on
3284 dst_page
->busy
= TRUE
;
3286 vm_page_wire(dst_page
);
3288 if (dst_page
->clustered
) {
3290 * expect the page not to be used
3291 * since it's coming in as part
3292 * of a speculative cluster...
3293 * pages that are 'consumed' will
3294 * get a hardware reference
3296 dst_page
->reference
= FALSE
;
3299 * expect the page to be used
3301 dst_page
->reference
= TRUE
;
3303 dst_page
->precious
= (cntrl_flags
& UPL_PRECIOUS
) ? TRUE
: FALSE
;
3305 if (dst_page
->phys_page
> upl
->highest_page
)
3306 upl
->highest_page
= dst_page
->phys_page
;
3307 if (user_page_list
) {
3308 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
3309 user_page_list
[entry
].dirty
= dst_page
->dirty
;
3310 user_page_list
[entry
].pageout
= dst_page
->pageout
;
3311 user_page_list
[entry
].absent
= dst_page
->absent
;
3312 user_page_list
[entry
].precious
= dst_page
->precious
;
3314 if (dst_page
->clustered
== TRUE
)
3315 user_page_list
[entry
].speculative
= dst_page
->speculative
;
3317 user_page_list
[entry
].speculative
= FALSE
;
3320 * if UPL_RET_ONLY_ABSENT is set, then
3321 * we are working with a fresh page and we've
3322 * just set the clustered flag on it to
3323 * indicate that it was drug in as part of a
3324 * speculative cluster... so leave it alone
3326 if ( !(cntrl_flags
& UPL_RET_ONLY_ABSENT
)) {
3328 * someone is explicitly grabbing this page...
3329 * update clustered and speculative state
3332 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
3334 delay_unlock_queues
:
3335 if (delayed_unlock
++ > UPL_DELAYED_UNLOCK_LIMIT
) {
3337 * pageout_scan takes the vm_page_lock_queues first
3338 * then tries for the object lock... to avoid what
3339 * is effectively a lock inversion, we'll go to the
3340 * trouble of taking them in that same order... otherwise
3341 * if this object contains the majority of the pages resident
3342 * in the UBC (or a small set of large objects actively being
3343 * worked on contain the majority of the pages), we could
3344 * cause the pageout_scan thread to 'starve' in its attempt
3345 * to find pages to move to the free queue, since it has to
3346 * successfully acquire the object lock of any candidate page
3347 * before it can steal/clean it.
3349 vm_object_unlock(object
);
3350 mutex_yield(&vm_page_queue_lock
);
3352 for (j
= 0; ; j
++) {
3353 if (vm_object_lock_try(object
))
3355 vm_page_unlock_queues();
3357 vm_page_lock_queues();
3363 dst_offset
+= PAGE_SIZE_64
;
3364 xfer_size
-= PAGE_SIZE
;
3366 if (alias_page
!= NULL
) {
3367 if (delayed_unlock
== 0) {
3368 vm_page_lock_queues();
3371 vm_page_free(alias_page
);
3374 vm_page_unlock_queues();
3376 if (page_list_count
!= NULL
) {
3377 if (upl
->flags
& UPL_INTERNAL
)
3378 *page_list_count
= 0;
3379 else if (*page_list_count
> entry
)
3380 *page_list_count
= entry
;
3382 vm_object_unlock(object
);
3384 return KERN_SUCCESS
;
3387 /* JMM - Backward compatability for now */
3389 vm_fault_list_request( /* forward */
3390 memory_object_control_t control
,
3391 vm_object_offset_t offset
,
3394 upl_page_info_t
**user_page_list_ptr
,
3395 unsigned int page_list_count
,
3398 vm_fault_list_request(
3399 memory_object_control_t control
,
3400 vm_object_offset_t offset
,
3403 upl_page_info_t
**user_page_list_ptr
,
3404 unsigned int page_list_count
,
3407 unsigned int local_list_count
;
3408 upl_page_info_t
*user_page_list
;
3411 if (user_page_list_ptr
!= NULL
) {
3412 local_list_count
= page_list_count
;
3413 user_page_list
= *user_page_list_ptr
;
3415 local_list_count
= 0;
3416 user_page_list
= NULL
;
3418 kr
= memory_object_upl_request(control
,
3426 if(kr
!= KERN_SUCCESS
)
3429 if ((user_page_list_ptr
!= NULL
) && (cntrl_flags
& UPL_INTERNAL
)) {
3430 *user_page_list_ptr
= UPL_GET_INTERNAL_PAGE_LIST(*upl_ptr
);
3433 return KERN_SUCCESS
;
3439 * Routine: vm_object_super_upl_request
3441 * Cause the population of a portion of a vm_object
3442 * in much the same way as memory_object_upl_request.
3443 * Depending on the nature of the request, the pages
3444 * returned may be contain valid data or be uninitialized.
3445 * However, the region may be expanded up to the super
3446 * cluster size provided.
3449 __private_extern__ kern_return_t
3450 vm_object_super_upl_request(
3452 vm_object_offset_t offset
,
3454 upl_size_t super_cluster
,
3456 upl_page_info_t
*user_page_list
,
3457 unsigned int *page_list_count
,
3460 if (object
->paging_offset
> offset
)
3461 return KERN_FAILURE
;
3463 assert(object
->paging_in_progress
);
3464 offset
= offset
- object
->paging_offset
;
3466 if (super_cluster
> size
) {
3468 vm_object_offset_t base_offset
;
3469 upl_size_t super_size
;
3471 base_offset
= (offset
& ~((vm_object_offset_t
) super_cluster
- 1));
3472 super_size
= (offset
+ size
) > (base_offset
+ super_cluster
) ? super_cluster
<<1 : super_cluster
;
3473 super_size
= ((base_offset
+ super_size
) > object
->size
) ? (object
->size
- base_offset
) : super_size
;
3475 if (offset
> (base_offset
+ super_size
)) {
3476 panic("vm_object_super_upl_request: Missed target pageout"
3477 " %#llx,%#llx, %#x, %#x, %#x, %#llx\n",
3478 offset
, base_offset
, super_size
, super_cluster
,
3479 size
, object
->paging_offset
);
3482 * apparently there is a case where the vm requests a
3483 * page to be written out who's offset is beyond the
3486 if ((offset
+ size
) > (base_offset
+ super_size
))
3487 super_size
= (offset
+ size
) - base_offset
;
3489 offset
= base_offset
;
3492 return vm_object_upl_request(object
, offset
, size
, upl
, user_page_list
, page_list_count
, cntrl_flags
);
3499 vm_map_address_t offset
,
3500 upl_size_t
*upl_size
,
3502 upl_page_info_array_t page_list
,
3503 unsigned int *count
,
3506 vm_map_entry_t entry
;
3508 int force_data_sync
;
3510 vm_object_t local_object
;
3511 vm_map_offset_t local_offset
;
3512 vm_map_offset_t local_start
;
3515 caller_flags
= *flags
;
3517 if (caller_flags
& ~UPL_VALID_FLAGS
) {
3519 * For forward compatibility's sake,
3520 * reject any unknown flag.
3522 return KERN_INVALID_VALUE
;
3524 force_data_sync
= (caller_flags
& UPL_FORCE_DATA_SYNC
);
3525 sync_cow_data
= !(caller_flags
& UPL_COPYOUT_FROM
);
3528 return KERN_INVALID_ARGUMENT
;
3533 if (vm_map_lookup_entry(map
, offset
, &entry
)) {
3535 if ((entry
->vme_end
- offset
) < *upl_size
)
3536 *upl_size
= entry
->vme_end
- offset
;
3538 if (caller_flags
& UPL_QUERY_OBJECT_TYPE
) {
3541 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
3542 if (entry
->object
.vm_object
->private)
3543 *flags
= UPL_DEV_MEMORY
;
3545 if (entry
->object
.vm_object
->phys_contiguous
)
3546 *flags
|= UPL_PHYS_CONTIG
;
3550 return KERN_SUCCESS
;
3552 if (entry
->object
.vm_object
== VM_OBJECT_NULL
|| !entry
->object
.vm_object
->phys_contiguous
) {
3553 if ((*upl_size
/page_size
) > MAX_UPL_TRANSFER
)
3554 *upl_size
= MAX_UPL_TRANSFER
* page_size
;
3557 * Create an object if necessary.
3559 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
3560 entry
->object
.vm_object
= vm_object_allocate((vm_size_t
)(entry
->vme_end
- entry
->vme_start
));
3563 if (!(caller_flags
& UPL_COPYOUT_FROM
)) {
3564 if (!(entry
->protection
& VM_PROT_WRITE
)) {
3566 return KERN_PROTECTION_FAILURE
;
3568 if (entry
->needs_copy
) {
3571 vm_object_offset_t new_offset
;
3574 vm_map_version_t version
;
3578 vm_map_lock_write_to_read(map
);
3580 if (vm_map_lookup_locked(&local_map
,
3581 offset
, VM_PROT_WRITE
,
3582 OBJECT_LOCK_EXCLUSIVE
,
3584 &new_offset
, &prot
, &wired
,
3587 vm_map_unlock(local_map
);
3588 return KERN_FAILURE
;
3590 if (real_map
!= map
)
3591 vm_map_unlock(real_map
);
3592 vm_object_unlock(object
);
3593 vm_map_unlock(local_map
);
3595 goto REDISCOVER_ENTRY
;
3598 if (entry
->is_sub_map
) {
3601 submap
= entry
->object
.sub_map
;
3602 local_start
= entry
->vme_start
;
3603 local_offset
= entry
->offset
;
3605 vm_map_reference(submap
);
3608 ret
= vm_map_create_upl(submap
,
3609 local_offset
+ (offset
- local_start
),
3610 upl_size
, upl
, page_list
, count
, flags
);
3611 vm_map_deallocate(submap
);
3615 if (sync_cow_data
) {
3616 if (entry
->object
.vm_object
->shadow
|| entry
->object
.vm_object
->copy
) {
3617 local_object
= entry
->object
.vm_object
;
3618 local_start
= entry
->vme_start
;
3619 local_offset
= entry
->offset
;
3621 vm_object_reference(local_object
);
3624 if (entry
->object
.vm_object
->shadow
&& entry
->object
.vm_object
->copy
) {
3625 vm_object_lock_request(
3626 local_object
->shadow
,
3627 (vm_object_offset_t
)
3628 ((offset
- local_start
) +
3630 local_object
->shadow_offset
,
3632 MEMORY_OBJECT_DATA_SYNC
,
3635 sync_cow_data
= FALSE
;
3636 vm_object_deallocate(local_object
);
3638 goto REDISCOVER_ENTRY
;
3641 if (force_data_sync
) {
3642 local_object
= entry
->object
.vm_object
;
3643 local_start
= entry
->vme_start
;
3644 local_offset
= entry
->offset
;
3646 vm_object_reference(local_object
);
3649 vm_object_lock_request(
3651 (vm_object_offset_t
)
3652 ((offset
- local_start
) + local_offset
),
3653 (vm_object_size_t
)*upl_size
, FALSE
,
3654 MEMORY_OBJECT_DATA_SYNC
,
3657 force_data_sync
= FALSE
;
3658 vm_object_deallocate(local_object
);
3660 goto REDISCOVER_ENTRY
;
3662 if (entry
->object
.vm_object
->private)
3663 *flags
= UPL_DEV_MEMORY
;
3667 if (entry
->object
.vm_object
->phys_contiguous
)
3668 *flags
|= UPL_PHYS_CONTIG
;
3670 local_object
= entry
->object
.vm_object
;
3671 local_offset
= entry
->offset
;
3672 local_start
= entry
->vme_start
;
3674 vm_object_reference(local_object
);
3677 ret
= vm_object_iopl_request(local_object
,
3678 (vm_object_offset_t
) ((offset
- local_start
) + local_offset
),
3684 vm_object_deallocate(local_object
);
3690 return(KERN_FAILURE
);
3694 * Internal routine to enter a UPL into a VM map.
3696 * JMM - This should just be doable through the standard
3697 * vm_map_enter() API.
3703 vm_map_offset_t
*dst_addr
)
3706 vm_object_offset_t offset
;
3707 vm_map_offset_t addr
;
3711 if (upl
== UPL_NULL
)
3712 return KERN_INVALID_ARGUMENT
;
3717 * check to see if already mapped
3719 if (UPL_PAGE_LIST_MAPPED
& upl
->flags
) {
3721 return KERN_FAILURE
;
3724 if ((!(upl
->flags
& UPL_SHADOWED
)) && !((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) ||
3725 (upl
->map_object
->phys_contiguous
))) {
3727 vm_page_t alias_page
;
3728 vm_object_offset_t new_offset
;
3730 wpl_array_t lite_list
;
3732 if (upl
->flags
& UPL_INTERNAL
) {
3733 lite_list
= (wpl_array_t
)
3734 ((((uintptr_t)upl
) + sizeof(struct upl
))
3735 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
3737 lite_list
= (wpl_array_t
)(((uintptr_t)upl
) + sizeof(struct upl
));
3739 object
= upl
->map_object
;
3740 upl
->map_object
= vm_object_allocate(upl
->size
);
3742 vm_object_lock(upl
->map_object
);
3744 upl
->map_object
->shadow
= object
;
3745 upl
->map_object
->pageout
= TRUE
;
3746 upl
->map_object
->can_persist
= FALSE
;
3747 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
3748 upl
->map_object
->shadow_offset
= upl
->offset
- object
->paging_offset
;
3749 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
3750 offset
= upl
->map_object
->shadow_offset
;
3754 upl
->flags
|= UPL_SHADOWED
;
3757 pg_num
= (new_offset
)/PAGE_SIZE
;
3759 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
3761 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
3763 vm_object_lock(object
);
3765 m
= vm_page_lookup(object
, offset
);
3766 if (m
== VM_PAGE_NULL
) {
3767 panic("vm_upl_map: page missing\n");
3771 * Convert the fictitious page to a private
3772 * shadow of the real page.
3774 assert(alias_page
->fictitious
);
3775 alias_page
->fictitious
= FALSE
;
3776 alias_page
->private = TRUE
;
3777 alias_page
->pageout
= TRUE
;
3779 * since m is a page in the upl it must
3780 * already be wired or BUSY, so it's
3781 * safe to assign the underlying physical
3784 alias_page
->phys_page
= m
->phys_page
;
3786 vm_object_unlock(object
);
3788 vm_page_lockspin_queues();
3789 vm_page_wire(alias_page
);
3790 vm_page_unlock_queues();
3794 * The virtual page ("m") has to be wired in some way
3795 * here or its physical page ("m->phys_page") could
3796 * be recycled at any time.
3797 * Assuming this is enforced by the caller, we can't
3798 * get an encrypted page here. Since the encryption
3799 * key depends on the VM page's "pager" object and
3800 * the "paging_offset", we couldn't handle 2 pageable
3801 * VM pages (with different pagers and paging_offsets)
3802 * sharing the same physical page: we could end up
3803 * encrypting with one key (via one VM page) and
3804 * decrypting with another key (via the alias VM page).
3806 ASSERT_PAGE_DECRYPTED(m
);
3808 vm_page_insert(alias_page
, upl
->map_object
, new_offset
);
3810 assert(!alias_page
->wanted
);
3811 alias_page
->busy
= FALSE
;
3812 alias_page
->absent
= FALSE
;
3815 offset
+= PAGE_SIZE_64
;
3816 new_offset
+= PAGE_SIZE_64
;
3818 vm_object_unlock(upl
->map_object
);
3820 if ((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) || upl
->map_object
->phys_contiguous
)
3821 offset
= upl
->offset
- upl
->map_object
->paging_offset
;
3826 vm_object_reference(upl
->map_object
);
3830 * NEED A UPL_MAP ALIAS
3832 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
3833 VM_FLAGS_ANYWHERE
, upl
->map_object
, offset
, FALSE
,
3834 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
3836 if (kr
!= KERN_SUCCESS
) {
3840 vm_object_lock(upl
->map_object
);
3842 for (addr
= *dst_addr
; size
> 0; size
-= PAGE_SIZE
, addr
+= PAGE_SIZE
) {
3843 m
= vm_page_lookup(upl
->map_object
, offset
);
3846 unsigned int cache_attr
;
3847 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
3852 PMAP_ENTER(map
->pmap
, addr
, m
, VM_PROT_ALL
, cache_attr
, TRUE
);
3854 offset
+= PAGE_SIZE_64
;
3856 vm_object_unlock(upl
->map_object
);
3859 * hold a reference for the mapping
3862 upl
->flags
|= UPL_PAGE_LIST_MAPPED
;
3863 upl
->kaddr
= *dst_addr
;
3866 return KERN_SUCCESS
;
3870 * Internal routine to remove a UPL mapping from a VM map.
3872 * XXX - This should just be doable through a standard
3873 * vm_map_remove() operation. Otherwise, implicit clean-up
3874 * of the target map won't be able to correctly remove
3875 * these (and release the reference on the UPL). Having
3876 * to do this means we can't map these into user-space
3887 if (upl
== UPL_NULL
)
3888 return KERN_INVALID_ARGUMENT
;
3892 if (upl
->flags
& UPL_PAGE_LIST_MAPPED
) {
3896 assert(upl
->ref_count
> 1);
3897 upl
->ref_count
--; /* removing mapping ref */
3899 upl
->flags
&= ~UPL_PAGE_LIST_MAPPED
;
3900 upl
->kaddr
= (vm_offset_t
) 0;
3904 vm_map_trunc_page(addr
),
3905 vm_map_round_page(addr
+ size
),
3908 return KERN_SUCCESS
;
3912 return KERN_FAILURE
;
3918 upl_offset_t offset
,
3921 upl_page_info_t
*page_list
,
3922 mach_msg_type_number_t count
,
3925 upl_size_t xfer_size
;
3926 vm_object_t shadow_object
;
3928 vm_object_offset_t target_offset
;
3930 wpl_array_t lite_list
;
3932 int delayed_unlock
= 0;
3933 int clear_refmod
= 0;
3934 int pgpgout_count
= 0;
3939 if (upl
== UPL_NULL
)
3940 return KERN_INVALID_ARGUMENT
;
3945 if (upl
->flags
& UPL_DEVICE_MEMORY
)
3947 else if ((offset
+ size
) <= upl
->size
)
3950 return KERN_FAILURE
;
3954 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
3956 * We used this UPL to block access to the pages by marking
3957 * them "busy". Now we need to clear the "busy" bit to allow
3958 * access to these pages again.
3960 flags
|= UPL_COMMIT_ALLOW_ACCESS
;
3962 if (upl
->flags
& UPL_CLEAR_DIRTY
)
3963 flags
|= UPL_COMMIT_CLEAR_DIRTY
;
3965 if (upl
->flags
& UPL_INTERNAL
)
3966 lite_list
= (wpl_array_t
) ((((uintptr_t)upl
) + sizeof(struct upl
))
3967 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
3969 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
3971 object
= upl
->map_object
;
3973 if (upl
->flags
& UPL_SHADOWED
) {
3974 vm_object_lock(object
);
3975 shadow_object
= object
->shadow
;
3977 shadow_object
= object
;
3979 entry
= offset
/PAGE_SIZE
;
3980 target_offset
= (vm_object_offset_t
)offset
;
3983 * pageout_scan takes the vm_page_lock_queues first
3984 * then tries for the object lock... to avoid what
3985 * is effectively a lock inversion, we'll go to the
3986 * trouble of taking them in that same order... otherwise
3987 * if this object contains the majority of the pages resident
3988 * in the UBC (or a small set of large objects actively being
3989 * worked on contain the majority of the pages), we could
3990 * cause the pageout_scan thread to 'starve' in its attempt
3991 * to find pages to move to the free queue, since it has to
3992 * successfully acquire the object lock of any candidate page
3993 * before it can steal/clean it.
3995 for (j
= 0; ; j
++) {
3996 vm_page_lock_queues();
3998 if (vm_object_lock_try(shadow_object
))
4000 vm_page_unlock_queues();
4010 if (upl
->flags
& UPL_LITE
) {
4013 pg_num
= target_offset
/PAGE_SIZE
;
4015 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
4016 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
4018 m
= vm_page_lookup(shadow_object
, target_offset
+ (upl
->offset
- shadow_object
->paging_offset
));
4021 if (upl
->flags
& UPL_SHADOWED
) {
4022 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
4028 if (m
== VM_PAGE_NULL
)
4029 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->shadow_offset
);
4032 if (m
!= VM_PAGE_NULL
) {
4036 if (upl
->flags
& UPL_IO_WIRE
) {
4041 page_list
[entry
].phys_addr
= 0;
4043 if (flags
& UPL_COMMIT_SET_DIRTY
)
4045 else if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
4047 if (m
->cs_validated
&& !m
->cs_tainted
) {
4050 * This page is no longer dirty
4051 * but could have been modified,
4052 * so it will need to be
4055 m
->cs_validated
= FALSE
;
4056 vm_cs_validated_resets
++;
4058 clear_refmod
|= VM_MEM_MODIFIED
;
4060 if (flags
& UPL_COMMIT_INACTIVATE
)
4061 vm_page_deactivate(m
);
4064 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
4066 if (flags
& UPL_COMMIT_ALLOW_ACCESS
) {
4068 * We blocked access to the pages in this UPL.
4069 * Clear the "busy" bit and wake up any waiter
4072 PAGE_WAKEUP_DONE(m
);
4074 goto commit_next_page
;
4077 * make sure to clear the hardware
4078 * modify or reference bits before
4079 * releasing the BUSY bit on this page
4080 * otherwise we risk losing a legitimate
4083 if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
4085 if (m
->cs_validated
&& !m
->cs_tainted
) {
4088 * This page is no longer dirty
4089 * but could have been modified,
4090 * so it will need to be
4093 m
->cs_validated
= FALSE
;
4094 vm_cs_validated_resets
++;
4096 clear_refmod
|= VM_MEM_MODIFIED
;
4099 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
4104 p
= &(page_list
[entry
]);
4106 if (p
->phys_addr
&& p
->pageout
&& !m
->pageout
) {
4110 } else if (p
->phys_addr
&&
4111 !p
->pageout
&& m
->pageout
&&
4112 !m
->dump_cleaning
) {
4115 m
->overwriting
= FALSE
;
4118 PAGE_WAKEUP_DONE(m
);
4120 page_list
[entry
].phys_addr
= 0;
4122 m
->dump_cleaning
= FALSE
;
4125 vm_pageout_throttle_up(m
);
4128 m
->cleaning
= FALSE
;
4129 m
->encrypted_cleaning
= FALSE
;
4131 #if MACH_CLUSTER_STATS
4132 if (m
->wanted
) vm_pageout_target_collisions
++;
4135 if (m
->cs_validated
&& !m
->cs_tainted
) {
4138 * This page is no longer dirty
4139 * but could have been modified,
4140 * so it will need to be
4143 m
->cs_validated
= FALSE
;
4144 vm_cs_validated_resets
++;
4147 if (m
->pmapped
&& (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
))
4152 * page was re-dirtied after we started
4153 * the pageout... reactivate it since
4154 * we don't know whether the on-disk
4155 * copy matches what is now in memory
4159 if (upl
->flags
& UPL_PAGEOUT
) {
4160 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
4161 VM_STAT_INCR(reactivations
);
4162 DTRACE_VM2(pgrec
, int, 1, (uint64_t *), NULL
);
4164 PAGE_WAKEUP_DONE(m
);
4167 * page has been successfully cleaned
4168 * go ahead and free it for other use
4171 if (m
->object
->internal
) {
4172 DTRACE_VM2(anonpgout
, int, 1, (uint64_t *), NULL
);
4174 DTRACE_VM2(fspgout
, int, 1, (uint64_t *), NULL
);
4179 if (upl
->flags
& UPL_PAGEOUT
) {
4180 CLUSTER_STAT(vm_pageout_target_page_freed
++;)
4182 if (page_list
[entry
].dirty
) {
4183 VM_STAT_INCR(pageouts
);
4184 DTRACE_VM2(pgout
, int, 1, (uint64_t *), NULL
);
4189 goto commit_next_page
;
4191 #if MACH_CLUSTER_STATS
4193 m
->dirty
= pmap_is_modified(m
->phys_page
);
4195 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
4196 else vm_pageout_cluster_cleaned
++;
4197 if (m
->wanted
) vm_pageout_cluster_collisions
++;
4200 if (m
->cs_validated
&& !m
->cs_tainted
) {
4203 * This page is no longer dirty
4204 * but could have been modified,
4205 * so it will need to be
4208 m
->cs_validated
= FALSE
;
4209 vm_cs_validated_resets
++;
4212 if ((m
->busy
) && (m
->cleaning
)) {
4214 * the request_page_list case
4217 m
->overwriting
= FALSE
;
4219 } else if (m
->overwriting
) {
4221 * alternate request page list, write to
4222 * page_list case. Occurs when the original
4223 * page was wired at the time of the list
4226 assert(m
->wire_count
!= 0);
4227 vm_page_unwire(m
);/* reactivates */
4228 m
->overwriting
= FALSE
;
4230 m
->cleaning
= FALSE
;
4231 m
->encrypted_cleaning
= FALSE
;
4234 * It is a part of the semantic of COPYOUT_FROM
4235 * UPLs that a commit implies cache sync
4236 * between the vm page and the backing store
4237 * this can be used to strip the precious bit
4240 if (upl
->flags
& UPL_PAGE_SYNC_DONE
)
4241 m
->precious
= FALSE
;
4243 if (flags
& UPL_COMMIT_SET_DIRTY
)
4246 if ((flags
& UPL_COMMIT_INACTIVATE
) && !m
->clustered
&& !m
->speculative
) {
4247 vm_page_deactivate(m
);
4248 } else if (!m
->active
&& !m
->inactive
&& !m
->speculative
) {
4251 vm_page_speculate(m
, TRUE
);
4252 else if (m
->reference
)
4253 vm_page_activate(m
);
4255 vm_page_deactivate(m
);
4257 if (flags
& UPL_COMMIT_ALLOW_ACCESS
) {
4259 * We blocked access to the pages in this URL.
4260 * Clear the "busy" bit on this page before we
4261 * wake up any waiter.
4266 * Wakeup any thread waiting for the page to be un-cleaning.
4271 target_offset
+= PAGE_SIZE_64
;
4272 xfer_size
-= PAGE_SIZE
;
4275 if (delayed_unlock
++ > UPL_DELAYED_UNLOCK_LIMIT
) {
4277 * pageout_scan takes the vm_page_lock_queues first
4278 * then tries for the object lock... to avoid what
4279 * is effectively a lock inversion, we'll go to the
4280 * trouble of taking them in that same order... otherwise
4281 * if this object contains the majority of the pages resident
4282 * in the UBC (or a small set of large objects actively being
4283 * worked on contain the majority of the pages), we could
4284 * cause the pageout_scan thread to 'starve' in its attempt
4285 * to find pages to move to the free queue, since it has to
4286 * successfully acquire the object lock of any candidate page
4287 * before it can steal/clean it.
4289 vm_object_unlock(shadow_object
);
4290 mutex_yield(&vm_page_queue_lock
);
4292 for (j
= 0; ; j
++) {
4293 if (vm_object_lock_try(shadow_object
))
4295 vm_page_unlock_queues();
4297 vm_page_lock_queues();
4303 vm_page_unlock_queues();
4307 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4309 } else if (upl
->flags
& UPL_LITE
) {
4313 pg_num
= upl
->size
/PAGE_SIZE
;
4314 pg_num
= (pg_num
+ 31) >> 5;
4317 for (i
= 0; i
< pg_num
; i
++) {
4318 if (lite_list
[i
] != 0) {
4324 if (queue_empty(&upl
->map_object
->memq
))
4327 if (occupied
== 0) {
4328 if (upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
)
4331 if (object
== shadow_object
) {
4333 * this is not a paging object
4334 * so we need to drop the paging reference
4335 * that was taken when we created the UPL
4336 * against this object
4338 vm_object_paging_end(shadow_object
);
4341 * we dontated the paging reference to
4342 * the map object... vm_pageout_object_terminate
4343 * will drop this reference
4347 vm_object_unlock(shadow_object
);
4348 if (object
!= shadow_object
)
4349 vm_object_unlock(object
);
4352 if (pgpgout_count
) {
4353 DTRACE_VM2(pgpgout
, int, pgpgout_count
, (uint64_t *), NULL
);
4356 return KERN_SUCCESS
;
4362 upl_offset_t offset
,
4367 upl_size_t xfer_size
;
4368 vm_object_t shadow_object
;
4370 vm_object_offset_t target_offset
;
4372 wpl_array_t lite_list
;
4374 int delayed_unlock
= 0;
4379 if (upl
== UPL_NULL
)
4380 return KERN_INVALID_ARGUMENT
;
4382 if ( (upl
->flags
& UPL_IO_WIRE
) && !(error
& UPL_ABORT_DUMP_PAGES
) )
4383 return upl_commit_range(upl
, offset
, size
, 0, NULL
, 0, empty
);
4385 if (upl
->flags
& UPL_DEVICE_MEMORY
)
4387 else if ((offset
+ size
) <= upl
->size
)
4390 return KERN_FAILURE
;
4394 if (upl
->flags
& UPL_INTERNAL
) {
4395 lite_list
= (wpl_array_t
)
4396 ((((uintptr_t)upl
) + sizeof(struct upl
))
4397 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4399 lite_list
= (wpl_array_t
)
4400 (((uintptr_t)upl
) + sizeof(struct upl
));
4402 object
= upl
->map_object
;
4404 if (upl
->flags
& UPL_SHADOWED
) {
4405 vm_object_lock(object
);
4406 shadow_object
= object
->shadow
;
4408 shadow_object
= object
;
4410 entry
= offset
/PAGE_SIZE
;
4411 target_offset
= (vm_object_offset_t
)offset
;
4414 * pageout_scan takes the vm_page_lock_queues first
4415 * then tries for the object lock... to avoid what
4416 * is effectively a lock inversion, we'll go to the
4417 * trouble of taking them in that same order... otherwise
4418 * if this object contains the majority of the pages resident
4419 * in the UBC (or a small set of large objects actively being
4420 * worked on contain the majority of the pages), we could
4421 * cause the pageout_scan thread to 'starve' in its attempt
4422 * to find pages to move to the free queue, since it has to
4423 * successfully acquire the object lock of any candidate page
4424 * before it can steal/clean it.
4426 for (j
= 0; ; j
++) {
4427 vm_page_lock_queues();
4429 if (vm_object_lock_try(shadow_object
))
4431 vm_page_unlock_queues();
4441 if (upl
->flags
& UPL_LITE
) {
4443 pg_num
= target_offset
/PAGE_SIZE
;
4445 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
4446 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
4448 m
= vm_page_lookup(shadow_object
, target_offset
+
4449 (upl
->offset
- shadow_object
->paging_offset
));
4452 if (upl
->flags
& UPL_SHADOWED
) {
4453 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
4458 if (m
== VM_PAGE_NULL
)
4459 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->shadow_offset
);
4462 if (m
!= VM_PAGE_NULL
) {
4465 boolean_t must_free
= TRUE
;
4467 m
->clustered
= FALSE
;
4469 * COPYOUT = FALSE case
4470 * check for error conditions which must
4471 * be passed back to the pages customer
4473 if (error
& UPL_ABORT_RESTART
) {
4479 } else if (error
& UPL_ABORT_UNAVAILABLE
) {
4483 } else if (error
& UPL_ABORT_ERROR
) {
4493 * If the page was already encrypted,
4494 * we don't really need to decrypt it
4495 * now. It will get decrypted later,
4496 * on demand, as soon as someone needs
4497 * to access its contents.
4500 m
->cleaning
= FALSE
;
4501 m
->encrypted_cleaning
= FALSE
;
4502 m
->overwriting
= FALSE
;
4503 PAGE_WAKEUP_DONE(m
);
4505 if (must_free
== TRUE
)
4508 vm_page_activate(m
);
4511 * Handle the trusted pager throttle.
4514 vm_pageout_throttle_up(m
);
4518 assert(m
->wire_count
== 1);
4522 m
->dump_cleaning
= FALSE
;
4523 m
->cleaning
= FALSE
;
4524 m
->encrypted_cleaning
= FALSE
;
4525 m
->overwriting
= FALSE
;
4527 vm_external_state_clr(m
->object
->existence_map
, m
->offset
);
4528 #endif /* MACH_PAGEMAP */
4529 if (error
& UPL_ABORT_DUMP_PAGES
) {
4530 pmap_disconnect(m
->phys_page
);
4533 if (error
& UPL_ABORT_REFERENCE
) {
4535 * we've been told to explictly
4536 * reference this page... for
4537 * file I/O, this is done by
4538 * implementing an LRU on the inactive q
4542 PAGE_WAKEUP_DONE(m
);
4546 if (delayed_unlock
++ > UPL_DELAYED_UNLOCK_LIMIT
) {
4548 * pageout_scan takes the vm_page_lock_queues first
4549 * then tries for the object lock... to avoid what
4550 * is effectively a lock inversion, we'll go to the
4551 * trouble of taking them in that same order... otherwise
4552 * if this object contains the majority of the pages resident
4553 * in the UBC (or a small set of large objects actively being
4554 * worked on contain the majority of the pages), we could
4555 * cause the pageout_scan thread to 'starve' in its attempt
4556 * to find pages to move to the free queue, since it has to
4557 * successfully acquire the object lock of any candidate page
4558 * before it can steal/clean it.
4560 vm_object_unlock(shadow_object
);
4561 mutex_yield(&vm_page_queue_lock
);
4563 for (j
= 0; ; j
++) {
4564 if (vm_object_lock_try(shadow_object
))
4566 vm_page_unlock_queues();
4568 vm_page_lock_queues();
4572 target_offset
+= PAGE_SIZE_64
;
4573 xfer_size
-= PAGE_SIZE
;
4577 vm_page_unlock_queues();
4581 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4583 } else if (upl
->flags
& UPL_LITE
) {
4587 pg_num
= upl
->size
/PAGE_SIZE
;
4588 pg_num
= (pg_num
+ 31) >> 5;
4591 for (i
= 0; i
< pg_num
; i
++) {
4592 if (lite_list
[i
] != 0) {
4598 if (queue_empty(&upl
->map_object
->memq
))
4601 if (occupied
== 0) {
4602 if (upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
)
4605 if (object
== shadow_object
) {
4607 * this is not a paging object
4608 * so we need to drop the paging reference
4609 * that was taken when we created the UPL
4610 * against this object
4612 vm_object_paging_end(shadow_object
);
4615 * we dontated the paging reference to
4616 * the map object... vm_pageout_object_terminate
4617 * will drop this reference
4621 vm_object_unlock(shadow_object
);
4622 if (object
!= shadow_object
)
4623 vm_object_unlock(object
);
4626 return KERN_SUCCESS
;
4637 return upl_abort_range(upl
, 0, upl
->size
, error
, &empty
);
4641 /* an option on commit should be wire */
4645 upl_page_info_t
*page_list
,
4646 mach_msg_type_number_t count
)
4650 return upl_commit_range(upl
, 0, upl
->size
, 0, page_list
, count
, &empty
);
4655 vm_object_iopl_request(
4657 vm_object_offset_t offset
,
4660 upl_page_info_array_t user_page_list
,
4661 unsigned int *page_list_count
,
4665 vm_object_offset_t dst_offset
;
4666 upl_size_t xfer_size
;
4669 wpl_array_t lite_list
= NULL
;
4670 int delayed_unlock
= 0;
4671 int no_zero_fill
= FALSE
;
4675 struct vm_object_fault_info fault_info
;
4678 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
4680 * For forward compatibility's sake,
4681 * reject any unknown flag.
4683 return KERN_INVALID_VALUE
;
4685 if (vm_lopage_poolsize
== 0)
4686 cntrl_flags
&= ~UPL_NEED_32BIT_ADDR
;
4688 if (cntrl_flags
& UPL_NEED_32BIT_ADDR
) {
4689 if ( (cntrl_flags
& (UPL_SET_IO_WIRE
| UPL_SET_LITE
)) != (UPL_SET_IO_WIRE
| UPL_SET_LITE
))
4690 return KERN_INVALID_VALUE
;
4692 if (object
->phys_contiguous
) {
4693 if ((offset
+ object
->shadow_offset
) >= (vm_object_offset_t
)max_valid_dma_address
)
4694 return KERN_INVALID_ADDRESS
;
4696 if (((offset
+ object
->shadow_offset
) + size
) >= (vm_object_offset_t
)max_valid_dma_address
)
4697 return KERN_INVALID_ADDRESS
;
4701 if (cntrl_flags
& UPL_ENCRYPT
) {
4704 * The paging path doesn't use this interface,
4705 * so we don't support the UPL_ENCRYPT flag
4706 * here. We won't encrypt the pages.
4708 assert(! (cntrl_flags
& UPL_ENCRYPT
));
4710 if (cntrl_flags
& UPL_NOZEROFILL
)
4711 no_zero_fill
= TRUE
;
4713 if (cntrl_flags
& UPL_COPYOUT_FROM
)
4714 prot
= VM_PROT_READ
;
4716 prot
= VM_PROT_READ
| VM_PROT_WRITE
;
4718 if (((size
/page_size
) > MAX_UPL_TRANSFER
) && !object
->phys_contiguous
)
4719 size
= MAX_UPL_TRANSFER
* page_size
;
4721 if (cntrl_flags
& UPL_SET_INTERNAL
) {
4722 if (page_list_count
!= NULL
)
4723 *page_list_count
= MAX_UPL_TRANSFER
;
4725 if (((cntrl_flags
& UPL_SET_INTERNAL
) && !(object
->phys_contiguous
)) &&
4726 ((page_list_count
!= NULL
) && (*page_list_count
!= 0) && *page_list_count
< (size
/page_size
)))
4727 return KERN_INVALID_ARGUMENT
;
4729 if ((!object
->internal
) && (object
->paging_offset
!= 0))
4730 panic("vm_object_iopl_request: external object with non-zero paging offset\n");
4733 if (object
->phys_contiguous
)
4738 if (cntrl_flags
& UPL_SET_INTERNAL
) {
4739 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
, UPL_IO_WIRE
, psize
);
4741 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
4742 lite_list
= (wpl_array_t
) (((uintptr_t)user_page_list
) +
4743 ((psize
/ PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4745 upl
= upl_create(UPL_CREATE_LITE
, UPL_IO_WIRE
, psize
);
4747 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
4750 user_page_list
[0].device
= FALSE
;
4753 upl
->map_object
= object
;
4756 vm_object_lock(object
);
4757 vm_object_paging_begin(object
);
4759 * paging in progress also protects the paging_offset
4761 upl
->offset
= offset
+ object
->paging_offset
;
4763 if (object
->phys_contiguous
) {
4765 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
4766 #endif /* UPL_DEBUG */
4768 vm_object_unlock(object
);
4771 * don't need any shadow mappings for this one
4772 * since it is already I/O memory
4774 upl
->flags
|= UPL_DEVICE_MEMORY
;
4776 upl
->highest_page
= (offset
+ object
->shadow_offset
+ size
- 1)>>PAGE_SHIFT
;
4778 if (user_page_list
) {
4779 user_page_list
[0].phys_addr
= (offset
+ object
->shadow_offset
)>>PAGE_SHIFT
;
4780 user_page_list
[0].device
= TRUE
;
4782 if (page_list_count
!= NULL
) {
4783 if (upl
->flags
& UPL_INTERNAL
)
4784 *page_list_count
= 0;
4786 *page_list_count
= 1;
4788 return KERN_SUCCESS
;
4791 * Protect user space from future COW operations
4793 object
->true_share
= TRUE
;
4795 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
4796 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
4799 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
4800 #endif /* UPL_DEBUG */
4802 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
4804 * The user requested that access to the pages in this URL
4805 * be blocked until the UPL is commited or aborted.
4807 upl
->flags
|= UPL_ACCESS_BLOCKED
;
4812 dst_offset
= offset
;
4814 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
4815 fault_info
.user_tag
= 0;
4816 fault_info
.lo_offset
= offset
;
4817 fault_info
.hi_offset
= offset
+ xfer_size
;
4818 fault_info
.no_cache
= FALSE
;
4821 vm_fault_return_t result
;
4824 dst_page
= vm_page_lookup(object
, dst_offset
);
4828 * If the page is encrypted, we need to decrypt it,
4829 * so force a soft page fault.
4831 if ((dst_page
== VM_PAGE_NULL
) || (dst_page
->busy
) ||
4832 (dst_page
->encrypted
) ||
4833 (dst_page
->unusual
&& (dst_page
->error
||
4834 dst_page
->restart
||
4836 dst_page
->fictitious
))) {
4840 kern_return_t error_code
;
4843 if (delayed_unlock
) {
4845 vm_page_unlock_queues();
4847 if (cntrl_flags
& UPL_SET_INTERRUPTIBLE
)
4848 interruptible
= THREAD_ABORTSAFE
;
4850 interruptible
= THREAD_UNINT
;
4852 fault_info
.interruptible
= interruptible
;
4853 fault_info
.cluster_size
= xfer_size
;
4855 result
= vm_fault_page(object
, dst_offset
,
4856 prot
| VM_PROT_WRITE
, FALSE
,
4857 &prot
, &dst_page
, &top_page
,
4859 &error_code
, no_zero_fill
,
4860 FALSE
, &fault_info
);
4864 case VM_FAULT_SUCCESS
:
4866 PAGE_WAKEUP_DONE(dst_page
);
4868 * Release paging references and
4869 * top-level placeholder page, if any.
4871 if (top_page
!= VM_PAGE_NULL
) {
4872 vm_object_t local_object
;
4874 local_object
= top_page
->object
;
4876 if (top_page
->object
!= dst_page
->object
) {
4877 vm_object_lock(local_object
);
4878 VM_PAGE_FREE(top_page
);
4879 vm_object_paging_end(local_object
);
4880 vm_object_unlock(local_object
);
4882 VM_PAGE_FREE(top_page
);
4883 vm_object_paging_end(local_object
);
4888 case VM_FAULT_RETRY
:
4889 vm_object_lock(object
);
4890 vm_object_paging_begin(object
);
4893 case VM_FAULT_FICTITIOUS_SHORTAGE
:
4894 vm_page_more_fictitious();
4896 vm_object_lock(object
);
4897 vm_object_paging_begin(object
);
4900 case VM_FAULT_MEMORY_SHORTAGE
:
4901 if (vm_page_wait(interruptible
)) {
4902 vm_object_lock(object
);
4903 vm_object_paging_begin(object
);
4908 case VM_FAULT_INTERRUPTED
:
4909 error_code
= MACH_SEND_INTERRUPTED
;
4910 case VM_FAULT_MEMORY_ERROR
:
4911 ret
= (error_code
? error_code
: KERN_MEMORY_ERROR
);
4913 vm_object_lock(object
);
4914 vm_object_paging_begin(object
);
4917 } while (result
!= VM_FAULT_SUCCESS
);
4920 if ( (cntrl_flags
& UPL_NEED_32BIT_ADDR
) &&
4921 dst_page
->phys_page
>= (max_valid_dma_address
>> PAGE_SHIFT
) ) {
4926 * support devices that can't DMA above 32 bits
4927 * by substituting pages from a pool of low address
4928 * memory for any pages we find above the 4G mark
4929 * can't substitute if the page is already wired because
4930 * we don't know whether that physical address has been
4931 * handed out to some other 64 bit capable DMA device to use
4933 if (dst_page
->wire_count
) {
4934 ret
= KERN_PROTECTION_FAILURE
;
4937 if (delayed_unlock
) {
4939 vm_page_unlock_queues();
4941 low_page
= vm_page_grablo();
4943 if (low_page
== VM_PAGE_NULL
) {
4944 ret
= KERN_RESOURCE_SHORTAGE
;
4948 * from here until the vm_page_replace completes
4949 * we musn't drop the object lock... we don't
4950 * want anyone refaulting this page in and using
4951 * it after we disconnect it... we want the fault
4952 * to find the new page being substituted.
4954 if (dst_page
->pmapped
)
4955 refmod
= pmap_disconnect(dst_page
->phys_page
);
4958 vm_page_copy(dst_page
, low_page
);
4960 low_page
->reference
= dst_page
->reference
;
4961 low_page
->dirty
= dst_page
->dirty
;
4963 if (refmod
& VM_MEM_REFERENCED
)
4964 low_page
->reference
= TRUE
;
4965 if (refmod
& VM_MEM_MODIFIED
)
4966 low_page
->dirty
= TRUE
;
4968 vm_page_lock_queues();
4969 vm_page_replace(low_page
, object
, dst_offset
);
4971 * keep the queue lock since we're going to
4972 * need it immediately
4976 dst_page
= low_page
;
4978 * vm_page_grablo returned the page marked
4979 * BUSY... we don't need a PAGE_WAKEUP_DONE
4980 * here, because we've never dropped the object lock
4982 dst_page
->busy
= FALSE
;
4984 if (delayed_unlock
== 0)
4985 vm_page_lock_queues();
4987 vm_page_wire(dst_page
);
4989 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
4991 * Mark the page "busy" to block any future page fault
4992 * on this page. We'll also remove the mapping
4993 * of all these pages before leaving this routine.
4995 assert(!dst_page
->fictitious
);
4996 dst_page
->busy
= TRUE
;
4998 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
4999 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
5002 * expect the page to be used
5003 * page queues lock must be held to set 'reference'
5005 dst_page
->reference
= TRUE
;
5007 if (!(cntrl_flags
& UPL_COPYOUT_FROM
))
5008 dst_page
->dirty
= TRUE
;
5010 if (dst_page
->phys_page
> upl
->highest_page
)
5011 upl
->highest_page
= dst_page
->phys_page
;
5013 if (user_page_list
) {
5014 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
5015 user_page_list
[entry
].dirty
= dst_page
->dirty
;
5016 user_page_list
[entry
].pageout
= dst_page
->pageout
;
5017 user_page_list
[entry
].absent
= dst_page
->absent
;
5018 user_page_list
[entry
].precious
= dst_page
->precious
;
5020 if (dst_page
->clustered
== TRUE
)
5021 user_page_list
[entry
].speculative
= dst_page
->speculative
;
5023 user_page_list
[entry
].speculative
= FALSE
;
5026 * someone is explicitly grabbing this page...
5027 * update clustered and speculative state
5030 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
5032 if (delayed_unlock
++ > UPL_DELAYED_UNLOCK_LIMIT
) {
5033 mutex_yield(&vm_page_queue_lock
);
5037 dst_offset
+= PAGE_SIZE_64
;
5038 xfer_size
-= PAGE_SIZE
;
5041 vm_page_unlock_queues();
5043 if (page_list_count
!= NULL
) {
5044 if (upl
->flags
& UPL_INTERNAL
)
5045 *page_list_count
= 0;
5046 else if (*page_list_count
> entry
)
5047 *page_list_count
= entry
;
5049 vm_object_unlock(object
);
5051 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
5053 * We've marked all the pages "busy" so that future
5054 * page faults will block.
5055 * Now remove the mapping for these pages, so that they
5056 * can't be accessed without causing a page fault.
5058 vm_object_pmap_protect(object
, offset
, (vm_object_size_t
)size
,
5059 PMAP_NULL
, 0, VM_PROT_NONE
);
5061 return KERN_SUCCESS
;
5065 vm_page_unlock_queues();
5067 for (; offset
< dst_offset
; offset
+= PAGE_SIZE
) {
5068 dst_page
= vm_page_lookup(object
, offset
);
5070 if (dst_page
== VM_PAGE_NULL
)
5071 panic("vm_object_iopl_request: Wired pages missing. \n");
5073 vm_page_lockspin_queues();
5074 vm_page_unwire(dst_page
);
5075 vm_page_unlock_queues();
5077 VM_STAT_INCR(reactivations
);
5079 vm_object_paging_end(object
);
5080 vm_object_unlock(object
);
5091 kern_return_t retval
;
5092 boolean_t upls_locked
;
5093 vm_object_t object1
, object2
;
5095 if (upl1
== UPL_NULL
|| upl2
== UPL_NULL
|| upl1
== upl2
) {
5096 return KERN_INVALID_ARGUMENT
;
5099 upls_locked
= FALSE
;
5102 * Since we need to lock both UPLs at the same time,
5103 * avoid deadlocks by always taking locks in the same order.
5112 upls_locked
= TRUE
; /* the UPLs will need to be unlocked */
5114 object1
= upl1
->map_object
;
5115 object2
= upl2
->map_object
;
5117 if (upl1
->offset
!= 0 || upl2
->offset
!= 0 ||
5118 upl1
->size
!= upl2
->size
) {
5120 * We deal only with full objects, not subsets.
5121 * That's because we exchange the entire backing store info
5122 * for the objects: pager, resident pages, etc... We can't do
5125 retval
= KERN_INVALID_VALUE
;
5130 * Tranpose the VM objects' backing store.
5132 retval
= vm_object_transpose(object1
, object2
,
5133 (vm_object_size_t
) upl1
->size
);
5135 if (retval
== KERN_SUCCESS
) {
5137 * Make each UPL point to the correct VM object, i.e. the
5138 * object holding the pages that the UPL refers to...
5141 queue_remove(&object1
->uplq
, upl1
, upl_t
, uplq
);
5142 queue_remove(&object2
->uplq
, upl2
, upl_t
, uplq
);
5144 upl1
->map_object
= object2
;
5145 upl2
->map_object
= object1
;
5147 queue_enter(&object1
->uplq
, upl2
, upl_t
, uplq
);
5148 queue_enter(&object2
->uplq
, upl1
, upl_t
, uplq
);
5159 upls_locked
= FALSE
;
5168 * Rationale: the user might have some encrypted data on disk (via
5169 * FileVault or any other mechanism). That data is then decrypted in
5170 * memory, which is safe as long as the machine is secure. But that
5171 * decrypted data in memory could be paged out to disk by the default
5172 * pager. The data would then be stored on disk in clear (not encrypted)
5173 * and it could be accessed by anyone who gets physical access to the
5174 * disk (if the laptop or the disk gets stolen for example). This weakens
5175 * the security offered by FileVault.
5177 * Solution: the default pager will optionally request that all the
5178 * pages it gathers for pageout be encrypted, via the UPL interfaces,
5179 * before it sends this UPL to disk via the vnode_pageout() path.
5183 * To avoid disrupting the VM LRU algorithms, we want to keep the
5184 * clean-in-place mechanisms, which allow us to send some extra pages to
5185 * swap (clustering) without actually removing them from the user's
5186 * address space. We don't want the user to unknowingly access encrypted
5187 * data, so we have to actually remove the encrypted pages from the page
5188 * table. When the user accesses the data, the hardware will fail to
5189 * locate the virtual page in its page table and will trigger a page
5190 * fault. We can then decrypt the page and enter it in the page table
5191 * again. Whenever we allow the user to access the contents of a page,
5192 * we have to make sure it's not encrypted.
5198 * Reserve of virtual addresses in the kernel address space.
5199 * We need to map the physical pages in the kernel, so that we
5200 * can call the encryption/decryption routines with a kernel
5201 * virtual address. We keep this pool of pre-allocated kernel
5202 * virtual addresses so that we don't have to scan the kernel's
5203 * virtaul address space each time we need to encrypt or decrypt
5205 * It would be nice to be able to encrypt and decrypt in physical
5206 * mode but that might not always be more efficient...
5208 decl_simple_lock_data(,vm_paging_lock
)
5209 #define VM_PAGING_NUM_PAGES 64
5210 vm_map_offset_t vm_paging_base_address
= 0;
5211 boolean_t vm_paging_page_inuse
[VM_PAGING_NUM_PAGES
] = { FALSE
, };
5212 int vm_paging_max_index
= 0;
5213 int vm_paging_page_waiter
= 0;
5214 int vm_paging_page_waiter_total
= 0;
5215 unsigned long vm_paging_no_kernel_page
= 0;
5216 unsigned long vm_paging_objects_mapped
= 0;
5217 unsigned long vm_paging_pages_mapped
= 0;
5218 unsigned long vm_paging_objects_mapped_slow
= 0;
5219 unsigned long vm_paging_pages_mapped_slow
= 0;
5222 vm_paging_map_init(void)
5225 vm_map_offset_t page_map_offset
;
5226 vm_map_entry_t map_entry
;
5228 assert(vm_paging_base_address
== 0);
5231 * Initialize our pool of pre-allocated kernel
5232 * virtual addresses.
5234 page_map_offset
= 0;
5235 kr
= vm_map_find_space(kernel_map
,
5237 VM_PAGING_NUM_PAGES
* PAGE_SIZE
,
5241 if (kr
!= KERN_SUCCESS
) {
5242 panic("vm_paging_map_init: kernel_map full\n");
5244 map_entry
->object
.vm_object
= kernel_object
;
5246 page_map_offset
- VM_MIN_KERNEL_ADDRESS
;
5247 vm_object_reference(kernel_object
);
5248 vm_map_unlock(kernel_map
);
5250 assert(vm_paging_base_address
== 0);
5251 vm_paging_base_address
= page_map_offset
;
5256 * vm_paging_map_object:
5257 * Maps part of a VM object's pages in the kernel
5258 * virtual address space, using the pre-allocated
5259 * kernel virtual addresses, if possible.
5261 * The VM object is locked. This lock will get
5262 * dropped and re-acquired though, so the caller
5263 * must make sure the VM object is kept alive
5264 * (by holding a VM map that has a reference
5265 * on it, for example, or taking an extra reference).
5266 * The page should also be kept busy to prevent
5267 * it from being reclaimed.
5270 vm_paging_map_object(
5271 vm_map_offset_t
*address
,
5274 vm_object_offset_t offset
,
5275 vm_map_size_t
*size
,
5276 boolean_t can_unlock_object
)
5279 vm_map_offset_t page_map_offset
;
5280 vm_map_size_t map_size
;
5281 vm_object_offset_t object_offset
;
5285 if (page
!= VM_PAGE_NULL
&& *size
== PAGE_SIZE
) {
5288 * Use one of the pre-allocated kernel virtual addresses
5289 * and just enter the VM page in the kernel address space
5290 * at that virtual address.
5292 simple_lock(&vm_paging_lock
);
5295 * Try and find an available kernel virtual address
5296 * from our pre-allocated pool.
5298 page_map_offset
= 0;
5300 for (i
= 0; i
< VM_PAGING_NUM_PAGES
; i
++) {
5301 if (vm_paging_page_inuse
[i
] == FALSE
) {
5303 vm_paging_base_address
+
5308 if (page_map_offset
!= 0) {
5309 /* found a space to map our page ! */
5313 if (can_unlock_object
) {
5315 * If we can afford to unlock the VM object,
5316 * let's take the slow path now...
5321 * We can't afford to unlock the VM object, so
5322 * let's wait for a space to become available...
5324 vm_paging_page_waiter_total
++;
5325 vm_paging_page_waiter
++;
5326 thread_sleep_fast_usimple_lock(&vm_paging_page_waiter
,
5329 vm_paging_page_waiter
--;
5330 /* ... and try again */
5333 if (page_map_offset
!= 0) {
5335 * We found a kernel virtual address;
5336 * map the physical page to that virtual address.
5338 if (i
> vm_paging_max_index
) {
5339 vm_paging_max_index
= i
;
5341 vm_paging_page_inuse
[i
] = TRUE
;
5342 simple_unlock(&vm_paging_lock
);
5344 if (page
->pmapped
== FALSE
) {
5345 pmap_sync_page_data_phys(page
->phys_page
);
5347 page
->pmapped
= TRUE
;
5350 * Keep the VM object locked over the PMAP_ENTER
5351 * and the actual use of the page by the kernel,
5352 * or this pmap mapping might get undone by a
5353 * vm_object_pmap_protect() call...
5355 PMAP_ENTER(kernel_pmap
,
5359 ((int) page
->object
->wimg_bits
&
5362 vm_paging_objects_mapped
++;
5363 vm_paging_pages_mapped
++;
5364 *address
= page_map_offset
;
5366 /* all done and mapped, ready to use ! */
5367 return KERN_SUCCESS
;
5371 * We ran out of pre-allocated kernel virtual
5372 * addresses. Just map the page in the kernel
5373 * the slow and regular way.
5375 vm_paging_no_kernel_page
++;
5376 simple_unlock(&vm_paging_lock
);
5379 if (! can_unlock_object
) {
5380 return KERN_NOT_SUPPORTED
;
5383 object_offset
= vm_object_trunc_page(offset
);
5384 map_size
= vm_map_round_page(*size
);
5387 * Try and map the required range of the object
5391 vm_object_reference_locked(object
); /* for the map entry */
5392 vm_object_unlock(object
);
5394 kr
= vm_map_enter(kernel_map
,
5405 if (kr
!= KERN_SUCCESS
) {
5408 vm_object_deallocate(object
); /* for the map entry */
5409 vm_object_lock(object
);
5416 * Enter the mapped pages in the page table now.
5418 vm_object_lock(object
);
5420 * VM object must be kept locked from before PMAP_ENTER()
5421 * until after the kernel is done accessing the page(s).
5422 * Otherwise, the pmap mappings in the kernel could be
5423 * undone by a call to vm_object_pmap_protect().
5426 for (page_map_offset
= 0;
5428 map_size
-= PAGE_SIZE_64
, page_map_offset
+= PAGE_SIZE_64
) {
5429 unsigned int cache_attr
;
5431 page
= vm_page_lookup(object
, offset
+ page_map_offset
);
5432 if (page
== VM_PAGE_NULL
) {
5433 printf("vm_paging_map_object: no page !?");
5434 vm_object_unlock(object
);
5435 kr
= vm_map_remove(kernel_map
, *address
, *size
,
5437 assert(kr
== KERN_SUCCESS
);
5440 vm_object_lock(object
);
5441 return KERN_MEMORY_ERROR
;
5443 if (page
->pmapped
== FALSE
) {
5444 pmap_sync_page_data_phys(page
->phys_page
);
5446 page
->pmapped
= TRUE
;
5447 page
->wpmapped
= TRUE
;
5448 cache_attr
= ((unsigned int) object
->wimg_bits
) & VM_WIMG_MASK
;
5450 //assert(pmap_verify_free(page->phys_page));
5451 PMAP_ENTER(kernel_pmap
,
5452 *address
+ page_map_offset
,
5459 vm_paging_objects_mapped_slow
++;
5460 vm_paging_pages_mapped_slow
+= map_size
/ PAGE_SIZE_64
;
5462 return KERN_SUCCESS
;
5467 * vm_paging_unmap_object:
5468 * Unmaps part of a VM object's pages from the kernel
5469 * virtual address space.
5471 * The VM object is locked. This lock will get
5472 * dropped and re-acquired though.
5475 vm_paging_unmap_object(
5477 vm_map_offset_t start
,
5478 vm_map_offset_t end
)
5483 if ((vm_paging_base_address
== 0) ||
5484 (start
< vm_paging_base_address
) ||
5485 (end
> (vm_paging_base_address
5486 + (VM_PAGING_NUM_PAGES
* PAGE_SIZE
)))) {
5488 * We didn't use our pre-allocated pool of
5489 * kernel virtual address. Deallocate the
5492 if (object
!= VM_OBJECT_NULL
) {
5493 vm_object_unlock(object
);
5495 kr
= vm_map_remove(kernel_map
, start
, end
, VM_MAP_NO_FLAGS
);
5496 if (object
!= VM_OBJECT_NULL
) {
5497 vm_object_lock(object
);
5499 assert(kr
== KERN_SUCCESS
);
5502 * We used a kernel virtual address from our
5503 * pre-allocated pool. Put it back in the pool
5506 assert(end
- start
== PAGE_SIZE
);
5507 i
= (start
- vm_paging_base_address
) >> PAGE_SHIFT
;
5509 /* undo the pmap mapping */
5510 pmap_remove(kernel_pmap
, start
, end
);
5512 simple_lock(&vm_paging_lock
);
5513 vm_paging_page_inuse
[i
] = FALSE
;
5514 if (vm_paging_page_waiter
) {
5515 thread_wakeup(&vm_paging_page_waiter
);
5517 simple_unlock(&vm_paging_lock
);
5524 * "iv" is the "initial vector". Ideally, we want to
5525 * have a different one for each page we encrypt, so that
5526 * crackers can't find encryption patterns too easily.
5528 #define SWAP_CRYPT_AES_KEY_SIZE 128 /* XXX 192 and 256 don't work ! */
5529 boolean_t swap_crypt_ctx_initialized
= FALSE
;
5530 aes_32t swap_crypt_key
[8]; /* big enough for a 256 key */
5531 aes_ctx swap_crypt_ctx
;
5532 const unsigned char swap_crypt_null_iv
[AES_BLOCK_SIZE
] = {0xa, };
5535 boolean_t swap_crypt_ctx_tested
= FALSE
;
5536 unsigned char swap_crypt_test_page_ref
[4096] __attribute__((aligned(4096)));
5537 unsigned char swap_crypt_test_page_encrypt
[4096] __attribute__((aligned(4096)));
5538 unsigned char swap_crypt_test_page_decrypt
[4096] __attribute__((aligned(4096)));
5541 extern u_long
random(void);
5544 * Initialize the encryption context: key and key size.
5546 void swap_crypt_ctx_initialize(void); /* forward */
5548 swap_crypt_ctx_initialize(void)
5553 * No need for locking to protect swap_crypt_ctx_initialized
5554 * because the first use of encryption will come from the
5555 * pageout thread (we won't pagein before there's been a pageout)
5556 * and there's only one pageout thread.
5558 if (swap_crypt_ctx_initialized
== FALSE
) {
5560 i
< (sizeof (swap_crypt_key
) /
5561 sizeof (swap_crypt_key
[0]));
5563 swap_crypt_key
[i
] = random();
5565 aes_encrypt_key((const unsigned char *) swap_crypt_key
,
5566 SWAP_CRYPT_AES_KEY_SIZE
,
5567 &swap_crypt_ctx
.encrypt
);
5568 aes_decrypt_key((const unsigned char *) swap_crypt_key
,
5569 SWAP_CRYPT_AES_KEY_SIZE
,
5570 &swap_crypt_ctx
.decrypt
);
5571 swap_crypt_ctx_initialized
= TRUE
;
5576 * Validate the encryption algorithms.
5578 if (swap_crypt_ctx_tested
== FALSE
) {
5580 for (i
= 0; i
< 4096; i
++) {
5581 swap_crypt_test_page_ref
[i
] = (char) i
;
5584 aes_encrypt_cbc(swap_crypt_test_page_ref
,
5586 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5587 swap_crypt_test_page_encrypt
,
5588 &swap_crypt_ctx
.encrypt
);
5590 aes_decrypt_cbc(swap_crypt_test_page_encrypt
,
5592 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5593 swap_crypt_test_page_decrypt
,
5594 &swap_crypt_ctx
.decrypt
);
5595 /* compare result with original */
5596 for (i
= 0; i
< 4096; i
++) {
5597 if (swap_crypt_test_page_decrypt
[i
] !=
5598 swap_crypt_test_page_ref
[i
]) {
5599 panic("encryption test failed");
5604 aes_encrypt_cbc(swap_crypt_test_page_decrypt
,
5606 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5607 swap_crypt_test_page_decrypt
,
5608 &swap_crypt_ctx
.encrypt
);
5609 /* decrypt in place */
5610 aes_decrypt_cbc(swap_crypt_test_page_decrypt
,
5612 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5613 swap_crypt_test_page_decrypt
,
5614 &swap_crypt_ctx
.decrypt
);
5615 for (i
= 0; i
< 4096; i
++) {
5616 if (swap_crypt_test_page_decrypt
[i
] !=
5617 swap_crypt_test_page_ref
[i
]) {
5618 panic("in place encryption test failed");
5622 swap_crypt_ctx_tested
= TRUE
;
5630 * Encrypt the given page, for secure paging.
5631 * The page might already be mapped at kernel virtual
5632 * address "kernel_mapping_offset". Otherwise, we need
5636 * The page's object is locked, but this lock will be released
5638 * The page is busy and not accessible by users (not entered in any pmap).
5643 vm_map_offset_t kernel_mapping_offset
)
5646 vm_map_size_t kernel_mapping_size
;
5647 vm_offset_t kernel_vaddr
;
5649 unsigned char aes_iv
[AES_BLOCK_SIZE
];
5651 memory_object_t pager_object
;
5652 vm_object_offset_t paging_offset
;
5656 if (! vm_pages_encrypted
) {
5657 vm_pages_encrypted
= TRUE
;
5661 assert(page
->dirty
|| page
->precious
);
5663 if (page
->encrypted
) {
5665 * Already encrypted: no need to do it again.
5667 vm_page_encrypt_already_encrypted_counter
++;
5670 ASSERT_PAGE_DECRYPTED(page
);
5673 * Take a paging-in-progress reference to keep the object
5674 * alive even if we have to unlock it (in vm_paging_map_object()
5677 vm_object_paging_begin(page
->object
);
5679 if (kernel_mapping_offset
== 0) {
5681 * The page hasn't already been mapped in kernel space
5682 * by the caller. Map it now, so that we can access
5683 * its contents and encrypt them.
5685 kernel_mapping_size
= PAGE_SIZE
;
5686 kr
= vm_paging_map_object(&kernel_mapping_offset
,
5690 &kernel_mapping_size
,
5692 if (kr
!= KERN_SUCCESS
) {
5693 panic("vm_page_encrypt: "
5694 "could not map page in kernel: 0x%x\n",
5698 kernel_mapping_size
= 0;
5700 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
5702 if (swap_crypt_ctx_initialized
== FALSE
) {
5703 swap_crypt_ctx_initialize();
5705 assert(swap_crypt_ctx_initialized
);
5708 * Prepare an "initial vector" for the encryption.
5709 * We use the "pager" and the "paging_offset" for that
5710 * page to obfuscate the encrypted data a bit more and
5711 * prevent crackers from finding patterns that they could
5712 * use to break the key.
5714 bzero(&encrypt_iv
.aes_iv
[0], sizeof (encrypt_iv
.aes_iv
));
5715 encrypt_iv
.vm
.pager_object
= page
->object
->pager
;
5716 encrypt_iv
.vm
.paging_offset
=
5717 page
->object
->paging_offset
+ page
->offset
;
5719 /* encrypt the "initial vector" */
5720 aes_encrypt_cbc((const unsigned char *) &encrypt_iv
.aes_iv
[0],
5723 &encrypt_iv
.aes_iv
[0],
5724 &swap_crypt_ctx
.encrypt
);
5729 aes_encrypt_cbc((const unsigned char *) kernel_vaddr
,
5730 &encrypt_iv
.aes_iv
[0],
5731 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5732 (unsigned char *) kernel_vaddr
,
5733 &swap_crypt_ctx
.encrypt
);
5735 vm_page_encrypt_counter
++;
5738 * Unmap the page from the kernel's address space,
5739 * if we had to map it ourselves. Otherwise, let
5740 * the caller undo the mapping if needed.
5742 if (kernel_mapping_size
!= 0) {
5743 vm_paging_unmap_object(page
->object
,
5744 kernel_mapping_offset
,
5745 kernel_mapping_offset
+ kernel_mapping_size
);
5749 * Clear the "reference" and "modified" bits.
5750 * This should clean up any impact the encryption had
5752 * The page was kept busy and disconnected from all pmaps,
5753 * so it can't have been referenced or modified from user
5755 * The software bits will be reset later after the I/O
5756 * has completed (in upl_commit_range()).
5758 pmap_clear_refmod(page
->phys_page
, VM_MEM_REFERENCED
| VM_MEM_MODIFIED
);
5760 page
->encrypted
= TRUE
;
5762 vm_object_paging_end(page
->object
);
5768 * Decrypt the given page.
5769 * The page might already be mapped at kernel virtual
5770 * address "kernel_mapping_offset". Otherwise, we need
5774 * The page's VM object is locked but will be unlocked and relocked.
5775 * The page is busy and not accessible by users (not entered in any pmap).
5780 vm_map_offset_t kernel_mapping_offset
)
5783 vm_map_size_t kernel_mapping_size
;
5784 vm_offset_t kernel_vaddr
;
5786 unsigned char aes_iv
[AES_BLOCK_SIZE
];
5788 memory_object_t pager_object
;
5789 vm_object_offset_t paging_offset
;
5794 assert(page
->encrypted
);
5797 * Take a paging-in-progress reference to keep the object
5798 * alive even if we have to unlock it (in vm_paging_map_object()
5801 vm_object_paging_begin(page
->object
);
5803 if (kernel_mapping_offset
== 0) {
5805 * The page hasn't already been mapped in kernel space
5806 * by the caller. Map it now, so that we can access
5807 * its contents and decrypt them.
5809 kernel_mapping_size
= PAGE_SIZE
;
5810 kr
= vm_paging_map_object(&kernel_mapping_offset
,
5814 &kernel_mapping_size
,
5816 if (kr
!= KERN_SUCCESS
) {
5817 panic("vm_page_decrypt: "
5818 "could not map page in kernel: 0x%x\n",
5822 kernel_mapping_size
= 0;
5824 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
5826 assert(swap_crypt_ctx_initialized
);
5829 * Prepare an "initial vector" for the decryption.
5830 * It has to be the same as the "initial vector" we
5831 * used to encrypt that page.
5833 bzero(&decrypt_iv
.aes_iv
[0], sizeof (decrypt_iv
.aes_iv
));
5834 decrypt_iv
.vm
.pager_object
= page
->object
->pager
;
5835 decrypt_iv
.vm
.paging_offset
=
5836 page
->object
->paging_offset
+ page
->offset
;
5838 /* encrypt the "initial vector" */
5839 aes_encrypt_cbc((const unsigned char *) &decrypt_iv
.aes_iv
[0],
5842 &decrypt_iv
.aes_iv
[0],
5843 &swap_crypt_ctx
.encrypt
);
5848 aes_decrypt_cbc((const unsigned char *) kernel_vaddr
,
5849 &decrypt_iv
.aes_iv
[0],
5850 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5851 (unsigned char *) kernel_vaddr
,
5852 &swap_crypt_ctx
.decrypt
);
5853 vm_page_decrypt_counter
++;
5856 * Unmap the page from the kernel's address space,
5857 * if we had to map it ourselves. Otherwise, let
5858 * the caller undo the mapping if needed.
5860 if (kernel_mapping_size
!= 0) {
5861 vm_paging_unmap_object(page
->object
,
5863 kernel_vaddr
+ PAGE_SIZE
);
5867 * After decryption, the page is actually clean.
5868 * It was encrypted as part of paging, which "cleans"
5869 * the "dirty" pages.
5870 * Noone could access it after it was encrypted
5871 * and the decryption doesn't count.
5873 page
->dirty
= FALSE
;
5874 if (page
->cs_validated
&& !page
->cs_tainted
) {
5877 * This page is no longer dirty
5878 * but could have been modified,
5879 * so it will need to be
5882 page
->cs_validated
= FALSE
;
5883 vm_cs_validated_resets
++;
5885 pmap_clear_refmod(page
->phys_page
, VM_MEM_MODIFIED
| VM_MEM_REFERENCED
);
5887 page
->encrypted
= FALSE
;
5890 * We've just modified the page's contents via the data cache and part
5891 * of the new contents might still be in the cache and not yet in RAM.
5892 * Since the page is now available and might get gathered in a UPL to
5893 * be part of a DMA transfer from a driver that expects the memory to
5894 * be coherent at this point, we have to flush the data cache.
5896 pmap_sync_page_attributes_phys(page
->phys_page
);
5898 * Since the page is not mapped yet, some code might assume that it
5899 * doesn't need to invalidate the instruction cache when writing to
5900 * that page. That code relies on "pmapped" being FALSE, so that the
5901 * caches get synchronized when the page is first mapped.
5903 assert(pmap_verify_free(page
->phys_page
));
5904 page
->pmapped
= FALSE
;
5905 page
->wpmapped
= FALSE
;
5907 vm_object_paging_end(page
->object
);
5910 unsigned long upl_encrypt_upls
= 0;
5911 unsigned long upl_encrypt_pages
= 0;
5917 * Encrypts all the pages in the UPL, within the specified range.
5923 upl_offset_t crypt_offset
,
5924 upl_size_t crypt_size
)
5926 upl_size_t upl_size
;
5927 upl_offset_t upl_offset
;
5928 vm_object_t upl_object
;
5930 vm_object_t shadow_object
;
5931 vm_object_offset_t shadow_offset
;
5932 vm_object_offset_t paging_offset
;
5933 vm_object_offset_t base_offset
;
5936 upl_encrypt_pages
+= crypt_size
/ PAGE_SIZE
;
5938 upl_object
= upl
->map_object
;
5939 upl_offset
= upl
->offset
;
5940 upl_size
= upl
->size
;
5942 vm_object_lock(upl_object
);
5945 * Find the VM object that contains the actual pages.
5947 if (upl_object
->pageout
) {
5948 shadow_object
= upl_object
->shadow
;
5950 * The offset in the shadow object is actually also
5951 * accounted for in upl->offset. It possibly shouldn't be
5952 * this way, but for now don't account for it twice.
5955 assert(upl_object
->paging_offset
== 0); /* XXX ? */
5956 vm_object_lock(shadow_object
);
5958 shadow_object
= upl_object
;
5962 paging_offset
= shadow_object
->paging_offset
;
5963 vm_object_paging_begin(shadow_object
);
5965 if (shadow_object
!= upl_object
)
5966 vm_object_unlock(upl_object
);
5969 base_offset
= shadow_offset
;
5970 base_offset
+= upl_offset
;
5971 base_offset
+= crypt_offset
;
5972 base_offset
-= paging_offset
;
5974 assert(crypt_offset
+ crypt_size
<= upl_size
);
5976 for (upl_offset
= 0;
5977 upl_offset
< crypt_size
;
5978 upl_offset
+= PAGE_SIZE
) {
5979 page
= vm_page_lookup(shadow_object
,
5980 base_offset
+ upl_offset
);
5981 if (page
== VM_PAGE_NULL
) {
5982 panic("upl_encrypt: "
5983 "no page for (obj=%p,off=%lld+%d)!\n",
5989 * Disconnect the page from all pmaps, so that nobody can
5990 * access it while it's encrypted. After that point, all
5991 * accesses to this page will cause a page fault and block
5992 * while the page is busy being encrypted. After the
5993 * encryption completes, any access will cause a
5994 * page fault and the page gets decrypted at that time.
5996 pmap_disconnect(page
->phys_page
);
5997 vm_page_encrypt(page
, 0);
5999 if (shadow_object
== vm_pageout_scan_wants_object
) {
6001 * Give vm_pageout_scan() a chance to convert more
6002 * pages from "clean-in-place" to "clean-and-free",
6003 * if it's interested in the same pages we selected
6006 vm_object_unlock(shadow_object
);
6007 vm_object_lock(shadow_object
);
6011 vm_object_paging_end(shadow_object
);
6012 vm_object_unlock(shadow_object
);
6019 __unused upl_offset_t crypt_offset
,
6020 __unused upl_size_t crypt_size
)
6026 __unused vm_page_t page
,
6027 __unused vm_map_offset_t kernel_mapping_offset
)
6033 __unused vm_page_t page
,
6034 __unused vm_map_offset_t kernel_mapping_offset
)
6041 upl_get_internal_pagelist_offset(void)
6043 return sizeof(struct upl
);
6052 upl
->flags
|= UPL_CLEAR_DIRTY
;
6054 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
6061 boolean_t
upl_device_page(upl_page_info_t
*upl
)
6063 return(UPL_DEVICE_PAGE(upl
));
6065 boolean_t
upl_page_present(upl_page_info_t
*upl
, int index
)
6067 return(UPL_PAGE_PRESENT(upl
, index
));
6069 boolean_t
upl_speculative_page(upl_page_info_t
*upl
, int index
)
6071 return(UPL_SPECULATIVE_PAGE(upl
, index
));
6073 boolean_t
upl_dirty_page(upl_page_info_t
*upl
, int index
)
6075 return(UPL_DIRTY_PAGE(upl
, index
));
6077 boolean_t
upl_valid_page(upl_page_info_t
*upl
, int index
)
6079 return(UPL_VALID_PAGE(upl
, index
));
6081 ppnum_t
upl_phys_page(upl_page_info_t
*upl
, int index
)
6083 return(UPL_PHYS_PAGE(upl
, index
));
6088 vm_countdirtypages(void)
6100 vm_page_lock_queues();
6101 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
6103 if (m
==(vm_page_t
)0) break;
6105 if(m
->dirty
) dpages
++;
6106 if(m
->pageout
) pgopages
++;
6107 if(m
->precious
) precpages
++;
6109 assert(m
->object
!= kernel_object
);
6110 m
= (vm_page_t
) queue_next(&m
->pageq
);
6111 if (m
==(vm_page_t
)0) break;
6113 } while (!queue_end(&vm_page_queue_inactive
,(queue_entry_t
) m
));
6114 vm_page_unlock_queues();
6116 vm_page_lock_queues();
6117 m
= (vm_page_t
) queue_first(&vm_page_queue_throttled
);
6119 if (m
==(vm_page_t
)0) break;
6123 assert(!m
->pageout
);
6124 assert(m
->object
!= kernel_object
);
6125 m
= (vm_page_t
) queue_next(&m
->pageq
);
6126 if (m
==(vm_page_t
)0) break;
6128 } while (!queue_end(&vm_page_queue_throttled
,(queue_entry_t
) m
));
6129 vm_page_unlock_queues();
6131 vm_page_lock_queues();
6132 m
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
6134 if (m
==(vm_page_t
)0) break;
6136 if(m
->dirty
) dpages
++;
6137 if(m
->pageout
) pgopages
++;
6138 if(m
->precious
) precpages
++;
6140 assert(m
->object
!= kernel_object
);
6141 m
= (vm_page_t
) queue_next(&m
->pageq
);
6142 if (m
==(vm_page_t
)0) break;
6144 } while (!queue_end(&vm_page_queue_zf
,(queue_entry_t
) m
));
6145 vm_page_unlock_queues();
6147 printf("IN Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
6153 vm_page_lock_queues();
6154 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
6157 if(m
== (vm_page_t
)0) break;
6158 if(m
->dirty
) dpages
++;
6159 if(m
->pageout
) pgopages
++;
6160 if(m
->precious
) precpages
++;
6162 assert(m
->object
!= kernel_object
);
6163 m
= (vm_page_t
) queue_next(&m
->pageq
);
6164 if(m
== (vm_page_t
)0) break;
6166 } while (!queue_end(&vm_page_queue_active
,(queue_entry_t
) m
));
6167 vm_page_unlock_queues();
6169 printf("AC Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
6172 #endif /* MACH_BSD */
6174 ppnum_t
upl_get_highest_page(
6177 return upl
->highest_page
;
6181 kern_return_t
upl_ubc_alias_set(upl_t upl
, unsigned int alias1
, unsigned int alias2
)
6183 upl
->ubc_alias1
= alias1
;
6184 upl
->ubc_alias2
= alias2
;
6185 return KERN_SUCCESS
;
6187 int upl_ubc_alias_get(upl_t upl
, unsigned int * al
, unsigned int * al2
)
6190 *al
= upl
->ubc_alias1
;
6192 *al2
= upl
->ubc_alias2
;
6193 return KERN_SUCCESS
;
6195 #endif /* UPL_DEBUG */
6200 #include <ddb/db_output.h>
6201 #include <ddb/db_print.h>
6202 #include <vm/vm_print.h>
6204 #define printf kdbprintf
6205 void db_pageout(void);
6211 iprintf("VM Statistics:\n");
6213 iprintf("pages:\n");
6215 iprintf("activ %5d inact %5d free %5d",
6216 vm_page_active_count
, vm_page_inactive_count
,
6217 vm_page_free_count
);
6218 printf(" wire %5d gobbl %5d\n",
6219 vm_page_wire_count
, vm_page_gobble_count
);
6221 iprintf("target:\n");
6223 iprintf("min %5d inact %5d free %5d",
6224 vm_page_free_min
, vm_page_inactive_target
,
6225 vm_page_free_target
);
6226 printf(" resrv %5d\n", vm_page_free_reserved
);
6228 iprintf("pause:\n");
6234 extern int c_laundry_pages_freed
;
6235 #endif /* MACH_COUNTERS */
6240 iprintf("Pageout Statistics:\n");
6242 iprintf("active %5d inactv %5d\n",
6243 vm_pageout_active
, vm_pageout_inactive
);
6244 iprintf("nolock %5d avoid %5d busy %5d absent %5d\n",
6245 vm_pageout_inactive_nolock
, vm_pageout_inactive_avoid
,
6246 vm_pageout_inactive_busy
, vm_pageout_inactive_absent
);
6247 iprintf("used %5d clean %5d dirty %5d\n",
6248 vm_pageout_inactive_used
, vm_pageout_inactive_clean
,
6249 vm_pageout_inactive_dirty
);
6251 iprintf("laundry_pages_freed %d\n", c_laundry_pages_freed
);
6252 #endif /* MACH_COUNTERS */
6253 #if MACH_CLUSTER_STATS
6254 iprintf("Cluster Statistics:\n");
6256 iprintf("dirtied %5d cleaned %5d collisions %5d\n",
6257 vm_pageout_cluster_dirtied
, vm_pageout_cluster_cleaned
,
6258 vm_pageout_cluster_collisions
);
6259 iprintf("clusters %5d conversions %5d\n",
6260 vm_pageout_cluster_clusters
, vm_pageout_cluster_conversions
);
6262 iprintf("Target Statistics:\n");
6264 iprintf("collisions %5d page_dirtied %5d page_freed %5d\n",
6265 vm_pageout_target_collisions
, vm_pageout_target_page_dirtied
,
6266 vm_pageout_target_page_freed
);
6268 #endif /* MACH_CLUSTER_STATS */
6272 #endif /* MACH_KDB */