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->vmp_q_state])
162 #define VM_PAGE_PAGEABLE(m) (vm_page_pageable_states[m->vmp_q_state])
163 #define VM_PAGE_NON_SPECULATIVE_PAGEABLE(m) (vm_page_non_speculative_pageable_states[m->vmp_q_state])
164 #define VM_PAGE_ACTIVE_OR_INACTIVE(m) (vm_page_active_or_inactive_states[m->vmp_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)
188 * The structure itself. See the block comment above for what (O) and (P) mean.
190 #define vmp_pageq vmp_q_un.vmp_q_pageq
191 #define vmp_snext vmp_q_un.vmp_q_snext
195 vm_page_queue_chain_t vmp_q_pageq
; /* queue info for FIFO queue or free list (P) */
196 struct vm_page
*vmp_q_snext
;
199 vm_page_queue_chain_t vmp_listq
; /* all pages in same object (O) */
201 #if CONFIG_BACKGROUND_QUEUE
202 vm_page_queue_chain_t vmp_backgroundq
; /* anonymous pages in the background pool (P) */
205 vm_object_offset_t vmp_offset
; /* offset into that object (O,P) */
206 vm_page_object_t vmp_object
; /* which object am I in (O&P) */
209 * The following word of flags is always protected by the "page queues" lock.
211 * We use 'vmp_wire_count' to store the local queue id if local queues are enabled.
212 * See the comments at 'vm_page_queues_remove' as to why this is safe to do.
214 #define vmp_local_id vmp_wire_count
215 unsigned int vmp_wire_count
:16, /* how many wired down maps use me? (O&P) */
216 vmp_q_state
:4, /* which q is the page on (P) */
218 vmp_on_backgroundq
:1,
219 vmp_gobbled
:1, /* page used internally (P) */
220 vmp_laundry
:1, /* page is being cleaned now (P)*/
221 vmp_no_cache
:1, /* page is not to be cached and should */
222 /* be reused ahead of other pages (P) */
223 vmp_private
:1, /* Page should not be returned to the free list (P) */
224 vmp_reference
:1, /* page has been used (P) */
225 vmp_unused_page_bits
:5;
228 * MUST keep the 2 32 bit words used as bit fields
229 * separated since the compiler has a nasty habit
230 * of using 64 bit loads and stores on them as
231 * if they were a single 64 bit field... since
232 * they are protected by 2 different locks, this
235 vm_page_packed_t vmp_next_m
; /* VP bucket link (O) */
238 * The following word of flags is protected by the "VM object" lock.
240 * IMPORTANT: the "vmp_pmapped", "vmp_xpmapped" and "vmp_clustered" bits can be modified while holding the
241 * VM object "shared" lock + the page lock provided through the pmap_lock_phys_page function.
242 * This is done in vm_fault_enter() and the CONSUME_CLUSTERED macro.
243 * It's also ok to modify them behind just the VM object "exclusive" lock.
245 unsigned int vmp_busy
:1, /* page is in transit (O) */
246 vmp_wanted
:1, /* someone is waiting for page (O) */
247 vmp_tabled
:1, /* page is in VP table (O) */
248 vmp_hashed
:1, /* page is in vm_page_buckets[] (O) + the bucket lock */
249 vmp_fictitious
:1, /* Physical page doesn't exist (O) */
250 vmp_clustered
:1, /* page is not the faulted page (O) or (O-shared AND pmap_page) */
251 vmp_pmapped
:1, /* page has at some time been entered into a pmap (O) or */
252 /* (O-shared AND pmap_page) */
253 vmp_xpmapped
:1, /* page has been entered with execute permission (O) or */
254 /* (O-shared AND pmap_page) */
255 vmp_wpmapped
:1, /* page has been entered at some point into a pmap for write (O) */
256 vmp_free_when_done
:1, /* page is to be freed once cleaning is completed (O) */
257 vmp_absent
:1, /* Data has been requested, but is not yet available (O) */
258 vmp_error
:1, /* Data manager was unable to provide data due to error (O) */
259 vmp_dirty
:1, /* Page must be cleaned (O) */
260 vmp_cleaning
:1, /* Page clean has begun (O) */
261 vmp_precious
:1, /* Page is precious; data must be returned even if clean (O) */
262 vmp_overwriting
:1, /* Request to unlock has been made without having data. (O) */
263 /* [See vm_fault_page_overwrite] */
264 vmp_restart
:1, /* Page was pushed higher in shadow chain by copy_call-related pagers */
265 /* start again at top of chain */
266 vmp_unusual
:1, /* Page is absent, error, restart or page locked */
267 vmp_cs_validated
:1, /* code-signing: page was checked */
268 vmp_cs_tainted
:1, /* code-signing: page is tainted */
269 vmp_cs_nx
:1, /* code-signing: page is nx */
272 vmp_written_by_kernel
:1, /* page was written by kernel (i.e. decompressed) */
273 vmp_unused_object_bits
:8;
275 #if !defined(__arm__) && !defined(__arm64__)
276 ppnum_t vmp_phys_page
; /* Physical page number of the page */
281 typedef struct vm_page
*vm_page_t
;
282 extern vm_page_t vm_pages
;
283 extern vm_page_t vm_page_array_beginning_addr
;
284 extern vm_page_t vm_page_array_ending_addr
;
287 #if defined(__arm__) || defined(__arm64__)
289 extern unsigned int vm_first_phys_ppnum
;
291 struct vm_page_with_ppnum
{
292 struct vm_page vm_page_wo_ppnum
;
294 ppnum_t vmp_phys_page
;
296 typedef struct vm_page_with_ppnum
*vm_page_with_ppnum_t
;
299 static inline ppnum_t
300 VM_PAGE_GET_PHYS_PAGE(vm_page_t m
)
302 if (m
>= vm_page_array_beginning_addr
&& m
< vm_page_array_ending_addr
) {
303 return (ppnum_t
)((uintptr_t)(m
- vm_page_array_beginning_addr
) + vm_first_phys_ppnum
);
305 return ((vm_page_with_ppnum_t
)m
)->vmp_phys_page
;
309 #define VM_PAGE_SET_PHYS_PAGE(m, ppnum) \
311 if ((m) < vm_page_array_beginning_addr || (m) >= vm_page_array_ending_addr) \
312 ((vm_page_with_ppnum_t)(m))->vmp_phys_page = ppnum; \
313 assert(ppnum == VM_PAGE_GET_PHYS_PAGE(m)); \
316 #define VM_PAGE_GET_COLOR(m) (VM_PAGE_GET_PHYS_PAGE(m) & vm_color_mask)
318 #else /* defined(__arm__) || defined(__arm64__) */
321 struct vm_page_with_ppnum
{
322 struct vm_page vm_page_with_ppnum
;
324 typedef struct vm_page_with_ppnum
*vm_page_with_ppnum_t
;
327 #define VM_PAGE_GET_PHYS_PAGE(page) (page)->vmp_phys_page
328 #define VM_PAGE_SET_PHYS_PAGE(page, ppnum) \
330 (page)->vmp_phys_page = ppnum; \
333 #define VM_PAGE_GET_CLUMP(m) ((VM_PAGE_GET_PHYS_PAGE(m)) >> vm_clump_shift)
334 #define VM_PAGE_GET_COLOR(m) ((VM_PAGE_GET_CLUMP(m)) & vm_color_mask)
336 #endif /* defined(__arm__) || defined(__arm64__) */
340 #if defined(__LP64__)
342 #define VM_VPLQ_ALIGNMENT 128
343 #define VM_PACKED_POINTER_ALIGNMENT 64 /* must be a power of 2 */
344 #define VM_PACKED_POINTER_SHIFT 6
346 #define VM_PACKED_FROM_VM_PAGES_ARRAY 0x80000000
348 static inline vm_page_packed_t
349 vm_page_pack_ptr(uintptr_t p
)
351 vm_page_packed_t packed_ptr
;
354 return (vm_page_packed_t
)0;
357 if (p
>= (uintptr_t)(vm_page_array_beginning_addr
) && p
< (uintptr_t)(vm_page_array_ending_addr
)) {
358 packed_ptr
= ((vm_page_packed_t
)(((vm_page_t
)p
- vm_page_array_beginning_addr
)));
359 assert(!(packed_ptr
& VM_PACKED_FROM_VM_PAGES_ARRAY
));
360 packed_ptr
|= VM_PACKED_FROM_VM_PAGES_ARRAY
;
364 assert((p
& (VM_PACKED_POINTER_ALIGNMENT
- 1)) == 0);
366 packed_ptr
= ((vm_page_packed_t
)(((uintptr_t)(p
- (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS
)) >> VM_PACKED_POINTER_SHIFT
));
367 assert(packed_ptr
!= 0);
368 assert(!(packed_ptr
& VM_PACKED_FROM_VM_PAGES_ARRAY
));
373 static inline uintptr_t
374 vm_page_unpack_ptr(uintptr_t p
)
376 extern unsigned int vm_pages_count
;
382 if (p
& VM_PACKED_FROM_VM_PAGES_ARRAY
) {
383 assert((uint32_t)(p
& ~VM_PACKED_FROM_VM_PAGES_ARRAY
) < vm_pages_count
);
384 return (uintptr_t)(&vm_pages
[(uint32_t)(p
& ~VM_PACKED_FROM_VM_PAGES_ARRAY
)]);
386 return (p
<< VM_PACKED_POINTER_SHIFT
) + (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS
;
390 #define VM_PAGE_PACK_PTR(p) vm_page_pack_ptr((uintptr_t)(p))
391 #define VM_PAGE_UNPACK_PTR(p) vm_page_unpack_ptr((uintptr_t)(p))
393 #define VM_PAGE_OBJECT(p) ((vm_object_t)(VM_PAGE_UNPACK_PTR(p->vmp_object)))
394 #define VM_PAGE_PACK_OBJECT(o) ((vm_page_object_t)(VM_PAGE_PACK_PTR(o)))
397 #define VM_PAGE_ZERO_PAGEQ_ENTRY(p) \
399 (p)->vmp_snext = 0; \
403 #define VM_PAGE_CONVERT_TO_QUEUE_ENTRY(p) VM_PAGE_PACK_PTR(p)
406 static __inline__
void
407 vm_page_enqueue_tail(
409 vm_page_queue_entry_t elt
)
411 vm_page_queue_entry_t old_tail
;
413 old_tail
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(que
->prev
);
414 elt
->next
= VM_PAGE_PACK_PTR(que
);
415 elt
->prev
= que
->prev
;
416 que
->prev
= old_tail
->next
= VM_PAGE_PACK_PTR(elt
);
420 static __inline__
void
422 vm_page_queue_entry_t elt
)
424 vm_page_queue_entry_t next
;
425 vm_page_queue_entry_t prev
;
426 vm_page_packed_t next_pck
= elt
->next
;
427 vm_page_packed_t prev_pck
= elt
->prev
;
429 next
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(next_pck
);
431 /* next may equal prev (and the queue head) if elt was the only element */
432 prev
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(prev_pck
);
434 next
->prev
= prev_pck
;
435 prev
->next
= next_pck
;
443 * Macro: vm_page_queue_init
445 * Initialize the given queue.
447 * void vm_page_queue_init(q)
448 * vm_page_queue_t q; \* MODIFIED *\
450 #define vm_page_queue_init(q) \
452 assert((((uintptr_t)q) & (VM_PACKED_POINTER_ALIGNMENT-1)) == 0); \
453 assert((VM_PAGE_UNPACK_PTR(VM_PAGE_PACK_PTR((uintptr_t)q))) == (uintptr_t)q); \
454 (q)->next = VM_PAGE_PACK_PTR(q); \
455 (q)->prev = VM_PAGE_PACK_PTR(q); \
460 * Macro: vm_page_queue_enter
462 * Insert a new element at the tail of the vm_page queue.
464 * void vm_page_queue_enter(q, elt, field)
467 * <field> is the list field in vm_page_t
469 * This macro's arguments have to match the generic "queue_enter()" macro which is
470 * what is used for this on 32 bit kernels.
472 #define vm_page_queue_enter(head, elt, field) \
474 vm_page_packed_t __pck_elt = VM_PAGE_PACK_PTR(elt); \
475 vm_page_packed_t __pck_head = VM_PAGE_PACK_PTR(head); \
476 vm_page_packed_t __pck_prev = (head)->prev; \
478 if (__pck_head == __pck_prev) { \
479 (head)->next = __pck_elt; \
482 __prev = (vm_page_t)VM_PAGE_UNPACK_PTR(__pck_prev); \
483 __prev->field.next = __pck_elt; \
485 (elt)->field.prev = __pck_prev; \
486 (elt)->field.next = __pck_head; \
487 (head)->prev = __pck_elt; \
491 #if defined(__x86_64__)
493 * These are helper macros for vm_page_queue_enter_clump to assist
494 * with conditional compilation (release / debug / development)
496 #if DEVELOPMENT || DEBUG
498 #define __DEBUG_CHECK_BUDDIES(__prev, __p, field) \
500 if (__prev != NULL) { \
501 assert(__p == (vm_page_t)VM_PAGE_UNPACK_PTR(__prev->next)); \
502 assert(__prev == (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(__p->field.prev)); \
506 #define __DEBUG_VERIFY_LINKS(__first, __n_free, __last_next) \
509 vm_page_queue_entry_t __tmp; \
510 for (__i = 0, __tmp = __first; __i < __n_free; __i++) { \
511 __tmp = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(__tmp->next); \
513 assert(__tmp == __last_next); \
516 #define __DEBUG_STAT_INCREMENT_INRANGE vm_clump_inrange++
517 #define __DEBUG_STAT_INCREMENT_INSERTS vm_clump_inserts++
518 #define __DEBUG_STAT_INCREMENT_PROMOTES(__n_free) vm_clump_promotes+=__n_free
522 #define __DEBUG_CHECK_BUDDIES(__prev, __p, field)
523 #define __DEBUG_VERIFY_LINKS(__first, __n_free, __last_next)
524 #define __DEBUG_STAT_INCREMENT_INRANGE
525 #define __DEBUG_STAT_INCREMENT_INSERTS
526 #define __DEBUG_STAT_INCREMENT_PROMOTES(__n_free)
528 #endif /* if DEVELOPMENT || DEBUG */
531 * Insert a new page into a free queue and clump pages within the same 16K boundary together
534 vm_page_queue_enter_clump(
535 vm_page_queue_t head
,
538 vm_page_queue_entry_t first
; /* first page in the clump */
539 vm_page_queue_entry_t last
; /* last page in the clump */
540 vm_page_queue_entry_t prev
= NULL
;
541 vm_page_queue_entry_t next
;
543 extern unsigned int vm_pages_count
;
544 extern unsigned int vm_clump_size
, vm_clump_mask
, vm_clump_shift
, vm_clump_promote_threshold
;
545 extern unsigned long vm_clump_allocs
, vm_clump_inserts
, vm_clump_inrange
, vm_clump_promotes
;
548 * If elt is part of the vm_pages[] array, find its neighboring buddies in the array.
550 if (vm_page_array_beginning_addr
<= elt
&& elt
< &vm_pages
[vm_pages_count
]) {
556 first
= last
= (vm_page_queue_entry_t
)elt
;
557 clump_num
= VM_PAGE_GET_CLUMP(elt
);
558 n
= VM_PAGE_GET_PHYS_PAGE(elt
) & vm_clump_mask
;
561 * Check for preceeding vm_pages[] entries in the same chunk
563 for (i
= 0, p
= elt
- 1; i
< n
&& vm_page_array_beginning_addr
<= p
; i
++, p
--) {
564 if (p
->vmp_q_state
== VM_PAGE_ON_FREE_Q
&& clump_num
== VM_PAGE_GET_CLUMP(p
)) {
566 prev
= (vm_page_queue_entry_t
)p
;
568 first
= (vm_page_queue_entry_t
)p
;
574 * Check the following vm_pages[] entries in the same chunk
576 for (i
= n
+ 1, p
= elt
+ 1; i
< vm_clump_size
&& p
< &vm_pages
[vm_pages_count
]; i
++, p
++) {
577 if (p
->vmp_q_state
== VM_PAGE_ON_FREE_Q
&& clump_num
== VM_PAGE_GET_CLUMP(p
)) {
578 if (last
== (vm_page_queue_entry_t
)elt
) { /* first one only */
579 __DEBUG_CHECK_BUDDIES(prev
, p
, vmp_pageq
);
583 prev
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(p
->vmp_pageq
.prev
);
585 last
= (vm_page_queue_entry_t
)p
;
589 __DEBUG_STAT_INCREMENT_INRANGE
;
592 /* if elt is not part of vm_pages or if 1st page in clump, insert at tail */
594 prev
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(head
->prev
);
597 /* insert the element */
598 next
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(prev
->next
);
599 elt
->vmp_pageq
.next
= prev
->next
;
600 elt
->vmp_pageq
.prev
= next
->prev
;
601 prev
->next
= next
->prev
= VM_PAGE_PACK_PTR(elt
);
602 __DEBUG_STAT_INCREMENT_INSERTS
;
605 * Check if clump needs to be promoted to head.
607 if (n_free
>= vm_clump_promote_threshold
&& n_free
> 1) {
608 vm_page_queue_entry_t first_prev
;
610 first_prev
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(first
->prev
);
612 /* If not at head already */
613 if (first_prev
!= head
) {
614 vm_page_queue_entry_t last_next
;
615 vm_page_queue_entry_t head_next
;
617 last_next
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(last
->next
);
619 /* verify that the links within the clump are consistent */
620 __DEBUG_VERIFY_LINKS(first
, n_free
, last_next
);
622 /* promote clump to head */
623 first_prev
->next
= last
->next
;
624 last_next
->prev
= first
->prev
;
625 first
->prev
= VM_PAGE_PACK_PTR(head
);
626 last
->next
= head
->next
;
628 head_next
= (vm_page_queue_entry_t
)VM_PAGE_UNPACK_PTR(head
->next
);
629 head_next
->prev
= VM_PAGE_PACK_PTR(last
);
630 head
->next
= VM_PAGE_PACK_PTR(first
);
631 __DEBUG_STAT_INCREMENT_PROMOTES(n_free
);
638 * Macro: vm_page_queue_enter_first
640 * Insert a new element at the head of the vm_page queue.
642 * void queue_enter_first(q, elt, , field)
645 * <field> is the linkage field in vm_page
647 * This macro's arguments have to match the generic "queue_enter_first()" macro which is
648 * what is used for this on 32 bit kernels.
650 #define vm_page_queue_enter_first(head, elt, field) \
652 vm_page_packed_t __pck_next = (head)->next; \
653 vm_page_packed_t __pck_head = VM_PAGE_PACK_PTR(head); \
654 vm_page_packed_t __pck_elt = VM_PAGE_PACK_PTR(elt); \
656 if (__pck_head == __pck_next) { \
657 (head)->prev = __pck_elt; \
660 __next = (vm_page_t)VM_PAGE_UNPACK_PTR(__pck_next); \
661 __next->field.prev = __pck_elt; \
664 (elt)->field.next = __pck_next; \
665 (elt)->field.prev = __pck_head; \
666 (head)->next = __pck_elt; \
671 * Macro: vm_page_queue_remove
673 * Remove an arbitrary page from a vm_page queue.
675 * void vm_page_queue_remove(q, qe, field)
676 * arguments as in vm_page_queue_enter
678 * This macro's arguments have to match the generic "queue_enter()" macro which is
679 * what is used for this on 32 bit kernels.
681 #define vm_page_queue_remove(head, elt, field) \
683 vm_page_packed_t __pck_next = (elt)->field.next; \
684 vm_page_packed_t __pck_prev = (elt)->field.prev; \
685 vm_page_t __next = (vm_page_t)VM_PAGE_UNPACK_PTR(__pck_next); \
686 vm_page_t __prev = (vm_page_t)VM_PAGE_UNPACK_PTR(__pck_prev); \
688 if ((void *)(head) == (void *)__next) { \
689 (head)->prev = __pck_prev; \
691 __next->field.prev = __pck_prev; \
694 if ((void *)(head) == (void *)__prev) { \
695 (head)->next = __pck_next; \
697 __prev->field.next = __pck_next; \
700 (elt)->field.next = 0; \
701 (elt)->field.prev = 0; \
706 * Macro: vm_page_queue_remove_first
709 * Remove and return the entry at the head of a vm_page queue.
712 * vm_page_queue_remove_first(head, entry, field)
713 * N.B. entry is returned by reference
715 * This macro's arguments have to match the generic "queue_remove_first()" macro which is
716 * what is used for this on 32 bit kernels.
718 #define vm_page_queue_remove_first(head, entry, field) \
720 vm_page_packed_t __pck_head = VM_PAGE_PACK_PTR(head); \
721 vm_page_packed_t __pck_next; \
724 (entry) = (vm_page_t)VM_PAGE_UNPACK_PTR((head)->next); \
725 __pck_next = (entry)->field.next; \
726 __next = (vm_page_t)VM_PAGE_UNPACK_PTR(__pck_next); \
728 if (__pck_head == __pck_next) { \
729 (head)->prev = __pck_head; \
731 __next->field.prev = __pck_head; \
734 (head)->next = __pck_next; \
735 (entry)->field.next = 0; \
736 (entry)->field.prev = 0; \
740 #if defined(__x86_64__)
742 * Macro: vm_page_queue_remove_first_with_clump
744 * Remove and return the entry at the head of the free queue
745 * end is set to 1 to indicate that we just returned the last page in a clump
748 * vm_page_queue_remove_first_with_clump(head, entry, end)
749 * entry is returned by reference
750 * end is returned by reference
752 #define vm_page_queue_remove_first_with_clump(head, entry, end) \
754 vm_page_packed_t __pck_head = VM_PAGE_PACK_PTR(head); \
755 vm_page_packed_t __pck_next; \
758 (entry) = (vm_page_t)VM_PAGE_UNPACK_PTR((head)->next); \
759 __pck_next = (entry)->vmp_pageq.next; \
760 __next = (vm_page_t)VM_PAGE_UNPACK_PTR(__pck_next); \
763 if (__pck_head == __pck_next) { \
764 (head)->prev = __pck_head; \
767 __next->vmp_pageq.prev = __pck_head; \
768 if (VM_PAGE_GET_CLUMP(entry) != VM_PAGE_GET_CLUMP(__next)) { \
773 (head)->next = __pck_next; \
774 (entry)->vmp_pageq.next = 0; \
775 (entry)->vmp_pageq.prev = 0; \
780 * Macro: vm_page_queue_end
782 * Tests whether a new entry is really the end of
785 * boolean_t vm_page_queue_end(q, qe)
787 * vm_page_queue_entry_t qe;
789 #define vm_page_queue_end(q, qe) ((q) == (qe))
793 * Macro: vm_page_queue_empty
795 * Tests whether a queue is empty.
797 * boolean_t vm_page_queue_empty(q)
800 #define vm_page_queue_empty(q) vm_page_queue_end((q), ((vm_page_queue_entry_t)vm_page_queue_first(q)))
805 * Macro: vm_page_queue_first
807 * Returns the first entry in the queue,
809 * uintpr_t vm_page_queue_first(q)
810 * vm_page_queue_t q; \* IN *\
812 #define vm_page_queue_first(q) (VM_PAGE_UNPACK_PTR((q)->next))
817 * Macro: vm_page_queue_last
819 * Returns the last entry in the queue.
821 * vm_page_queue_entry_t queue_last(q)
822 * queue_t q; \* IN *\
824 #define vm_page_queue_last(q) (VM_PAGE_UNPACK_PTR((q)->prev))
829 * Macro: vm_page_queue_next
831 * Returns the entry after an item in the queue.
833 * uintpr_t vm_page_queue_next(qc)
834 * vm_page_queue_t qc;
836 #define vm_page_queue_next(qc) (VM_PAGE_UNPACK_PTR((qc)->next))
841 * Macro: vm_page_queue_prev
843 * Returns the entry before an item in the queue.
845 * uinptr_t vm_page_queue_prev(qc)
846 * vm_page_queue_t qc;
848 #define vm_page_queue_prev(qc) (VM_PAGE_UNPACK_PTR((qc)->prev))
853 * Macro: vm_page_queue_iterate
855 * iterate over each item in a vm_page queue.
856 * Generates a 'for' loop, setting elt to
857 * each item in turn (by reference).
859 * vm_page_queue_iterate(q, elt, field)
862 * <field> is the chain field in vm_page_t
864 #define vm_page_queue_iterate(head, elt, field) \
865 for ((elt) = (vm_page_t)vm_page_queue_first(head); \
866 !vm_page_queue_end((head), (vm_page_queue_entry_t)(elt)); \
867 (elt) = (vm_page_t)vm_page_queue_next(&(elt)->field)) \
871 #define VM_VPLQ_ALIGNMENT 128
872 #define VM_PACKED_POINTER_ALIGNMENT 4
873 #define VM_PACKED_POINTER_SHIFT 0
875 #define VM_PACKED_FROM_VM_PAGES_ARRAY 0
877 #define VM_PAGE_PACK_PTR(p) (p)
878 #define VM_PAGE_UNPACK_PTR(p) ((uintptr_t)(p))
880 #define VM_PAGE_OBJECT(p) (vm_object_t)(p->vmp_object)
881 #define VM_PAGE_PACK_OBJECT(o) ((vm_page_object_t)(VM_PAGE_PACK_PTR(o)))
884 #define VM_PAGE_ZERO_PAGEQ_ENTRY(p) \
886 (p)->vmp_pageq.next = 0; \
887 (p)->vmp_pageq.prev = 0; \
890 #define VM_PAGE_CONVERT_TO_QUEUE_ENTRY(p) ((queue_entry_t)(p))
892 #define vm_page_remque remque
893 #define vm_page_enqueue_tail enqueue_tail
894 #define vm_page_queue_init queue_init
895 #define vm_page_queue_enter(h, e, f) queue_enter(h, e, vm_page_t, f)
896 #define vm_page_queue_enter_first(h, e, f) queue_enter_first(h, e, vm_page_t, f)
897 #define vm_page_queue_remove(h, e, f) queue_remove(h, e, vm_page_t, f)
898 #define vm_page_queue_remove_first(h, e, f) queue_remove_first(h, e, vm_page_t, f)
899 #define vm_page_queue_end queue_end
900 #define vm_page_queue_empty queue_empty
901 #define vm_page_queue_first queue_first
902 #define vm_page_queue_last queue_last
903 #define vm_page_queue_next queue_next
904 #define vm_page_queue_prev queue_prev
905 #define vm_page_queue_iterate(h, e, f) queue_iterate(h, e, vm_page_t, f)
912 * VM_PAGE_MIN_SPECULATIVE_AGE_Q through VM_PAGE_MAX_SPECULATIVE_AGE_Q
913 * represents a set of aging bins that are 'protected'...
915 * VM_PAGE_SPECULATIVE_AGED_Q is a list of the speculative pages that have
916 * not yet been 'claimed' but have been aged out of the protective bins
917 * this occurs in vm_page_speculate when it advances to the next bin
918 * and discovers that it is still occupied... at that point, all of the
919 * pages in that bin are moved to the VM_PAGE_SPECULATIVE_AGED_Q. the pages
920 * in that bin are all guaranteed to have reached at least the maximum age
921 * we allow for a protected page... they can be older if there is no
922 * memory pressure to pull them from the bin, or there are no new speculative pages
923 * being generated to push them out.
924 * this list is the one that vm_pageout_scan will prefer when looking
925 * for pages to move to the underweight free list
927 * VM_PAGE_MAX_SPECULATIVE_AGE_Q * VM_PAGE_SPECULATIVE_Q_AGE_MS
928 * defines the amount of time a speculative page is normally
929 * allowed to live in the 'protected' state (i.e. not available
930 * to be stolen if vm_pageout_scan is running and looking for
931 * pages)... however, if the total number of speculative pages
932 * in the protected state exceeds our limit (defined in vm_pageout.c)
933 * and there are none available in VM_PAGE_SPECULATIVE_AGED_Q, then
934 * vm_pageout_scan is allowed to steal pages from the protected
935 * bucket even if they are underage.
937 * vm_pageout_scan is also allowed to pull pages from a protected
938 * bin if the bin has reached the "age of consent" we've set
940 #define VM_PAGE_MAX_SPECULATIVE_AGE_Q 10
941 #define VM_PAGE_MIN_SPECULATIVE_AGE_Q 1
942 #define VM_PAGE_SPECULATIVE_AGED_Q 0
944 #define VM_PAGE_SPECULATIVE_Q_AGE_MS 500
946 struct vm_speculative_age_q
{
948 * memory queue for speculative pages via clustered pageins
950 vm_page_queue_head_t age_q
;
951 mach_timespec_t age_ts
;
952 } __attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
957 struct vm_speculative_age_q vm_page_queue_speculative
[];
959 extern int speculative_steal_index
;
960 extern int speculative_age_index
;
961 extern unsigned int vm_page_speculative_q_age_ms
;
964 typedef struct vm_locks_array
{
965 char pad
__attribute__ ((aligned(64)));
966 lck_mtx_t vm_page_queue_lock2
__attribute__ ((aligned(64)));
967 lck_mtx_t vm_page_queue_free_lock2
__attribute__ ((aligned(64)));
968 char pad2
__attribute__ ((aligned(64)));
972 #if CONFIG_BACKGROUND_QUEUE
973 extern void vm_page_assign_background_state(vm_page_t mem
);
974 extern void vm_page_update_background_state(vm_page_t mem
);
975 extern void vm_page_add_to_backgroundq(vm_page_t mem
, boolean_t first
);
976 extern void vm_page_remove_from_backgroundq(vm_page_t mem
);
979 #define VM_PAGE_WIRED(m) ((m)->vmp_q_state == VM_PAGE_IS_WIRED)
980 #define NEXT_PAGE(m) ((m)->vmp_snext)
981 #define NEXT_PAGE_PTR(m) (&(m)->vmp_snext)
984 * XXX The unusual bit should not be necessary. Most of the bit
985 * XXX fields above really want to be masks.
989 * For debugging, this macro can be defined to perform
990 * some useful check on a page structure.
991 * INTENTIONALLY left as a no-op so that the
992 * current call-sites can be left intact for future uses.
995 #define VM_PAGE_CHECK(mem) \
1001 * The free page list is actually n lists, one per color,
1002 * where the number of colors is a function of the machine's
1003 * cache geometry set at system initialization. To disable
1004 * coloring, set vm_colors to 1 and vm_color_mask to 0.
1005 * The boot-arg "colors" may be used to override vm_colors.
1006 * Note that there is little harm in having more colors than needed.
1009 #define MAX_COLORS 128
1010 #define DEFAULT_COLORS 32
1013 unsigned int vm_colors
; /* must be in range 1..MAX_COLORS */
1015 unsigned int vm_color_mask
; /* must be (vm_colors-1) */
1017 unsigned int vm_cache_geometry_colors
; /* optimal #colors based on cache geometry */
1020 * Wired memory is a very limited resource and we can't let users exhaust it
1021 * and deadlock the entire system. We enforce the following limits:
1023 * vm_per_task_user_wire_limit
1024 * how much memory can be user-wired in one user task
1026 * vm_global_user_wire_limit (default: same as vm_per_task_user_wire_limit)
1027 * how much memory can be user-wired in all user tasks
1029 * These values are set to defaults based on the number of pages managed
1030 * by the VM system. They can be overriden via sysctls.
1031 * See kmem_set_user_wire_limits for details on the default values.
1033 * Regardless of the amount of memory in the system, we never reserve
1034 * more than VM_NOT_USER_WIREABLE_MAX bytes as unlockable.
1036 #if defined(__LP64__)
1037 #define VM_NOT_USER_WIREABLE_MAX (32ULL*1024*1024*1024) /* 32GB */
1039 #define VM_NOT_USER_WIREABLE_MAX (1UL*1024*1024*1024) /* 1GB */
1040 #endif /* __LP64__ */
1042 vm_map_size_t vm_per_task_user_wire_limit
;
1044 vm_map_size_t vm_global_user_wire_limit
;
1047 * Each pageable resident page falls into one of three lists:
1050 * Available for allocation now. The free list is
1051 * actually an array of lists, one per color.
1053 * Not referenced in any map, but still has an
1054 * object/offset-page mapping, and may be dirty.
1055 * This is the list of pages that should be
1056 * paged out next. There are actually two
1057 * inactive lists, one for pages brought in from
1058 * disk or other backing store, and another
1059 * for "zero-filled" pages. See vm_pageout_scan()
1060 * for the distinction and usage.
1062 * A list of pages which have been placed in
1063 * at least one physical map. This list is
1064 * ordered, in LRU-like fashion.
1068 #define VPL_LOCK_SPIN 1
1071 vm_page_queue_head_t vpl_queue
;
1072 unsigned int vpl_count
;
1073 unsigned int vpl_internal_count
;
1074 unsigned int vpl_external_count
;
1075 #ifdef VPL_LOCK_SPIN
1076 lck_spin_t vpl_lock
;
1079 lck_mtx_ext_t vpl_lock_ext
;
1085 char cache_line_pad
[VM_VPLQ_ALIGNMENT
];
1090 unsigned int vm_page_local_q_count
;
1092 struct vplq
*vm_page_local_q
;
1094 unsigned int vm_page_local_q_soft_limit
;
1096 unsigned int vm_page_local_q_hard_limit
;
1098 vm_locks_array_t vm_page_locks
;
1101 vm_page_queue_head_t vm_lopage_queue_free
; /* low memory free queue */
1103 vm_page_queue_head_t vm_page_queue_active
; /* active memory queue */
1105 vm_page_queue_head_t vm_page_queue_inactive
; /* inactive memory queue for normal pages */
1106 #if CONFIG_SECLUDED_MEMORY
1108 vm_page_queue_head_t vm_page_queue_secluded
; /* reclaimable pages secluded for Camera */
1109 #endif /* CONFIG_SECLUDED_MEMORY */
1111 vm_page_queue_head_t vm_page_queue_cleaned
; /* clean-queue inactive memory */
1113 vm_page_queue_head_t vm_page_queue_anonymous
; /* inactive memory queue for anonymous pages */
1115 vm_page_queue_head_t vm_page_queue_throttled
; /* memory queue for throttled pageout pages */
1118 queue_head_t vm_objects_wired
;
1120 lck_spin_t vm_objects_wired_lock
;
1122 #if CONFIG_BACKGROUND_QUEUE
1124 #define VM_PAGE_BACKGROUND_TARGET_MAX 50000
1126 #define VM_PAGE_BG_DISABLED 0
1127 #define VM_PAGE_BG_LEVEL_1 1
1130 vm_page_queue_head_t vm_page_queue_background
;
1132 uint64_t vm_page_background_promoted_count
;
1134 uint32_t vm_page_background_count
;
1136 uint32_t vm_page_background_target
;
1138 uint32_t vm_page_background_internal_count
;
1140 uint32_t vm_page_background_external_count
;
1142 uint32_t vm_page_background_mode
;
1144 uint32_t vm_page_background_exclude_external
;
1149 vm_offset_t first_phys_addr
; /* physical address for first_page */
1151 vm_offset_t last_phys_addr
; /* physical address for last_page */
1154 unsigned int vm_page_free_count
; /* How many pages are free? (sum of all colors) */
1156 unsigned int vm_page_active_count
; /* How many pages are active? */
1158 unsigned int vm_page_inactive_count
; /* How many pages are inactive? */
1159 #if CONFIG_SECLUDED_MEMORY
1161 unsigned int vm_page_secluded_count
; /* How many pages are secluded? */
1163 unsigned int vm_page_secluded_count_free
; /* how many of them are free? */
1165 unsigned int vm_page_secluded_count_inuse
; /* how many of them are in use? */
1167 * We keep filling the secluded pool with new eligible pages and
1168 * we can overshoot our target by a lot.
1169 * When there's memory pressure, vm_pageout_scan() will re-balance the queues,
1170 * pushing the extra secluded pages to the active or free queue.
1171 * Since these "over target" secluded pages are actually "available", jetsam
1172 * should consider them as such, so make them visible to jetsam via the
1173 * "vm_page_secluded_count_over_target" counter and update it whenever we
1174 * update vm_page_secluded_count or vm_page_secluded_target.
1177 unsigned int vm_page_secluded_count_over_target
;
1178 #define VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE() \
1180 if (vm_page_secluded_count > vm_page_secluded_target) { \
1181 vm_page_secluded_count_over_target = \
1182 (vm_page_secluded_count - vm_page_secluded_target); \
1184 vm_page_secluded_count_over_target = 0; \
1187 #define VM_PAGE_SECLUDED_COUNT_OVER_TARGET() vm_page_secluded_count_over_target
1188 #else /* CONFIG_SECLUDED_MEMORY */
1189 #define VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE() \
1192 #define VM_PAGE_SECLUDED_COUNT_OVER_TARGET() 0
1193 #endif /* CONFIG_SECLUDED_MEMORY */
1195 unsigned int vm_page_cleaned_count
; /* How many pages are in the clean queue? */
1197 unsigned int vm_page_throttled_count
;/* How many inactives are throttled */
1199 unsigned int vm_page_speculative_count
; /* How many speculative pages are unclaimed? */
1200 extern unsigned int vm_page_pageable_internal_count
;
1201 extern unsigned int vm_page_pageable_external_count
;
1203 unsigned int vm_page_xpmapped_external_count
; /* How many pages are mapped executable? */
1205 unsigned int vm_page_external_count
; /* How many pages are file-backed? */
1207 unsigned int vm_page_internal_count
; /* How many pages are anonymous? */
1209 unsigned int vm_page_wire_count
; /* How many pages are wired? */
1211 unsigned int vm_page_wire_count_initial
; /* How many pages wired at startup */
1213 unsigned int vm_page_wire_count_on_boot
; /* even earlier than _initial */
1215 unsigned int vm_page_free_target
; /* How many do we want free? */
1217 unsigned int vm_page_free_min
; /* When to wakeup pageout */
1219 unsigned int vm_page_throttle_limit
; /* When to throttle new page creation */
1221 unsigned int vm_page_inactive_target
;/* How many do we want inactive? */
1222 #if CONFIG_SECLUDED_MEMORY
1224 unsigned int vm_page_secluded_target
;/* How many do we want secluded? */
1225 #endif /* CONFIG_SECLUDED_MEMORY */
1227 unsigned int vm_page_anonymous_min
; /* When it's ok to pre-clean */
1229 unsigned int vm_page_free_reserved
; /* How many pages reserved to do pageout */
1231 unsigned int vm_page_gobble_count
;
1233 unsigned int vm_page_stolen_count
; /* Count of stolen pages not acccounted in zones */
1235 unsigned int vm_page_kern_lpage_count
; /* Count of large pages used in early boot */
1238 #if DEVELOPMENT || DEBUG
1240 unsigned int vm_page_speculative_used
;
1244 unsigned int vm_page_purgeable_count
;/* How many pages are purgeable now ? */
1246 unsigned int vm_page_purgeable_wired_count
;/* How many purgeable pages are wired now ? */
1248 uint64_t vm_page_purged_count
; /* How many pages got purged so far ? */
1250 extern unsigned int vm_page_free_wanted
;
1251 /* how many threads are waiting for memory */
1253 extern unsigned int vm_page_free_wanted_privileged
;
1254 /* how many VM privileged threads are waiting for memory */
1255 #if CONFIG_SECLUDED_MEMORY
1256 extern unsigned int vm_page_free_wanted_secluded
;
1257 /* how many threads are waiting for secluded memory */
1258 #endif /* CONFIG_SECLUDED_MEMORY */
1260 extern const ppnum_t vm_page_fictitious_addr
;
1261 /* (fake) phys_addr of fictitious pages */
1263 extern const ppnum_t vm_page_guard_addr
;
1264 /* (fake) phys_addr of guard pages */
1267 extern boolean_t vm_page_deactivate_hint
;
1269 extern int vm_compressor_mode
;
1272 * Defaults to true, so highest memory is used first.
1274 extern boolean_t vm_himemory_mode
;
1276 extern boolean_t vm_lopage_needed
;
1277 extern uint32_t vm_lopage_free_count
;
1278 extern uint32_t vm_lopage_free_limit
;
1279 extern uint32_t vm_lopage_lowater
;
1280 extern boolean_t vm_lopage_refill
;
1281 extern uint64_t max_valid_dma_address
;
1282 extern ppnum_t max_valid_low_ppnum
;
1285 * Prototypes for functions exported by this module.
1287 extern void vm_page_bootstrap(
1288 vm_offset_t
*startp
,
1291 extern void vm_page_module_init(void);
1293 extern void vm_page_init_local_q(void);
1295 extern void vm_page_create(
1299 extern vm_page_t
kdp_vm_page_lookup(
1301 vm_object_offset_t offset
);
1303 extern vm_page_t
vm_page_lookup(
1305 vm_object_offset_t offset
);
1307 extern vm_page_t
vm_page_grab_fictitious(void);
1309 extern vm_page_t
vm_page_grab_guard(void);
1311 extern void vm_page_release_fictitious(
1314 extern void vm_free_delayed_pages(void);
1316 extern void vm_page_more_fictitious(void);
1318 extern int vm_pool_low(void);
1320 extern vm_page_t
vm_page_grab(void);
1321 extern vm_page_t
vm_page_grab_options(int flags
);
1323 #define VM_PAGE_GRAB_OPTIONS_NONE 0x00000000
1324 #if CONFIG_SECLUDED_MEMORY
1325 #define VM_PAGE_GRAB_SECLUDED 0x00000001
1326 #endif /* CONFIG_SECLUDED_MEMORY */
1327 #define VM_PAGE_GRAB_Q_LOCK_HELD 0x00000002
1329 extern vm_page_t
vm_page_grablo(void);
1331 extern void vm_page_release(
1333 boolean_t page_queues_locked
);
1335 extern boolean_t
vm_page_wait(
1336 int interruptible
);
1338 extern vm_page_t
vm_page_alloc(
1340 vm_object_offset_t offset
);
1342 extern vm_page_t
vm_page_alloc_guard(
1344 vm_object_offset_t offset
);
1346 extern void vm_page_init(
1351 extern void vm_page_free(
1354 extern void vm_page_free_unlocked(
1356 boolean_t remove_from_hash
);
1358 extern void vm_page_balance_inactive(
1361 extern void vm_page_activate(
1364 extern void vm_page_deactivate(
1367 extern void vm_page_deactivate_internal(
1369 boolean_t clear_hw_reference
);
1371 extern void vm_page_enqueue_cleaned(vm_page_t page
);
1373 extern void vm_page_lru(
1376 extern void vm_page_speculate(
1380 extern void vm_page_speculate_ageit(
1381 struct vm_speculative_age_q
*aq
);
1383 extern void vm_page_reactivate_all_throttled(void);
1385 extern void vm_page_reactivate_local(uint32_t lid
, boolean_t force
, boolean_t nolocks
);
1387 extern void vm_page_rename(
1389 vm_object_t new_object
,
1390 vm_object_offset_t new_offset
);
1392 extern void vm_page_insert(
1395 vm_object_offset_t offset
);
1397 extern void vm_page_insert_wired(
1400 vm_object_offset_t offset
,
1403 extern void vm_page_insert_internal(
1406 vm_object_offset_t offset
,
1408 boolean_t queues_lock_held
,
1409 boolean_t insert_in_hash
,
1410 boolean_t batch_pmap_op
,
1411 boolean_t delayed_accounting
,
1412 uint64_t *delayed_ledger_update
);
1414 extern void vm_page_replace(
1417 vm_object_offset_t offset
);
1419 extern void vm_page_remove(
1421 boolean_t remove_from_hash
);
1423 extern void vm_page_zero_fill(
1426 extern void vm_page_part_zero_fill(
1431 extern void vm_page_copy(
1433 vm_page_t dest_page
);
1435 extern void vm_page_part_copy(
1442 extern void vm_page_wire(
1445 boolean_t check_memorystatus
);
1447 extern void vm_page_unwire(
1451 extern void vm_set_page_size(void);
1453 extern void vm_page_gobble(
1456 extern void vm_page_validate_cs(vm_page_t page
);
1457 extern void vm_page_validate_cs_mapped(
1460 extern void vm_page_validate_cs_mapped_slow(
1463 extern void vm_page_validate_cs_mapped_chunk(
1466 vm_offset_t chunk_offset
,
1467 vm_size_t chunk_size
,
1468 boolean_t
*validated
,
1471 extern void vm_page_free_prepare_queues(
1474 extern void vm_page_free_prepare_object(
1476 boolean_t remove_from_hash
);
1479 extern wait_result_t
vm_page_sleep(
1485 extern void vm_pressure_response(void);
1488 extern void memorystatus_pages_update(unsigned int pages_avail
);
1490 #define VM_CHECK_MEMORYSTATUS do { \
1491 memorystatus_pages_update( \
1492 vm_page_pageable_external_count + \
1493 vm_page_free_count + \
1494 VM_PAGE_SECLUDED_COUNT_OVER_TARGET() + \
1495 (VM_DYNAMIC_PAGING_ENABLED() ? 0 : vm_page_purgeable_count) \
1499 #else /* CONFIG_JETSAM */
1503 #define VM_CHECK_MEMORYSTATUS do {} while(0)
1505 #else /* CONFIG_EMBEDDED */
1507 #define VM_CHECK_MEMORYSTATUS vm_pressure_response()
1509 #endif /* CONFIG_EMBEDDED */
1511 #endif /* CONFIG_JETSAM */
1514 * Functions implemented as macros. m->vmp_wanted and m->vmp_busy are
1515 * protected by the object lock.
1519 #define SET_PAGE_DIRTY(m, set_pmap_modified) \
1521 vm_page_t __page__ = (m); \
1522 if (__page__->vmp_pmapped == TRUE && \
1523 __page__->vmp_wpmapped == TRUE && \
1524 __page__->vmp_dirty == FALSE && \
1525 (set_pmap_modified)) { \
1526 pmap_set_modify(VM_PAGE_GET_PHYS_PAGE(__page__)); \
1528 __page__->vmp_dirty = TRUE; \
1530 #else /* CONFIG_EMBEDDED */
1531 #define SET_PAGE_DIRTY(m, set_pmap_modified) \
1533 vm_page_t __page__ = (m); \
1534 __page__->vmp_dirty = TRUE; \
1536 #endif /* CONFIG_EMBEDDED */
1538 #define PAGE_ASSERT_WAIT(m, interruptible) \
1539 (((m)->vmp_wanted = TRUE), \
1540 assert_wait((event_t) (m), (interruptible)))
1543 #define PAGE_SLEEP(o, m, interruptible) \
1544 vm_page_sleep(o, m, interruptible)
1546 #define PAGE_SLEEP(o, m, interruptible) \
1547 (((m)->vmp_wanted = TRUE), \
1548 thread_sleep_vm_object((o), (m), (interruptible)))
1551 #define PAGE_WAKEUP_DONE(m) \
1553 (m)->vmp_busy = FALSE; \
1554 if ((m)->vmp_wanted) { \
1555 (m)->vmp_wanted = FALSE; \
1556 thread_wakeup((event_t) (m)); \
1560 #define PAGE_WAKEUP(m) \
1562 if ((m)->vmp_wanted) { \
1563 (m)->vmp_wanted = FALSE; \
1564 thread_wakeup((event_t) (m)); \
1568 #define VM_PAGE_FREE(p) \
1570 vm_page_free_unlocked(p, TRUE); \
1573 #define VM_PAGE_GRAB_FICTITIOUS(M) \
1575 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
1576 vm_page_more_fictitious(); \
1579 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
1581 #define vm_page_queue_lock (vm_page_locks.vm_page_queue_lock2)
1582 #define vm_page_queue_free_lock (vm_page_locks.vm_page_queue_free_lock2)
1584 #define vm_page_lock_queues() lck_mtx_lock(&vm_page_queue_lock)
1585 #define vm_page_trylock_queues() lck_mtx_try_lock(&vm_page_queue_lock)
1586 #define vm_page_unlock_queues() lck_mtx_unlock(&vm_page_queue_lock)
1588 #define vm_page_lockspin_queues() lck_mtx_lock_spin(&vm_page_queue_lock)
1589 #define vm_page_trylockspin_queues() lck_mtx_try_lock_spin(&vm_page_queue_lock)
1590 #define vm_page_lockconvert_queues() lck_mtx_convert_spin(&vm_page_queue_lock)
1592 #ifdef VPL_LOCK_SPIN
1593 extern lck_grp_t vm_page_lck_grp_local
;
1595 #define VPL_LOCK_INIT(vlq, vpl_grp, vpl_attr) lck_spin_init(&vlq->vpl_lock, vpl_grp, vpl_attr)
1596 #define VPL_LOCK(vpl) lck_spin_lock_grp(vpl, &vm_page_lck_grp_local)
1597 #define VPL_UNLOCK(vpl) lck_spin_unlock(vpl)
1599 #define VPL_LOCK_INIT(vlq, vpl_grp, vpl_attr) lck_mtx_init_ext(&vlq->vpl_lock, &vlq->vpl_lock_ext, vpl_grp, vpl_attr)
1600 #define VPL_LOCK(vpl) lck_mtx_lock_spin(vpl)
1601 #define VPL_UNLOCK(vpl) lck_mtx_unlock(vpl)
1605 #if DEVELOPMENT || DEBUG
1606 #define VM_PAGE_SPECULATIVE_USED_ADD() \
1608 OSAddAtomic(1, &vm_page_speculative_used); \
1611 #define VM_PAGE_SPECULATIVE_USED_ADD()
1615 #define VM_PAGE_CONSUME_CLUSTERED(mem) \
1617 ppnum_t __phys_page; \
1618 __phys_page = VM_PAGE_GET_PHYS_PAGE(mem); \
1619 pmap_lock_phys_page(__phys_page); \
1620 if (mem->vmp_clustered) { \
1622 o = VM_PAGE_OBJECT(mem); \
1625 mem->vmp_clustered = FALSE; \
1626 VM_PAGE_SPECULATIVE_USED_ADD(); \
1628 pmap_unlock_phys_page(__phys_page); \
1632 #define VM_PAGE_COUNT_AS_PAGEIN(mem) \
1636 o = VM_PAGE_OBJECT(mem); \
1637 DTRACE_VM2(pgin, int, 1, (uint64_t *), NULL); \
1638 current_task()->pageins++; \
1639 if (o->internal) { \
1640 DTRACE_VM2(anonpgin, int, 1, (uint64_t *), NULL); \
1642 DTRACE_VM2(fspgin, int, 1, (uint64_t *), NULL); \
1647 /* adjust for stolen pages accounted elsewhere */
1648 #define VM_PAGE_MOVE_STOLEN(page_count) \
1650 vm_page_stolen_count -= (page_count); \
1651 vm_page_wire_count_initial -= (page_count); \
1654 #define DW_vm_page_unwire 0x01
1655 #define DW_vm_page_wire 0x02
1656 #define DW_vm_page_free 0x04
1657 #define DW_vm_page_activate 0x08
1658 #define DW_vm_page_deactivate_internal 0x10
1659 #define DW_vm_page_speculate 0x20
1660 #define DW_vm_page_lru 0x40
1661 #define DW_vm_pageout_throttle_up 0x80
1662 #define DW_PAGE_WAKEUP 0x100
1663 #define DW_clear_busy 0x200
1664 #define DW_clear_reference 0x400
1665 #define DW_set_reference 0x800
1666 #define DW_move_page 0x1000
1667 #define DW_VM_PAGE_QUEUES_REMOVE 0x2000
1668 #define DW_enqueue_cleaned 0x4000
1669 #define DW_vm_phantom_cache_update 0x8000
1671 struct vm_page_delayed_work
{
1676 void vm_page_do_delayed_work(vm_object_t object
, vm_tag_t tag
, struct vm_page_delayed_work
*dwp
, int dw_count
);
1678 extern unsigned int vm_max_delayed_work_limit
;
1680 #define DEFAULT_DELAYED_WORK_LIMIT 32
1682 #define DELAYED_WORK_LIMIT(max) ((vm_max_delayed_work_limit >= max ? max : vm_max_delayed_work_limit))
1685 * vm_page_do_delayed_work may need to drop the object lock...
1686 * if it does, we need the pages it's looking at to
1687 * be held stable via the busy bit, so if busy isn't already
1688 * set, we need to set it and ask vm_page_do_delayed_work
1689 * to clear it and wakeup anyone that might have blocked on
1690 * it once we're done processing the page.
1693 #define VM_PAGE_ADD_DELAYED_WORK(dwp, mem, dw_cnt) \
1695 if (mem->vmp_busy == FALSE) { \
1696 mem->vmp_busy = TRUE; \
1697 if ( !(dwp->dw_mask & DW_vm_page_free)) \
1698 dwp->dw_mask |= (DW_clear_busy | DW_PAGE_WAKEUP); \
1705 extern vm_page_t
vm_object_page_grab(vm_object_t
);
1707 #if VM_PAGE_BUCKETS_CHECK
1708 extern void vm_page_buckets_check(void);
1709 #endif /* VM_PAGE_BUCKETS_CHECK */
1711 extern void vm_page_queues_remove(vm_page_t mem
, boolean_t remove_from_backgroundq
);
1712 extern void vm_page_remove_internal(vm_page_t page
);
1713 extern void vm_page_enqueue_inactive(vm_page_t mem
, boolean_t first
);
1714 extern void vm_page_enqueue_active(vm_page_t mem
, boolean_t first
);
1715 extern void vm_page_check_pageable_safe(vm_page_t page
);
1717 #if CONFIG_SECLUDED_MEMORY
1718 extern uint64_t secluded_shutoff_trigger
;
1719 extern void start_secluded_suppression(task_t
);
1720 extern void stop_secluded_suppression(task_t
);
1721 #endif /* CONFIG_SECLUDED_MEMORY */
1724 #endif /* _VM_VM_PAGE_H_ */