2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * Resident memory system definitions.
66 #ifndef _VM_VM_PAGE_H_
67 #define _VM_VM_PAGE_H_
70 #include <vm/vm_options.h>
71 #include <mach/boolean.h>
72 #include <mach/vm_prot.h>
73 #include <mach/vm_param.h>
79 * in order to make the size of a vm_page_t 64 bytes (cache line size for both arm64 and x86_64)
80 * we'll keep the next_m pointer packed... as long as the kernel virtual space where we allocate
81 * vm_page_t's from doesn't span more then 256 Gbytes, we're safe. There are live tests in the
82 * vm_page_t array allocation and the zone init code to determine if we can safely pack and unpack
83 * pointers from the 2 ends of these spaces
85 typedef uint32_t vm_page_packed_t
;
87 struct vm_page_packed_queue_entry
{
88 vm_page_packed_t next
; /* next element */
89 vm_page_packed_t prev
; /* previous element */
92 typedef struct vm_page_packed_queue_entry
*vm_page_queue_t
;
93 typedef struct vm_page_packed_queue_entry vm_page_queue_head_t
;
94 typedef struct vm_page_packed_queue_entry vm_page_queue_chain_t
;
95 typedef struct vm_page_packed_queue_entry
*vm_page_queue_entry_t
;
97 typedef vm_page_packed_t vm_page_object_t
;
102 * we can't do the packing trick on 32 bit architectures, so
103 * just turn the macros into noops.
105 typedef struct vm_page
*vm_page_packed_t
;
107 #define vm_page_queue_t queue_t
108 #define vm_page_queue_head_t queue_head_t
109 #define vm_page_queue_chain_t queue_chain_t
110 #define vm_page_queue_entry_t queue_entry_t
112 #define vm_page_object_t vm_object_t
116 #include <vm/vm_object.h>
117 #include <kern/queue.h>
118 #include <kern/locks.h>
120 #include <kern/macro_help.h>
121 #include <libkern/OSAtomic.h>
125 #define VM_PAGE_COMPRESSOR_COUNT (compressor_object->resident_page_count)
128 * Management of resident (logical) pages.
130 * A small structure is kept for each resident
131 * page, indexed by page number. Each structure
132 * is an element of several lists:
134 * A hash table bucket used to quickly
135 * perform object/offset lookups
137 * A list of all pages for a given object,
138 * so they can be quickly deactivated at
139 * time of deallocation.
141 * An ordered list of pages due for pageout.
143 * In addition, the structure contains the object
144 * and offset to which this page belongs (for pageout),
145 * and sundry status bits.
147 * Fields in this structure are locked either by the lock on the
148 * object that the page belongs to (O) or by the lock on the page
149 * queues (P). [Some fields require that both locks be held to
150 * change that field; holding either lock is sufficient to read.]
153 #define VM_PAGE_NULL ((vm_page_t) 0)
155 extern char vm_page_inactive_states
[];
156 extern char vm_page_pageable_states
[];
157 extern char vm_page_non_speculative_pageable_states
[];
158 extern char vm_page_active_or_inactive_states
[];
161 #define VM_PAGE_INACTIVE(m) (vm_page_inactive_states[m->vm_page_q_state])
162 #define VM_PAGE_PAGEABLE(m) (vm_page_pageable_states[m->vm_page_q_state])
163 #define VM_PAGE_NON_SPECULATIVE_PAGEABLE(m) (vm_page_non_speculative_pageable_states[m->vm_page_q_state])
164 #define VM_PAGE_ACTIVE_OR_INACTIVE(m) (vm_page_active_or_inactive_states[m->vm_page_q_state])
167 #define VM_PAGE_NOT_ON_Q 0 /* page is not present on any queue, nor is it wired... mainly a transient state */
168 #define VM_PAGE_IS_WIRED 1 /* page is currently wired */
169 #define VM_PAGE_USED_BY_COMPRESSOR 2 /* page is in use by the compressor to hold compressed data */
170 #define VM_PAGE_ON_FREE_Q 3 /* page is on the main free queue */
171 #define VM_PAGE_ON_FREE_LOCAL_Q 4 /* page is on one of the per-CPU free queues */
172 #define VM_PAGE_ON_FREE_LOPAGE_Q 5 /* page is on the lopage pool free list */
173 #define VM_PAGE_ON_THROTTLED_Q 6 /* page is on the throttled queue... we stash anonymous pages here when not paging */
174 #define VM_PAGE_ON_PAGEOUT_Q 7 /* page is on one of the pageout queues (internal/external) awaiting processing */
175 #define VM_PAGE_ON_SPECULATIVE_Q 8 /* page is on one of the speculative queues */
176 #define VM_PAGE_ON_ACTIVE_LOCAL_Q 9 /* page has recently been created and is being held in one of the per-CPU local queues */
177 #define VM_PAGE_ON_ACTIVE_Q 10 /* page is in global active queue */
178 #define VM_PAGE_ON_INACTIVE_INTERNAL_Q 11 /* page is on the inactive internal queue a.k.a. anonymous queue */
179 #define VM_PAGE_ON_INACTIVE_EXTERNAL_Q 12 /* page in on the inactive external queue a.k.a. file backed queue */
180 #define VM_PAGE_ON_INACTIVE_CLEANED_Q 13 /* page has been cleaned to a backing file and is ready to be stolen */
181 #define VM_PAGE_ON_SECLUDED_Q 14 /* page is on secluded queue */
182 #define VM_PAGE_Q_STATE_LAST_VALID_VALUE 14 /* we currently use 4 bits for the state... don't let this go beyond 15 */
184 #define VM_PAGE_Q_STATE_ARRAY_SIZE (VM_PAGE_Q_STATE_LAST_VALID_VALUE+1)
187 #define pageq pageq_un.vm_page_pageq
188 #define snext pageq_un.vm_page_snext
192 vm_page_queue_chain_t vm_page_pageq
; /* queue info for FIFO queue or free list (P) */
193 struct vm_page
*vm_page_snext
;
196 vm_page_queue_chain_t listq
; /* all pages in same object (O) */
198 #if CONFIG_BACKGROUND_QUEUE
199 vm_page_queue_chain_t vm_page_backgroundq
; /* anonymous pages in the background pool (P) */
202 vm_object_offset_t offset
; /* offset into that object (O,P) */
203 vm_page_object_t vm_page_object
; /* which object am I in (O&P) */
206 * The following word of flags is protected
207 * by the "page queues" lock.
209 * we use the 'wire_count' field to store the local
210 * queue id if local queues are enabled...
211 * see the comments at 'vm_page_queues_remove' as to
212 * why this is safe to do
214 #define local_id wire_count
215 unsigned int wire_count
:16, /* how many wired down maps use me? (O&P) */
216 vm_page_q_state
:4, /* which q is the page on (P) */
218 vm_page_in_background
:1,
219 vm_page_on_backgroundq
:1,
221 gobbled
:1, /* page used internally (P) */
222 laundry
:1, /* page is being cleaned now (P)*/
223 no_cache
:1, /* page is not to be cached and should
224 * be reused ahead of other pages (P) */
225 private:1, /* Page should not be returned to
226 * the free list (P) */
227 reference
:1, /* page has been used (P) */
229 __unused_pageq_bits
:5; /* 5 bits available here */
232 * MUST keep the 2 32 bit words used as bit fields
233 * separated since the compiler has a nasty habit
234 * of using 64 bit loads and stores on them as
235 * if they were a single 64 bit field... since
236 * they are protected by 2 different locks, this
239 vm_page_packed_t next_m
; /* VP bucket link (O) */
242 * The following word of flags is protected
243 * by the "VM object" lock.
246 /* boolean_t */ busy
:1, /* page is in transit (O) */
247 wanted
:1, /* someone is waiting for page (O) */
248 tabled
:1, /* page is in VP table (O) */
249 hashed
:1, /* page is in vm_page_buckets[]
250 (O) + the bucket lock */
251 fictitious
:1, /* Physical page doesn't exist (O) */
253 * IMPORTANT: the "pmapped", "xpmapped" and "clustered" bits can be modified while holding the
254 * VM object "shared" lock + the page lock provided through the pmap_lock_phys_page function.
255 * This is done in vm_fault_enter and the CONSUME_CLUSTERED macro.
256 * It's also ok to modify them behind just the VM object "exclusive" lock.
258 clustered
:1, /* page is not the faulted page (O) or (O-shared AND pmap_page) */
259 pmapped
:1, /* page has been entered at some
260 * point into a pmap (O) or (O-shared AND pmap_page) */
261 xpmapped
:1, /* page has been entered with execute permission (O)
262 or (O-shared AND pmap_page) */
264 wpmapped
:1, /* page has been entered at some
265 * point into a pmap for write (O) */
266 free_when_done
:1, /* page is to be freed once cleaning is completed (O) */
267 absent
:1, /* Data has been requested, but is
268 * not yet available (O) */
269 error
:1, /* Data manager was unable to provide
270 * data due to error (O) */
271 dirty
:1, /* Page must be cleaned (O) */
272 cleaning
:1, /* Page clean has begun (O) */
273 precious
:1, /* Page is precious; data must be
274 * returned even if clean (O) */
275 overwriting
:1, /* Request to unlock has been made
276 * without having data. (O)
277 * [See vm_fault_page_overwrite] */
278 restart
:1, /* Page was pushed higher in shadow
279 chain by copy_call-related pagers;
280 start again at top of chain */
281 unusual
:1, /* Page is absent, error, restart or
283 cs_validated
:1, /* code-signing: page was checked */
284 cs_tainted
:1, /* code-signing: page is tainted */
285 cs_nx
:1, /* code-signing: page is nx */
289 written_by_kernel
:1, /* page was written by kernel (i.e. decompressed) */
290 __unused_object_bits
:7; /* 7 bits available here */
292 #if !defined(__arm__) && !defined(__arm64__)
293 ppnum_t phys_page
; /* Physical address of page, passed
294 * to pmap_enter (read-only) */
299 typedef struct vm_page
*vm_page_t
;
300 extern vm_page_t vm_pages
;
301 extern vm_page_t vm_page_array_beginning_addr
;
302 extern vm_page_t vm_page_array_ending_addr
;
305 #if defined(__arm__) || defined(__arm64__)
307 extern unsigned int vm_first_phys_ppnum
;
309 struct vm_page_with_ppnum
{
310 struct vm_page vm_page_wo_ppnum
;
314 typedef struct vm_page_with_ppnum
*vm_page_with_ppnum_t
;
317 static inline ppnum_t
VM_PAGE_GET_PHYS_PAGE(vm_page_t m
)
319 if (m
>= vm_page_array_beginning_addr
&& m
< vm_page_array_ending_addr
)
320 return ((ppnum_t
)((uintptr_t)(m
- vm_page_array_beginning_addr
) + vm_first_phys_ppnum
));
322 return (((vm_page_with_ppnum_t
)m
)->phys_page
);
325 #define VM_PAGE_SET_PHYS_PAGE(m, ppnum) \
327 if ((m) < vm_page_array_beginning_addr || (m) >= vm_page_array_ending_addr) \
328 ((vm_page_with_ppnum_t)(m))->phys_page = ppnum; \
329 assert(ppnum == VM_PAGE_GET_PHYS_PAGE(m)); \
332 #define VM_PAGE_GET_COLOR(m) (VM_PAGE_GET_PHYS_PAGE(m) & vm_color_mask)
334 #else /* defined(__arm__) || defined(__arm64__) */
337 struct vm_page_with_ppnum
{
338 struct vm_page vm_page_with_ppnum
;
340 typedef struct vm_page_with_ppnum
*vm_page_with_ppnum_t
;
343 #define VM_PAGE_GET_PHYS_PAGE(page) (page)->phys_page
344 #define VM_PAGE_SET_PHYS_PAGE(page, ppnum) \
346 (page)->phys_page = ppnum; \
349 #define VM_PAGE_GET_CLUMP(m) ((VM_PAGE_GET_PHYS_PAGE(m)) >> vm_clump_shift)
350 #define VM_PAGE_GET_COLOR(m) ((VM_PAGE_GET_CLUMP(m)) & vm_color_mask)
352 #endif /* defined(__arm__) || defined(__arm64__) */
356 #if defined(__LP64__)
358 #define VM_VPLQ_ALIGNMENT 128
359 #define VM_PACKED_POINTER_ALIGNMENT 64 /* must be a power of 2 */
360 #define VM_PACKED_POINTER_SHIFT 6
362 #define VM_PACKED_FROM_VM_PAGES_ARRAY 0x80000000
364 static inline vm_page_packed_t
vm_page_pack_ptr(uintptr_t p
)
366 vm_page_packed_t packed_ptr
;
369 return ((vm_page_packed_t
)0);
371 if (p
>= (uintptr_t)(vm_page_array_beginning_addr
) && p
< (uintptr_t)(vm_page_array_ending_addr
)) {
372 packed_ptr
= ((vm_page_packed_t
)(((vm_page_t
)p
- vm_page_array_beginning_addr
)));
373 assert(! (packed_ptr
& VM_PACKED_FROM_VM_PAGES_ARRAY
));
374 packed_ptr
|= VM_PACKED_FROM_VM_PAGES_ARRAY
;
378 assert((p
& (VM_PACKED_POINTER_ALIGNMENT
- 1)) == 0);
380 packed_ptr
= ((vm_page_packed_t
)(((uintptr_t)(p
- (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS
)) >> VM_PACKED_POINTER_SHIFT
));
381 assert(packed_ptr
!= 0);
382 assert(! (packed_ptr
& VM_PACKED_FROM_VM_PAGES_ARRAY
));
387 static inline uintptr_t vm_page_unpack_ptr(uintptr_t p
)
390 return ((uintptr_t)0);
392 if (p
& VM_PACKED_FROM_VM_PAGES_ARRAY
)
393 return ((uintptr_t)(&vm_pages
[(uint32_t)(p
& ~VM_PACKED_FROM_VM_PAGES_ARRAY
)]));
394 return (((p
<< VM_PACKED_POINTER_SHIFT
) + (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS
));
398 #define VM_PAGE_PACK_PTR(p) vm_page_pack_ptr((uintptr_t)(p))
399 #define VM_PAGE_UNPACK_PTR(p) vm_page_unpack_ptr((uintptr_t)(p))
401 #define VM_PAGE_OBJECT(p) ((vm_object_t)(VM_PAGE_UNPACK_PTR(p->vm_page_object)))
402 #define VM_PAGE_PACK_OBJECT(o) ((vm_page_object_t)(VM_PAGE_PACK_PTR(o)))
405 #define VM_PAGE_ZERO_PAGEQ_ENTRY(p) \
411 #define VM_PAGE_CONVERT_TO_QUEUE_ENTRY(p) VM_PAGE_PACK_PTR(p)
414 static __inline__
void
415 vm_page_enqueue_tail(
417 vm_page_queue_entry_t elt
)
419 vm_page_queue_entry_t old_tail
;
421 old_tail
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(que
->prev
);
422 elt
->next
= VM_PAGE_PACK_PTR(que
);
423 elt
->prev
= que
->prev
;
424 old_tail
->next
= VM_PAGE_PACK_PTR(elt
);
425 que
->prev
= VM_PAGE_PACK_PTR(elt
);
429 static __inline__
void
431 vm_page_queue_entry_t elt
)
433 vm_page_queue_entry_t next_elt
, prev_elt
;
435 next_elt
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(elt
->next
);
437 /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
438 prev_elt
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(elt
->prev
);
440 next_elt
->prev
= VM_PAGE_PACK_PTR(prev_elt
);
441 prev_elt
->next
= VM_PAGE_PACK_PTR(next_elt
);
449 * Macro: vm_page_queue_init
451 * Initialize the given queue.
453 * void vm_page_queue_init(q)
454 * vm_page_queue_t q; \* MODIFIED *\
456 #define vm_page_queue_init(q) \
458 assert((((uintptr_t)q) & (VM_PACKED_POINTER_ALIGNMENT-1)) == 0); \
459 assert((VM_PAGE_UNPACK_PTR(VM_PAGE_PACK_PTR((uintptr_t)q))) == (uintptr_t)q); \
460 (q)->next = VM_PAGE_PACK_PTR(q); \
461 (q)->prev = VM_PAGE_PACK_PTR(q); \
466 * Macro: vm_page_queue_enter
468 * Insert a new element at the tail of the queue.
470 * void vm_page_queue_enter(q, elt, type, field)
473 * <type> is what's in our queue
474 * <field> is the chain field in (*<type>)
476 * This should only be used with Method 2 queue iteration (element chains)
478 #define vm_page_queue_enter(head, elt, type, field) \
480 vm_page_queue_entry_t __prev; \
482 __prev = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((head)->prev)); \
483 if ((head) == __prev) { \
484 (head)->next = VM_PAGE_PACK_PTR(elt); \
487 ((type)(void *)__prev)->field.next = VM_PAGE_PACK_PTR(elt); \
489 (elt)->field.prev = VM_PAGE_PACK_PTR(__prev); \
490 (elt)->field.next = VM_PAGE_PACK_PTR(head); \
491 (head)->prev = VM_PAGE_PACK_PTR(elt); \
496 * These are helper macros for vm_page_queue_enter_clump to assist
497 * with conditional compilation (release / debug / development)
499 #if DEVELOPMENT || DEBUG
501 #define __DEBUG_CHECK_BUDDIES(__check, __prev, __p, field) \
503 if(__check) { /* if first forward buddy.. */ \
504 if(__prev) { /* ..and if a backward buddy was found, verify link consistency */ \
505 assert(__p == (vm_page_t) VM_PAGE_UNPACK_PTR(__prev->next)); \
506 assert(__prev == (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__p->field.prev)); \
512 #define __DEBUG_VERIFY_LINKS(__i, __first, __n_free, __last_next) \
514 vm_page_queue_entry_t __tmp; \
515 for(__i=0, __tmp=__first; __i<__n_free; __i++) \
516 __tmp=(vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__tmp->next); \
517 assert(__tmp == __last_next); \
520 #define __DEBUG_STAT_INCREMENT_INRANGE vm_clump_inrange++
521 #define __DEBUG_STAT_INCREMENT_INSERTS vm_clump_inserts++
522 #define __DEBUG_STAT_INCREMENT_PROMOTES(__n_free) vm_clump_promotes+=__n_free
526 #define __DEBUG_CHECK_BUDDIES(__check, __prev, __p, field) __check=1
527 #define __DEBUG_VERIFY_LINKS(__i, __first, __n_free, __last_next)
528 #define __DEBUG_STAT_INCREMENT_INRANGE
529 #define __DEBUG_STAT_INCREMENT_INSERTS
530 #define __DEBUG_STAT_INCREMENT_PROMOTES(__n_free)
532 #endif /* if DEVELOPMENT || DEBUG */
535 * Macro: vm_page_queue_enter_clump
537 * Insert a new element into the free queue and clump pages within the same 16K boundary together
540 * void vm_page_queue_enter_clump(q, elt, type, field)
543 * <type> is what's in our queue
544 * <field> is the chain field in (*<type>)
546 * This should only be used with Method 2 queue iteration (element chains)
548 #if defined(__x86_64__)
549 #define vm_page_queue_enter_clump(head, elt, type, field) \
551 ppnum_t __clump_num; \
552 unsigned int __i, __n, __n_free=1, __check=1; \
553 vm_page_queue_entry_t __prev=0, __next, __last, __last_next, __first, __first_prev, __head_next; \
556 /* if elt is part of vm_pages[] */ \
557 if((elt) >= vm_page_array_beginning_addr && (elt) < vm_page_array_boundary) { \
558 __first = __last = (vm_page_queue_entry_t) (elt); \
559 __clump_num = VM_PAGE_GET_CLUMP(elt); \
560 __n = VM_PAGE_GET_PHYS_PAGE(elt) & vm_clump_mask; \
561 /* scan backward looking for a buddy page */ \
562 for(__i=0, __p=(elt)-1; __i<__n && __p>=vm_page_array_beginning_addr; __i++, __p--) { \
563 if(__p->vm_page_q_state == VM_PAGE_ON_FREE_Q && __clump_num == VM_PAGE_GET_CLUMP(__p)) { \
564 if(__prev == 0) __prev = (vm_page_queue_entry_t) __p; \
565 __first = (vm_page_queue_entry_t) __p; \
569 /* scan forward looking for a buddy page */ \
570 for(__i=__n+1, __p=(elt)+1; __i<vm_clump_size && __p<vm_page_array_boundary; __i++, __p++) { \
571 if(__p->vm_page_q_state == VM_PAGE_ON_FREE_Q && __clump_num == VM_PAGE_GET_CLUMP(__p)) { \
572 __DEBUG_CHECK_BUDDIES(__check, __prev, __p, field); \
573 if(__prev == 0) __prev = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__p->field.prev); \
574 __last = (vm_page_queue_entry_t) __p; \
578 __DEBUG_STAT_INCREMENT_INRANGE; \
580 /* if elt is not part of vm_pages or if 1st page in clump, insert at tail */ \
581 if(__prev == 0) __prev = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR((head)->prev); \
583 /* insert the element */ \
584 __next = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__prev->next); \
585 (elt)->field.next = __prev->next; \
586 (elt)->field.prev = __next->prev; \
587 __prev->next = __next->prev = VM_PAGE_PACK_PTR(elt); \
588 __DEBUG_STAT_INCREMENT_INSERTS; \
590 /* check if clump needs to be promoted to head */ \
591 if(__n_free >= vm_clump_promote_threshold && __n_free > 1) { \
592 __first_prev = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__first->prev); \
593 if(__first_prev != (head)) { /* if not at head already */ \
594 __last_next = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__last->next); \
595 /* verify that the links within the clump are consistent */ \
596 __DEBUG_VERIFY_LINKS(__i, __first, __n_free, __last_next); \
597 /* promote clump to head */ \
598 __first_prev->next = __last->next; \
599 __last_next->prev = __first->prev; \
600 __first->prev = VM_PAGE_PACK_PTR(head); \
601 __last->next = (head)->next; \
602 __head_next = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR((head)->next); \
603 __head_next->prev = VM_PAGE_PACK_PTR(__last); \
604 (head)->next = VM_PAGE_PACK_PTR(__first); \
605 __DEBUG_STAT_INCREMENT_PROMOTES(__n_free); \
612 * Macro: vm_page_queue_enter_first
614 * Insert a new element at the head of the queue.
616 * void queue_enter_first(q, elt, type, field)
619 * <type> is what's in our queue
620 * <field> is the chain field in (*<type>)
622 * This should only be used with Method 2 queue iteration (element chains)
624 #define vm_page_queue_enter_first(head, elt, type, field) \
626 vm_page_queue_entry_t __next; \
628 __next = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((head)->next)); \
629 if ((head) == __next) { \
630 (head)->prev = VM_PAGE_PACK_PTR(elt); \
633 ((type)(void *)__next)->field.prev = VM_PAGE_PACK_PTR(elt); \
635 (elt)->field.next = VM_PAGE_PACK_PTR(__next); \
636 (elt)->field.prev = VM_PAGE_PACK_PTR(head); \
637 (head)->next = VM_PAGE_PACK_PTR(elt); \
642 * Macro: vm_page_queue_remove
644 * Remove an arbitrary item from the queue.
646 * void vm_page_queue_remove(q, qe, type, field)
647 * arguments as in vm_page_queue_enter
649 * This should only be used with Method 2 queue iteration (element chains)
651 #define vm_page_queue_remove(head, elt, type, field) \
653 vm_page_queue_entry_t __next, __prev; \
655 __next = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((elt)->field.next)); \
656 __prev = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((elt)->field.prev)); \
658 if ((head) == __next) \
659 (head)->prev = VM_PAGE_PACK_PTR(__prev); \
661 ((type)(void *)__next)->field.prev = VM_PAGE_PACK_PTR(__prev); \
663 if ((head) == __prev) \
664 (head)->next = VM_PAGE_PACK_PTR(__next); \
666 ((type)(void *)__prev)->field.next = VM_PAGE_PACK_PTR(__next); \
668 (elt)->field.next = 0; \
669 (elt)->field.prev = 0; \
674 * Macro: vm_page_queue_remove_first
676 * Remove and return the entry at the head of
679 * vm_page_queue_remove_first(head, entry, type, field)
680 * entry is returned by reference
682 * This should only be used with Method 2 queue iteration (element chains)
684 #define vm_page_queue_remove_first(head, entry, type, field) \
686 vm_page_queue_entry_t __next; \
688 (entry) = (type)(void *) VM_PAGE_UNPACK_PTR(((head)->next)); \
689 __next = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((entry)->field.next)); \
691 if ((head) == __next) \
692 (head)->prev = VM_PAGE_PACK_PTR(head); \
694 ((type)(void *)(__next))->field.prev = VM_PAGE_PACK_PTR(head); \
695 (head)->next = VM_PAGE_PACK_PTR(__next); \
697 (entry)->field.next = 0; \
698 (entry)->field.prev = 0; \
703 * Macro: vm_page_queue_remove_first_with_clump
705 * Remove and return the entry at the head of the free queue
706 * end is set to 1 to indicate that we just returned the last page in a clump
709 * vm_page_queue_remove_first_with_clump(head, entry, type, field, end)
710 * entry is returned by reference
711 * end is returned by reference
713 * This should only be used with Method 2 queue iteration (element chains)
715 #if defined(__x86_64__)
716 #define vm_page_queue_remove_first_with_clump(head, entry, type, field, end) \
718 vm_page_queue_entry_t __next; \
720 (entry) = (type)(void *) VM_PAGE_UNPACK_PTR(((head)->next)); \
721 __next = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((entry)->field.next)); \
724 if ((head) == __next) { \
725 (head)->prev = VM_PAGE_PACK_PTR(head); \
729 ((type)(void *)(__next))->field.prev = VM_PAGE_PACK_PTR(head); \
730 if(VM_PAGE_GET_CLUMP(entry) != VM_PAGE_GET_CLUMP(((type)(void *)(__next)))) (end)=1; \
732 (head)->next = VM_PAGE_PACK_PTR(__next); \
734 (entry)->field.next = 0; \
735 (entry)->field.prev = 0; \
741 * Macro: vm_page_queue_end
743 * Tests whether a new entry is really the end of
746 * boolean_t vm_page_queue_end(q, qe)
748 * vm_page_queue_entry_t qe;
750 #define vm_page_queue_end(q, qe) ((q) == (qe))
754 * Macro: vm_page_queue_empty
756 * Tests whether a queue is empty.
758 * boolean_t vm_page_queue_empty(q)
761 #define vm_page_queue_empty(q) vm_page_queue_end((q), ((vm_page_queue_entry_t)vm_page_queue_first(q)))
766 * Macro: vm_page_queue_first
768 * Returns the first entry in the queue,
770 * uintpr_t vm_page_queue_first(q)
771 * vm_page_queue_t q; \* IN *\
773 #define vm_page_queue_first(q) (VM_PAGE_UNPACK_PTR((q)->next))
778 * Macro: vm_page_queue_last
780 * Returns the last entry in the queue.
782 * vm_page_queue_entry_t queue_last(q)
783 * queue_t q; \* IN *\
785 #define vm_page_queue_last(q) (VM_PAGE_UNPACK_PTR((q)->prev))
790 * Macro: vm_page_queue_next
792 * Returns the entry after an item in the queue.
794 * uintpr_t vm_page_queue_next(qc)
795 * vm_page_queue_t qc;
797 #define vm_page_queue_next(qc) (VM_PAGE_UNPACK_PTR((qc)->next))
802 * Macro: vm_page_queue_prev
804 * Returns the entry before an item in the queue.
806 * uinptr_t vm_page_queue_prev(qc)
807 * vm_page_queue_t qc;
809 #define vm_page_queue_prev(qc) (VM_PAGE_UNPACK_PTR((qc)->prev))
814 * Macro: vm_page_queue_iterate
816 * iterate over each item in the queue.
817 * Generates a 'for' loop, setting elt to
818 * each item in turn (by reference).
820 * vm_page_queue_iterate(q, elt, type, field)
823 * <type> is what's in our queue
824 * <field> is the chain field in (*<type>)
826 * This should only be used with Method 2 queue iteration (element chains)
828 #define vm_page_queue_iterate(head, elt, type, field) \
829 for ((elt) = (type)(void *) vm_page_queue_first(head); \
830 !vm_page_queue_end((head), (vm_page_queue_entry_t)(elt)); \
831 (elt) = (type)(void *) vm_page_queue_next(&(elt)->field))
835 #define VM_VPLQ_ALIGNMENT 128
836 #define VM_PACKED_POINTER_ALIGNMENT 4
837 #define VM_PACKED_POINTER_SHIFT 0
839 #define VM_PACKED_FROM_VM_PAGES_ARRAY 0
841 #define VM_PAGE_PACK_PTR(p) (p)
842 #define VM_PAGE_UNPACK_PTR(p) ((uintptr_t)(p))
844 #define VM_PAGE_OBJECT(p) (vm_object_t)(p->vm_page_object)
845 #define VM_PAGE_PACK_OBJECT(o) ((vm_page_object_t)(VM_PAGE_PACK_PTR(o)))
848 #define VM_PAGE_ZERO_PAGEQ_ENTRY(p) \
850 (p)->pageq.next = 0; \
851 (p)->pageq.prev = 0; \
854 #define VM_PAGE_CONVERT_TO_QUEUE_ENTRY(p) ((queue_entry_t)(p))
856 #define vm_page_remque remque
857 #define vm_page_enqueue_tail enqueue_tail
858 #define vm_page_queue_init queue_init
859 #define vm_page_queue_enter queue_enter
860 #define vm_page_queue_enter_first queue_enter_first
861 #define vm_page_queue_remove queue_remove
862 #define vm_page_queue_remove_first queue_remove_first
863 #define vm_page_queue_end queue_end
864 #define vm_page_queue_empty queue_empty
865 #define vm_page_queue_first queue_first
866 #define vm_page_queue_last queue_last
867 #define vm_page_queue_next queue_next
868 #define vm_page_queue_prev queue_prev
869 #define vm_page_queue_iterate queue_iterate
876 * VM_PAGE_MIN_SPECULATIVE_AGE_Q through VM_PAGE_MAX_SPECULATIVE_AGE_Q
877 * represents a set of aging bins that are 'protected'...
879 * VM_PAGE_SPECULATIVE_AGED_Q is a list of the speculative pages that have
880 * not yet been 'claimed' but have been aged out of the protective bins
881 * this occurs in vm_page_speculate when it advances to the next bin
882 * and discovers that it is still occupied... at that point, all of the
883 * pages in that bin are moved to the VM_PAGE_SPECULATIVE_AGED_Q. the pages
884 * in that bin are all guaranteed to have reached at least the maximum age
885 * we allow for a protected page... they can be older if there is no
886 * memory pressure to pull them from the bin, or there are no new speculative pages
887 * being generated to push them out.
888 * this list is the one that vm_pageout_scan will prefer when looking
889 * for pages to move to the underweight free list
891 * VM_PAGE_MAX_SPECULATIVE_AGE_Q * VM_PAGE_SPECULATIVE_Q_AGE_MS
892 * defines the amount of time a speculative page is normally
893 * allowed to live in the 'protected' state (i.e. not available
894 * to be stolen if vm_pageout_scan is running and looking for
895 * pages)... however, if the total number of speculative pages
896 * in the protected state exceeds our limit (defined in vm_pageout.c)
897 * and there are none available in VM_PAGE_SPECULATIVE_AGED_Q, then
898 * vm_pageout_scan is allowed to steal pages from the protected
899 * bucket even if they are underage.
901 * vm_pageout_scan is also allowed to pull pages from a protected
902 * bin if the bin has reached the "age of consent" we've set
904 #define VM_PAGE_MAX_SPECULATIVE_AGE_Q 10
905 #define VM_PAGE_MIN_SPECULATIVE_AGE_Q 1
906 #define VM_PAGE_SPECULATIVE_AGED_Q 0
908 #define VM_PAGE_SPECULATIVE_Q_AGE_MS 500
910 struct vm_speculative_age_q
{
912 * memory queue for speculative pages via clustered pageins
914 vm_page_queue_head_t age_q
;
915 mach_timespec_t age_ts
;
916 } __attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
921 struct vm_speculative_age_q vm_page_queue_speculative
[];
923 extern int speculative_steal_index
;
924 extern int speculative_age_index
;
925 extern unsigned int vm_page_speculative_q_age_ms
;
928 typedef struct vm_locks_array
{
929 char pad
__attribute__ ((aligned (64)));
930 lck_mtx_t vm_page_queue_lock2
__attribute__ ((aligned (64)));
931 lck_mtx_t vm_page_queue_free_lock2
__attribute__ ((aligned (64)));
932 char pad2
__attribute__ ((aligned (64)));
936 #if CONFIG_BACKGROUND_QUEUE
937 extern void vm_page_assign_background_state(vm_page_t mem
);
938 extern void vm_page_update_background_state(vm_page_t mem
);
939 extern void vm_page_add_to_backgroundq(vm_page_t mem
, boolean_t first
);
940 extern void vm_page_remove_from_backgroundq(vm_page_t mem
);
943 #define VM_PAGE_WIRED(m) ((m)->vm_page_q_state == VM_PAGE_IS_WIRED)
944 #define NEXT_PAGE(m) ((m)->snext)
945 #define NEXT_PAGE_PTR(m) (&(m)->snext)
948 * XXX The unusual bit should not be necessary. Most of the bit
949 * XXX fields above really want to be masks.
953 * For debugging, this macro can be defined to perform
954 * some useful check on a page structure.
955 * INTENTIONALLY left as a no-op so that the
956 * current call-sites can be left intact for future uses.
959 #define VM_PAGE_CHECK(mem) \
965 * The free page list is actually n lists, one per color,
966 * where the number of colors is a function of the machine's
967 * cache geometry set at system initialization. To disable
968 * coloring, set vm_colors to 1 and vm_color_mask to 0.
969 * The boot-arg "colors" may be used to override vm_colors.
970 * Note that there is little harm in having more colors than needed.
973 #define MAX_COLORS 128
974 #define DEFAULT_COLORS 32
977 unsigned int vm_colors
; /* must be in range 1..MAX_COLORS */
979 unsigned int vm_color_mask
; /* must be (vm_colors-1) */
981 unsigned int vm_cache_geometry_colors
; /* optimal #colors based on cache geometry */
984 * Wired memory is a very limited resource and we can't let users exhaust it
985 * and deadlock the entire system. We enforce the following limits:
987 * vm_user_wire_limit (default: all memory minus vm_global_no_user_wire_amount)
988 * how much memory can be user-wired in one user task
990 * vm_global_user_wire_limit (default: same as vm_user_wire_limit)
991 * how much memory can be user-wired in all user tasks
993 * vm_global_no_user_wire_amount (default: VM_NOT_USER_WIREABLE)
994 * how much memory must remain user-unwired at any time
996 #define VM_NOT_USER_WIREABLE (64*1024*1024) /* 64MB */
998 vm_map_size_t vm_user_wire_limit
;
1000 vm_map_size_t vm_global_user_wire_limit
;
1002 vm_map_size_t vm_global_no_user_wire_amount
;
1005 * Each pageable resident page falls into one of three lists:
1008 * Available for allocation now. The free list is
1009 * actually an array of lists, one per color.
1011 * Not referenced in any map, but still has an
1012 * object/offset-page mapping, and may be dirty.
1013 * This is the list of pages that should be
1014 * paged out next. There are actually two
1015 * inactive lists, one for pages brought in from
1016 * disk or other backing store, and another
1017 * for "zero-filled" pages. See vm_pageout_scan()
1018 * for the distinction and usage.
1020 * A list of pages which have been placed in
1021 * at least one physical map. This list is
1022 * ordered, in LRU-like fashion.
1026 #define VPL_LOCK_SPIN 1
1029 vm_page_queue_head_t vpl_queue
;
1030 unsigned int vpl_count
;
1031 unsigned int vpl_internal_count
;
1032 unsigned int vpl_external_count
;
1033 #ifdef VPL_LOCK_SPIN
1034 lck_spin_t vpl_lock
;
1037 lck_mtx_ext_t vpl_lock_ext
;
1043 char cache_line_pad
[VM_VPLQ_ALIGNMENT
];
1048 unsigned int vm_page_local_q_count
;
1050 struct vplq
*vm_page_local_q
;
1052 unsigned int vm_page_local_q_soft_limit
;
1054 unsigned int vm_page_local_q_hard_limit
;
1056 vm_locks_array_t vm_page_locks
;
1059 vm_page_queue_head_t vm_lopage_queue_free
; /* low memory free queue */
1061 vm_page_queue_head_t vm_page_queue_active
; /* active memory queue */
1063 vm_page_queue_head_t vm_page_queue_inactive
; /* inactive memory queue for normal pages */
1064 #if CONFIG_SECLUDED_MEMORY
1066 vm_page_queue_head_t vm_page_queue_secluded
; /* reclaimable pages secluded for Camera */
1067 #endif /* CONFIG_SECLUDED_MEMORY */
1069 vm_page_queue_head_t vm_page_queue_cleaned
; /* clean-queue inactive memory */
1071 vm_page_queue_head_t vm_page_queue_anonymous
; /* inactive memory queue for anonymous pages */
1073 vm_page_queue_head_t vm_page_queue_throttled
; /* memory queue for throttled pageout pages */
1076 queue_head_t vm_objects_wired
;
1078 lck_spin_t vm_objects_wired_lock
;
1080 #if CONFIG_BACKGROUND_QUEUE
1082 #define VM_PAGE_BACKGROUND_TARGET_MAX 50000
1084 #define VM_PAGE_BG_DISABLED 0
1085 #define VM_PAGE_BG_LEVEL_1 1
1088 vm_page_queue_head_t vm_page_queue_background
;
1090 uint64_t vm_page_background_promoted_count
;
1092 uint32_t vm_page_background_count
;
1094 uint32_t vm_page_background_target
;
1096 uint32_t vm_page_background_internal_count
;
1098 uint32_t vm_page_background_external_count
;
1100 uint32_t vm_page_background_mode
;
1102 uint32_t vm_page_background_exclude_external
;
1107 vm_offset_t first_phys_addr
; /* physical address for first_page */
1109 vm_offset_t last_phys_addr
; /* physical address for last_page */
1112 unsigned int vm_page_free_count
; /* How many pages are free? (sum of all colors) */
1114 unsigned int vm_page_active_count
; /* How many pages are active? */
1116 unsigned int vm_page_inactive_count
; /* How many pages are inactive? */
1117 #if CONFIG_SECLUDED_MEMORY
1119 unsigned int vm_page_secluded_count
; /* How many pages are secluded? */
1121 unsigned int vm_page_secluded_count_free
;
1123 unsigned int vm_page_secluded_count_inuse
;
1124 #endif /* CONFIG_SECLUDED_MEMORY */
1126 unsigned int vm_page_cleaned_count
; /* How many pages are in the clean queue? */
1128 unsigned int vm_page_throttled_count
;/* How many inactives are throttled */
1130 unsigned int vm_page_speculative_count
; /* How many speculative pages are unclaimed? */
1131 extern unsigned int vm_page_pageable_internal_count
;
1132 extern unsigned int vm_page_pageable_external_count
;
1134 unsigned int vm_page_xpmapped_external_count
; /* How many pages are mapped executable? */
1136 unsigned int vm_page_external_count
; /* How many pages are file-backed? */
1138 unsigned int vm_page_internal_count
; /* How many pages are anonymous? */
1140 unsigned int vm_page_wire_count
; /* How many pages are wired? */
1142 unsigned int vm_page_wire_count_initial
; /* How many pages wired at startup */
1144 unsigned int vm_page_free_target
; /* How many do we want free? */
1146 unsigned int vm_page_free_min
; /* When to wakeup pageout */
1148 unsigned int vm_page_throttle_limit
; /* When to throttle new page creation */
1150 uint32_t vm_page_creation_throttle
; /* When to throttle new page creation */
1152 unsigned int vm_page_inactive_target
;/* How many do we want inactive? */
1153 #if CONFIG_SECLUDED_MEMORY
1155 unsigned int vm_page_secluded_target
;/* How many do we want secluded? */
1156 #endif /* CONFIG_SECLUDED_MEMORY */
1158 unsigned int vm_page_anonymous_min
; /* When it's ok to pre-clean */
1160 unsigned int vm_page_inactive_min
; /* When to wakeup pageout */
1162 unsigned int vm_page_free_reserved
; /* How many pages reserved to do pageout */
1164 unsigned int vm_page_throttle_count
; /* Count of page allocations throttled */
1166 unsigned int vm_page_gobble_count
;
1168 unsigned int vm_page_stolen_count
; /* Count of stolen pages not acccounted in zones */
1171 #if DEVELOPMENT || DEBUG
1173 unsigned int vm_page_speculative_used
;
1177 unsigned int vm_page_purgeable_count
;/* How many pages are purgeable now ? */
1179 unsigned int vm_page_purgeable_wired_count
;/* How many purgeable pages are wired now ? */
1181 uint64_t vm_page_purged_count
; /* How many pages got purged so far ? */
1183 extern unsigned int vm_page_free_wanted
;
1184 /* how many threads are waiting for memory */
1186 extern unsigned int vm_page_free_wanted_privileged
;
1187 /* how many VM privileged threads are waiting for memory */
1188 #if CONFIG_SECLUDED_MEMORY
1189 extern unsigned int vm_page_free_wanted_secluded
;
1190 /* how many threads are waiting for secluded memory */
1191 #endif /* CONFIG_SECLUDED_MEMORY */
1193 extern const ppnum_t vm_page_fictitious_addr
;
1194 /* (fake) phys_addr of fictitious pages */
1196 extern const ppnum_t vm_page_guard_addr
;
1197 /* (fake) phys_addr of guard pages */
1200 extern boolean_t vm_page_deactivate_hint
;
1202 extern int vm_compressor_mode
;
1205 0 = all pages avail ( default. )
1206 1 = disable high mem ( cap max pages to 4G)
1209 extern int vm_himemory_mode
;
1211 extern boolean_t vm_lopage_needed
;
1212 extern uint32_t vm_lopage_free_count
;
1213 extern uint32_t vm_lopage_free_limit
;
1214 extern uint32_t vm_lopage_lowater
;
1215 extern boolean_t vm_lopage_refill
;
1216 extern uint64_t max_valid_dma_address
;
1217 extern ppnum_t max_valid_low_ppnum
;
1220 * Prototypes for functions exported by this module.
1222 extern void vm_page_bootstrap(
1223 vm_offset_t
*startp
,
1226 extern void vm_page_module_init(void);
1228 extern void vm_page_init_local_q(void);
1230 extern void vm_page_create(
1234 extern vm_page_t
kdp_vm_page_lookup(
1236 vm_object_offset_t offset
);
1238 extern vm_page_t
vm_page_lookup(
1240 vm_object_offset_t offset
);
1242 extern vm_page_t
vm_page_grab_fictitious(void);
1244 extern vm_page_t
vm_page_grab_guard(void);
1246 extern void vm_page_release_fictitious(
1249 extern void vm_page_more_fictitious(void);
1251 extern int vm_pool_low(void);
1253 extern vm_page_t
vm_page_grab(void);
1254 extern vm_page_t
vm_page_grab_options(int flags
);
1255 #if CONFIG_SECLUDED_MEMORY
1256 #define VM_PAGE_GRAB_SECLUDED 0x00000001
1257 #endif /* CONFIG_SECLUDED_MEMORY */
1259 extern vm_page_t
vm_page_grablo(void);
1261 extern void vm_page_release(
1263 boolean_t page_queues_locked
);
1265 extern boolean_t
vm_page_wait(
1266 int interruptible
);
1268 extern vm_page_t
vm_page_alloc(
1270 vm_object_offset_t offset
);
1272 extern vm_page_t
vm_page_alloc_guard(
1274 vm_object_offset_t offset
);
1276 extern void vm_page_init(
1281 extern void vm_page_free(
1284 extern void vm_page_free_unlocked(
1286 boolean_t remove_from_hash
);
1288 extern void vm_page_activate(
1291 extern void vm_page_deactivate(
1294 extern void vm_page_deactivate_internal(
1296 boolean_t clear_hw_reference
);
1298 extern void vm_page_enqueue_cleaned(vm_page_t page
);
1300 extern void vm_page_lru(
1303 extern void vm_page_speculate(
1307 extern void vm_page_speculate_ageit(
1308 struct vm_speculative_age_q
*aq
);
1310 extern void vm_page_reactivate_all_throttled(void);
1312 extern void vm_page_reactivate_local(uint32_t lid
, boolean_t force
, boolean_t nolocks
);
1314 extern void vm_page_rename(
1316 vm_object_t new_object
,
1317 vm_object_offset_t new_offset
);
1319 extern void vm_page_insert(
1322 vm_object_offset_t offset
);
1324 extern void vm_page_insert_wired(
1327 vm_object_offset_t offset
,
1330 extern void vm_page_insert_internal(
1333 vm_object_offset_t offset
,
1335 boolean_t queues_lock_held
,
1336 boolean_t insert_in_hash
,
1337 boolean_t batch_pmap_op
,
1338 boolean_t delayed_accounting
,
1339 uint64_t *delayed_ledger_update
);
1341 extern void vm_page_replace(
1344 vm_object_offset_t offset
);
1346 extern void vm_page_remove(
1348 boolean_t remove_from_hash
);
1350 extern void vm_page_zero_fill(
1353 extern void vm_page_part_zero_fill(
1358 extern void vm_page_copy(
1360 vm_page_t dest_page
);
1362 extern void vm_page_part_copy(
1369 extern void vm_page_wire(
1372 boolean_t check_memorystatus
);
1374 extern void vm_page_unwire(
1378 extern void vm_set_page_size(void);
1380 extern void vm_page_gobble(
1383 extern void vm_page_validate_cs(vm_page_t page
);
1384 extern void vm_page_validate_cs_mapped(
1387 extern void vm_page_validate_cs_mapped_chunk(
1390 vm_offset_t chunk_offset
,
1391 vm_size_t chunk_size
,
1392 boolean_t
*validated
,
1395 extern void vm_page_free_prepare_queues(
1398 extern void vm_page_free_prepare_object(
1400 boolean_t remove_from_hash
);
1403 extern wait_result_t
vm_page_sleep(
1409 extern void vm_pressure_response(void);
1412 extern void memorystatus_pages_update(unsigned int pages_avail
);
1414 #define VM_CHECK_MEMORYSTATUS do { \
1415 memorystatus_pages_update( \
1416 vm_page_pageable_external_count + \
1417 vm_page_free_count + \
1418 (VM_DYNAMIC_PAGING_ENABLED() ? 0 : vm_page_purgeable_count) \
1422 #else /* CONFIG_JETSAM */
1426 #define VM_CHECK_MEMORYSTATUS do {} while(0)
1428 #else /* CONFIG_EMBEDDED */
1430 #define VM_CHECK_MEMORYSTATUS vm_pressure_response()
1432 #endif /* CONFIG_EMBEDDED */
1434 #endif /* CONFIG_JETSAM */
1437 * Functions implemented as macros. m->wanted and m->busy are
1438 * protected by the object lock.
1442 #define SET_PAGE_DIRTY(m, set_pmap_modified) \
1444 vm_page_t __page__ = (m); \
1445 if (__page__->dirty == FALSE && (set_pmap_modified)) { \
1446 pmap_set_modify(VM_PAGE_GET_PHYS_PAGE(__page__)); \
1448 __page__->dirty = TRUE; \
1450 #else /* CONFIG_EMBEDDED */
1451 #define SET_PAGE_DIRTY(m, set_pmap_modified) \
1453 vm_page_t __page__ = (m); \
1454 __page__->dirty = TRUE; \
1456 #endif /* CONFIG_EMBEDDED */
1458 #define PAGE_ASSERT_WAIT(m, interruptible) \
1459 (((m)->wanted = TRUE), \
1460 assert_wait((event_t) (m), (interruptible)))
1463 #define PAGE_SLEEP(o, m, interruptible) \
1464 vm_page_sleep(o, m, interruptible)
1466 #define PAGE_SLEEP(o, m, interruptible) \
1467 (((m)->wanted = TRUE), \
1468 thread_sleep_vm_object((o), (m), (interruptible)))
1471 #define PAGE_WAKEUP_DONE(m) \
1473 (m)->busy = FALSE; \
1474 if ((m)->wanted) { \
1475 (m)->wanted = FALSE; \
1476 thread_wakeup((event_t) (m)); \
1480 #define PAGE_WAKEUP(m) \
1482 if ((m)->wanted) { \
1483 (m)->wanted = FALSE; \
1484 thread_wakeup((event_t) (m)); \
1488 #define VM_PAGE_FREE(p) \
1490 vm_page_free_unlocked(p, TRUE); \
1493 #define VM_PAGE_GRAB_FICTITIOUS(M) \
1495 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
1496 vm_page_more_fictitious(); \
1499 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
1501 #define vm_page_queue_lock (vm_page_locks.vm_page_queue_lock2)
1502 #define vm_page_queue_free_lock (vm_page_locks.vm_page_queue_free_lock2)
1504 #define vm_page_lock_queues() lck_mtx_lock(&vm_page_queue_lock)
1505 #define vm_page_trylock_queues() lck_mtx_try_lock(&vm_page_queue_lock)
1506 #define vm_page_unlock_queues() lck_mtx_unlock(&vm_page_queue_lock)
1508 #define vm_page_lockspin_queues() lck_mtx_lock_spin(&vm_page_queue_lock)
1509 #define vm_page_trylockspin_queues() lck_mtx_try_lock_spin(&vm_page_queue_lock)
1510 #define vm_page_lockconvert_queues() lck_mtx_convert_spin(&vm_page_queue_lock)
1512 #ifdef VPL_LOCK_SPIN
1513 #define VPL_LOCK_INIT(vlq, vpl_grp, vpl_attr) lck_spin_init(&vlq->vpl_lock, vpl_grp, vpl_attr)
1514 #define VPL_LOCK(vpl) lck_spin_lock(vpl)
1515 #define VPL_UNLOCK(vpl) lck_spin_unlock(vpl)
1517 #define VPL_LOCK_INIT(vlq, vpl_grp, vpl_attr) lck_mtx_init_ext(&vlq->vpl_lock, &vlq->vpl_lock_ext, vpl_grp, vpl_attr)
1518 #define VPL_LOCK(vpl) lck_mtx_lock_spin(vpl)
1519 #define VPL_UNLOCK(vpl) lck_mtx_unlock(vpl)
1523 #if DEVELOPMENT || DEBUG
1524 #define VM_PAGE_SPECULATIVE_USED_ADD() \
1526 OSAddAtomic(1, &vm_page_speculative_used); \
1529 #define VM_PAGE_SPECULATIVE_USED_ADD()
1533 #define VM_PAGE_CONSUME_CLUSTERED(mem) \
1535 ppnum_t __phys_page; \
1536 __phys_page = VM_PAGE_GET_PHYS_PAGE(mem); \
1537 pmap_lock_phys_page(__phys_page); \
1538 if (mem->clustered) { \
1540 o = VM_PAGE_OBJECT(mem); \
1543 mem->clustered = FALSE; \
1544 VM_PAGE_SPECULATIVE_USED_ADD(); \
1546 pmap_unlock_phys_page(__phys_page); \
1550 #define VM_PAGE_COUNT_AS_PAGEIN(mem) \
1554 o = VM_PAGE_OBJECT(mem); \
1555 DTRACE_VM2(pgin, int, 1, (uint64_t *), NULL); \
1556 current_task()->pageins++; \
1557 if (o->internal) { \
1558 DTRACE_VM2(anonpgin, int, 1, (uint64_t *), NULL); \
1560 DTRACE_VM2(fspgin, int, 1, (uint64_t *), NULL); \
1565 /* adjust for stolen pages accounted elsewhere */
1566 #define VM_PAGE_MOVE_STOLEN(page_count) \
1568 vm_page_stolen_count -= (page_count); \
1569 vm_page_wire_count_initial -= (page_count); \
1572 #define DW_vm_page_unwire 0x01
1573 #define DW_vm_page_wire 0x02
1574 #define DW_vm_page_free 0x04
1575 #define DW_vm_page_activate 0x08
1576 #define DW_vm_page_deactivate_internal 0x10
1577 #define DW_vm_page_speculate 0x20
1578 #define DW_vm_page_lru 0x40
1579 #define DW_vm_pageout_throttle_up 0x80
1580 #define DW_PAGE_WAKEUP 0x100
1581 #define DW_clear_busy 0x200
1582 #define DW_clear_reference 0x400
1583 #define DW_set_reference 0x800
1584 #define DW_move_page 0x1000
1585 #define DW_VM_PAGE_QUEUES_REMOVE 0x2000
1586 #define DW_enqueue_cleaned 0x4000
1587 #define DW_vm_phantom_cache_update 0x8000
1589 struct vm_page_delayed_work
{
1594 void vm_page_do_delayed_work(vm_object_t object
, vm_tag_t tag
, struct vm_page_delayed_work
*dwp
, int dw_count
);
1596 extern unsigned int vm_max_delayed_work_limit
;
1598 #define DEFAULT_DELAYED_WORK_LIMIT 32
1600 #define DELAYED_WORK_LIMIT(max) ((vm_max_delayed_work_limit >= max ? max : vm_max_delayed_work_limit))
1603 * vm_page_do_delayed_work may need to drop the object lock...
1604 * if it does, we need the pages it's looking at to
1605 * be held stable via the busy bit, so if busy isn't already
1606 * set, we need to set it and ask vm_page_do_delayed_work
1607 * to clear it and wakeup anyone that might have blocked on
1608 * it once we're done processing the page.
1611 #define VM_PAGE_ADD_DELAYED_WORK(dwp, mem, dw_cnt) \
1613 if (mem->busy == FALSE) { \
1615 if ( !(dwp->dw_mask & DW_vm_page_free)) \
1616 dwp->dw_mask |= (DW_clear_busy | DW_PAGE_WAKEUP); \
1623 extern vm_page_t
vm_object_page_grab(vm_object_t
);
1625 #if VM_PAGE_BUCKETS_CHECK
1626 extern void vm_page_buckets_check(void);
1627 #endif /* VM_PAGE_BUCKETS_CHECK */
1629 extern void vm_page_queues_remove(vm_page_t mem
, boolean_t remove_from_backgroundq
);
1630 extern void vm_page_remove_internal(vm_page_t page
);
1631 extern void vm_page_enqueue_inactive(vm_page_t mem
, boolean_t first
);
1632 extern void vm_page_enqueue_active(vm_page_t mem
, boolean_t first
);
1633 extern void vm_page_check_pageable_safe(vm_page_t page
);
1636 #endif /* _VM_VM_PAGE_H_ */