2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 * File: vm/vm_pageout.c
55 * Author: Avadis Tevanian, Jr., Michael Wayne Young
58 * The proverbial page-out daemon.
64 #include <mach_pagemap.h>
65 #include <mach_cluster_stats.h>
67 #include <advisory_pageout.h>
69 #include <mach/mach_types.h>
70 #include <mach/memory_object.h>
71 #include <mach/memory_object_default.h>
72 #include <mach/memory_object_control_server.h>
73 #include <mach/mach_host_server.h>
75 #include <mach/vm_map.h>
76 #include <mach/vm_param.h>
77 #include <mach/vm_statistics.h>
79 #include <kern/kern_types.h>
80 #include <kern/counters.h>
81 #include <kern/host_statistics.h>
82 #include <kern/machine.h>
83 #include <kern/misc_protos.h>
84 #include <kern/thread.h>
86 #include <kern/kalloc.h>
88 #include <machine/vm_tuning.h>
91 #include <vm/vm_fault.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_pageout.h>
96 #include <vm/vm_protos.h> /* must be last */
102 #include <ppc/mappings.h>
104 #include <../bsd/crypto/aes/aes.h>
106 extern ipc_port_t memory_manager_default
;
109 #ifndef VM_PAGEOUT_BURST_ACTIVE_THROTTLE
110 #define VM_PAGEOUT_BURST_ACTIVE_THROTTLE 10000 /* maximum iterations of the active queue to move pages to inactive */
113 #ifndef VM_PAGEOUT_BURST_INACTIVE_THROTTLE
114 #define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 4096 /* maximum iterations of the inactive queue w/o stealing/cleaning a page */
117 #ifndef VM_PAGEOUT_DEADLOCK_RELIEF
118 #define VM_PAGEOUT_DEADLOCK_RELIEF 100 /* number of pages to move to break deadlock */
121 #ifndef VM_PAGEOUT_INACTIVE_RELIEF
122 #define VM_PAGEOUT_INACTIVE_RELIEF 50 /* minimum number of pages to move to the inactive q */
125 #ifndef VM_PAGE_LAUNDRY_MAX
126 #define VM_PAGE_LAUNDRY_MAX 16UL /* maximum pageouts on a given pageout queue */
127 #endif /* VM_PAGEOUT_LAUNDRY_MAX */
129 #ifndef VM_PAGEOUT_BURST_WAIT
130 #define VM_PAGEOUT_BURST_WAIT 30 /* milliseconds per page */
131 #endif /* VM_PAGEOUT_BURST_WAIT */
133 #ifndef VM_PAGEOUT_EMPTY_WAIT
134 #define VM_PAGEOUT_EMPTY_WAIT 200 /* milliseconds */
135 #endif /* VM_PAGEOUT_EMPTY_WAIT */
137 #ifndef VM_PAGEOUT_DEADLOCK_WAIT
138 #define VM_PAGEOUT_DEADLOCK_WAIT 300 /* milliseconds */
139 #endif /* VM_PAGEOUT_DEADLOCK_WAIT */
141 #ifndef VM_PAGEOUT_IDLE_WAIT
142 #define VM_PAGEOUT_IDLE_WAIT 10 /* milliseconds */
143 #endif /* VM_PAGEOUT_IDLE_WAIT */
147 * To obtain a reasonable LRU approximation, the inactive queue
148 * needs to be large enough to give pages on it a chance to be
149 * referenced a second time. This macro defines the fraction
150 * of active+inactive pages that should be inactive.
151 * The pageout daemon uses it to update vm_page_inactive_target.
153 * If vm_page_free_count falls below vm_page_free_target and
154 * vm_page_inactive_count is below vm_page_inactive_target,
155 * then the pageout daemon starts running.
158 #ifndef VM_PAGE_INACTIVE_TARGET
159 #define VM_PAGE_INACTIVE_TARGET(avail) ((avail) * 1 / 3)
160 #endif /* VM_PAGE_INACTIVE_TARGET */
163 * Once the pageout daemon starts running, it keeps going
164 * until vm_page_free_count meets or exceeds vm_page_free_target.
167 #ifndef VM_PAGE_FREE_TARGET
168 #define VM_PAGE_FREE_TARGET(free) (15 + (free) / 80)
169 #endif /* VM_PAGE_FREE_TARGET */
172 * The pageout daemon always starts running once vm_page_free_count
173 * falls below vm_page_free_min.
176 #ifndef VM_PAGE_FREE_MIN
177 #define VM_PAGE_FREE_MIN(free) (10 + (free) / 100)
178 #endif /* VM_PAGE_FREE_MIN */
181 * When vm_page_free_count falls below vm_page_free_reserved,
182 * only vm-privileged threads can allocate pages. vm-privilege
183 * allows the pageout daemon and default pager (and any other
184 * associated threads needed for default pageout) to continue
185 * operation by dipping into the reserved pool of pages.
188 #ifndef VM_PAGE_FREE_RESERVED
189 #define VM_PAGE_FREE_RESERVED(n) \
190 ((6 * VM_PAGE_LAUNDRY_MAX) + (n))
191 #endif /* VM_PAGE_FREE_RESERVED */
195 * must hold the page queues lock to
196 * manipulate this structure
198 struct vm_pageout_queue
{
199 queue_head_t pgo_pending
; /* laundry pages to be processed by pager's iothread */
200 unsigned int pgo_laundry
; /* current count of laundry pages on queue or in flight */
201 unsigned int pgo_maxlaundry
;
203 unsigned int pgo_idle
:1, /* iothread is blocked waiting for work to do */
204 pgo_busy
:1, /* iothread is currently processing request from pgo_pending */
205 pgo_throttled
:1,/* vm_pageout_scan thread needs a wakeup when pgo_laundry drops */
209 #define VM_PAGE_Q_THROTTLED(q) \
210 ((q)->pgo_laundry >= (q)->pgo_maxlaundry)
214 * Exported variable used to broadcast the activation of the pageout scan
215 * Working Set uses this to throttle its use of pmap removes. In this
216 * way, code which runs within memory in an uncontested context does
217 * not keep encountering soft faults.
220 unsigned int vm_pageout_scan_event_counter
= 0;
223 * Forward declarations for internal routines.
226 static void vm_pageout_garbage_collect(int);
227 static void vm_pageout_iothread_continue(struct vm_pageout_queue
*);
228 static void vm_pageout_iothread_external(void);
229 static void vm_pageout_iothread_internal(void);
230 static void vm_pageout_queue_steal(vm_page_t
);
232 extern void vm_pageout_continue(void);
233 extern void vm_pageout_scan(void);
235 unsigned int vm_pageout_reserved_internal
= 0;
236 unsigned int vm_pageout_reserved_really
= 0;
238 unsigned int vm_pageout_idle_wait
= 0; /* milliseconds */
239 unsigned int vm_pageout_empty_wait
= 0; /* milliseconds */
240 unsigned int vm_pageout_burst_wait
= 0; /* milliseconds */
241 unsigned int vm_pageout_deadlock_wait
= 0; /* milliseconds */
242 unsigned int vm_pageout_deadlock_relief
= 0;
243 unsigned int vm_pageout_inactive_relief
= 0;
244 unsigned int vm_pageout_burst_active_throttle
= 0;
245 unsigned int vm_pageout_burst_inactive_throttle
= 0;
248 * Protection against zero fill flushing live working sets derived
249 * from existing backing store and files
251 unsigned int vm_accellerate_zf_pageout_trigger
= 400;
252 unsigned int vm_zf_iterator
;
253 unsigned int vm_zf_iterator_count
= 40;
254 unsigned int last_page_zf
;
255 unsigned int vm_zf_count
= 0;
258 * These variables record the pageout daemon's actions:
259 * how many pages it looks at and what happens to those pages.
260 * No locking needed because only one thread modifies the variables.
263 unsigned int vm_pageout_active
= 0; /* debugging */
264 unsigned int vm_pageout_inactive
= 0; /* debugging */
265 unsigned int vm_pageout_inactive_throttled
= 0; /* debugging */
266 unsigned int vm_pageout_inactive_forced
= 0; /* debugging */
267 unsigned int vm_pageout_inactive_nolock
= 0; /* debugging */
268 unsigned int vm_pageout_inactive_avoid
= 0; /* debugging */
269 unsigned int vm_pageout_inactive_busy
= 0; /* debugging */
270 unsigned int vm_pageout_inactive_absent
= 0; /* debugging */
271 unsigned int vm_pageout_inactive_used
= 0; /* debugging */
272 unsigned int vm_pageout_inactive_clean
= 0; /* debugging */
273 unsigned int vm_pageout_inactive_dirty
= 0; /* debugging */
274 unsigned int vm_pageout_dirty_no_pager
= 0; /* debugging */
275 unsigned int vm_pageout_purged_objects
= 0; /* debugging */
276 unsigned int vm_stat_discard
= 0; /* debugging */
277 unsigned int vm_stat_discard_sent
= 0; /* debugging */
278 unsigned int vm_stat_discard_failure
= 0; /* debugging */
279 unsigned int vm_stat_discard_throttle
= 0; /* debugging */
281 unsigned int vm_pageout_scan_active_throttled
= 0;
282 unsigned int vm_pageout_scan_inactive_throttled
= 0;
283 unsigned int vm_pageout_scan_throttle
= 0; /* debugging */
284 unsigned int vm_pageout_scan_burst_throttle
= 0; /* debugging */
285 unsigned int vm_pageout_scan_empty_throttle
= 0; /* debugging */
286 unsigned int vm_pageout_scan_deadlock_detected
= 0; /* debugging */
287 unsigned int vm_pageout_scan_active_throttle_success
= 0; /* debugging */
288 unsigned int vm_pageout_scan_inactive_throttle_success
= 0; /* debugging */
290 * Backing store throttle when BS is exhausted
292 unsigned int vm_backing_store_low
= 0;
294 unsigned int vm_pageout_out_of_line
= 0;
295 unsigned int vm_pageout_in_place
= 0;
299 * counters and statistics...
301 unsigned long vm_page_decrypt_counter
= 0;
302 unsigned long vm_page_decrypt_for_upl_counter
= 0;
303 unsigned long vm_page_encrypt_counter
= 0;
304 unsigned long vm_page_encrypt_abort_counter
= 0;
305 unsigned long vm_page_encrypt_already_encrypted_counter
= 0;
306 boolean_t vm_pages_encrypted
= FALSE
; /* are there encrypted pages ? */
309 struct vm_pageout_queue vm_pageout_queue_internal
;
310 struct vm_pageout_queue vm_pageout_queue_external
;
314 * Routine: vm_backing_store_disable
316 * Suspend non-privileged threads wishing to extend
317 * backing store when we are low on backing store
318 * (Synchronized by caller)
321 vm_backing_store_disable(
325 vm_backing_store_low
= 1;
327 if(vm_backing_store_low
) {
328 vm_backing_store_low
= 0;
329 thread_wakeup((event_t
) &vm_backing_store_low
);
336 * Routine: vm_pageout_object_allocate
338 * Allocate an object for use as out-of-line memory in a
339 * data_return/data_initialize message.
340 * The page must be in an unlocked object.
342 * If the page belongs to a trusted pager, cleaning in place
343 * will be used, which utilizes a special "pageout object"
344 * containing private alias pages for the real page frames.
345 * Untrusted pagers use normal out-of-line memory.
348 vm_pageout_object_allocate(
351 vm_object_offset_t offset
)
353 vm_object_t object
= m
->object
;
354 vm_object_t new_object
;
356 assert(object
->pager_ready
);
358 new_object
= vm_object_allocate(size
);
360 if (object
->pager_trusted
) {
361 assert (offset
< object
->size
);
363 vm_object_lock(new_object
);
364 new_object
->pageout
= TRUE
;
365 new_object
->shadow
= object
;
366 new_object
->can_persist
= FALSE
;
367 new_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
368 new_object
->shadow_offset
= offset
;
369 vm_object_unlock(new_object
);
372 * Take a paging reference on the object. This will be dropped
373 * in vm_pageout_object_terminate()
375 vm_object_lock(object
);
376 vm_object_paging_begin(object
);
377 vm_page_lock_queues();
378 vm_page_unlock_queues();
379 vm_object_unlock(object
);
381 vm_pageout_in_place
++;
383 vm_pageout_out_of_line
++;
387 #if MACH_CLUSTER_STATS
388 unsigned long vm_pageout_cluster_dirtied
= 0;
389 unsigned long vm_pageout_cluster_cleaned
= 0;
390 unsigned long vm_pageout_cluster_collisions
= 0;
391 unsigned long vm_pageout_cluster_clusters
= 0;
392 unsigned long vm_pageout_cluster_conversions
= 0;
393 unsigned long vm_pageout_target_collisions
= 0;
394 unsigned long vm_pageout_target_page_dirtied
= 0;
395 unsigned long vm_pageout_target_page_freed
= 0;
396 #define CLUSTER_STAT(clause) clause
397 #else /* MACH_CLUSTER_STATS */
398 #define CLUSTER_STAT(clause)
399 #endif /* MACH_CLUSTER_STATS */
402 * Routine: vm_pageout_object_terminate
404 * Destroy the pageout_object allocated by
405 * vm_pageout_object_allocate(), and perform all of the
406 * required cleanup actions.
409 * The object must be locked, and will be returned locked.
412 vm_pageout_object_terminate(
415 vm_object_t shadow_object
;
416 boolean_t shadow_internal
;
419 * Deal with the deallocation (last reference) of a pageout object
420 * (used for cleaning-in-place) by dropping the paging references/
421 * freeing pages in the original object.
424 assert(object
->pageout
);
425 shadow_object
= object
->shadow
;
426 vm_object_lock(shadow_object
);
427 shadow_internal
= shadow_object
->internal
;
429 while (!queue_empty(&object
->memq
)) {
431 vm_object_offset_t offset
;
433 p
= (vm_page_t
) queue_first(&object
->memq
);
438 assert(!p
->cleaning
);
444 m
= vm_page_lookup(shadow_object
,
445 offset
+ object
->shadow_offset
);
447 if(m
== VM_PAGE_NULL
)
450 /* used as a trigger on upl_commit etc to recognize the */
451 /* pageout daemon's subseqent desire to pageout a cleaning */
452 /* page. When the bit is on the upl commit code will */
453 /* respect the pageout bit in the target page over the */
454 /* caller's page list indication */
455 m
->dump_cleaning
= FALSE
;
458 * Account for the paging reference taken when
459 * m->cleaning was set on this page.
461 vm_object_paging_end(shadow_object
);
462 assert((m
->dirty
) || (m
->precious
) ||
463 (m
->busy
&& m
->cleaning
));
466 * Handle the trusted pager throttle.
467 * Also decrement the burst throttle (if external).
469 vm_page_lock_queues();
471 vm_pageout_throttle_up(m
);
475 * Handle the "target" page(s). These pages are to be freed if
476 * successfully cleaned. Target pages are always busy, and are
477 * wired exactly once. The initial target pages are not mapped,
478 * (so cannot be referenced or modified) but converted target
479 * pages may have been modified between the selection as an
480 * adjacent page and conversion to a target.
484 assert(m
->wire_count
== 1);
487 #if MACH_CLUSTER_STATS
488 if (m
->wanted
) vm_pageout_target_collisions
++;
491 * Revoke all access to the page. Since the object is
492 * locked, and the page is busy, this prevents the page
493 * from being dirtied after the pmap_disconnect() call
496 * Since the page is left "dirty" but "not modifed", we
497 * can detect whether the page was redirtied during
498 * pageout by checking the modify state.
500 if (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
)
506 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
507 vm_page_unwire(m
);/* reactivates */
508 VM_STAT(reactivations
++);
511 CLUSTER_STAT(vm_pageout_target_page_freed
++;)
512 vm_page_free(m
);/* clears busy, etc. */
514 vm_page_unlock_queues();
518 * Handle the "adjacent" pages. These pages were cleaned in
519 * place, and should be left alone.
520 * If prep_pin_count is nonzero, then someone is using the
521 * page, so make it active.
523 if (!m
->active
&& !m
->inactive
&& !m
->private) {
527 vm_page_deactivate(m
);
529 if((m
->busy
) && (m
->cleaning
)) {
531 /* the request_page_list case, (COPY_OUT_FROM FALSE) */
534 /* We do not re-set m->dirty ! */
535 /* The page was busy so no extraneous activity */
536 /* could have occurred. COPY_INTO is a read into the */
537 /* new pages. CLEAN_IN_PLACE does actually write */
538 /* out the pages but handling outside of this code */
539 /* will take care of resetting dirty. We clear the */
540 /* modify however for the Programmed I/O case. */
541 pmap_clear_modify(m
->phys_page
);
544 if(shadow_object
->absent_count
== 1)
545 vm_object_absent_release(shadow_object
);
547 shadow_object
->absent_count
--;
549 m
->overwriting
= FALSE
;
550 } else if (m
->overwriting
) {
551 /* alternate request page list, write to page_list */
552 /* case. Occurs when the original page was wired */
553 /* at the time of the list request */
554 assert(m
->wire_count
!= 0);
555 vm_page_unwire(m
);/* reactivates */
556 m
->overwriting
= FALSE
;
559 * Set the dirty state according to whether or not the page was
560 * modified during the pageout. Note that we purposefully do
561 * NOT call pmap_clear_modify since the page is still mapped.
562 * If the page were to be dirtied between the 2 calls, this
563 * this fact would be lost. This code is only necessary to
564 * maintain statistics, since the pmap module is always
565 * consulted if m->dirty is false.
567 #if MACH_CLUSTER_STATS
568 m
->dirty
= pmap_is_modified(m
->phys_page
);
570 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
571 else vm_pageout_cluster_cleaned
++;
572 if (m
->wanted
) vm_pageout_cluster_collisions
++;
580 * Wakeup any thread waiting for the page to be un-cleaning.
583 vm_page_unlock_queues();
586 * Account for the paging reference taken in vm_paging_object_allocate.
588 vm_object_paging_end(shadow_object
);
589 vm_object_unlock(shadow_object
);
591 assert(object
->ref_count
== 0);
592 assert(object
->paging_in_progress
== 0);
593 assert(object
->resident_page_count
== 0);
598 * Routine: vm_pageout_setup
600 * Set up a page for pageout (clean & flush).
602 * Move the page to a new object, as part of which it will be
603 * sent to its memory manager in a memory_object_data_write or
604 * memory_object_initialize message.
606 * The "new_object" and "new_offset" arguments
607 * indicate where the page should be moved.
610 * The page in question must not be on any pageout queues,
611 * and must be busy. The object to which it belongs
612 * must be unlocked, and the caller must hold a paging
613 * reference to it. The new_object must not be locked.
615 * This routine returns a pointer to a place-holder page,
616 * inserted at the same offset, to block out-of-order
617 * requests for the page. The place-holder page must
618 * be freed after the data_write or initialize message
621 * The original page is put on a paging queue and marked
626 register vm_page_t m
,
627 register vm_object_t new_object
,
628 vm_object_offset_t new_offset
)
630 register vm_object_t old_object
= m
->object
;
631 vm_object_offset_t paging_offset
;
632 vm_object_offset_t offset
;
633 register vm_page_t holding_page
;
634 register vm_page_t new_m
;
635 boolean_t need_to_wire
= FALSE
;
639 "vm_pageout_setup, obj 0x%X off 0x%X page 0x%X new obj 0x%X offset 0x%X\n",
640 (integer_t
)m
->object
, (integer_t
)m
->offset
,
641 (integer_t
)m
, (integer_t
)new_object
,
642 (integer_t
)new_offset
);
643 assert(m
&& m
->busy
&& !m
->absent
&& !m
->fictitious
&& !m
->error
&&
646 assert(m
->dirty
|| m
->precious
);
649 * Create a place-holder page where the old one was, to prevent
650 * attempted pageins of this page while we're unlocked.
652 VM_PAGE_GRAB_FICTITIOUS(holding_page
);
654 vm_object_lock(old_object
);
657 paging_offset
= offset
+ old_object
->paging_offset
;
659 if (old_object
->pager_trusted
) {
661 * This pager is trusted, so we can clean this page
662 * in place. Leave it in the old object, and mark it
663 * cleaning & pageout.
665 new_m
= holding_page
;
666 holding_page
= VM_PAGE_NULL
;
669 * Set up new page to be private shadow of real page.
671 new_m
->phys_page
= m
->phys_page
;
672 new_m
->fictitious
= FALSE
;
673 new_m
->pageout
= TRUE
;
676 * Mark real page as cleaning (indicating that we hold a
677 * paging reference to be released via m_o_d_r_c) and
678 * pageout (indicating that the page should be freed
679 * when the pageout completes).
681 pmap_clear_modify(m
->phys_page
);
682 vm_page_lock_queues();
683 new_m
->private = TRUE
;
689 assert(m
->wire_count
== 1);
690 vm_page_unlock_queues();
694 m
->page_lock
= VM_PROT_NONE
;
696 m
->unlock_request
= VM_PROT_NONE
;
699 * Cannot clean in place, so rip the old page out of the
700 * object, and stick the holding page in. Set new_m to the
701 * page in the new object.
703 vm_page_lock_queues();
704 VM_PAGE_QUEUES_REMOVE(m
);
707 vm_page_insert(holding_page
, old_object
, offset
);
708 vm_page_unlock_queues();
713 new_m
->page_lock
= VM_PROT_NONE
;
714 new_m
->unlock_request
= VM_PROT_NONE
;
716 if (old_object
->internal
)
720 * Record that this page has been written out
723 vm_external_state_set(old_object
->existence_map
, offset
);
724 #endif /* MACH_PAGEMAP */
726 vm_object_unlock(old_object
);
728 vm_object_lock(new_object
);
731 * Put the page into the new object. If it is a not wired
732 * (if it's the real page) it will be activated.
735 vm_page_lock_queues();
736 vm_page_insert(new_m
, new_object
, new_offset
);
740 vm_page_activate(new_m
);
741 PAGE_WAKEUP_DONE(new_m
);
742 vm_page_unlock_queues();
744 vm_object_unlock(new_object
);
747 * Return the placeholder page to simplify cleanup.
749 return (holding_page
);
753 * Routine: vm_pageclean_setup
755 * Purpose: setup a page to be cleaned (made non-dirty), but not
756 * necessarily flushed from the VM page cache.
757 * This is accomplished by cleaning in place.
759 * The page must not be busy, and the object and page
760 * queues must be locked.
767 vm_object_t new_object
,
768 vm_object_offset_t new_offset
)
770 vm_object_t old_object
= m
->object
;
772 assert(!m
->cleaning
);
775 "vm_pageclean_setup, obj 0x%X off 0x%X page 0x%X new 0x%X new_off 0x%X\n",
776 (integer_t
)old_object
, m
->offset
, (integer_t
)m
,
777 (integer_t
)new_m
, new_offset
);
779 pmap_clear_modify(m
->phys_page
);
780 vm_object_paging_begin(old_object
);
783 * Record that this page has been written out
786 vm_external_state_set(old_object
->existence_map
, m
->offset
);
787 #endif /*MACH_PAGEMAP*/
790 * Mark original page as cleaning in place.
797 * Convert the fictitious page to a private shadow of
800 assert(new_m
->fictitious
);
801 new_m
->fictitious
= FALSE
;
802 new_m
->private = TRUE
;
803 new_m
->pageout
= TRUE
;
804 new_m
->phys_page
= m
->phys_page
;
807 vm_page_insert(new_m
, new_object
, new_offset
);
808 assert(!new_m
->wanted
);
816 vm_object_t new_object
,
817 vm_object_offset_t new_offset
)
820 "vm_pageclean_copy, page 0x%X new_m 0x%X new_obj 0x%X offset 0x%X\n",
821 m
, new_m
, new_object
, new_offset
, 0);
823 assert((!m
->busy
) && (!m
->cleaning
));
825 assert(!new_m
->private && !new_m
->fictitious
);
827 pmap_clear_modify(m
->phys_page
);
830 vm_object_paging_begin(m
->object
);
831 vm_page_unlock_queues();
832 vm_object_unlock(m
->object
);
835 * Copy the original page to the new page.
837 vm_page_copy(m
, new_m
);
840 * Mark the old page as clean. A request to pmap_is_modified
841 * will get the right answer.
843 vm_object_lock(m
->object
);
846 vm_object_paging_end(m
->object
);
848 vm_page_lock_queues();
849 if (!m
->active
&& !m
->inactive
)
853 vm_page_insert(new_m
, new_object
, new_offset
);
854 vm_page_activate(new_m
);
855 new_m
->busy
= FALSE
; /* No other thread can be waiting */
860 * Routine: vm_pageout_initialize_page
862 * Causes the specified page to be initialized in
863 * the appropriate memory object. This routine is used to push
864 * pages into a copy-object when they are modified in the
867 * The page is moved to a temporary object and paged out.
870 * The page in question must not be on any pageout queues.
871 * The object to which it belongs must be locked.
872 * The page must be busy, but not hold a paging reference.
875 * Move this page to a completely new object.
878 vm_pageout_initialize_page(
882 vm_object_offset_t paging_offset
;
883 vm_page_t holding_page
;
887 "vm_pageout_initialize_page, page 0x%X\n",
888 (integer_t
)m
, 0, 0, 0, 0);
892 * Verify that we really want to clean this page
899 * Create a paging reference to let us play with the object.
902 paging_offset
= m
->offset
+ object
->paging_offset
;
903 vm_object_paging_begin(object
);
904 if (m
->absent
|| m
->error
|| m
->restart
||
905 (!m
->dirty
&& !m
->precious
)) {
907 panic("reservation without pageout?"); /* alan */
908 vm_object_unlock(object
);
912 /* set the page for future call to vm_fault_list_request */
914 vm_page_lock_queues();
915 pmap_clear_modify(m
->phys_page
);
918 m
->list_req_pending
= TRUE
;
922 vm_page_unlock_queues();
923 vm_object_unlock(object
);
926 * Write the data to its pager.
927 * Note that the data is passed by naming the new object,
928 * not a virtual address; the pager interface has been
929 * manipulated to use the "internal memory" data type.
930 * [The object reference from its allocation is donated
931 * to the eventual recipient.]
933 memory_object_data_initialize(object
->pager
,
937 vm_object_lock(object
);
940 #if MACH_CLUSTER_STATS
941 #define MAXCLUSTERPAGES 16
943 unsigned long pages_in_cluster
;
944 unsigned long pages_at_higher_offsets
;
945 unsigned long pages_at_lower_offsets
;
946 } cluster_stats
[MAXCLUSTERPAGES
];
947 #endif /* MACH_CLUSTER_STATS */
949 boolean_t allow_clustered_pageouts
= FALSE
;
952 * vm_pageout_cluster:
954 * Given a page, queue it to the appropriate I/O thread,
955 * which will page it out and attempt to clean adjacent pages
956 * in the same operation.
958 * The page must be busy, and the object and queues locked. We will take a
959 * paging reference to prevent deallocation or collapse when we
960 * release the object lock back at the call site. The I/O thread
961 * is responsible for consuming this reference
963 * The page must not be on any pageout queue.
967 vm_pageout_cluster(vm_page_t m
)
969 vm_object_t object
= m
->object
;
970 struct vm_pageout_queue
*q
;
974 "vm_pageout_cluster, object 0x%X offset 0x%X page 0x%X\n",
975 (integer_t
)object
, m
->offset
, (integer_t
)m
, 0, 0);
978 * Only a certain kind of page is appreciated here.
980 assert(m
->busy
&& (m
->dirty
|| m
->precious
) && (m
->wire_count
== 0));
981 assert(!m
->cleaning
&& !m
->pageout
&& !m
->inactive
&& !m
->active
);
984 * protect the object from collapse -
985 * locking in the object's paging_offset.
987 vm_object_paging_begin(object
);
990 * set the page for future call to vm_fault_list_request
991 * page should already be marked busy
994 m
->list_req_pending
= TRUE
;
999 if (object
->internal
== TRUE
)
1000 q
= &vm_pageout_queue_internal
;
1002 q
= &vm_pageout_queue_external
;
1005 m
->pageout_queue
= TRUE
;
1006 queue_enter(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
1008 if (q
->pgo_idle
== TRUE
) {
1009 q
->pgo_idle
= FALSE
;
1010 thread_wakeup((event_t
) &q
->pgo_pending
);
1015 unsigned long vm_pageout_throttle_up_count
= 0;
1018 * A page is back from laundry. See if there are some pages waiting to
1019 * go to laundry and if we can let some of them go now.
1021 * Object and page queues must be locked.
1024 vm_pageout_throttle_up(
1027 struct vm_pageout_queue
*q
;
1029 vm_pageout_throttle_up_count
++;
1032 assert(m
->object
!= VM_OBJECT_NULL
);
1033 assert(m
->object
!= kernel_object
);
1035 if (m
->object
->internal
== TRUE
)
1036 q
= &vm_pageout_queue_internal
;
1038 q
= &vm_pageout_queue_external
;
1043 if (q
->pgo_throttled
== TRUE
) {
1044 q
->pgo_throttled
= FALSE
;
1045 thread_wakeup((event_t
) &q
->pgo_laundry
);
1051 * vm_pageout_scan does the dirty work for the pageout daemon.
1052 * It returns with vm_page_queue_free_lock held and
1053 * vm_page_free_wanted == 0.
1056 #define DELAYED_UNLOCK_LIMIT (3 * MAX_UPL_TRANSFER)
1059 #define FCS_DELAYED 1
1060 #define FCS_DEADLOCK_DETECTED 2
1062 struct flow_control
{
1067 extern kern_return_t
sysclk_gettime(mach_timespec_t
*);
1071 vm_pageout_scan(void)
1073 unsigned int loop_count
= 0;
1074 unsigned int inactive_burst_count
= 0;
1075 unsigned int active_burst_count
= 0;
1076 vm_page_t local_freeq
= 0;
1077 int local_freed
= 0;
1078 int delayed_unlock
= 0;
1079 int need_internal_inactive
= 0;
1080 int refmod_state
= 0;
1081 int vm_pageout_deadlock_target
= 0;
1082 struct vm_pageout_queue
*iq
;
1083 struct vm_pageout_queue
*eq
;
1084 struct flow_control flow_control
;
1085 boolean_t active_throttled
= FALSE
;
1086 boolean_t inactive_throttled
= FALSE
;
1088 unsigned int msecs
= 0;
1092 flow_control
.state
= FCS_IDLE
;
1093 iq
= &vm_pageout_queue_internal
;
1094 eq
= &vm_pageout_queue_external
;
1096 XPR(XPR_VM_PAGEOUT
, "vm_pageout_scan\n", 0, 0, 0, 0, 0);
1099 * We want to gradually dribble pages from the active queue
1100 * to the inactive queue. If we let the inactive queue get
1101 * very small, and then suddenly dump many pages into it,
1102 * those pages won't get a sufficient chance to be referenced
1103 * before we start taking them from the inactive queue.
1105 * We must limit the rate at which we send pages to the pagers.
1106 * data_write messages consume memory, for message buffers and
1107 * for map-copy objects. If we get too far ahead of the pagers,
1108 * we can potentially run out of memory.
1110 * We can use the laundry count to limit directly the number
1111 * of pages outstanding to the default pager. A similar
1112 * strategy for external pagers doesn't work, because
1113 * external pagers don't have to deallocate the pages sent them,
1114 * and because we might have to send pages to external pagers
1115 * even if they aren't processing writes. So we also
1116 * use a burst count to limit writes to external pagers.
1118 * When memory is very tight, we can't rely on external pagers to
1119 * clean pages. They probably aren't running, because they
1120 * aren't vm-privileged. If we kept sending dirty pages to them,
1121 * we could exhaust the free list.
1123 vm_page_lock_queues();
1129 * Recalculate vm_page_inactivate_target.
1131 vm_page_inactive_target
= VM_PAGE_INACTIVE_TARGET(vm_page_active_count
+
1132 vm_page_inactive_count
);
1138 if (delayed_unlock
== 0)
1139 vm_page_lock_queues();
1141 active_burst_count
= vm_page_active_count
;
1143 if (active_burst_count
> vm_pageout_burst_active_throttle
)
1144 active_burst_count
= vm_pageout_burst_active_throttle
;
1147 * Move pages from active to inactive.
1149 while ((need_internal_inactive
||
1150 vm_page_inactive_count
< vm_page_inactive_target
) &&
1151 !queue_empty(&vm_page_queue_active
) &&
1152 ((active_burst_count
--) > 0)) {
1154 vm_pageout_active
++;
1156 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
1158 assert(m
->active
&& !m
->inactive
);
1159 assert(!m
->laundry
);
1160 assert(m
->object
!= kernel_object
);
1163 * Try to lock object; since we've already got the
1164 * page queues lock, we can only 'try' for this one.
1165 * if the 'try' fails, we need to do a mutex_pause
1166 * to allow the owner of the object lock a chance to
1167 * run... otherwise, we're likely to trip over this
1168 * object in the same state as we work our way through
1169 * the queue... clumps of pages associated with the same
1170 * object are fairly typical on the inactive and active queues
1172 if (m
->object
!= object
) {
1173 if (object
!= NULL
) {
1174 vm_object_unlock(object
);
1177 if (!vm_object_lock_try(m
->object
)) {
1179 * move page to end of active queue and continue
1181 queue_remove(&vm_page_queue_active
, m
,
1183 queue_enter(&vm_page_queue_active
, m
,
1186 goto done_with_activepage
;
1191 * if the page is BUSY, then we pull it
1192 * off the active queue and leave it alone.
1193 * when BUSY is cleared, it will get stuck
1194 * back on the appropriate queue
1197 queue_remove(&vm_page_queue_active
, m
,
1199 m
->pageq
.next
= NULL
;
1200 m
->pageq
.prev
= NULL
;
1203 vm_page_active_count
--;
1206 goto done_with_activepage
;
1208 if (need_internal_inactive
) {
1210 * If we're unable to make forward progress
1211 * with the current set of pages on the
1212 * inactive queue due to busy objects or
1213 * throttled pageout queues, then
1214 * move a page that is already clean
1215 * or belongs to a pageout queue that
1216 * isn't currently throttled
1218 active_throttled
= FALSE
;
1220 if (object
->internal
) {
1221 if ((VM_PAGE_Q_THROTTLED(iq
) || !IP_VALID(memory_manager_default
)))
1222 active_throttled
= TRUE
;
1223 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
1224 active_throttled
= TRUE
;
1226 if (active_throttled
== TRUE
) {
1228 refmod_state
= pmap_get_refmod(m
->phys_page
);
1230 if (refmod_state
& VM_MEM_REFERENCED
)
1231 m
->reference
= TRUE
;
1232 if (refmod_state
& VM_MEM_MODIFIED
)
1235 if (m
->dirty
|| m
->precious
) {
1237 * page is dirty and targets a THROTTLED queue
1238 * so all we can do is move it back to the
1239 * end of the active queue to get it out
1242 queue_remove(&vm_page_queue_active
, m
,
1244 queue_enter(&vm_page_queue_active
, m
,
1247 vm_pageout_scan_active_throttled
++;
1249 goto done_with_activepage
;
1252 vm_pageout_scan_active_throttle_success
++;
1253 need_internal_inactive
--;
1256 * Deactivate the page while holding the object
1257 * locked, so we know the page is still not busy.
1258 * This should prevent races between pmap_enter
1259 * and pmap_clear_reference. The page might be
1260 * absent or fictitious, but vm_page_deactivate
1263 vm_page_deactivate(m
);
1264 done_with_activepage
:
1265 if (delayed_unlock
++ > DELAYED_UNLOCK_LIMIT
) {
1267 if (object
!= NULL
) {
1268 vm_object_unlock(object
);
1272 vm_page_free_list(local_freeq
);
1278 vm_page_unlock_queues();
1281 vm_page_lock_queues();
1283 * continue the while loop processing
1284 * the active queue... need to hold
1285 * the page queues lock
1293 /**********************************************************************
1294 * above this point we're playing with the active queue
1295 * below this point we're playing with the throttling mechanisms
1296 * and the inactive queue
1297 **********************************************************************/
1302 * We are done if we have met our target *and*
1303 * nobody is still waiting for a page.
1305 if (vm_page_free_count
+ local_freed
>= vm_page_free_target
) {
1306 if (object
!= NULL
) {
1307 vm_object_unlock(object
);
1311 vm_page_free_list(local_freeq
);
1316 mutex_lock(&vm_page_queue_free_lock
);
1318 if ((vm_page_free_count
>= vm_page_free_target
) &&
1319 (vm_page_free_wanted
== 0)) {
1321 vm_page_unlock_queues();
1323 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
1326 mutex_unlock(&vm_page_queue_free_lock
);
1331 * Sometimes we have to pause:
1332 * 1) No inactive pages - nothing to do.
1333 * 2) Flow control - default pageout queue is full
1334 * 3) Loop control - no acceptable pages found on the inactive queue
1335 * within the last vm_pageout_burst_inactive_throttle iterations
1337 if ((queue_empty(&vm_page_queue_inactive
) && queue_empty(&vm_page_queue_zf
))) {
1338 vm_pageout_scan_empty_throttle
++;
1339 msecs
= vm_pageout_empty_wait
;
1340 goto vm_pageout_scan_delay
;
1342 } else if (inactive_burst_count
>= vm_pageout_burst_inactive_throttle
) {
1343 vm_pageout_scan_burst_throttle
++;
1344 msecs
= vm_pageout_burst_wait
;
1345 goto vm_pageout_scan_delay
;
1347 } else if (VM_PAGE_Q_THROTTLED(iq
)) {
1349 switch (flow_control
.state
) {
1352 reset_deadlock_timer
:
1353 ts
.tv_sec
= vm_pageout_deadlock_wait
/ 1000;
1354 ts
.tv_nsec
= (vm_pageout_deadlock_wait
% 1000) * 1000 * NSEC_PER_USEC
;
1355 sysclk_gettime(&flow_control
.ts
);
1356 ADD_MACH_TIMESPEC(&flow_control
.ts
, &ts
);
1358 flow_control
.state
= FCS_DELAYED
;
1359 msecs
= vm_pageout_deadlock_wait
;
1364 sysclk_gettime(&ts
);
1366 if (CMP_MACH_TIMESPEC(&ts
, &flow_control
.ts
) >= 0) {
1368 * the pageout thread for the default pager is potentially
1369 * deadlocked since the
1370 * default pager queue has been throttled for more than the
1371 * allowable time... we need to move some clean pages or dirty
1372 * pages belonging to the external pagers if they aren't throttled
1373 * vm_page_free_wanted represents the number of threads currently
1374 * blocked waiting for pages... we'll move one page for each of
1375 * these plus a fixed amount to break the logjam... once we're done
1376 * moving this number of pages, we'll re-enter the FSC_DELAYED state
1377 * with a new timeout target since we have no way of knowing
1378 * whether we've broken the deadlock except through observation
1379 * of the queue associated with the default pager... we need to
1380 * stop moving pagings and allow the system to run to see what
1381 * state it settles into.
1383 vm_pageout_deadlock_target
= vm_pageout_deadlock_relief
+ vm_page_free_wanted
;
1384 vm_pageout_scan_deadlock_detected
++;
1385 flow_control
.state
= FCS_DEADLOCK_DETECTED
;
1387 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
1388 goto consider_inactive
;
1391 * just resniff instead of trying
1392 * to compute a new delay time... we're going to be
1393 * awakened immediately upon a laundry completion,
1394 * so we won't wait any longer than necessary
1396 msecs
= vm_pageout_idle_wait
;
1399 case FCS_DEADLOCK_DETECTED
:
1400 if (vm_pageout_deadlock_target
)
1401 goto consider_inactive
;
1402 goto reset_deadlock_timer
;
1405 vm_pageout_scan_throttle
++;
1406 iq
->pgo_throttled
= TRUE
;
1407 vm_pageout_scan_delay
:
1408 if (object
!= NULL
) {
1409 vm_object_unlock(object
);
1413 vm_page_free_list(local_freeq
);
1418 assert_wait_timeout((event_t
) &iq
->pgo_laundry
, THREAD_INTERRUPTIBLE
, msecs
, 1000*NSEC_PER_USEC
);
1420 counter(c_vm_pageout_scan_block
++);
1422 vm_page_unlock_queues();
1424 thread_block(THREAD_CONTINUE_NULL
);
1426 vm_page_lock_queues();
1429 iq
->pgo_throttled
= FALSE
;
1431 if (loop_count
>= vm_page_inactive_count
) {
1432 if (VM_PAGE_Q_THROTTLED(eq
) || VM_PAGE_Q_THROTTLED(iq
)) {
1434 * Make sure we move enough "appropriate"
1435 * pages to the inactive queue before trying
1438 need_internal_inactive
= vm_pageout_inactive_relief
;
1442 inactive_burst_count
= 0;
1449 flow_control
.state
= FCS_IDLE
;
1452 inactive_burst_count
++;
1453 vm_pageout_inactive
++;
1455 if (!queue_empty(&vm_page_queue_inactive
)) {
1456 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
1458 if (m
->clustered
&& (m
->no_isync
== TRUE
)) {
1462 if (vm_zf_count
< vm_accellerate_zf_pageout_trigger
) {
1466 if((vm_zf_iterator
+=1) >= vm_zf_iterator_count
) {
1470 if (queue_empty(&vm_page_queue_zf
) ||
1471 (((last_page_zf
) || (vm_zf_iterator
== 0)) &&
1472 !queue_empty(&vm_page_queue_inactive
))) {
1473 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
1476 m
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
1480 assert(!m
->active
&& m
->inactive
);
1481 assert(!m
->laundry
);
1482 assert(m
->object
!= kernel_object
);
1485 * Try to lock object; since we've alread got the
1486 * page queues lock, we can only 'try' for this one.
1487 * if the 'try' fails, we need to do a mutex_pause
1488 * to allow the owner of the object lock a chance to
1489 * run... otherwise, we're likely to trip over this
1490 * object in the same state as we work our way through
1491 * the queue... clumps of pages associated with the same
1492 * object are fairly typical on the inactive and active queues
1494 if (m
->object
!= object
) {
1495 if (object
!= NULL
) {
1496 vm_object_unlock(object
);
1499 if (!vm_object_lock_try(m
->object
)) {
1501 * Move page to end and continue.
1502 * Don't re-issue ticket
1505 queue_remove(&vm_page_queue_zf
, m
,
1507 queue_enter(&vm_page_queue_zf
, m
,
1510 queue_remove(&vm_page_queue_inactive
, m
,
1512 queue_enter(&vm_page_queue_inactive
, m
,
1515 vm_pageout_inactive_nolock
++;
1518 * force us to dump any collected free pages
1519 * and to pause before moving on
1521 delayed_unlock
= DELAYED_UNLOCK_LIMIT
+ 1;
1523 goto done_with_inactivepage
;
1528 * If the page belongs to a purgable object with no pending copies
1529 * against it, then we reap all of the pages in the object
1530 * and note that the object has been "emptied". It'll be up to the
1531 * application the discover this and recreate its contents if desired.
1533 if ((object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
1534 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) &&
1535 object
->copy
== VM_OBJECT_NULL
) {
1537 (void) vm_object_purge(object
);
1538 vm_pageout_purged_objects
++;
1540 * we've just taken all of the pages from this object,
1541 * so drop the lock now since we're not going to find
1542 * any more pages belonging to it anytime soon
1544 vm_object_unlock(object
);
1547 inactive_burst_count
= 0;
1549 goto done_with_inactivepage
;
1553 * Paging out pages of external objects which
1554 * are currently being created must be avoided.
1555 * The pager may claim for memory, thus leading to a
1556 * possible dead lock between it and the pageout thread,
1557 * if such pages are finally chosen. The remaining assumption
1558 * is that there will finally be enough available pages in the
1559 * inactive pool to page out in order to satisfy all memory
1560 * claimed by the thread which concurrently creates the pager.
1562 if (!object
->pager_initialized
&& object
->pager_created
) {
1564 * Move page to end and continue, hoping that
1565 * there will be enough other inactive pages to
1566 * page out so that the thread which currently
1567 * initializes the pager will succeed.
1568 * Don't re-grant the ticket, the page should
1569 * pulled from the queue and paged out whenever
1570 * one of its logically adjacent fellows is
1574 queue_remove(&vm_page_queue_zf
, m
,
1576 queue_enter(&vm_page_queue_zf
, m
,
1579 vm_zf_iterator
= vm_zf_iterator_count
- 1;
1581 queue_remove(&vm_page_queue_inactive
, m
,
1583 queue_enter(&vm_page_queue_inactive
, m
,
1588 vm_pageout_inactive_avoid
++;
1590 goto done_with_inactivepage
;
1593 * Remove the page from the inactive list.
1596 queue_remove(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
1598 queue_remove(&vm_page_queue_inactive
, m
, vm_page_t
, pageq
);
1600 m
->pageq
.next
= NULL
;
1601 m
->pageq
.prev
= NULL
;
1602 m
->inactive
= FALSE
;
1604 vm_page_inactive_count
--;
1606 if (m
->busy
|| !object
->alive
) {
1608 * Somebody is already playing with this page.
1609 * Leave it off the pageout queues.
1611 vm_pageout_inactive_busy
++;
1613 goto done_with_inactivepage
;
1617 * If it's absent or in error, we can reclaim the page.
1620 if (m
->absent
|| m
->error
) {
1621 vm_pageout_inactive_absent
++;
1623 if (vm_pageout_deadlock_target
) {
1624 vm_pageout_scan_inactive_throttle_success
++;
1625 vm_pageout_deadlock_target
--;
1628 vm_page_remove(m
); /* clears tabled, object, offset */
1630 vm_object_absent_release(object
);
1632 assert(m
->pageq
.next
== NULL
&&
1633 m
->pageq
.prev
== NULL
);
1634 m
->pageq
.next
= (queue_entry_t
)local_freeq
;
1638 inactive_burst_count
= 0;
1640 goto done_with_inactivepage
;
1643 assert(!m
->private);
1644 assert(!m
->fictitious
);
1647 * If already cleaning this page in place, convert from
1648 * "adjacent" to "target". We can leave the page mapped,
1649 * and vm_pageout_object_terminate will determine whether
1650 * to free or reactivate.
1656 m
->dump_cleaning
= TRUE
;
1659 CLUSTER_STAT(vm_pageout_cluster_conversions
++);
1661 inactive_burst_count
= 0;
1663 goto done_with_inactivepage
;
1667 * If it's being used, reactivate.
1668 * (Fictitious pages are either busy or absent.)
1670 if ( (!m
->reference
) ) {
1671 refmod_state
= pmap_get_refmod(m
->phys_page
);
1673 if (refmod_state
& VM_MEM_REFERENCED
)
1674 m
->reference
= TRUE
;
1675 if (refmod_state
& VM_MEM_MODIFIED
)
1680 vm_page_activate(m
);
1681 VM_STAT(reactivations
++);
1683 vm_pageout_inactive_used
++;
1685 inactive_burst_count
= 0;
1687 goto done_with_inactivepage
;
1691 "vm_pageout_scan, replace object 0x%X offset 0x%X page 0x%X\n",
1692 (integer_t
)object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
1695 * we've got a candidate page to steal...
1697 * m->dirty is up to date courtesy of the
1698 * preceding check for m->reference... if
1699 * we get here, then m->reference had to be
1700 * FALSE which means we did a pmap_get_refmod
1701 * and updated both m->reference and m->dirty
1703 * if it's dirty or precious we need to
1704 * see if the target queue is throtttled
1705 * it if is, we need to skip over it by moving it back
1706 * to the end of the inactive queue
1708 inactive_throttled
= FALSE
;
1710 if (m
->dirty
|| m
->precious
) {
1711 if (object
->internal
) {
1712 if ((VM_PAGE_Q_THROTTLED(iq
) || !IP_VALID(memory_manager_default
)))
1713 inactive_throttled
= TRUE
;
1714 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
1715 inactive_throttled
= TRUE
;
1718 if (inactive_throttled
== TRUE
) {
1720 queue_enter(&vm_page_queue_zf
, m
,
1723 queue_enter(&vm_page_queue_inactive
, m
,
1727 vm_page_inactive_count
++;
1730 vm_pageout_scan_inactive_throttled
++;
1732 goto done_with_inactivepage
;
1735 * we've got a page that we can steal...
1736 * eliminate all mappings and make sure
1737 * we have the up-to-date modified state
1738 * first take the page BUSY, so that no new
1739 * mappings can be made
1744 * if we need to do a pmap_disconnect then we
1745 * need to re-evaluate m->dirty since the pmap_disconnect
1746 * provides the true state atomically... the
1747 * page was still mapped up to the pmap_disconnect
1748 * and may have been dirtied at the last microsecond
1750 * we also check for the page being referenced 'late'
1751 * if it was, we first need to do a WAKEUP_DONE on it
1752 * since we already set m->busy = TRUE, before
1753 * going off to reactivate it
1755 * if we don't need the pmap_disconnect, then
1756 * m->dirty is up to date courtesy of the
1757 * earlier check for m->reference... if
1758 * we get here, then m->reference had to be
1759 * FALSE which means we did a pmap_get_refmod
1760 * and updated both m->reference and m->dirty...
1762 if (m
->no_isync
== FALSE
) {
1763 refmod_state
= pmap_disconnect(m
->phys_page
);
1765 if (refmod_state
& VM_MEM_MODIFIED
)
1767 if (refmod_state
& VM_MEM_REFERENCED
) {
1768 m
->reference
= TRUE
;
1770 PAGE_WAKEUP_DONE(m
);
1771 goto was_referenced
;
1775 * If it's clean and not precious, we can free the page.
1777 if (!m
->dirty
&& !m
->precious
) {
1778 vm_pageout_inactive_clean
++;
1781 vm_pageout_cluster(m
);
1783 vm_pageout_inactive_dirty
++;
1785 inactive_burst_count
= 0;
1787 done_with_inactivepage
:
1788 if (delayed_unlock
++ > DELAYED_UNLOCK_LIMIT
) {
1790 if (object
!= NULL
) {
1791 vm_object_unlock(object
);
1795 vm_page_free_list(local_freeq
);
1801 vm_page_unlock_queues();
1805 * back to top of pageout scan loop
1811 int vm_page_free_count_init
;
1814 vm_page_free_reserve(
1817 int free_after_reserve
;
1819 vm_page_free_reserved
+= pages
;
1821 free_after_reserve
= vm_page_free_count_init
- vm_page_free_reserved
;
1823 vm_page_free_min
= vm_page_free_reserved
+
1824 VM_PAGE_FREE_MIN(free_after_reserve
);
1826 vm_page_free_target
= vm_page_free_reserved
+
1827 VM_PAGE_FREE_TARGET(free_after_reserve
);
1829 if (vm_page_free_target
< vm_page_free_min
+ 5)
1830 vm_page_free_target
= vm_page_free_min
+ 5;
1834 * vm_pageout is the high level pageout daemon.
1838 vm_pageout_continue(void)
1840 vm_pageout_scan_event_counter
++;
1842 /* we hold vm_page_queue_free_lock now */
1843 assert(vm_page_free_wanted
== 0);
1844 assert_wait((event_t
) &vm_page_free_wanted
, THREAD_UNINT
);
1845 mutex_unlock(&vm_page_queue_free_lock
);
1847 counter(c_vm_pageout_block
++);
1848 thread_block((thread_continue_t
)vm_pageout_continue
);
1854 * must be called with the
1855 * queues and object locks held
1858 vm_pageout_queue_steal(vm_page_t m
)
1860 struct vm_pageout_queue
*q
;
1862 if (m
->object
->internal
== TRUE
)
1863 q
= &vm_pageout_queue_internal
;
1865 q
= &vm_pageout_queue_external
;
1868 m
->pageout_queue
= FALSE
;
1869 queue_remove(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
1871 m
->pageq
.next
= NULL
;
1872 m
->pageq
.prev
= NULL
;
1874 vm_object_paging_end(m
->object
);
1880 #ifdef FAKE_DEADLOCK
1882 #define FAKE_COUNT 5000
1884 int internal_count
= 0;
1885 int fake_deadlock
= 0;
1890 vm_pageout_iothread_continue(struct vm_pageout_queue
*q
)
1894 boolean_t need_wakeup
;
1896 vm_page_lock_queues();
1898 while ( !queue_empty(&q
->pgo_pending
) ) {
1901 queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
1902 m
->pageout_queue
= FALSE
;
1903 vm_page_unlock_queues();
1905 m
->pageq
.next
= NULL
;
1906 m
->pageq
.prev
= NULL
;
1907 #ifdef FAKE_DEADLOCK
1908 if (q
== &vm_pageout_queue_internal
) {
1914 if ((internal_count
== FAKE_COUNT
)) {
1916 pg_count
= vm_page_free_count
+ vm_page_free_reserved
;
1918 if (kmem_alloc(kernel_map
, &addr
, PAGE_SIZE
* pg_count
) == KERN_SUCCESS
) {
1919 kmem_free(kernel_map
, addr
, PAGE_SIZE
* pg_count
);
1928 if (!object
->pager_initialized
) {
1929 vm_object_lock(object
);
1932 * If there is no memory object for the page, create
1933 * one and hand it to the default pager.
1936 if (!object
->pager_initialized
)
1937 vm_object_collapse(object
, (vm_object_offset_t
)0);
1938 if (!object
->pager_initialized
)
1939 vm_object_pager_create(object
);
1940 if (!object
->pager_initialized
) {
1942 * Still no pager for the object.
1943 * Reactivate the page.
1945 * Should only happen if there is no
1948 m
->list_req_pending
= FALSE
;
1949 m
->cleaning
= FALSE
;
1953 vm_pageout_throttle_up(m
);
1955 vm_page_lock_queues();
1956 vm_pageout_dirty_no_pager
++;
1957 vm_page_activate(m
);
1958 vm_page_unlock_queues();
1961 * And we are done with it.
1963 PAGE_WAKEUP_DONE(m
);
1965 vm_object_paging_end(object
);
1966 vm_object_unlock(object
);
1968 vm_page_lock_queues();
1970 } else if (object
->pager
== MEMORY_OBJECT_NULL
) {
1972 * This pager has been destroyed by either
1973 * memory_object_destroy or vm_object_destroy, and
1974 * so there is nowhere for the page to go.
1975 * Just free the page... VM_PAGE_FREE takes
1976 * care of cleaning up all the state...
1977 * including doing the vm_pageout_throttle_up
1981 vm_object_paging_end(object
);
1982 vm_object_unlock(object
);
1984 vm_page_lock_queues();
1987 vm_object_unlock(object
);
1990 * we expect the paging_in_progress reference to have
1991 * already been taken on the object before it was added
1992 * to the appropriate pageout I/O queue... this will
1993 * keep the object from being terminated and/or the
1994 * paging_offset from changing until the I/O has
1995 * completed... therefore no need to lock the object to
1996 * pull the paging_offset from it.
1998 * Send the data to the pager.
1999 * any pageout clustering happens there
2001 memory_object_data_return(object
->pager
,
2002 m
->offset
+ object
->paging_offset
,
2010 vm_object_lock(object
);
2011 vm_object_paging_end(object
);
2012 vm_object_unlock(object
);
2014 vm_page_lock_queues();
2016 assert_wait((event_t
) q
, THREAD_UNINT
);
2019 if (q
->pgo_throttled
== TRUE
&& !VM_PAGE_Q_THROTTLED(q
)) {
2020 q
->pgo_throttled
= FALSE
;
2023 need_wakeup
= FALSE
;
2025 q
->pgo_busy
= FALSE
;
2027 vm_page_unlock_queues();
2029 if (need_wakeup
== TRUE
)
2030 thread_wakeup((event_t
) &q
->pgo_laundry
);
2032 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_continue
, (void *) &q
->pgo_pending
);
2038 vm_pageout_iothread_external(void)
2041 vm_pageout_iothread_continue(&vm_pageout_queue_external
);
2047 vm_pageout_iothread_internal(void)
2049 thread_t self
= current_thread();
2051 self
->options
|= TH_OPT_VMPRIV
;
2053 vm_pageout_iothread_continue(&vm_pageout_queue_internal
);
2058 vm_pageout_garbage_collect(int collect
)
2064 * consider_zone_gc should be last, because the other operations
2065 * might return memory to zones.
2067 consider_machine_collect();
2070 consider_machine_adjust();
2073 assert_wait((event_t
) &vm_pageout_garbage_collect
, THREAD_UNINT
);
2075 thread_block_parameter((thread_continue_t
) vm_pageout_garbage_collect
, (void *)1);
2084 thread_t self
= current_thread();
2086 kern_return_t result
;
2090 * Set thread privileges.
2094 self
->priority
= BASEPRI_PREEMPT
- 1;
2095 set_sched_pri(self
, self
->priority
);
2096 thread_unlock(self
);
2100 * Initialize some paging parameters.
2103 if (vm_pageout_idle_wait
== 0)
2104 vm_pageout_idle_wait
= VM_PAGEOUT_IDLE_WAIT
;
2106 if (vm_pageout_burst_wait
== 0)
2107 vm_pageout_burst_wait
= VM_PAGEOUT_BURST_WAIT
;
2109 if (vm_pageout_empty_wait
== 0)
2110 vm_pageout_empty_wait
= VM_PAGEOUT_EMPTY_WAIT
;
2112 if (vm_pageout_deadlock_wait
== 0)
2113 vm_pageout_deadlock_wait
= VM_PAGEOUT_DEADLOCK_WAIT
;
2115 if (vm_pageout_deadlock_relief
== 0)
2116 vm_pageout_deadlock_relief
= VM_PAGEOUT_DEADLOCK_RELIEF
;
2118 if (vm_pageout_inactive_relief
== 0)
2119 vm_pageout_inactive_relief
= VM_PAGEOUT_INACTIVE_RELIEF
;
2121 if (vm_pageout_burst_active_throttle
== 0)
2122 vm_pageout_burst_active_throttle
= VM_PAGEOUT_BURST_ACTIVE_THROTTLE
;
2124 if (vm_pageout_burst_inactive_throttle
== 0)
2125 vm_pageout_burst_inactive_throttle
= VM_PAGEOUT_BURST_INACTIVE_THROTTLE
;
2128 * Set kernel task to low backing store privileged
2131 task_lock(kernel_task
);
2132 kernel_task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
2133 task_unlock(kernel_task
);
2135 vm_page_free_count_init
= vm_page_free_count
;
2138 * even if we've already called vm_page_free_reserve
2139 * call it again here to insure that the targets are
2140 * accurately calculated (it uses vm_page_free_count_init)
2141 * calling it with an arg of 0 will not change the reserve
2142 * but will re-calculate free_min and free_target
2144 if (vm_page_free_reserved
< VM_PAGE_FREE_RESERVED(processor_count
)) {
2145 vm_page_free_reserve((VM_PAGE_FREE_RESERVED(processor_count
)) - vm_page_free_reserved
);
2147 vm_page_free_reserve(0);
2150 queue_init(&vm_pageout_queue_external
.pgo_pending
);
2151 vm_pageout_queue_external
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
2152 vm_pageout_queue_external
.pgo_laundry
= 0;
2153 vm_pageout_queue_external
.pgo_idle
= FALSE
;
2154 vm_pageout_queue_external
.pgo_busy
= FALSE
;
2155 vm_pageout_queue_external
.pgo_throttled
= FALSE
;
2157 queue_init(&vm_pageout_queue_internal
.pgo_pending
);
2158 vm_pageout_queue_internal
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
2159 vm_pageout_queue_internal
.pgo_laundry
= 0;
2160 vm_pageout_queue_internal
.pgo_idle
= FALSE
;
2161 vm_pageout_queue_internal
.pgo_busy
= FALSE
;
2162 vm_pageout_queue_internal
.pgo_throttled
= FALSE
;
2165 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_internal
, NULL
, BASEPRI_PREEMPT
- 1, &thread
);
2166 if (result
!= KERN_SUCCESS
)
2167 panic("vm_pageout_iothread_internal: create failed");
2169 thread_deallocate(thread
);
2172 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_external
, NULL
, BASEPRI_PREEMPT
- 1, &thread
);
2173 if (result
!= KERN_SUCCESS
)
2174 panic("vm_pageout_iothread_external: create failed");
2176 thread_deallocate(thread
);
2179 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_garbage_collect
, NULL
, BASEPRI_PREEMPT
- 2, &thread
);
2180 if (result
!= KERN_SUCCESS
)
2181 panic("vm_pageout_garbage_collect: create failed");
2183 thread_deallocate(thread
);
2186 vm_pageout_continue();
2197 int page_field_size
; /* bit field in word size buf */
2199 page_field_size
= 0;
2200 if (flags
& UPL_CREATE_LITE
) {
2201 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
2202 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
2204 if(flags
& UPL_CREATE_INTERNAL
) {
2205 upl
= (upl_t
)kalloc(sizeof(struct upl
)
2206 + (sizeof(struct upl_page_info
)*(size
/PAGE_SIZE
))
2209 upl
= (upl_t
)kalloc(sizeof(struct upl
) + page_field_size
);
2212 upl
->src_object
= NULL
;
2213 upl
->kaddr
= (vm_offset_t
)0;
2215 upl
->map_object
= NULL
;
2219 upl
->ubc_alias1
= 0;
2220 upl
->ubc_alias2
= 0;
2221 #endif /* UPL_DEBUG */
2229 int page_field_size
; /* bit field in word size buf */
2235 if (upl
->map_object
->pageout
) {
2236 object
= upl
->map_object
->shadow
;
2238 object
= upl
->map_object
;
2240 vm_object_lock(object
);
2241 queue_iterate(&object
->uplq
, upl_ele
, upl_t
, uplq
) {
2242 if(upl_ele
== upl
) {
2243 queue_remove(&object
->uplq
,
2244 upl_ele
, upl_t
, uplq
);
2248 vm_object_unlock(object
);
2250 #endif /* UPL_DEBUG */
2251 /* drop a reference on the map_object whether or */
2252 /* not a pageout object is inserted */
2253 if(upl
->map_object
->pageout
)
2254 vm_object_deallocate(upl
->map_object
);
2256 page_field_size
= 0;
2257 if (upl
->flags
& UPL_LITE
) {
2258 page_field_size
= ((upl
->size
/PAGE_SIZE
) + 7) >> 3;
2259 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
2261 if(upl
->flags
& UPL_INTERNAL
) {
2263 sizeof(struct upl
) +
2264 (sizeof(struct upl_page_info
) * (upl
->size
/PAGE_SIZE
))
2267 kfree(upl
, sizeof(struct upl
) + page_field_size
);
2271 void uc_upl_dealloc(upl_t upl
);
2272 __private_extern__
void
2276 upl
->ref_count
-= 1;
2277 if(upl
->ref_count
== 0) {
2287 upl
->ref_count
-= 1;
2288 if(upl
->ref_count
== 0) {
2294 * Statistics about UPL enforcement of copy-on-write obligations.
2296 unsigned long upl_cow
= 0;
2297 unsigned long upl_cow_again
= 0;
2298 unsigned long upl_cow_contiguous
= 0;
2299 unsigned long upl_cow_pages
= 0;
2300 unsigned long upl_cow_again_pages
= 0;
2301 unsigned long upl_cow_contiguous_pages
= 0;
2304 * Routine: vm_object_upl_request
2306 * Cause the population of a portion of a vm_object.
2307 * Depending on the nature of the request, the pages
2308 * returned may be contain valid data or be uninitialized.
2309 * A page list structure, listing the physical pages
2310 * will be returned upon request.
2311 * This function is called by the file system or any other
2312 * supplier of backing store to a pager.
2313 * IMPORTANT NOTE: The caller must still respect the relationship
2314 * between the vm_object and its backing memory object. The
2315 * caller MUST NOT substitute changes in the backing file
2316 * without first doing a memory_object_lock_request on the
2317 * target range unless it is know that the pages are not
2318 * shared with another entity at the pager level.
2320 * if a page list structure is present
2321 * return the mapped physical pages, where a
2322 * page is not present, return a non-initialized
2323 * one. If the no_sync bit is turned on, don't
2324 * call the pager unlock to synchronize with other
2325 * possible copies of the page. Leave pages busy
2326 * in the original object, if a page list structure
2327 * was specified. When a commit of the page list
2328 * pages is done, the dirty bit will be set for each one.
2330 * If a page list structure is present, return
2331 * all mapped pages. Where a page does not exist
2332 * map a zero filled one. Leave pages busy in
2333 * the original object. If a page list structure
2334 * is not specified, this call is a no-op.
2336 * Note: access of default pager objects has a rather interesting
2337 * twist. The caller of this routine, presumably the file system
2338 * page cache handling code, will never actually make a request
2339 * against a default pager backed object. Only the default
2340 * pager will make requests on backing store related vm_objects
2341 * In this way the default pager can maintain the relationship
2342 * between backing store files (abstract memory objects) and
2343 * the vm_objects (cache objects), they support.
2347 __private_extern__ kern_return_t
2348 vm_object_upl_request(
2350 vm_object_offset_t offset
,
2353 upl_page_info_array_t user_page_list
,
2354 unsigned int *page_list_count
,
2357 vm_page_t dst_page
= VM_PAGE_NULL
;
2358 vm_object_offset_t dst_offset
= offset
;
2359 upl_size_t xfer_size
= size
;
2360 boolean_t do_m_lock
= FALSE
;
2365 #if MACH_CLUSTER_STATS
2366 boolean_t encountered_lrp
= FALSE
;
2368 vm_page_t alias_page
= NULL
;
2371 wpl_array_t lite_list
= NULL
;
2372 vm_object_t last_copy_object
;
2375 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
2377 * For forward compatibility's sake,
2378 * reject any unknown flag.
2380 return KERN_INVALID_VALUE
;
2383 page_ticket
= (cntrl_flags
& UPL_PAGE_TICKET_MASK
)
2384 >> UPL_PAGE_TICKET_SHIFT
;
2386 if(((size
/PAGE_SIZE
) > MAX_UPL_TRANSFER
) && !object
->phys_contiguous
) {
2387 size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2390 if(cntrl_flags
& UPL_SET_INTERNAL
)
2391 if(page_list_count
!= NULL
)
2392 *page_list_count
= MAX_UPL_TRANSFER
;
2394 if((!object
->internal
) && (object
->paging_offset
!= 0))
2395 panic("vm_object_upl_request: vnode object with non-zero paging offset\n");
2397 if((cntrl_flags
& UPL_COPYOUT_FROM
) && (upl_ptr
== NULL
)) {
2398 return KERN_SUCCESS
;
2401 vm_object_lock(object
);
2402 vm_object_paging_begin(object
);
2403 vm_object_unlock(object
);
2406 if(cntrl_flags
& UPL_SET_INTERNAL
) {
2407 if(cntrl_flags
& UPL_SET_LITE
) {
2408 uintptr_t page_field_size
;
2410 UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
,
2412 user_page_list
= (upl_page_info_t
*)
2413 (((uintptr_t)upl
) + sizeof(struct upl
));
2414 lite_list
= (wpl_array_t
)
2415 (((uintptr_t)user_page_list
) +
2417 sizeof(upl_page_info_t
)));
2418 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
2420 (page_field_size
+ 3) & 0xFFFFFFFC;
2421 bzero((char *)lite_list
, page_field_size
);
2423 UPL_LITE
| UPL_INTERNAL
;
2425 upl
= upl_create(UPL_CREATE_INTERNAL
, size
);
2426 user_page_list
= (upl_page_info_t
*)
2427 (((uintptr_t)upl
) + sizeof(struct upl
));
2428 upl
->flags
= UPL_INTERNAL
;
2431 if(cntrl_flags
& UPL_SET_LITE
) {
2432 uintptr_t page_field_size
;
2433 upl
= upl_create(UPL_CREATE_LITE
, size
);
2434 lite_list
= (wpl_array_t
)
2435 (((uintptr_t)upl
) + sizeof(struct upl
));
2436 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
2438 (page_field_size
+ 3) & 0xFFFFFFFC;
2439 bzero((char *)lite_list
, page_field_size
);
2440 upl
->flags
= UPL_LITE
;
2442 upl
= upl_create(UPL_CREATE_EXTERNAL
, size
);
2447 if (object
->phys_contiguous
) {
2448 if ((cntrl_flags
& UPL_WILL_MODIFY
) &&
2449 object
->copy
!= VM_OBJECT_NULL
) {
2450 /* Honor copy-on-write obligations */
2454 * We could still have a race...
2455 * A is here building the UPL for a write().
2456 * A pushes the pages to the current copy
2458 * A returns the UPL to the caller.
2459 * B comes along and establishes another
2460 * private mapping on this object, inserting
2461 * a new copy object between the original
2462 * object and the old copy object.
2463 * B reads a page and gets the original contents
2464 * from the original object.
2465 * A modifies the page in the original object.
2466 * B reads the page again and sees A's changes,
2469 * The problem is that the pages are not
2470 * marked "busy" in the original object, so
2471 * nothing prevents B from reading it before
2472 * before A's changes are completed.
2474 * The "paging_in_progress" might protect us
2475 * from the insertion of a new copy object
2476 * though... To be verified.
2478 vm_object_lock_request(object
,
2482 MEMORY_OBJECT_COPY_SYNC
,
2484 upl_cow_contiguous
++;
2485 upl_cow_contiguous_pages
+= size
>> PAGE_SHIFT
;
2488 upl
->map_object
= object
;
2489 /* don't need any shadow mappings for this one */
2490 /* since it is already I/O memory */
2491 upl
->flags
|= UPL_DEVICE_MEMORY
;
2494 /* paging_in_progress protects paging_offset */
2495 upl
->offset
= offset
+ object
->paging_offset
;
2498 if(user_page_list
) {
2499 user_page_list
[0].phys_addr
=
2500 (offset
+ object
->shadow_offset
)>>PAGE_SHIFT
;
2501 user_page_list
[0].device
= TRUE
;
2504 if(page_list_count
!= NULL
) {
2505 if (upl
->flags
& UPL_INTERNAL
) {
2506 *page_list_count
= 0;
2508 *page_list_count
= 1;
2512 return KERN_SUCCESS
;
2516 user_page_list
[0].device
= FALSE
;
2518 if(cntrl_flags
& UPL_SET_LITE
) {
2519 upl
->map_object
= object
;
2521 upl
->map_object
= vm_object_allocate(size
);
2523 * No neeed to lock the new object: nobody else knows
2524 * about it yet, so it's all ours so far.
2526 upl
->map_object
->shadow
= object
;
2527 upl
->map_object
->pageout
= TRUE
;
2528 upl
->map_object
->can_persist
= FALSE
;
2529 upl
->map_object
->copy_strategy
=
2530 MEMORY_OBJECT_COPY_NONE
;
2531 upl
->map_object
->shadow_offset
= offset
;
2532 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
2536 if (!(cntrl_flags
& UPL_SET_LITE
)) {
2537 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
2542 * Just mark the UPL as "encrypted" here.
2543 * We'll actually encrypt the pages later,
2544 * in upl_encrypt(), when the caller has
2545 * selected which pages need to go to swap.
2547 if (cntrl_flags
& UPL_ENCRYPT
) {
2548 upl
->flags
|= UPL_ENCRYPTED
;
2550 if (cntrl_flags
& UPL_FOR_PAGEOUT
) {
2551 upl
->flags
|= UPL_PAGEOUT
;
2553 vm_object_lock(object
);
2555 /* we can lock in the paging_offset once paging_in_progress is set */
2558 upl
->offset
= offset
+ object
->paging_offset
;
2561 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
2562 #endif /* UPL_DEBUG */
2565 if ((cntrl_flags
& UPL_WILL_MODIFY
) &&
2566 object
->copy
!= VM_OBJECT_NULL
) {
2567 /* Honor copy-on-write obligations */
2570 * The caller is gathering these pages and
2571 * might modify their contents. We need to
2572 * make sure that the copy object has its own
2573 * private copies of these pages before we let
2574 * the caller modify them.
2576 vm_object_update(object
,
2581 FALSE
, /* should_return */
2582 MEMORY_OBJECT_COPY_SYNC
,
2585 upl_cow_pages
+= size
>> PAGE_SHIFT
;
2588 /* remember which copy object we synchronized with */
2589 last_copy_object
= object
->copy
;
2592 if(cntrl_flags
& UPL_COPYOUT_FROM
) {
2593 upl
->flags
|= UPL_PAGE_SYNC_DONE
;
2596 if((alias_page
== NULL
) &&
2597 !(cntrl_flags
& UPL_SET_LITE
)) {
2598 vm_object_unlock(object
);
2599 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
2600 vm_object_lock(object
);
2602 if ( ((dst_page
= vm_page_lookup(object
, dst_offset
)) == VM_PAGE_NULL
) ||
2603 dst_page
->fictitious
||
2606 (dst_page
->wire_count
&& !dst_page
->pageout
) ||
2608 ((!dst_page
->inactive
) && (cntrl_flags
& UPL_FOR_PAGEOUT
) &&
2609 (dst_page
->page_ticket
!= page_ticket
) &&
2610 ((dst_page
->page_ticket
+1) != page_ticket
)) ) {
2613 user_page_list
[entry
].phys_addr
= 0;
2616 * grab this up front...
2617 * a high percentange of the time we're going to
2618 * need the hardware modification state a bit later
2619 * anyway... so we can eliminate an extra call into
2620 * the pmap layer by grabbing it here and recording it
2622 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
2624 if (cntrl_flags
& UPL_RET_ONLY_DIRTY
) {
2626 * we're only asking for DIRTY pages to be returned
2629 if (dst_page
->list_req_pending
|| !(cntrl_flags
& UPL_FOR_PAGEOUT
)) {
2631 * if we were the page stolen by vm_pageout_scan to be
2632 * cleaned (as opposed to a buddy being clustered in
2633 * or this request is not being driven by a PAGEOUT cluster
2634 * then we only need to check for the page being diry or
2635 * precious to decide whether to return it
2637 if (dst_page
->dirty
|| dst_page
->precious
||
2638 (refmod_state
& VM_MEM_MODIFIED
)) {
2643 * this is a request for a PAGEOUT cluster and this page
2644 * is merely along for the ride as a 'buddy'... not only
2645 * does it have to be dirty to be returned, but it also
2646 * can't have been referenced recently... note that we've
2647 * already filtered above based on whether this page is
2648 * currently on the inactive queue or it meets the page
2649 * ticket (generation count) check
2651 if ( !(refmod_state
& VM_MEM_REFERENCED
) &&
2652 ((refmod_state
& VM_MEM_MODIFIED
) ||
2653 dst_page
->dirty
|| dst_page
->precious
) ) {
2657 * if we reach here, we're not to return
2658 * the page... go on to the next one
2661 user_page_list
[entry
].phys_addr
= 0;
2663 dst_offset
+= PAGE_SIZE_64
;
2664 xfer_size
-= PAGE_SIZE
;
2668 if(dst_page
->busy
&&
2669 (!(dst_page
->list_req_pending
&&
2670 dst_page
->pageout
))) {
2671 if(cntrl_flags
& UPL_NOBLOCK
) {
2672 if(user_page_list
) {
2673 user_page_list
[entry
].phys_addr
= 0;
2676 dst_offset
+= PAGE_SIZE_64
;
2677 xfer_size
-= PAGE_SIZE
;
2681 * someone else is playing with the
2682 * page. We will have to wait.
2684 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
2687 /* Someone else already cleaning the page? */
2688 if((dst_page
->cleaning
|| dst_page
->absent
||
2689 dst_page
->wire_count
!= 0) &&
2690 !dst_page
->list_req_pending
) {
2691 if(user_page_list
) {
2692 user_page_list
[entry
].phys_addr
= 0;
2695 dst_offset
+= PAGE_SIZE_64
;
2696 xfer_size
-= PAGE_SIZE
;
2699 /* eliminate all mappings from the */
2700 /* original object and its prodigy */
2702 vm_page_lock_queues();
2704 if (dst_page
->pageout_queue
== TRUE
)
2706 * we've buddied up a page for a clustered pageout
2707 * that has already been moved to the pageout
2708 * queue by pageout_scan... we need to remove
2709 * it from the queue and drop the laundry count
2712 vm_pageout_queue_steal(dst_page
);
2713 #if MACH_CLUSTER_STATS
2714 /* pageout statistics gathering. count */
2715 /* all the pages we will page out that */
2716 /* were not counted in the initial */
2717 /* vm_pageout_scan work */
2718 if(dst_page
->list_req_pending
)
2719 encountered_lrp
= TRUE
;
2720 if((dst_page
->dirty
||
2721 (dst_page
->object
->internal
&&
2722 dst_page
->precious
)) &&
2723 (dst_page
->list_req_pending
2725 if(encountered_lrp
) {
2727 (pages_at_higher_offsets
++;)
2730 (pages_at_lower_offsets
++;)
2734 /* Turn off busy indication on pending */
2735 /* pageout. Note: we can only get here */
2736 /* in the request pending case. */
2737 dst_page
->list_req_pending
= FALSE
;
2738 dst_page
->busy
= FALSE
;
2739 dst_page
->cleaning
= FALSE
;
2741 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
2742 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
2744 if(cntrl_flags
& UPL_SET_LITE
) {
2746 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
2747 lite_list
[pg_num
>>5] |=
2750 pmap_clear_modify(dst_page
->phys_page
);
2752 * Record that this page has been
2756 vm_external_state_set(
2757 object
->existence_map
,
2759 #endif /*MACH_PAGEMAP*/
2762 * Mark original page as cleaning
2765 dst_page
->cleaning
= TRUE
;
2766 dst_page
->dirty
= TRUE
;
2767 dst_page
->precious
= FALSE
;
2769 /* use pageclean setup, it is more */
2770 /* convenient even for the pageout */
2773 vm_object_lock(upl
->map_object
);
2774 vm_pageclean_setup(dst_page
,
2775 alias_page
, upl
->map_object
,
2777 vm_object_unlock(upl
->map_object
);
2779 alias_page
->absent
= FALSE
;
2784 dst_page
->dirty
= FALSE
;
2785 dst_page
->precious
= TRUE
;
2788 if(dst_page
->pageout
)
2789 dst_page
->busy
= TRUE
;
2791 if ( (cntrl_flags
& UPL_ENCRYPT
) ) {
2794 * We want to deny access to the target page
2795 * because its contents are about to be
2796 * encrypted and the user would be very
2797 * confused to see encrypted data instead
2800 dst_page
->busy
= TRUE
;
2802 if ( !(cntrl_flags
& UPL_CLEAN_IN_PLACE
) ) {
2804 * deny access to the target page
2805 * while it is being worked on
2807 if ((!dst_page
->pageout
) &&
2808 (dst_page
->wire_count
== 0)) {
2809 dst_page
->busy
= TRUE
;
2810 dst_page
->pageout
= TRUE
;
2811 vm_page_wire(dst_page
);
2815 if(user_page_list
) {
2816 user_page_list
[entry
].phys_addr
2817 = dst_page
->phys_page
;
2818 user_page_list
[entry
].dirty
=
2820 user_page_list
[entry
].pageout
=
2822 user_page_list
[entry
].absent
=
2824 user_page_list
[entry
].precious
=
2827 vm_page_unlock_queues();
2831 * The caller is gathering this page and might
2832 * access its contents later on. Decrypt the
2833 * page before adding it to the UPL, so that
2834 * the caller never sees encrypted data.
2836 if (! (cntrl_flags
& UPL_ENCRYPT
) &&
2837 dst_page
->encrypted
) {
2838 assert(dst_page
->busy
);
2840 vm_page_decrypt(dst_page
, 0);
2841 vm_page_decrypt_for_upl_counter
++;
2844 * Retry this page, since anything
2845 * could have changed while we were
2852 dst_offset
+= PAGE_SIZE_64
;
2853 xfer_size
-= PAGE_SIZE
;
2857 if((alias_page
== NULL
) &&
2858 !(cntrl_flags
& UPL_SET_LITE
)) {
2859 vm_object_unlock(object
);
2860 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
2861 vm_object_lock(object
);
2864 if ((cntrl_flags
& UPL_WILL_MODIFY
) &&
2865 object
->copy
!= last_copy_object
) {
2866 /* Honor copy-on-write obligations */
2869 * The copy object has changed since we
2870 * last synchronized for copy-on-write.
2871 * Another copy object might have been
2872 * inserted while we released the object's
2873 * lock. Since someone could have seen the
2874 * original contents of the remaining pages
2875 * through that new object, we have to
2876 * synchronize with it again for the remaining
2877 * pages only. The previous pages are "busy"
2878 * so they can not be seen through the new
2879 * mapping. The new mapping will see our
2880 * upcoming changes for those previous pages,
2881 * but that's OK since they couldn't see what
2882 * was there before. It's just a race anyway
2883 * and there's no guarantee of consistency or
2884 * atomicity. We just don't want new mappings
2885 * to see both the *before* and *after* pages.
2887 if (object
->copy
!= VM_OBJECT_NULL
) {
2890 dst_offset
,/* current offset */
2891 xfer_size
, /* remaining size */
2894 FALSE
, /* should_return */
2895 MEMORY_OBJECT_COPY_SYNC
,
2898 upl_cow_again_pages
+=
2899 xfer_size
>> PAGE_SHIFT
;
2901 /* remember the copy object we synced with */
2902 last_copy_object
= object
->copy
;
2905 dst_page
= vm_page_lookup(object
, dst_offset
);
2907 if(dst_page
!= VM_PAGE_NULL
) {
2908 if((cntrl_flags
& UPL_RET_ONLY_ABSENT
) &&
2909 !((dst_page
->list_req_pending
)
2910 && (dst_page
->absent
))) {
2911 /* we are doing extended range */
2912 /* requests. we want to grab */
2913 /* pages around some which are */
2914 /* already present. */
2915 if(user_page_list
) {
2916 user_page_list
[entry
].phys_addr
= 0;
2919 dst_offset
+= PAGE_SIZE_64
;
2920 xfer_size
-= PAGE_SIZE
;
2923 if((dst_page
->cleaning
) &&
2924 !(dst_page
->list_req_pending
)) {
2925 /*someone else is writing to the */
2926 /* page. We will have to wait. */
2927 PAGE_SLEEP(object
,dst_page
,THREAD_UNINT
);
2930 if ((dst_page
->fictitious
&&
2931 dst_page
->list_req_pending
)) {
2932 /* dump the fictitious page */
2933 dst_page
->list_req_pending
= FALSE
;
2934 dst_page
->clustered
= FALSE
;
2936 vm_page_lock_queues();
2937 vm_page_free(dst_page
);
2938 vm_page_unlock_queues();
2941 } else if ((dst_page
->absent
&&
2942 dst_page
->list_req_pending
)) {
2943 /* the default_pager case */
2944 dst_page
->list_req_pending
= FALSE
;
2945 dst_page
->busy
= FALSE
;
2948 if(dst_page
== VM_PAGE_NULL
) {
2949 if(object
->private) {
2951 * This is a nasty wrinkle for users
2952 * of upl who encounter device or
2953 * private memory however, it is
2954 * unavoidable, only a fault can
2955 * reslove the actual backing
2956 * physical page by asking the
2959 if(user_page_list
) {
2960 user_page_list
[entry
].phys_addr
= 0;
2963 dst_offset
+= PAGE_SIZE_64
;
2964 xfer_size
-= PAGE_SIZE
;
2967 /* need to allocate a page */
2968 dst_page
= vm_page_alloc(object
, dst_offset
);
2969 if (dst_page
== VM_PAGE_NULL
) {
2970 vm_object_unlock(object
);
2972 vm_object_lock(object
);
2975 dst_page
->busy
= FALSE
;
2977 if(cntrl_flags
& UPL_NO_SYNC
) {
2978 dst_page
->page_lock
= 0;
2979 dst_page
->unlock_request
= 0;
2982 if(cntrl_flags
& UPL_RET_ONLY_ABSENT
) {
2984 * if UPL_RET_ONLY_ABSENT was specified,
2985 * than we're definitely setting up a
2986 * upl for a clustered read/pagein
2987 * operation... mark the pages as clustered
2988 * so vm_fault can correctly attribute them
2989 * to the 'pagein' bucket the first time
2990 * a fault happens on them
2992 dst_page
->clustered
= TRUE
;
2994 dst_page
->absent
= TRUE
;
2995 object
->absent_count
++;
2998 if(cntrl_flags
& UPL_NO_SYNC
) {
2999 dst_page
->page_lock
= 0;
3000 dst_page
->unlock_request
= 0;
3007 if (cntrl_flags
& UPL_ENCRYPT
) {
3009 * The page is going to be encrypted when we
3010 * get it from the pager, so mark it so.
3012 dst_page
->encrypted
= TRUE
;
3015 * Otherwise, the page will not contain
3018 dst_page
->encrypted
= FALSE
;
3021 dst_page
->overwriting
= TRUE
;
3022 if(dst_page
->fictitious
) {
3023 panic("need corner case for fictitious page");
3025 if(dst_page
->page_lock
) {
3030 /* eliminate all mappings from the */
3031 /* original object and its prodigy */
3033 if(dst_page
->busy
) {
3034 /*someone else is playing with the */
3035 /* page. We will have to wait. */
3036 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
3039 vm_page_lock_queues();
3041 if( !(cntrl_flags
& UPL_FILE_IO
))
3042 hw_dirty
= pmap_disconnect(dst_page
->phys_page
) & VM_MEM_MODIFIED
;
3044 hw_dirty
= pmap_get_refmod(dst_page
->phys_page
) & VM_MEM_MODIFIED
;
3045 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
3047 if(cntrl_flags
& UPL_SET_LITE
) {
3049 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
3050 lite_list
[pg_num
>>5] |=
3053 pmap_clear_modify(dst_page
->phys_page
);
3055 * Record that this page has been
3059 vm_external_state_set(
3060 object
->existence_map
,
3062 #endif /*MACH_PAGEMAP*/
3065 * Mark original page as cleaning
3068 dst_page
->cleaning
= TRUE
;
3069 dst_page
->dirty
= TRUE
;
3070 dst_page
->precious
= FALSE
;
3072 /* use pageclean setup, it is more */
3073 /* convenient even for the pageout */
3075 vm_object_lock(upl
->map_object
);
3076 vm_pageclean_setup(dst_page
,
3077 alias_page
, upl
->map_object
,
3079 vm_object_unlock(upl
->map_object
);
3081 alias_page
->absent
= FALSE
;
3085 if(cntrl_flags
& UPL_CLEAN_IN_PLACE
) {
3086 /* clean in place for read implies */
3087 /* that a write will be done on all */
3088 /* the pages that are dirty before */
3089 /* a upl commit is done. The caller */
3090 /* is obligated to preserve the */
3091 /* contents of all pages marked */
3093 upl
->flags
|= UPL_CLEAR_DIRTY
;
3097 dst_page
->dirty
= FALSE
;
3098 dst_page
->precious
= TRUE
;
3101 if (dst_page
->wire_count
== 0) {
3102 /* deny access to the target page while */
3103 /* it is being worked on */
3104 dst_page
->busy
= TRUE
;
3106 vm_page_wire(dst_page
);
3108 if(cntrl_flags
& UPL_RET_ONLY_ABSENT
) {
3110 * expect the page not to be used
3111 * since it's coming in as part
3112 * of a cluster and could be
3113 * speculative... pages that
3114 * are 'consumed' will get a
3115 * hardware reference
3117 dst_page
->reference
= FALSE
;
3120 * expect the page to be used
3122 dst_page
->reference
= TRUE
;
3124 dst_page
->precious
=
3125 (cntrl_flags
& UPL_PRECIOUS
)
3127 if(user_page_list
) {
3128 user_page_list
[entry
].phys_addr
3129 = dst_page
->phys_page
;
3130 user_page_list
[entry
].dirty
=
3132 user_page_list
[entry
].pageout
=
3134 user_page_list
[entry
].absent
=
3136 user_page_list
[entry
].precious
=
3139 vm_page_unlock_queues();
3142 dst_offset
+= PAGE_SIZE_64
;
3143 xfer_size
-= PAGE_SIZE
;
3147 if (upl
->flags
& UPL_INTERNAL
) {
3148 if(page_list_count
!= NULL
)
3149 *page_list_count
= 0;
3150 } else if (*page_list_count
> entry
) {
3151 if(page_list_count
!= NULL
)
3152 *page_list_count
= entry
;
3155 if(alias_page
!= NULL
) {
3156 vm_page_lock_queues();
3157 vm_page_free(alias_page
);
3158 vm_page_unlock_queues();
3162 vm_prot_t access_required
;
3163 /* call back all associated pages from other users of the pager */
3164 /* all future updates will be on data which is based on the */
3165 /* changes we are going to make here. Note: it is assumed that */
3166 /* we already hold copies of the data so we will not be seeing */
3167 /* an avalanche of incoming data from the pager */
3168 access_required
= (cntrl_flags
& UPL_COPYOUT_FROM
)
3169 ? VM_PROT_READ
: VM_PROT_WRITE
;
3173 if(!object
->pager_ready
) {
3174 wait_result_t wait_result
;
3176 wait_result
= vm_object_sleep(object
,
3177 VM_OBJECT_EVENT_PAGER_READY
,
3179 if (wait_result
!= THREAD_AWAKENED
) {
3180 vm_object_unlock(object
);
3181 return KERN_FAILURE
;
3186 vm_object_unlock(object
);
3187 rc
= memory_object_data_unlock(
3189 dst_offset
+ object
->paging_offset
,
3192 if (rc
!= KERN_SUCCESS
&& rc
!= MACH_SEND_INTERRUPTED
)
3193 return KERN_FAILURE
;
3194 vm_object_lock(object
);
3196 if (rc
== KERN_SUCCESS
)
3200 /* lets wait on the last page requested */
3201 /* NOTE: we will have to update lock completed routine to signal */
3202 if(dst_page
!= VM_PAGE_NULL
&&
3203 (access_required
& dst_page
->page_lock
) != access_required
) {
3204 PAGE_ASSERT_WAIT(dst_page
, THREAD_UNINT
);
3205 vm_object_unlock(object
);
3206 thread_block(THREAD_CONTINUE_NULL
);
3207 return KERN_SUCCESS
;
3211 vm_object_unlock(object
);
3212 return KERN_SUCCESS
;
3215 /* JMM - Backward compatability for now */
3217 vm_fault_list_request( /* forward */
3218 memory_object_control_t control
,
3219 vm_object_offset_t offset
,
3222 upl_page_info_t
**user_page_list_ptr
,
3223 int page_list_count
,
3226 vm_fault_list_request(
3227 memory_object_control_t control
,
3228 vm_object_offset_t offset
,
3231 upl_page_info_t
**user_page_list_ptr
,
3232 int page_list_count
,
3235 int local_list_count
;
3236 upl_page_info_t
*user_page_list
;
3239 if (user_page_list_ptr
!= NULL
) {
3240 local_list_count
= page_list_count
;
3241 user_page_list
= *user_page_list_ptr
;
3243 local_list_count
= 0;
3244 user_page_list
= NULL
;
3246 kr
= memory_object_upl_request(control
,
3254 if(kr
!= KERN_SUCCESS
)
3257 if ((user_page_list_ptr
!= NULL
) && (cntrl_flags
& UPL_INTERNAL
)) {
3258 *user_page_list_ptr
= UPL_GET_INTERNAL_PAGE_LIST(*upl_ptr
);
3261 return KERN_SUCCESS
;
3267 * Routine: vm_object_super_upl_request
3269 * Cause the population of a portion of a vm_object
3270 * in much the same way as memory_object_upl_request.
3271 * Depending on the nature of the request, the pages
3272 * returned may be contain valid data or be uninitialized.
3273 * However, the region may be expanded up to the super
3274 * cluster size provided.
3277 __private_extern__ kern_return_t
3278 vm_object_super_upl_request(
3280 vm_object_offset_t offset
,
3282 upl_size_t super_cluster
,
3284 upl_page_info_t
*user_page_list
,
3285 unsigned int *page_list_count
,
3288 vm_page_t target_page
;
3292 if(object
->paging_offset
> offset
)
3293 return KERN_FAILURE
;
3295 assert(object
->paging_in_progress
);
3296 offset
= offset
- object
->paging_offset
;
3298 if(cntrl_flags
& UPL_FOR_PAGEOUT
) {
3300 vm_object_lock(object
);
3302 if((target_page
= vm_page_lookup(object
, offset
))
3304 ticket
= target_page
->page_ticket
;
3305 cntrl_flags
= cntrl_flags
& ~(int)UPL_PAGE_TICKET_MASK
;
3306 cntrl_flags
= cntrl_flags
|
3307 ((ticket
<< UPL_PAGE_TICKET_SHIFT
)
3308 & UPL_PAGE_TICKET_MASK
);
3310 vm_object_unlock(object
);
3313 if (super_cluster
> size
) {
3315 vm_object_offset_t base_offset
;
3316 upl_size_t super_size
;
3318 base_offset
= (offset
&
3319 ~((vm_object_offset_t
) super_cluster
- 1));
3320 super_size
= (offset
+size
) > (base_offset
+ super_cluster
) ?
3321 super_cluster
<<1 : super_cluster
;
3322 super_size
= ((base_offset
+ super_size
) > object
->size
) ?
3323 (object
->size
- base_offset
) : super_size
;
3324 if(offset
> (base_offset
+ super_size
))
3325 panic("vm_object_super_upl_request: Missed target pageout"
3326 " %#llx,%#llx, %#x, %#x, %#x, %#llx\n",
3327 offset
, base_offset
, super_size
, super_cluster
,
3328 size
, object
->paging_offset
);
3330 * apparently there is a case where the vm requests a
3331 * page to be written out who's offset is beyond the
3334 if((offset
+ size
) > (base_offset
+ super_size
))
3335 super_size
= (offset
+ size
) - base_offset
;
3337 offset
= base_offset
;
3340 return vm_object_upl_request(object
, offset
, size
,
3341 upl
, user_page_list
, page_list_count
,
3349 vm_map_address_t offset
,
3350 upl_size_t
*upl_size
,
3352 upl_page_info_array_t page_list
,
3353 unsigned int *count
,
3356 vm_map_entry_t entry
;
3358 int force_data_sync
;
3360 vm_object_t local_object
;
3361 vm_map_offset_t local_offset
;
3362 vm_map_offset_t local_start
;
3365 caller_flags
= *flags
;
3367 if (caller_flags
& ~UPL_VALID_FLAGS
) {
3369 * For forward compatibility's sake,
3370 * reject any unknown flag.
3372 return KERN_INVALID_VALUE
;
3375 force_data_sync
= (caller_flags
& UPL_FORCE_DATA_SYNC
);
3376 sync_cow_data
= !(caller_flags
& UPL_COPYOUT_FROM
);
3379 return KERN_INVALID_ARGUMENT
;
3384 if (vm_map_lookup_entry(map
, offset
, &entry
)) {
3385 if (entry
->object
.vm_object
== VM_OBJECT_NULL
||
3386 !entry
->object
.vm_object
->phys_contiguous
) {
3387 if((*upl_size
/page_size
) > MAX_UPL_TRANSFER
) {
3388 *upl_size
= MAX_UPL_TRANSFER
* page_size
;
3391 if((entry
->vme_end
- offset
) < *upl_size
) {
3392 *upl_size
= entry
->vme_end
- offset
;
3394 if (caller_flags
& UPL_QUERY_OBJECT_TYPE
) {
3395 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
3397 } else if (entry
->object
.vm_object
->private) {
3398 *flags
= UPL_DEV_MEMORY
;
3399 if (entry
->object
.vm_object
->phys_contiguous
) {
3400 *flags
|= UPL_PHYS_CONTIG
;
3406 return KERN_SUCCESS
;
3409 * Create an object if necessary.
3411 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
3412 entry
->object
.vm_object
= vm_object_allocate(
3413 (vm_size_t
)(entry
->vme_end
- entry
->vme_start
));
3416 if (!(caller_flags
& UPL_COPYOUT_FROM
)) {
3417 if (!(entry
->protection
& VM_PROT_WRITE
)) {
3419 return KERN_PROTECTION_FAILURE
;
3421 if (entry
->needs_copy
) {
3424 vm_map_offset_t offset_hi
;
3425 vm_map_offset_t offset_lo
;
3426 vm_object_offset_t new_offset
;
3429 vm_behavior_t behavior
;
3430 vm_map_version_t version
;
3434 vm_map_lock_write_to_read(map
);
3435 if(vm_map_lookup_locked(&local_map
,
3436 offset
, VM_PROT_WRITE
,
3438 &new_offset
, &prot
, &wired
,
3439 &behavior
, &offset_lo
,
3440 &offset_hi
, &real_map
)) {
3441 vm_map_unlock(local_map
);
3442 return KERN_FAILURE
;
3444 if (real_map
!= map
) {
3445 vm_map_unlock(real_map
);
3447 vm_object_unlock(object
);
3448 vm_map_unlock(local_map
);
3450 goto REDISCOVER_ENTRY
;
3453 if (entry
->is_sub_map
) {
3456 submap
= entry
->object
.sub_map
;
3457 local_start
= entry
->vme_start
;
3458 local_offset
= entry
->offset
;
3459 vm_map_reference(submap
);
3462 ret
= (vm_map_create_upl(submap
,
3463 local_offset
+ (offset
- local_start
),
3464 upl_size
, upl
, page_list
, count
,
3467 vm_map_deallocate(submap
);
3471 if (sync_cow_data
) {
3472 if (entry
->object
.vm_object
->shadow
3473 || entry
->object
.vm_object
->copy
) {
3475 local_object
= entry
->object
.vm_object
;
3476 local_start
= entry
->vme_start
;
3477 local_offset
= entry
->offset
;
3478 vm_object_reference(local_object
);
3481 if (entry
->object
.vm_object
->shadow
&&
3482 entry
->object
.vm_object
->copy
) {
3483 vm_object_lock_request(
3484 local_object
->shadow
,
3485 (vm_object_offset_t
)
3486 ((offset
- local_start
) +
3488 local_object
->shadow_offset
,
3490 MEMORY_OBJECT_DATA_SYNC
,
3493 sync_cow_data
= FALSE
;
3494 vm_object_deallocate(local_object
);
3495 goto REDISCOVER_ENTRY
;
3499 if (force_data_sync
) {
3501 local_object
= entry
->object
.vm_object
;
3502 local_start
= entry
->vme_start
;
3503 local_offset
= entry
->offset
;
3504 vm_object_reference(local_object
);
3507 vm_object_lock_request(
3509 (vm_object_offset_t
)
3510 ((offset
- local_start
) + local_offset
),
3511 (vm_object_size_t
)*upl_size
, FALSE
,
3512 MEMORY_OBJECT_DATA_SYNC
,
3514 force_data_sync
= FALSE
;
3515 vm_object_deallocate(local_object
);
3516 goto REDISCOVER_ENTRY
;
3519 if(!(entry
->object
.vm_object
->private)) {
3520 if(*upl_size
> (MAX_UPL_TRANSFER
*PAGE_SIZE
))
3521 *upl_size
= (MAX_UPL_TRANSFER
*PAGE_SIZE
);
3522 if(entry
->object
.vm_object
->phys_contiguous
) {
3523 *flags
= UPL_PHYS_CONTIG
;
3528 *flags
= UPL_DEV_MEMORY
| UPL_PHYS_CONTIG
;
3530 local_object
= entry
->object
.vm_object
;
3531 local_offset
= entry
->offset
;
3532 local_start
= entry
->vme_start
;
3533 vm_object_reference(local_object
);
3535 if(caller_flags
& UPL_SET_IO_WIRE
) {
3536 ret
= (vm_object_iopl_request(local_object
,
3537 (vm_object_offset_t
)
3538 ((offset
- local_start
)
3546 ret
= (vm_object_upl_request(local_object
,
3547 (vm_object_offset_t
)
3548 ((offset
- local_start
)
3556 vm_object_deallocate(local_object
);
3561 return(KERN_FAILURE
);
3566 * Internal routine to enter a UPL into a VM map.
3568 * JMM - This should just be doable through the standard
3569 * vm_map_enter() API.
3575 vm_map_offset_t
*dst_addr
)
3578 vm_object_offset_t offset
;
3579 vm_map_offset_t addr
;
3583 if (upl
== UPL_NULL
)
3584 return KERN_INVALID_ARGUMENT
;
3588 /* check to see if already mapped */
3589 if(UPL_PAGE_LIST_MAPPED
& upl
->flags
) {
3591 return KERN_FAILURE
;
3594 if((!(upl
->map_object
->pageout
)) &&
3595 !((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) ||
3596 (upl
->map_object
->phys_contiguous
))) {
3598 vm_page_t alias_page
;
3599 vm_object_offset_t new_offset
;
3601 wpl_array_t lite_list
;
3603 if(upl
->flags
& UPL_INTERNAL
) {
3604 lite_list
= (wpl_array_t
)
3605 ((((uintptr_t)upl
) + sizeof(struct upl
))
3606 + ((upl
->size
/PAGE_SIZE
)
3607 * sizeof(upl_page_info_t
)));
3609 lite_list
= (wpl_array_t
)
3610 (((uintptr_t)upl
) + sizeof(struct upl
));
3612 object
= upl
->map_object
;
3613 upl
->map_object
= vm_object_allocate(upl
->size
);
3614 vm_object_lock(upl
->map_object
);
3615 upl
->map_object
->shadow
= object
;
3616 upl
->map_object
->pageout
= TRUE
;
3617 upl
->map_object
->can_persist
= FALSE
;
3618 upl
->map_object
->copy_strategy
=
3619 MEMORY_OBJECT_COPY_NONE
;
3620 upl
->map_object
->shadow_offset
=
3621 upl
->offset
- object
->paging_offset
;
3622 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
3623 offset
= upl
->map_object
->shadow_offset
;
3627 vm_object_lock(object
);
3630 pg_num
= (new_offset
)/PAGE_SIZE
;
3631 if(lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
3632 vm_object_unlock(object
);
3633 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
3634 vm_object_lock(object
);
3635 m
= vm_page_lookup(object
, offset
);
3636 if (m
== VM_PAGE_NULL
) {
3637 panic("vm_upl_map: page missing\n");
3640 vm_object_paging_begin(object
);
3643 * Convert the fictitious page to a private
3644 * shadow of the real page.
3646 assert(alias_page
->fictitious
);
3647 alias_page
->fictitious
= FALSE
;
3648 alias_page
->private = TRUE
;
3649 alias_page
->pageout
= TRUE
;
3650 alias_page
->phys_page
= m
->phys_page
;
3652 vm_page_lock_queues();
3653 vm_page_wire(alias_page
);
3654 vm_page_unlock_queues();
3658 * The virtual page ("m") has to be wired in some way
3659 * here or its physical page ("m->phys_page") could
3660 * be recycled at any time.
3661 * Assuming this is enforced by the caller, we can't
3662 * get an encrypted page here. Since the encryption
3663 * key depends on the VM page's "pager" object and
3664 * the "paging_offset", we couldn't handle 2 pageable
3665 * VM pages (with different pagers and paging_offsets)
3666 * sharing the same physical page: we could end up
3667 * encrypting with one key (via one VM page) and
3668 * decrypting with another key (via the alias VM page).
3670 ASSERT_PAGE_DECRYPTED(m
);
3672 vm_page_insert(alias_page
,
3673 upl
->map_object
, new_offset
);
3674 assert(!alias_page
->wanted
);
3675 alias_page
->busy
= FALSE
;
3676 alias_page
->absent
= FALSE
;
3680 offset
+= PAGE_SIZE_64
;
3681 new_offset
+= PAGE_SIZE_64
;
3683 vm_object_unlock(object
);
3684 vm_object_unlock(upl
->map_object
);
3686 if ((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) || upl
->map_object
->phys_contiguous
)
3687 offset
= upl
->offset
- upl
->map_object
->paging_offset
;
3693 vm_object_lock(upl
->map_object
);
3694 upl
->map_object
->ref_count
++;
3695 vm_object_res_reference(upl
->map_object
);
3696 vm_object_unlock(upl
->map_object
);
3701 /* NEED A UPL_MAP ALIAS */
3702 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
3703 VM_FLAGS_ANYWHERE
, upl
->map_object
, offset
, FALSE
,
3704 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
3706 if (kr
!= KERN_SUCCESS
) {
3711 vm_object_lock(upl
->map_object
);
3713 for(addr
=*dst_addr
; size
> 0; size
-=PAGE_SIZE
,addr
+=PAGE_SIZE
) {
3714 m
= vm_page_lookup(upl
->map_object
, offset
);
3716 unsigned int cache_attr
;
3717 cache_attr
= ((unsigned int)m
->object
->wimg_bits
) & VM_WIMG_MASK
;
3719 PMAP_ENTER(map
->pmap
, addr
,
3723 offset
+=PAGE_SIZE_64
;
3725 vm_object_unlock(upl
->map_object
);
3727 upl
->ref_count
++; /* hold a reference for the mapping */
3728 upl
->flags
|= UPL_PAGE_LIST_MAPPED
;
3729 upl
->kaddr
= *dst_addr
;
3731 return KERN_SUCCESS
;
3735 * Internal routine to remove a UPL mapping from a VM map.
3737 * XXX - This should just be doable through a standard
3738 * vm_map_remove() operation. Otherwise, implicit clean-up
3739 * of the target map won't be able to correctly remove
3740 * these (and release the reference on the UPL). Having
3741 * to do this means we can't map these into user-space
3752 if (upl
== UPL_NULL
)
3753 return KERN_INVALID_ARGUMENT
;
3756 if(upl
->flags
& UPL_PAGE_LIST_MAPPED
) {
3759 assert(upl
->ref_count
> 1);
3760 upl
->ref_count
--; /* removing mapping ref */
3761 upl
->flags
&= ~UPL_PAGE_LIST_MAPPED
;
3762 upl
->kaddr
= (vm_offset_t
) 0;
3766 vm_map_trunc_page(addr
),
3767 vm_map_round_page(addr
+ size
),
3769 return KERN_SUCCESS
;
3772 return KERN_FAILURE
;
3778 upl_offset_t offset
,
3781 upl_page_info_t
*page_list
,
3782 mach_msg_type_number_t count
,
3785 upl_size_t xfer_size
= size
;
3786 vm_object_t shadow_object
;
3787 vm_object_t object
= upl
->map_object
;
3788 vm_object_offset_t target_offset
;
3790 wpl_array_t lite_list
;
3792 int delayed_unlock
= 0;
3793 int clear_refmod
= 0;
3794 boolean_t shadow_internal
;
3798 if (upl
== UPL_NULL
)
3799 return KERN_INVALID_ARGUMENT
;
3805 if (object
->pageout
) {
3806 shadow_object
= object
->shadow
;
3808 shadow_object
= object
;
3813 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
3815 * We used this UPL to block access to the pages by marking
3816 * them "busy". Now we need to clear the "busy" bit to allow
3817 * access to these pages again.
3819 flags
|= UPL_COMMIT_ALLOW_ACCESS
;
3822 if (upl
->flags
& UPL_CLEAR_DIRTY
)
3823 flags
|= UPL_COMMIT_CLEAR_DIRTY
;
3825 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
3827 } else if ((offset
+ size
) > upl
->size
) {
3829 return KERN_FAILURE
;
3832 if (upl
->flags
& UPL_INTERNAL
) {
3833 lite_list
= (wpl_array_t
)
3834 ((((uintptr_t)upl
) + sizeof(struct upl
))
3835 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
3837 lite_list
= (wpl_array_t
)
3838 (((uintptr_t)upl
) + sizeof(struct upl
));
3840 if (object
!= shadow_object
)
3841 vm_object_lock(object
);
3842 vm_object_lock(shadow_object
);
3844 shadow_internal
= shadow_object
->internal
;
3846 entry
= offset
/PAGE_SIZE
;
3847 target_offset
= (vm_object_offset_t
)offset
;
3855 if (upl
->flags
& UPL_LITE
) {
3858 pg_num
= target_offset
/PAGE_SIZE
;
3860 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
3861 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
3862 m
= vm_page_lookup(shadow_object
,
3863 target_offset
+ (upl
->offset
-
3864 shadow_object
->paging_offset
));
3867 if (object
->pageout
) {
3868 if ((t
= vm_page_lookup(object
, target_offset
)) != NULL
) {
3871 if (delayed_unlock
) {
3873 vm_page_unlock_queues();
3881 object
->shadow_offset
);
3883 if (m
!= VM_PAGE_NULL
)
3884 vm_object_paging_end(m
->object
);
3887 if (m
!= VM_PAGE_NULL
) {
3891 if (upl
->flags
& UPL_IO_WIRE
) {
3893 if (delayed_unlock
== 0)
3894 vm_page_lock_queues();
3898 if (delayed_unlock
++ > DELAYED_UNLOCK_LIMIT
) {
3900 vm_page_unlock_queues();
3903 page_list
[entry
].phys_addr
= 0;
3905 if (flags
& UPL_COMMIT_SET_DIRTY
) {
3907 } else if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
3909 clear_refmod
|= VM_MEM_MODIFIED
;
3911 if (flags
& UPL_COMMIT_INACTIVATE
) {
3912 m
->reference
= FALSE
;
3913 clear_refmod
|= VM_MEM_REFERENCED
;
3914 vm_page_deactivate(m
);
3917 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
3919 if (flags
& UPL_COMMIT_ALLOW_ACCESS
) {
3921 * We blocked access to the pages in this UPL.
3922 * Clear the "busy" bit and wake up any waiter
3925 PAGE_WAKEUP_DONE(m
);
3928 target_offset
+= PAGE_SIZE_64
;
3929 xfer_size
-= PAGE_SIZE
;
3933 if (delayed_unlock
== 0)
3934 vm_page_lock_queues();
3936 * make sure to clear the hardware
3937 * modify or reference bits before
3938 * releasing the BUSY bit on this page
3939 * otherwise we risk losing a legitimate
3942 if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
3944 clear_refmod
|= VM_MEM_MODIFIED
;
3946 if (flags
& UPL_COMMIT_INACTIVATE
)
3947 clear_refmod
|= VM_MEM_REFERENCED
;
3950 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
3953 p
= &(page_list
[entry
]);
3954 if(p
->phys_addr
&& p
->pageout
&& !m
->pageout
) {
3958 } else if (page_list
[entry
].phys_addr
&&
3959 !p
->pageout
&& m
->pageout
&&
3960 !m
->dump_cleaning
) {
3963 m
->overwriting
= FALSE
;
3965 PAGE_WAKEUP_DONE(m
);
3967 page_list
[entry
].phys_addr
= 0;
3969 m
->dump_cleaning
= FALSE
;
3971 vm_pageout_throttle_up(m
);
3974 m
->cleaning
= FALSE
;
3976 #if MACH_CLUSTER_STATS
3977 if (m
->wanted
) vm_pageout_target_collisions
++;
3979 if (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
)
3985 vm_page_unwire(m
);/* reactivates */
3987 if (upl
->flags
& UPL_PAGEOUT
) {
3988 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
3989 VM_STAT(reactivations
++);
3991 PAGE_WAKEUP_DONE(m
);
3993 vm_page_free(m
);/* clears busy, etc. */
3995 if (upl
->flags
& UPL_PAGEOUT
) {
3996 CLUSTER_STAT(vm_pageout_target_page_freed
++;)
3998 if (page_list
[entry
].dirty
)
3999 VM_STAT(pageouts
++);
4002 if (delayed_unlock
++ > DELAYED_UNLOCK_LIMIT
) {
4004 vm_page_unlock_queues();
4006 target_offset
+= PAGE_SIZE_64
;
4007 xfer_size
-= PAGE_SIZE
;
4011 #if MACH_CLUSTER_STATS
4012 m
->dirty
= pmap_is_modified(m
->phys_page
);
4014 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
4015 else vm_pageout_cluster_cleaned
++;
4016 if (m
->wanted
) vm_pageout_cluster_collisions
++;
4021 if((m
->busy
) && (m
->cleaning
)) {
4022 /* the request_page_list case */
4025 if(shadow_object
->absent_count
== 1)
4026 vm_object_absent_release(shadow_object
);
4028 shadow_object
->absent_count
--;
4030 m
->overwriting
= FALSE
;
4033 } else if (m
->overwriting
) {
4034 /* alternate request page list, write to
4035 * page_list case. Occurs when the original
4036 * page was wired at the time of the list
4038 assert(m
->wire_count
!= 0);
4039 vm_page_unwire(m
);/* reactivates */
4040 m
->overwriting
= FALSE
;
4042 m
->cleaning
= FALSE
;
4044 /* It is a part of the semantic of COPYOUT_FROM */
4045 /* UPLs that a commit implies cache sync */
4046 /* between the vm page and the backing store */
4047 /* this can be used to strip the precious bit */
4048 /* as well as clean */
4049 if (upl
->flags
& UPL_PAGE_SYNC_DONE
)
4050 m
->precious
= FALSE
;
4052 if (flags
& UPL_COMMIT_SET_DIRTY
)
4055 if (flags
& UPL_COMMIT_INACTIVATE
) {
4056 m
->reference
= FALSE
;
4057 vm_page_deactivate(m
);
4058 } else if (!m
->active
&& !m
->inactive
) {
4060 vm_page_activate(m
);
4062 vm_page_deactivate(m
);
4065 if (flags
& UPL_COMMIT_ALLOW_ACCESS
) {
4067 * We blocked access to the pages in this URL.
4068 * Clear the "busy" bit on this page before we
4069 * wake up any waiter.
4075 * Wakeup any thread waiting for the page to be un-cleaning.
4079 if (delayed_unlock
++ > DELAYED_UNLOCK_LIMIT
) {
4081 vm_page_unlock_queues();
4084 target_offset
+= PAGE_SIZE_64
;
4085 xfer_size
-= PAGE_SIZE
;
4089 vm_page_unlock_queues();
4093 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4095 } else if (upl
->flags
& UPL_LITE
) {
4098 pg_num
= upl
->size
/PAGE_SIZE
;
4099 pg_num
= (pg_num
+ 31) >> 5;
4101 for(i
= 0; i
<pg_num
; i
++) {
4102 if(lite_list
[i
] != 0) {
4108 if(queue_empty(&upl
->map_object
->memq
)) {
4114 if(upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
) {
4117 if(object
== shadow_object
)
4118 vm_object_paging_end(shadow_object
);
4120 vm_object_unlock(shadow_object
);
4121 if (object
!= shadow_object
)
4122 vm_object_unlock(object
);
4125 return KERN_SUCCESS
;
4131 upl_offset_t offset
,
4136 upl_size_t xfer_size
= size
;
4137 vm_object_t shadow_object
;
4138 vm_object_t object
= upl
->map_object
;
4139 vm_object_offset_t target_offset
;
4141 wpl_array_t lite_list
;
4143 boolean_t shadow_internal
;
4147 if (upl
== UPL_NULL
)
4148 return KERN_INVALID_ARGUMENT
;
4150 if (upl
->flags
& UPL_IO_WIRE
) {
4151 return upl_commit_range(upl
,
4156 if(object
->pageout
) {
4157 shadow_object
= object
->shadow
;
4159 shadow_object
= object
;
4163 if(upl
->flags
& UPL_DEVICE_MEMORY
) {
4165 } else if ((offset
+ size
) > upl
->size
) {
4167 return KERN_FAILURE
;
4169 if (object
!= shadow_object
)
4170 vm_object_lock(object
);
4171 vm_object_lock(shadow_object
);
4173 shadow_internal
= shadow_object
->internal
;
4175 if(upl
->flags
& UPL_INTERNAL
) {
4176 lite_list
= (wpl_array_t
)
4177 ((((uintptr_t)upl
) + sizeof(struct upl
))
4178 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4180 lite_list
= (wpl_array_t
)
4181 (((uintptr_t)upl
) + sizeof(struct upl
));
4184 entry
= offset
/PAGE_SIZE
;
4185 target_offset
= (vm_object_offset_t
)offset
;
4190 if(upl
->flags
& UPL_LITE
) {
4192 pg_num
= target_offset
/PAGE_SIZE
;
4193 if(lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
4194 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
4195 m
= vm_page_lookup(shadow_object
,
4196 target_offset
+ (upl
->offset
-
4197 shadow_object
->paging_offset
));
4200 if(object
->pageout
) {
4201 if ((t
= vm_page_lookup(object
, target_offset
))
4209 object
->shadow_offset
);
4211 if(m
!= VM_PAGE_NULL
)
4212 vm_object_paging_end(m
->object
);
4215 if(m
!= VM_PAGE_NULL
) {
4216 vm_page_lock_queues();
4218 boolean_t must_free
= TRUE
;
4220 /* COPYOUT = FALSE case */
4221 /* check for error conditions which must */
4222 /* be passed back to the pages customer */
4223 if(error
& UPL_ABORT_RESTART
) {
4226 vm_object_absent_release(m
->object
);
4227 m
->page_error
= KERN_MEMORY_ERROR
;
4230 } else if(error
& UPL_ABORT_UNAVAILABLE
) {
4234 } else if(error
& UPL_ABORT_ERROR
) {
4237 vm_object_absent_release(m
->object
);
4238 m
->page_error
= KERN_MEMORY_ERROR
;
4245 * If the page was already encrypted,
4246 * we don't really need to decrypt it
4247 * now. It will get decrypted later,
4248 * on demand, as soon as someone needs
4249 * to access its contents.
4252 m
->cleaning
= FALSE
;
4253 m
->overwriting
= FALSE
;
4254 PAGE_WAKEUP_DONE(m
);
4256 if (must_free
== TRUE
) {
4259 vm_page_activate(m
);
4261 vm_page_unlock_queues();
4263 target_offset
+= PAGE_SIZE_64
;
4264 xfer_size
-= PAGE_SIZE
;
4269 * Handle the trusted pager throttle.
4272 vm_pageout_throttle_up(m
);
4276 assert(m
->wire_count
== 1);
4280 m
->dump_cleaning
= FALSE
;
4281 m
->cleaning
= FALSE
;
4282 m
->overwriting
= FALSE
;
4284 vm_external_state_clr(
4285 m
->object
->existence_map
, m
->offset
);
4286 #endif /* MACH_PAGEMAP */
4287 if(error
& UPL_ABORT_DUMP_PAGES
) {
4289 pmap_disconnect(m
->phys_page
);
4291 PAGE_WAKEUP_DONE(m
);
4293 vm_page_unlock_queues();
4295 target_offset
+= PAGE_SIZE_64
;
4296 xfer_size
-= PAGE_SIZE
;
4300 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4302 } else if (upl
->flags
& UPL_LITE
) {
4305 pg_num
= upl
->size
/PAGE_SIZE
;
4306 pg_num
= (pg_num
+ 31) >> 5;
4308 for(i
= 0; i
<pg_num
; i
++) {
4309 if(lite_list
[i
] != 0) {
4315 if(queue_empty(&upl
->map_object
->memq
)) {
4321 if(upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
) {
4324 if(object
== shadow_object
)
4325 vm_object_paging_end(shadow_object
);
4327 vm_object_unlock(shadow_object
);
4328 if (object
!= shadow_object
)
4329 vm_object_unlock(object
);
4333 return KERN_SUCCESS
;
4341 vm_object_t object
= NULL
;
4342 vm_object_t shadow_object
= NULL
;
4343 vm_object_offset_t offset
;
4344 vm_object_offset_t shadow_offset
;
4345 vm_object_offset_t target_offset
;
4347 wpl_array_t lite_list
;
4350 boolean_t shadow_internal
;
4352 if (upl
== UPL_NULL
)
4353 return KERN_INVALID_ARGUMENT
;
4355 if (upl
->flags
& UPL_IO_WIRE
) {
4357 return upl_commit_range(upl
,
4363 if(upl
->flags
& UPL_DEVICE_MEMORY
) {
4365 return KERN_SUCCESS
;
4368 object
= upl
->map_object
;
4370 if (object
== NULL
) {
4371 panic("upl_abort: upl object is not backed by an object");
4373 return KERN_INVALID_ARGUMENT
;
4376 if(object
->pageout
) {
4377 shadow_object
= object
->shadow
;
4378 shadow_offset
= object
->shadow_offset
;
4380 shadow_object
= object
;
4381 shadow_offset
= upl
->offset
- object
->paging_offset
;
4384 if(upl
->flags
& UPL_INTERNAL
) {
4385 lite_list
= (wpl_array_t
)
4386 ((((uintptr_t)upl
) + sizeof(struct upl
))
4387 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4389 lite_list
= (wpl_array_t
)
4390 (((uintptr_t)upl
) + sizeof(struct upl
));
4394 if (object
!= shadow_object
)
4395 vm_object_lock(object
);
4396 vm_object_lock(shadow_object
);
4398 shadow_internal
= shadow_object
->internal
;
4400 for(i
= 0; i
<(upl
->size
); i
+=PAGE_SIZE
, offset
+= PAGE_SIZE_64
) {
4402 target_offset
= offset
+ shadow_offset
;
4403 if(upl
->flags
& UPL_LITE
) {
4405 pg_num
= offset
/PAGE_SIZE
;
4406 if(lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
4407 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
4409 shadow_object
, target_offset
);
4412 if(object
->pageout
) {
4413 if ((t
= vm_page_lookup(object
, offset
)) != NULL
) {
4418 shadow_object
, target_offset
);
4420 if(m
!= VM_PAGE_NULL
)
4421 vm_object_paging_end(m
->object
);
4424 if(m
!= VM_PAGE_NULL
) {
4425 vm_page_lock_queues();
4427 boolean_t must_free
= TRUE
;
4429 /* COPYOUT = FALSE case */
4430 /* check for error conditions which must */
4431 /* be passed back to the pages customer */
4432 if(error
& UPL_ABORT_RESTART
) {
4435 vm_object_absent_release(m
->object
);
4436 m
->page_error
= KERN_MEMORY_ERROR
;
4439 } else if(error
& UPL_ABORT_UNAVAILABLE
) {
4443 } else if(error
& UPL_ABORT_ERROR
) {
4446 vm_object_absent_release(m
->object
);
4447 m
->page_error
= KERN_MEMORY_ERROR
;
4454 * If the page was already encrypted,
4455 * we don't really need to decrypt it
4456 * now. It will get decrypted later,
4457 * on demand, as soon as someone needs
4458 * to access its contents.
4461 m
->cleaning
= FALSE
;
4462 m
->overwriting
= FALSE
;
4463 PAGE_WAKEUP_DONE(m
);
4465 if (must_free
== TRUE
) {
4468 vm_page_activate(m
);
4470 vm_page_unlock_queues();
4474 * Handle the trusted pager throttle.
4477 vm_pageout_throttle_up(m
);
4481 assert(m
->wire_count
== 1);
4485 m
->dump_cleaning
= FALSE
;
4486 m
->cleaning
= FALSE
;
4487 m
->overwriting
= FALSE
;
4489 vm_external_state_clr(
4490 m
->object
->existence_map
, m
->offset
);
4491 #endif /* MACH_PAGEMAP */
4492 if(error
& UPL_ABORT_DUMP_PAGES
) {
4494 pmap_disconnect(m
->phys_page
);
4496 PAGE_WAKEUP_DONE(m
);
4498 vm_page_unlock_queues();
4502 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4504 } else if (upl
->flags
& UPL_LITE
) {
4507 pg_num
= upl
->size
/PAGE_SIZE
;
4508 pg_num
= (pg_num
+ 31) >> 5;
4510 for(j
= 0; j
<pg_num
; j
++) {
4511 if(lite_list
[j
] != 0) {
4517 if(queue_empty(&upl
->map_object
->memq
)) {
4523 if(object
== shadow_object
)
4524 vm_object_paging_end(shadow_object
);
4526 vm_object_unlock(shadow_object
);
4527 if (object
!= shadow_object
)
4528 vm_object_unlock(object
);
4531 return KERN_SUCCESS
;
4534 /* an option on commit should be wire */
4538 upl_page_info_t
*page_list
,
4539 mach_msg_type_number_t count
)
4541 if (upl
== UPL_NULL
)
4542 return KERN_INVALID_ARGUMENT
;
4544 if(upl
->flags
& (UPL_LITE
| UPL_IO_WIRE
)) {
4546 return upl_commit_range(upl
, 0, upl
->size
, 0,
4547 page_list
, count
, &empty
);
4554 if (upl
->flags
& UPL_DEVICE_MEMORY
)
4557 if (upl
->flags
& UPL_ENCRYPTED
) {
4560 * This UPL was encrypted, but we don't need
4561 * to decrypt here. We'll decrypt each page
4562 * later, on demand, as soon as someone needs
4563 * to access the page's contents.
4567 if ((upl
->flags
& UPL_CLEAR_DIRTY
) ||
4568 (upl
->flags
& UPL_PAGE_SYNC_DONE
) || page_list
) {
4569 vm_object_t shadow_object
= upl
->map_object
->shadow
;
4570 vm_object_t object
= upl
->map_object
;
4571 vm_object_offset_t target_offset
;
4572 upl_size_t xfer_end
;
4578 if (object
!= shadow_object
)
4579 vm_object_lock(object
);
4580 vm_object_lock(shadow_object
);
4583 target_offset
= object
->shadow_offset
;
4584 xfer_end
= upl
->size
+ object
->shadow_offset
;
4586 while(target_offset
< xfer_end
) {
4588 if ((t
= vm_page_lookup(object
,
4589 target_offset
- object
->shadow_offset
))
4591 target_offset
+= PAGE_SIZE_64
;
4596 m
= vm_page_lookup(shadow_object
, target_offset
);
4597 if(m
!= VM_PAGE_NULL
) {
4600 * If this page was encrypted, we
4601 * don't need to decrypt it here.
4602 * We'll decrypt it later, on demand,
4603 * as soon as someone needs to access
4607 if (upl
->flags
& UPL_CLEAR_DIRTY
) {
4608 pmap_clear_modify(m
->phys_page
);
4611 /* It is a part of the semantic of */
4612 /* COPYOUT_FROM UPLs that a commit */
4613 /* implies cache sync between the */
4614 /* vm page and the backing store */
4615 /* this can be used to strip the */
4616 /* precious bit as well as clean */
4617 if (upl
->flags
& UPL_PAGE_SYNC_DONE
)
4618 m
->precious
= FALSE
;
4621 p
= &(page_list
[entry
]);
4622 if(page_list
[entry
].phys_addr
&&
4623 p
->pageout
&& !m
->pageout
) {
4624 vm_page_lock_queues();
4628 vm_page_unlock_queues();
4629 } else if (page_list
[entry
].phys_addr
&&
4630 !p
->pageout
&& m
->pageout
&&
4631 !m
->dump_cleaning
) {
4632 vm_page_lock_queues();
4635 m
->overwriting
= FALSE
;
4637 PAGE_WAKEUP_DONE(m
);
4638 vm_page_unlock_queues();
4640 page_list
[entry
].phys_addr
= 0;
4643 target_offset
+= PAGE_SIZE_64
;
4646 vm_object_unlock(shadow_object
);
4647 if (object
!= shadow_object
)
4648 vm_object_unlock(object
);
4651 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
4652 vm_object_lock(upl
->map_object
->shadow
);
4653 if(upl
->map_object
== upl
->map_object
->shadow
)
4654 vm_object_paging_end(upl
->map_object
->shadow
);
4655 vm_object_unlock(upl
->map_object
->shadow
);
4658 return KERN_SUCCESS
;
4664 vm_object_iopl_request(
4666 vm_object_offset_t offset
,
4669 upl_page_info_array_t user_page_list
,
4670 unsigned int *page_list_count
,
4674 vm_object_offset_t dst_offset
= offset
;
4675 upl_size_t xfer_size
= size
;
4678 wpl_array_t lite_list
= NULL
;
4679 int page_field_size
;
4680 int delayed_unlock
= 0;
4681 int no_zero_fill
= FALSE
;
4682 vm_page_t alias_page
= NULL
;
4687 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
4689 * For forward compatibility's sake,
4690 * reject any unknown flag.
4692 return KERN_INVALID_VALUE
;
4695 if (cntrl_flags
& UPL_ENCRYPT
) {
4698 * The paging path doesn't use this interface,
4699 * so we don't support the UPL_ENCRYPT flag
4700 * here. We won't encrypt the pages.
4702 assert(! (cntrl_flags
& UPL_ENCRYPT
));
4705 if (cntrl_flags
& UPL_NOZEROFILL
)
4706 no_zero_fill
= TRUE
;
4708 if (cntrl_flags
& UPL_COPYOUT_FROM
)
4709 prot
= VM_PROT_READ
;
4711 prot
= VM_PROT_READ
| VM_PROT_WRITE
;
4713 if(((size
/page_size
) > MAX_UPL_TRANSFER
) && !object
->phys_contiguous
) {
4714 size
= MAX_UPL_TRANSFER
* page_size
;
4717 if(cntrl_flags
& UPL_SET_INTERNAL
)
4718 if(page_list_count
!= NULL
)
4719 *page_list_count
= MAX_UPL_TRANSFER
;
4720 if(((cntrl_flags
& UPL_SET_INTERNAL
) && !(object
->phys_contiguous
)) &&
4721 ((page_list_count
!= NULL
) && (*page_list_count
!= 0)
4722 && *page_list_count
< (size
/page_size
)))
4723 return KERN_INVALID_ARGUMENT
;
4725 if((!object
->internal
) && (object
->paging_offset
!= 0))
4726 panic("vm_object_upl_request: vnode object with non-zero paging offset\n");
4728 if(object
->phys_contiguous
) {
4729 /* No paging operations are possible against this memory */
4730 /* and so no need for map object, ever */
4731 cntrl_flags
|= UPL_SET_LITE
;
4735 if(cntrl_flags
& UPL_SET_INTERNAL
) {
4736 if(cntrl_flags
& UPL_SET_LITE
) {
4738 UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
,
4740 user_page_list
= (upl_page_info_t
*)
4741 (((uintptr_t)upl
) + sizeof(struct upl
));
4742 lite_list
= (wpl_array_t
)
4743 (((uintptr_t)user_page_list
) +
4745 sizeof(upl_page_info_t
)));
4746 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
4748 (page_field_size
+ 3) & 0xFFFFFFFC;
4749 bzero((char *)lite_list
, page_field_size
);
4751 UPL_LITE
| UPL_INTERNAL
| UPL_IO_WIRE
;
4753 upl
= upl_create(UPL_CREATE_INTERNAL
, size
);
4754 user_page_list
= (upl_page_info_t
*)
4756 + sizeof(struct upl
));
4757 upl
->flags
= UPL_INTERNAL
| UPL_IO_WIRE
;
4760 if(cntrl_flags
& UPL_SET_LITE
) {
4761 upl
= upl_create(UPL_CREATE_LITE
, size
);
4762 lite_list
= (wpl_array_t
)
4763 (((uintptr_t)upl
) + sizeof(struct upl
));
4764 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
4766 (page_field_size
+ 3) & 0xFFFFFFFC;
4767 bzero((char *)lite_list
, page_field_size
);
4768 upl
->flags
= UPL_LITE
| UPL_IO_WIRE
;
4770 upl
= upl_create(UPL_CREATE_EXTERNAL
, size
);
4771 upl
->flags
= UPL_IO_WIRE
;
4775 if(object
->phys_contiguous
) {
4776 upl
->map_object
= object
;
4777 /* don't need any shadow mappings for this one */
4778 /* since it is already I/O memory */
4779 upl
->flags
|= UPL_DEVICE_MEMORY
;
4781 vm_object_lock(object
);
4782 vm_object_paging_begin(object
);
4783 vm_object_unlock(object
);
4785 /* paging in progress also protects the paging_offset */
4786 upl
->offset
= offset
+ object
->paging_offset
;
4789 if(user_page_list
) {
4790 user_page_list
[0].phys_addr
=
4791 (offset
+ object
->shadow_offset
)>>PAGE_SHIFT
;
4792 user_page_list
[0].device
= TRUE
;
4795 if(page_list_count
!= NULL
) {
4796 if (upl
->flags
& UPL_INTERNAL
) {
4797 *page_list_count
= 0;
4799 *page_list_count
= 1;
4802 return KERN_SUCCESS
;
4805 user_page_list
[0].device
= FALSE
;
4807 if(cntrl_flags
& UPL_SET_LITE
) {
4808 upl
->map_object
= object
;
4810 upl
->map_object
= vm_object_allocate(size
);
4811 vm_object_lock(upl
->map_object
);
4812 upl
->map_object
->shadow
= object
;
4813 upl
->map_object
->pageout
= TRUE
;
4814 upl
->map_object
->can_persist
= FALSE
;
4815 upl
->map_object
->copy_strategy
=
4816 MEMORY_OBJECT_COPY_NONE
;
4817 upl
->map_object
->shadow_offset
= offset
;
4818 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
4819 vm_object_unlock(upl
->map_object
);
4822 vm_object_lock(object
);
4823 vm_object_paging_begin(object
);
4825 if (!object
->phys_contiguous
) {
4826 /* Protect user space from future COW operations */
4827 object
->true_share
= TRUE
;
4828 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
4829 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
4832 /* we can lock the upl offset now that paging_in_progress is set */
4835 upl
->offset
= offset
+ object
->paging_offset
;
4838 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
4839 #endif /* UPL_DEBUG */
4842 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
4844 * The user requested that access to the pages in this URL
4845 * be blocked until the UPL is commited or aborted.
4847 upl
->flags
|= UPL_ACCESS_BLOCKED
;
4852 if((alias_page
== NULL
) && !(cntrl_flags
& UPL_SET_LITE
)) {
4853 if (delayed_unlock
) {
4855 vm_page_unlock_queues();
4857 vm_object_unlock(object
);
4858 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
4859 vm_object_lock(object
);
4861 dst_page
= vm_page_lookup(object
, dst_offset
);
4865 * If the page is encrypted, we need to decrypt it,
4866 * so force a soft page fault.
4868 if ((dst_page
== VM_PAGE_NULL
) || (dst_page
->busy
) ||
4869 (dst_page
->encrypted
) ||
4870 (dst_page
->unusual
&& (dst_page
->error
||
4871 dst_page
->restart
||
4873 dst_page
->fictitious
||
4874 (prot
& dst_page
->page_lock
)))) {
4875 vm_fault_return_t result
;
4878 kern_return_t error_code
;
4881 vm_object_offset_t lo_offset
= offset
;
4882 vm_object_offset_t hi_offset
= offset
+ size
;
4885 if (delayed_unlock
) {
4887 vm_page_unlock_queues();
4890 if(cntrl_flags
& UPL_SET_INTERRUPTIBLE
) {
4891 interruptible
= THREAD_ABORTSAFE
;
4893 interruptible
= THREAD_UNINT
;
4896 result
= vm_fault_page(object
, dst_offset
,
4897 prot
| VM_PROT_WRITE
, FALSE
,
4899 lo_offset
, hi_offset
,
4900 VM_BEHAVIOR_SEQUENTIAL
,
4901 &prot
, &dst_page
, &top_page
,
4903 &error_code
, no_zero_fill
, FALSE
, NULL
, 0);
4906 case VM_FAULT_SUCCESS
:
4908 PAGE_WAKEUP_DONE(dst_page
);
4911 * Release paging references and
4912 * top-level placeholder page, if any.
4915 if(top_page
!= VM_PAGE_NULL
) {
4916 vm_object_t local_object
;
4920 != dst_page
->object
) {
4923 VM_PAGE_FREE(top_page
);
4924 vm_object_paging_end(
4929 VM_PAGE_FREE(top_page
);
4930 vm_object_paging_end(
4938 case VM_FAULT_RETRY
:
4939 vm_object_lock(object
);
4940 vm_object_paging_begin(object
);
4943 case VM_FAULT_FICTITIOUS_SHORTAGE
:
4944 vm_page_more_fictitious();
4945 vm_object_lock(object
);
4946 vm_object_paging_begin(object
);
4949 case VM_FAULT_MEMORY_SHORTAGE
:
4950 if (vm_page_wait(interruptible
)) {
4951 vm_object_lock(object
);
4952 vm_object_paging_begin(object
);
4957 case VM_FAULT_INTERRUPTED
:
4958 error_code
= MACH_SEND_INTERRUPTED
;
4959 case VM_FAULT_MEMORY_ERROR
:
4960 ret
= (error_code
? error_code
:
4962 vm_object_lock(object
);
4963 for(; offset
< dst_offset
;
4964 offset
+= PAGE_SIZE
) {
4965 dst_page
= vm_page_lookup(
4967 if(dst_page
== VM_PAGE_NULL
)
4968 panic("vm_object_iopl_request: Wired pages missing. \n");
4969 vm_page_lock_queues();
4970 vm_page_unwire(dst_page
);
4971 vm_page_unlock_queues();
4972 VM_STAT(reactivations
++);
4974 vm_object_unlock(object
);
4978 } while ((result
!= VM_FAULT_SUCCESS
)
4979 || (result
== VM_FAULT_INTERRUPTED
));
4981 if (delayed_unlock
== 0)
4982 vm_page_lock_queues();
4983 vm_page_wire(dst_page
);
4985 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
4987 * Mark the page "busy" to block any future page fault
4988 * on this page. We'll also remove the mapping
4989 * of all these pages before leaving this routine.
4991 assert(!dst_page
->fictitious
);
4992 dst_page
->busy
= TRUE
;
4996 if (cntrl_flags
& UPL_SET_LITE
) {
4998 pg_num
= (dst_offset
-offset
)/PAGE_SIZE
;
4999 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
5002 * Convert the fictitious page to a
5003 * private shadow of the real page.
5005 assert(alias_page
->fictitious
);
5006 alias_page
->fictitious
= FALSE
;
5007 alias_page
->private = TRUE
;
5008 alias_page
->pageout
= TRUE
;
5009 alias_page
->phys_page
= dst_page
->phys_page
;
5010 vm_page_wire(alias_page
);
5012 vm_page_insert(alias_page
,
5013 upl
->map_object
, size
- xfer_size
);
5014 assert(!alias_page
->wanted
);
5015 alias_page
->busy
= FALSE
;
5016 alias_page
->absent
= FALSE
;
5019 /* expect the page to be used */
5020 dst_page
->reference
= TRUE
;
5022 if (!(cntrl_flags
& UPL_COPYOUT_FROM
))
5023 dst_page
->dirty
= TRUE
;
5026 if (user_page_list
) {
5027 user_page_list
[entry
].phys_addr
5028 = dst_page
->phys_page
;
5029 user_page_list
[entry
].dirty
=
5031 user_page_list
[entry
].pageout
=
5033 user_page_list
[entry
].absent
=
5035 user_page_list
[entry
].precious
=
5039 if (delayed_unlock
++ > DELAYED_UNLOCK_LIMIT
) {
5041 vm_page_unlock_queues();
5044 dst_offset
+= PAGE_SIZE_64
;
5045 xfer_size
-= PAGE_SIZE
;
5048 vm_page_unlock_queues();
5050 if (upl
->flags
& UPL_INTERNAL
) {
5051 if(page_list_count
!= NULL
)
5052 *page_list_count
= 0;
5053 } else if (*page_list_count
> entry
) {
5054 if(page_list_count
!= NULL
)
5055 *page_list_count
= entry
;
5058 if (alias_page
!= NULL
) {
5059 vm_page_lock_queues();
5060 vm_page_free(alias_page
);
5061 vm_page_unlock_queues();
5064 vm_object_unlock(object
);
5066 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
5068 * We've marked all the pages "busy" so that future
5069 * page faults will block.
5070 * Now remove the mapping for these pages, so that they
5071 * can't be accessed without causing a page fault.
5073 vm_object_pmap_protect(object
, offset
, (vm_object_size_t
)size
,
5074 PMAP_NULL
, 0, VM_PROT_NONE
);
5077 return KERN_SUCCESS
;
5085 kern_return_t retval
;
5086 boolean_t upls_locked
;
5087 vm_object_t object1
, object2
;
5089 if (upl1
== UPL_NULL
|| upl2
== UPL_NULL
|| upl1
== upl2
) {
5090 return KERN_INVALID_ARGUMENT
;
5093 upls_locked
= FALSE
;
5096 * Since we need to lock both UPLs at the same time,
5097 * avoid deadlocks by always taking locks in the same order.
5106 upls_locked
= TRUE
; /* the UPLs will need to be unlocked */
5108 object1
= upl1
->map_object
;
5109 object2
= upl2
->map_object
;
5111 if (upl1
->offset
!= 0 || upl2
->offset
!= 0 ||
5112 upl1
->size
!= upl2
->size
) {
5114 * We deal only with full objects, not subsets.
5115 * That's because we exchange the entire backing store info
5116 * for the objects: pager, resident pages, etc... We can't do
5119 retval
= KERN_INVALID_VALUE
;
5124 * Tranpose the VM objects' backing store.
5126 retval
= vm_object_transpose(object1
, object2
,
5127 (vm_object_size_t
) upl1
->size
);
5129 if (retval
== KERN_SUCCESS
) {
5131 * Make each UPL point to the correct VM object, i.e. the
5132 * object holding the pages that the UPL refers to...
5134 upl1
->map_object
= object2
;
5135 upl2
->map_object
= object1
;
5145 upls_locked
= FALSE
;
5154 * Rationale: the user might have some encrypted data on disk (via
5155 * FileVault or any other mechanism). That data is then decrypted in
5156 * memory, which is safe as long as the machine is secure. But that
5157 * decrypted data in memory could be paged out to disk by the default
5158 * pager. The data would then be stored on disk in clear (not encrypted)
5159 * and it could be accessed by anyone who gets physical access to the
5160 * disk (if the laptop or the disk gets stolen for example). This weakens
5161 * the security offered by FileVault.
5163 * Solution: the default pager will optionally request that all the
5164 * pages it gathers for pageout be encrypted, via the UPL interfaces,
5165 * before it sends this UPL to disk via the vnode_pageout() path.
5169 * To avoid disrupting the VM LRU algorithms, we want to keep the
5170 * clean-in-place mechanisms, which allow us to send some extra pages to
5171 * swap (clustering) without actually removing them from the user's
5172 * address space. We don't want the user to unknowingly access encrypted
5173 * data, so we have to actually remove the encrypted pages from the page
5174 * table. When the user accesses the data, the hardware will fail to
5175 * locate the virtual page in its page table and will trigger a page
5176 * fault. We can then decrypt the page and enter it in the page table
5177 * again. Whenever we allow the user to access the contents of a page,
5178 * we have to make sure it's not encrypted.
5184 * Reserve of virtual addresses in the kernel address space.
5185 * We need to map the physical pages in the kernel, so that we
5186 * can call the encryption/decryption routines with a kernel
5187 * virtual address. We keep this pool of pre-allocated kernel
5188 * virtual addresses so that we don't have to scan the kernel's
5189 * virtaul address space each time we need to encrypt or decrypt
5191 * It would be nice to be able to encrypt and decrypt in physical
5192 * mode but that might not always be more efficient...
5194 decl_simple_lock_data(,vm_paging_lock
)
5195 #define VM_PAGING_NUM_PAGES 64
5196 vm_map_offset_t vm_paging_base_address
= 0;
5197 boolean_t vm_paging_page_inuse
[VM_PAGING_NUM_PAGES
] = { FALSE
, };
5198 int vm_paging_max_index
= 0;
5199 unsigned long vm_paging_no_kernel_page
= 0;
5200 unsigned long vm_paging_objects_mapped
= 0;
5201 unsigned long vm_paging_pages_mapped
= 0;
5202 unsigned long vm_paging_objects_mapped_slow
= 0;
5203 unsigned long vm_paging_pages_mapped_slow
= 0;
5207 * vm_paging_map_object:
5208 * Maps part of a VM object's pages in the kernel
5209 * virtual address space, using the pre-allocated
5210 * kernel virtual addresses, if possible.
5212 * The VM object is locked. This lock will get
5213 * dropped and re-acquired though.
5216 vm_paging_map_object(
5217 vm_map_offset_t
*address
,
5220 vm_object_offset_t offset
,
5221 vm_map_size_t
*size
)
5224 vm_map_offset_t page_map_offset
;
5225 vm_map_size_t map_size
;
5226 vm_object_offset_t object_offset
;
5229 vm_map_entry_t map_entry
;
5230 #endif /* __ppc__ */
5234 if (page
!= VM_PAGE_NULL
&& *size
== PAGE_SIZE
) {
5236 * Optimization for the PowerPC.
5237 * Use one of the pre-allocated kernel virtual addresses
5238 * and just enter the VM page in the kernel address space
5239 * at that virtual address.
5241 vm_object_unlock(object
);
5242 simple_lock(&vm_paging_lock
);
5244 if (vm_paging_base_address
== 0) {
5246 * Initialize our pool of pre-allocated kernel
5247 * virtual addresses.
5249 simple_unlock(&vm_paging_lock
);
5250 page_map_offset
= 0;
5251 kr
= vm_map_find_space(kernel_map
,
5253 VM_PAGING_NUM_PAGES
* PAGE_SIZE
,
5256 if (kr
!= KERN_SUCCESS
) {
5257 panic("vm_paging_map_object: "
5258 "kernel_map full\n");
5260 map_entry
->object
.vm_object
= kernel_object
;
5262 page_map_offset
- VM_MIN_KERNEL_ADDRESS
;
5263 vm_object_reference(kernel_object
);
5264 vm_map_unlock(kernel_map
);
5266 simple_lock(&vm_paging_lock
);
5267 if (vm_paging_base_address
!= 0) {
5268 /* someone raced us and won: undo */
5269 simple_unlock(&vm_paging_lock
);
5270 kr
= vm_map_remove(kernel_map
,
5273 (VM_PAGING_NUM_PAGES
5276 assert(kr
== KERN_SUCCESS
);
5277 simple_lock(&vm_paging_lock
);
5279 vm_paging_base_address
= page_map_offset
;
5284 * Try and find an available kernel virtual address
5285 * from our pre-allocated pool.
5287 page_map_offset
= 0;
5288 for (i
= 0; i
< VM_PAGING_NUM_PAGES
; i
++) {
5289 if (vm_paging_page_inuse
[i
] == FALSE
) {
5290 page_map_offset
= vm_paging_base_address
+
5296 if (page_map_offset
!= 0) {
5298 * We found a kernel virtual address;
5299 * map the physical page to that virtual address.
5301 if (i
> vm_paging_max_index
) {
5302 vm_paging_max_index
= i
;
5304 vm_paging_page_inuse
[i
] = TRUE
;
5305 simple_unlock(&vm_paging_lock
);
5306 pmap_map_block(kernel_pmap
,
5309 1, /* Size is number of 4k pages */
5311 ((int) page
->object
->wimg_bits
&
5314 vm_paging_objects_mapped
++;
5315 vm_paging_pages_mapped
++;
5316 *address
= page_map_offset
;
5317 vm_object_lock(object
);
5319 /* all done and mapped, ready to use ! */
5320 return KERN_SUCCESS
;
5324 * We ran out of pre-allocated kernel virtual
5325 * addresses. Just map the page in the kernel
5326 * the slow and regular way.
5328 vm_paging_no_kernel_page
++;
5329 simple_unlock(&vm_paging_lock
);
5330 vm_object_lock(object
);
5332 #endif /* __ppc__ */
5334 object_offset
= vm_object_trunc_page(offset
);
5335 map_size
= vm_map_round_page(*size
);
5338 * Try and map the required range of the object
5342 /* don't go beyond the object's end... */
5343 if (object_offset
>= object
->size
) {
5345 } else if (map_size
> object
->size
- offset
) {
5346 map_size
= object
->size
- offset
;
5349 vm_object_reference_locked(object
); /* for the map entry */
5350 vm_object_unlock(object
);
5352 kr
= vm_map_enter(kernel_map
,
5363 if (kr
!= KERN_SUCCESS
) {
5366 vm_object_deallocate(object
); /* for the map entry */
5373 * Enter the mapped pages in the page table now.
5375 vm_object_lock(object
);
5376 for (page_map_offset
= 0;
5378 map_size
-= PAGE_SIZE_64
, page_map_offset
+= PAGE_SIZE_64
) {
5379 unsigned int cache_attr
;
5381 page
= vm_page_lookup(object
, offset
+ page_map_offset
);
5382 if (page
== VM_PAGE_NULL
) {
5383 panic("vm_paging_map_object: no page !?");
5385 if (page
->no_isync
== TRUE
) {
5386 pmap_sync_page_data_phys(page
->phys_page
);
5388 cache_attr
= ((unsigned int) object
->wimg_bits
) & VM_WIMG_MASK
;
5390 PMAP_ENTER(kernel_pmap
,
5391 *address
+ page_map_offset
,
5398 vm_paging_objects_mapped_slow
++;
5399 vm_paging_pages_mapped_slow
+= map_size
/ PAGE_SIZE_64
;
5401 return KERN_SUCCESS
;
5406 * vm_paging_unmap_object:
5407 * Unmaps part of a VM object's pages from the kernel
5408 * virtual address space.
5410 * The VM object is locked. This lock will get
5411 * dropped and re-acquired though.
5414 vm_paging_unmap_object(
5416 vm_map_offset_t start
,
5417 vm_map_offset_t end
)
5422 #endif /* __ppc__ */
5424 if ((vm_paging_base_address
!= 0) &&
5425 ((start
< vm_paging_base_address
) ||
5426 (end
> (vm_paging_base_address
5427 + (VM_PAGING_NUM_PAGES
* PAGE_SIZE
))))) {
5429 * We didn't use our pre-allocated pool of
5430 * kernel virtual address. Deallocate the
5433 if (object
!= VM_OBJECT_NULL
) {
5434 vm_object_unlock(object
);
5436 kr
= vm_map_remove(kernel_map
, start
, end
, VM_MAP_NO_FLAGS
);
5437 if (object
!= VM_OBJECT_NULL
) {
5438 vm_object_lock(object
);
5440 assert(kr
== KERN_SUCCESS
);
5443 * We used a kernel virtual address from our
5444 * pre-allocated pool. Put it back in the pool
5448 assert(end
- start
== PAGE_SIZE
);
5449 i
= (start
- vm_paging_base_address
) >> PAGE_SHIFT
;
5451 /* undo the pmap mapping */
5452 mapping_remove(kernel_pmap
, start
);
5454 simple_lock(&vm_paging_lock
);
5455 vm_paging_page_inuse
[i
] = FALSE
;
5456 simple_unlock(&vm_paging_lock
);
5457 #endif /* __ppc__ */
5463 * "iv" is the "initial vector". Ideally, we want to
5464 * have a different one for each page we encrypt, so that
5465 * crackers can't find encryption patterns too easily.
5467 #define SWAP_CRYPT_AES_KEY_SIZE 128 /* XXX 192 and 256 don't work ! */
5468 boolean_t swap_crypt_ctx_initialized
= FALSE
;
5469 aes_32t swap_crypt_key
[8]; /* big enough for a 256 key */
5470 aes_ctx swap_crypt_ctx
;
5471 const unsigned char swap_crypt_null_iv
[AES_BLOCK_SIZE
] = {0xa, };
5474 boolean_t swap_crypt_ctx_tested
= FALSE
;
5475 unsigned char swap_crypt_test_page_ref
[4096] __attribute__((aligned(4096)));
5476 unsigned char swap_crypt_test_page_encrypt
[4096] __attribute__((aligned(4096)));
5477 unsigned char swap_crypt_test_page_decrypt
[4096] __attribute__((aligned(4096)));
5480 extern u_long
random(void);
5483 * Initialize the encryption context: key and key size.
5485 void swap_crypt_ctx_initialize(void); /* forward */
5487 swap_crypt_ctx_initialize(void)
5492 * No need for locking to protect swap_crypt_ctx_initialized
5493 * because the first use of encryption will come from the
5494 * pageout thread (we won't pagein before there's been a pageout)
5495 * and there's only one pageout thread.
5497 if (swap_crypt_ctx_initialized
== FALSE
) {
5499 i
< (sizeof (swap_crypt_key
) /
5500 sizeof (swap_crypt_key
[0]));
5502 swap_crypt_key
[i
] = random();
5504 aes_encrypt_key((const unsigned char *) swap_crypt_key
,
5505 SWAP_CRYPT_AES_KEY_SIZE
,
5506 &swap_crypt_ctx
.encrypt
);
5507 aes_decrypt_key((const unsigned char *) swap_crypt_key
,
5508 SWAP_CRYPT_AES_KEY_SIZE
,
5509 &swap_crypt_ctx
.decrypt
);
5510 swap_crypt_ctx_initialized
= TRUE
;
5515 * Validate the encryption algorithms.
5517 if (swap_crypt_ctx_tested
== FALSE
) {
5519 for (i
= 0; i
< 4096; i
++) {
5520 swap_crypt_test_page_ref
[i
] = (char) i
;
5523 aes_encrypt_cbc(swap_crypt_test_page_ref
,
5525 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5526 swap_crypt_test_page_encrypt
,
5527 &swap_crypt_ctx
.encrypt
);
5529 aes_decrypt_cbc(swap_crypt_test_page_encrypt
,
5531 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5532 swap_crypt_test_page_decrypt
,
5533 &swap_crypt_ctx
.decrypt
);
5534 /* compare result with original */
5535 for (i
= 0; i
< 4096; i
++) {
5536 if (swap_crypt_test_page_decrypt
[i
] !=
5537 swap_crypt_test_page_ref
[i
]) {
5538 panic("encryption test failed");
5543 aes_encrypt_cbc(swap_crypt_test_page_decrypt
,
5545 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5546 swap_crypt_test_page_decrypt
,
5547 &swap_crypt_ctx
.encrypt
);
5548 /* decrypt in place */
5549 aes_decrypt_cbc(swap_crypt_test_page_decrypt
,
5551 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5552 swap_crypt_test_page_decrypt
,
5553 &swap_crypt_ctx
.decrypt
);
5554 for (i
= 0; i
< 4096; i
++) {
5555 if (swap_crypt_test_page_decrypt
[i
] !=
5556 swap_crypt_test_page_ref
[i
]) {
5557 panic("in place encryption test failed");
5561 swap_crypt_ctx_tested
= TRUE
;
5569 * Encrypt the given page, for secure paging.
5570 * The page might already be mapped at kernel virtual
5571 * address "kernel_mapping_offset". Otherwise, we need
5575 * The page's object is locked, but this lock will be released
5577 * The page is busy and not accessible by users (not entered in any pmap).
5582 vm_map_offset_t kernel_mapping_offset
)
5584 int clear_refmod
= 0;
5586 boolean_t page_was_referenced
;
5587 boolean_t page_was_modified
;
5588 vm_map_size_t kernel_mapping_size
;
5589 vm_offset_t kernel_vaddr
;
5591 unsigned char aes_iv
[AES_BLOCK_SIZE
];
5593 memory_object_t pager_object
;
5594 vm_object_offset_t paging_offset
;
5598 if (! vm_pages_encrypted
) {
5599 vm_pages_encrypted
= TRUE
;
5603 assert(page
->dirty
|| page
->precious
);
5605 if (page
->encrypted
) {
5607 * Already encrypted: no need to do it again.
5609 vm_page_encrypt_already_encrypted_counter
++;
5612 ASSERT_PAGE_DECRYPTED(page
);
5615 * Gather the "reference" and "modified" status of the page.
5616 * We'll restore these values after the encryption, so that
5617 * the encryption is transparent to the rest of the system
5618 * and doesn't impact the VM's LRU logic.
5620 page_was_referenced
=
5621 (page
->reference
|| pmap_is_referenced(page
->phys_page
));
5623 (page
->dirty
|| pmap_is_modified(page
->phys_page
));
5625 if (kernel_mapping_offset
== 0) {
5627 * The page hasn't already been mapped in kernel space
5628 * by the caller. Map it now, so that we can access
5629 * its contents and encrypt them.
5631 kernel_mapping_size
= PAGE_SIZE
;
5632 kr
= vm_paging_map_object(&kernel_mapping_offset
,
5636 &kernel_mapping_size
);
5637 if (kr
!= KERN_SUCCESS
) {
5638 panic("vm_page_encrypt: "
5639 "could not map page in kernel: 0x%x\n",
5643 kernel_mapping_size
= 0;
5645 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
5647 if (swap_crypt_ctx_initialized
== FALSE
) {
5648 swap_crypt_ctx_initialize();
5650 assert(swap_crypt_ctx_initialized
);
5653 * Prepare an "initial vector" for the encryption.
5654 * We use the "pager" and the "paging_offset" for that
5655 * page to obfuscate the encrypted data a bit more and
5656 * prevent crackers from finding patterns that they could
5657 * use to break the key.
5659 bzero(&encrypt_iv
.aes_iv
[0], sizeof (encrypt_iv
.aes_iv
));
5660 encrypt_iv
.vm
.pager_object
= page
->object
->pager
;
5661 encrypt_iv
.vm
.paging_offset
=
5662 page
->object
->paging_offset
+ page
->offset
;
5664 vm_object_unlock(page
->object
);
5666 /* encrypt the "initial vector" */
5667 aes_encrypt_cbc((const unsigned char *) &encrypt_iv
.aes_iv
[0],
5670 &encrypt_iv
.aes_iv
[0],
5671 &swap_crypt_ctx
.encrypt
);
5676 aes_encrypt_cbc((const unsigned char *) kernel_vaddr
,
5677 &encrypt_iv
.aes_iv
[0],
5678 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5679 (unsigned char *) kernel_vaddr
,
5680 &swap_crypt_ctx
.encrypt
);
5682 vm_page_encrypt_counter
++;
5684 vm_object_lock(page
->object
);
5687 * Unmap the page from the kernel's address space,
5688 * if we had to map it ourselves. Otherwise, let
5689 * the caller undo the mapping if needed.
5691 if (kernel_mapping_size
!= 0) {
5692 vm_paging_unmap_object(page
->object
,
5693 kernel_mapping_offset
,
5694 kernel_mapping_offset
+ kernel_mapping_size
);
5698 * Restore the "reference" and "modified" bits.
5699 * This should clean up any impact the encryption had
5702 if (! page_was_referenced
) {
5703 clear_refmod
|= VM_MEM_REFERENCED
;
5704 page
->reference
= FALSE
;
5706 if (! page_was_modified
) {
5707 clear_refmod
|= VM_MEM_MODIFIED
;
5708 page
->dirty
= FALSE
;
5711 pmap_clear_refmod(page
->phys_page
, clear_refmod
);
5713 page
->encrypted
= TRUE
;
5719 * Decrypt the given page.
5720 * The page might already be mapped at kernel virtual
5721 * address "kernel_mapping_offset". Otherwise, we need
5725 * The page's VM object is locked but will be unlocked and relocked.
5726 * The page is busy and not accessible by users (not entered in any pmap).
5731 vm_map_offset_t kernel_mapping_offset
)
5733 int clear_refmod
= 0;
5735 vm_map_size_t kernel_mapping_size
;
5736 vm_offset_t kernel_vaddr
;
5737 boolean_t page_was_referenced
;
5739 unsigned char aes_iv
[AES_BLOCK_SIZE
];
5741 memory_object_t pager_object
;
5742 vm_object_offset_t paging_offset
;
5747 assert(page
->encrypted
);
5750 * Gather the "reference" status of the page.
5751 * We'll restore its value after the decryption, so that
5752 * the decryption is transparent to the rest of the system
5753 * and doesn't impact the VM's LRU logic.
5755 page_was_referenced
=
5756 (page
->reference
|| pmap_is_referenced(page
->phys_page
));
5758 if (kernel_mapping_offset
== 0) {
5760 * The page hasn't already been mapped in kernel space
5761 * by the caller. Map it now, so that we can access
5762 * its contents and decrypt them.
5764 kernel_mapping_size
= PAGE_SIZE
;
5765 kr
= vm_paging_map_object(&kernel_mapping_offset
,
5769 &kernel_mapping_size
);
5770 if (kr
!= KERN_SUCCESS
) {
5771 panic("vm_page_decrypt: "
5772 "could not map page in kernel: 0x%x\n");
5775 kernel_mapping_size
= 0;
5777 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
5779 assert(swap_crypt_ctx_initialized
);
5782 * Prepare an "initial vector" for the decryption.
5783 * It has to be the same as the "initial vector" we
5784 * used to encrypt that page.
5786 bzero(&decrypt_iv
.aes_iv
[0], sizeof (decrypt_iv
.aes_iv
));
5787 decrypt_iv
.vm
.pager_object
= page
->object
->pager
;
5788 decrypt_iv
.vm
.paging_offset
=
5789 page
->object
->paging_offset
+ page
->offset
;
5791 vm_object_unlock(page
->object
);
5793 /* encrypt the "initial vector" */
5794 aes_encrypt_cbc((const unsigned char *) &decrypt_iv
.aes_iv
[0],
5797 &decrypt_iv
.aes_iv
[0],
5798 &swap_crypt_ctx
.encrypt
);
5803 aes_decrypt_cbc((const unsigned char *) kernel_vaddr
,
5804 &decrypt_iv
.aes_iv
[0],
5805 PAGE_SIZE
/ AES_BLOCK_SIZE
,
5806 (unsigned char *) kernel_vaddr
,
5807 &swap_crypt_ctx
.decrypt
);
5808 vm_page_decrypt_counter
++;
5810 vm_object_lock(page
->object
);
5813 * Unmap the page from the kernel's address space,
5814 * if we had to map it ourselves. Otherwise, let
5815 * the caller undo the mapping if needed.
5817 if (kernel_mapping_size
!= 0) {
5818 vm_paging_unmap_object(page
->object
,
5820 kernel_vaddr
+ PAGE_SIZE
);
5824 * After decryption, the page is actually clean.
5825 * It was encrypted as part of paging, which "cleans"
5826 * the "dirty" pages.
5827 * Noone could access it after it was encrypted
5828 * and the decryption doesn't count.
5830 page
->dirty
= FALSE
;
5831 clear_refmod
= VM_MEM_MODIFIED
;
5833 /* restore the "reference" bit */
5834 if (! page_was_referenced
) {
5835 page
->reference
= FALSE
;
5836 clear_refmod
|= VM_MEM_REFERENCED
;
5838 pmap_clear_refmod(page
->phys_page
, clear_refmod
);
5840 page
->encrypted
= FALSE
;
5843 * We've just modified the page's contents via the data cache and part
5844 * of the new contents might still be in the cache and not yet in RAM.
5845 * Since the page is now available and might get gathered in a UPL to
5846 * be part of a DMA transfer from a driver that expects the memory to
5847 * be coherent at this point, we have to flush the data cache.
5849 pmap_sync_page_data_phys(page
->phys_page
);
5851 * Since the page is not mapped yet, some code might assume that it
5852 * doesn't need to invalidate the instruction cache when writing to
5853 * that page. That code relies on "no_isync" being set, so that the
5854 * caches get syncrhonized when the page is first mapped. So we need
5855 * to set "no_isync" here too, despite the fact that we just
5856 * synchronized the caches above...
5858 page
->no_isync
= TRUE
;
5861 unsigned long upl_encrypt_upls
= 0;
5862 unsigned long upl_encrypt_pages
= 0;
5868 * Encrypts all the pages in the UPL, within the specified range.
5874 upl_offset_t crypt_offset
,
5875 upl_size_t crypt_size
)
5877 upl_size_t upl_size
;
5878 upl_offset_t upl_offset
;
5879 vm_object_t upl_object
;
5881 vm_object_t shadow_object
;
5882 vm_object_offset_t shadow_offset
;
5883 vm_object_offset_t paging_offset
;
5884 vm_object_offset_t base_offset
;
5887 upl_encrypt_pages
+= crypt_size
/ PAGE_SIZE
;
5891 upl_object
= upl
->map_object
;
5892 upl_offset
= upl
->offset
;
5893 upl_size
= upl
->size
;
5897 vm_object_lock(upl_object
);
5900 * Find the VM object that contains the actual pages.
5902 if (upl_object
->pageout
) {
5903 shadow_object
= upl_object
->shadow
;
5905 * The offset in the shadow object is actually also
5906 * accounted for in upl->offset. It possibly shouldn't be
5907 * this way, but for now don't account for it twice.
5910 assert(upl_object
->paging_offset
== 0); /* XXX ? */
5911 vm_object_lock(shadow_object
);
5913 shadow_object
= upl_object
;
5917 paging_offset
= shadow_object
->paging_offset
;
5918 vm_object_paging_begin(shadow_object
);
5920 if (shadow_object
!= upl_object
) {
5921 vm_object_unlock(shadow_object
);
5923 vm_object_unlock(upl_object
);
5925 base_offset
= shadow_offset
;
5926 base_offset
+= upl_offset
;
5927 base_offset
+= crypt_offset
;
5928 base_offset
-= paging_offset
;
5930 * Unmap the pages, so that nobody can continue accessing them while
5931 * they're encrypted. After that point, all accesses to these pages
5932 * will cause a page fault and block while the page is being encrypted
5933 * (busy). After the encryption completes, any access will cause a
5934 * page fault and the page gets decrypted at that time.
5936 assert(crypt_offset
+ crypt_size
<= upl_size
);
5937 vm_object_pmap_protect(shadow_object
,
5939 (vm_object_size_t
)crypt_size
,
5944 /* XXX FBDP could the object have changed significantly here ? */
5945 vm_object_lock(shadow_object
);
5947 for (upl_offset
= 0;
5948 upl_offset
< crypt_size
;
5949 upl_offset
+= PAGE_SIZE
) {
5950 page
= vm_page_lookup(shadow_object
,
5951 base_offset
+ upl_offset
);
5952 if (page
== VM_PAGE_NULL
) {
5953 panic("upl_encrypt: "
5954 "no page for (obj=%p,off=%lld+%d)!\n",
5959 vm_page_encrypt(page
, 0);
5962 vm_object_paging_end(shadow_object
);
5963 vm_object_unlock(shadow_object
);
5967 upl_get_internal_pagelist_offset(void)
5969 return sizeof(struct upl
);
5976 upl
->flags
|= UPL_CLEAR_DIRTY
;
5983 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
5989 boolean_t
upl_page_present(upl_page_info_t
*upl
, int index
)
5991 return(UPL_PAGE_PRESENT(upl
, index
));
5993 boolean_t
upl_dirty_page(upl_page_info_t
*upl
, int index
)
5995 return(UPL_DIRTY_PAGE(upl
, index
));
5997 boolean_t
upl_valid_page(upl_page_info_t
*upl
, int index
)
5999 return(UPL_VALID_PAGE(upl
, index
));
6001 ppnum_t
upl_phys_page(upl_page_info_t
*upl
, int index
)
6003 return(UPL_PHYS_PAGE(upl
, index
));
6007 vm_countdirtypages(void)
6019 vm_page_lock_queues();
6020 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
6022 if (m
==(vm_page_t
)0) break;
6024 if(m
->dirty
) dpages
++;
6025 if(m
->pageout
) pgopages
++;
6026 if(m
->precious
) precpages
++;
6028 assert(m
->object
!= kernel_object
);
6029 m
= (vm_page_t
) queue_next(&m
->pageq
);
6030 if (m
==(vm_page_t
)0) break;
6032 } while (!queue_end(&vm_page_queue_inactive
,(queue_entry_t
) m
));
6033 vm_page_unlock_queues();
6035 vm_page_lock_queues();
6036 m
= (vm_page_t
) queue_first(&vm_page_queue_zf
);
6038 if (m
==(vm_page_t
)0) break;
6040 if(m
->dirty
) dpages
++;
6041 if(m
->pageout
) pgopages
++;
6042 if(m
->precious
) precpages
++;
6044 assert(m
->object
!= kernel_object
);
6045 m
= (vm_page_t
) queue_next(&m
->pageq
);
6046 if (m
==(vm_page_t
)0) break;
6048 } while (!queue_end(&vm_page_queue_zf
,(queue_entry_t
) m
));
6049 vm_page_unlock_queues();
6051 printf("IN Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
6057 vm_page_lock_queues();
6058 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
6061 if(m
== (vm_page_t
)0) break;
6062 if(m
->dirty
) dpages
++;
6063 if(m
->pageout
) pgopages
++;
6064 if(m
->precious
) precpages
++;
6066 assert(m
->object
!= kernel_object
);
6067 m
= (vm_page_t
) queue_next(&m
->pageq
);
6068 if(m
== (vm_page_t
)0) break;
6070 } while (!queue_end(&vm_page_queue_active
,(queue_entry_t
) m
));
6071 vm_page_unlock_queues();
6073 printf("AC Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
6076 #endif /* MACH_BSD */
6079 kern_return_t
upl_ubc_alias_set(upl_t upl
, unsigned int alias1
, unsigned int alias2
)
6081 upl
->ubc_alias1
= alias1
;
6082 upl
->ubc_alias2
= alias2
;
6083 return KERN_SUCCESS
;
6085 int upl_ubc_alias_get(upl_t upl
, unsigned int * al
, unsigned int * al2
)
6088 *al
= upl
->ubc_alias1
;
6090 *al2
= upl
->ubc_alias2
;
6091 return KERN_SUCCESS
;
6093 #endif /* UPL_DEBUG */
6098 #include <ddb/db_output.h>
6099 #include <ddb/db_print.h>
6100 #include <vm/vm_print.h>
6102 #define printf kdbprintf
6103 void db_pageout(void);
6109 iprintf("VM Statistics:\n");
6111 iprintf("pages:\n");
6113 iprintf("activ %5d inact %5d free %5d",
6114 vm_page_active_count
, vm_page_inactive_count
,
6115 vm_page_free_count
);
6116 printf(" wire %5d gobbl %5d\n",
6117 vm_page_wire_count
, vm_page_gobble_count
);
6119 iprintf("target:\n");
6121 iprintf("min %5d inact %5d free %5d",
6122 vm_page_free_min
, vm_page_inactive_target
,
6123 vm_page_free_target
);
6124 printf(" resrv %5d\n", vm_page_free_reserved
);
6126 iprintf("pause:\n");
6132 extern int c_laundry_pages_freed
;
6133 #endif /* MACH_COUNTERS */
6138 iprintf("Pageout Statistics:\n");
6140 iprintf("active %5d inactv %5d\n",
6141 vm_pageout_active
, vm_pageout_inactive
);
6142 iprintf("nolock %5d avoid %5d busy %5d absent %5d\n",
6143 vm_pageout_inactive_nolock
, vm_pageout_inactive_avoid
,
6144 vm_pageout_inactive_busy
, vm_pageout_inactive_absent
);
6145 iprintf("used %5d clean %5d dirty %5d\n",
6146 vm_pageout_inactive_used
, vm_pageout_inactive_clean
,
6147 vm_pageout_inactive_dirty
);
6149 iprintf("laundry_pages_freed %d\n", c_laundry_pages_freed
);
6150 #endif /* MACH_COUNTERS */
6151 #if MACH_CLUSTER_STATS
6152 iprintf("Cluster Statistics:\n");
6154 iprintf("dirtied %5d cleaned %5d collisions %5d\n",
6155 vm_pageout_cluster_dirtied
, vm_pageout_cluster_cleaned
,
6156 vm_pageout_cluster_collisions
);
6157 iprintf("clusters %5d conversions %5d\n",
6158 vm_pageout_cluster_clusters
, vm_pageout_cluster_conversions
);
6160 iprintf("Target Statistics:\n");
6162 iprintf("collisions %5d page_dirtied %5d page_freed %5d\n",
6163 vm_pageout_target_collisions
, vm_pageout_target_page_dirtied
,
6164 vm_pageout_target_page_freed
);
6166 #endif /* MACH_CLUSTER_STATS */
6170 #endif /* MACH_KDB */