]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_page.h
xnu-6153.141.1.tar.gz
[apple/xnu.git] / osfmk / vm / vm_page.h
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: vm/vm_page.h
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 * Date: 1985
62 *
63 * Resident memory system definitions.
64 */
65
66 #ifndef _VM_VM_PAGE_H_
67 #define _VM_VM_PAGE_H_
68
69 #include <debug.h>
70 #include <vm/vm_options.h>
71 #include <mach/boolean.h>
72 #include <mach/vm_prot.h>
73 #include <mach/vm_param.h>
74
75
76 #if defined(__LP64__)
77
78 /*
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
84 */
85 typedef uint32_t vm_page_packed_t;
86
87 struct vm_page_packed_queue_entry {
88 vm_page_packed_t next; /* next element */
89 vm_page_packed_t prev; /* previous element */
90 };
91
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;
96
97 typedef vm_page_packed_t vm_page_object_t;
98
99 #else
100
101 /*
102 * we can't do the packing trick on 32 bit architectures, so
103 * just turn the macros into noops.
104 */
105 typedef struct vm_page *vm_page_packed_t;
106
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
111
112 #define vm_page_object_t vm_object_t
113 #endif
114
115
116 #include <vm/vm_object.h>
117 #include <kern/queue.h>
118 #include <kern/locks.h>
119
120 #include <kern/macro_help.h>
121 #include <libkern/OSAtomic.h>
122
123
124
125 #define VM_PAGE_COMPRESSOR_COUNT (compressor_object->resident_page_count)
126
127 /*
128 * Management of resident (logical) pages.
129 *
130 * A small structure is kept for each resident
131 * page, indexed by page number. Each structure
132 * is an element of several lists:
133 *
134 * A hash table bucket used to quickly
135 * perform object/offset lookups
136 *
137 * A list of all pages for a given object,
138 * so they can be quickly deactivated at
139 * time of deallocation.
140 *
141 * An ordered list of pages due for pageout.
142 *
143 * In addition, the structure contains the object
144 * and offset to which this page belongs (for pageout),
145 * and sundry status bits.
146 *
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.]
151 */
152
153 #define VM_PAGE_NULL ((vm_page_t) 0)
154
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[];
159
160
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])
165
166
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 */
183
184 #define VM_PAGE_Q_STATE_ARRAY_SIZE (VM_PAGE_Q_STATE_LAST_VALID_VALUE+1)
185
186
187 /*
188 * The structure itself. See the block comment above for what (O) and (P) mean.
189 */
190 #define vmp_pageq vmp_q_un.vmp_q_pageq
191 #define vmp_snext vmp_q_un.vmp_q_snext
192
193 struct vm_page {
194 union {
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;
197 } vmp_q_un;
198
199 vm_page_queue_chain_t vmp_listq; /* all pages in same object (O) */
200
201 #if CONFIG_BACKGROUND_QUEUE
202 vm_page_queue_chain_t vmp_backgroundq; /* anonymous pages in the background pool (P) */
203 #endif
204
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) */
207
208 /*
209 * The following word of flags is always protected by the "page queues" lock.
210 *
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.
213 */
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) */
217 vmp_in_background:1,
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;
226
227 /*
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
233 * is a real problem
234 */
235 vm_page_packed_t vmp_next_m; /* VP bucket link (O) */
236
237 /*
238 * The following word of flags is protected by the "VM object" lock.
239 *
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.
244 */
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 */
270 vmp_reusable:1,
271 vmp_lopage:1,
272 vmp_written_by_kernel:1, /* page was written by kernel (i.e. decompressed) */
273 vmp_unused_object_bits:8;
274
275 #if !defined(__arm__) && !defined(__arm64__)
276 ppnum_t vmp_phys_page; /* Physical page number of the page */
277 #endif
278 };
279
280
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;
285
286
287 #if defined(__arm__) || defined(__arm64__)
288
289 extern unsigned int vm_first_phys_ppnum;
290
291 struct vm_page_with_ppnum {
292 struct vm_page vm_page_wo_ppnum;
293
294 ppnum_t vmp_phys_page;
295 };
296 typedef struct vm_page_with_ppnum *vm_page_with_ppnum_t;
297
298
299 static inline ppnum_t
300 VM_PAGE_GET_PHYS_PAGE(vm_page_t m)
301 {
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);
304 } else {
305 return ((vm_page_with_ppnum_t)m)->vmp_phys_page;
306 }
307 }
308
309 #define VM_PAGE_SET_PHYS_PAGE(m, ppnum) \
310 MACRO_BEGIN \
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)); \
314 MACRO_END
315
316 #define VM_PAGE_GET_COLOR(m) (VM_PAGE_GET_PHYS_PAGE(m) & vm_color_mask)
317
318 #else /* defined(__arm__) || defined(__arm64__) */
319
320
321 struct vm_page_with_ppnum {
322 struct vm_page vm_page_with_ppnum;
323 };
324 typedef struct vm_page_with_ppnum *vm_page_with_ppnum_t;
325
326
327 #define VM_PAGE_GET_PHYS_PAGE(page) (page)->vmp_phys_page
328 #define VM_PAGE_SET_PHYS_PAGE(page, ppnum) \
329 MACRO_BEGIN \
330 (page)->vmp_phys_page = ppnum; \
331 MACRO_END
332
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)
335
336 #endif /* defined(__arm__) || defined(__arm64__) */
337
338
339
340 #if defined(__LP64__)
341
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
345
346 #define VM_PACKED_FROM_VM_PAGES_ARRAY 0x80000000
347
348 static inline vm_page_packed_t
349 vm_page_pack_ptr(uintptr_t p)
350 {
351 vm_page_packed_t packed_ptr;
352
353 if (!p) {
354 return (vm_page_packed_t)0;
355 }
356
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;
361 return packed_ptr;
362 }
363
364 assert((p & (VM_PACKED_POINTER_ALIGNMENT - 1)) == 0);
365
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));
369 return packed_ptr;
370 }
371
372
373 static inline uintptr_t
374 vm_page_unpack_ptr(uintptr_t p)
375 {
376 extern unsigned int vm_pages_count;
377
378 if (!p) {
379 return (uintptr_t)0;
380 }
381
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)]);
385 }
386 return (p << VM_PACKED_POINTER_SHIFT) + (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS;
387 }
388
389
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))
392
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)))
395
396
397 #define VM_PAGE_ZERO_PAGEQ_ENTRY(p) \
398 MACRO_BEGIN \
399 (p)->vmp_snext = 0; \
400 MACRO_END
401
402
403 #define VM_PAGE_CONVERT_TO_QUEUE_ENTRY(p) VM_PAGE_PACK_PTR(p)
404
405
406 static __inline__ void
407 vm_page_enqueue_tail(
408 vm_page_queue_t que,
409 vm_page_queue_entry_t elt)
410 {
411 vm_page_queue_entry_t old_tail;
412
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);
417 }
418
419
420 static __inline__ void
421 vm_page_remque(
422 vm_page_queue_entry_t elt)
423 {
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;
428
429 next = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(next_pck);
430
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);
433
434 next->prev = prev_pck;
435 prev->next = next_pck;
436
437 elt->next = 0;
438 elt->prev = 0;
439 }
440
441
442 /*
443 * Macro: vm_page_queue_init
444 * Function:
445 * Initialize the given queue.
446 * Header:
447 * void vm_page_queue_init(q)
448 * vm_page_queue_t q; \* MODIFIED *\
449 */
450 #define vm_page_queue_init(q) \
451 MACRO_BEGIN \
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); \
456 MACRO_END
457
458
459 /*
460 * Macro: vm_page_queue_enter
461 * Function:
462 * Insert a new element at the tail of the vm_page queue.
463 * Header:
464 * void vm_page_queue_enter(q, elt, field)
465 * queue_t q;
466 * vm_page_t elt;
467 * <field> is the list field in vm_page_t
468 *
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.
471 */
472 #define vm_page_queue_enter(head, elt, field) \
473 MACRO_BEGIN \
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; \
477 \
478 if (__pck_head == __pck_prev) { \
479 (head)->next = __pck_elt; \
480 } else { \
481 vm_page_t __prev; \
482 __prev = (vm_page_t)VM_PAGE_UNPACK_PTR(__pck_prev); \
483 __prev->field.next = __pck_elt; \
484 } \
485 (elt)->field.prev = __pck_prev; \
486 (elt)->field.next = __pck_head; \
487 (head)->prev = __pck_elt; \
488 MACRO_END
489
490
491 #if defined(__x86_64__)
492 /*
493 * These are helper macros for vm_page_queue_enter_clump to assist
494 * with conditional compilation (release / debug / development)
495 */
496 #if DEVELOPMENT || DEBUG
497
498 #define __DEBUG_CHECK_BUDDIES(__prev, __p, field) \
499 MACRO_BEGIN \
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)); \
503 } \
504 MACRO_END
505
506 #define __DEBUG_VERIFY_LINKS(__first, __n_free, __last_next) \
507 MACRO_BEGIN \
508 unsigned int __i; \
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); \
512 } \
513 assert(__tmp == __last_next); \
514 MACRO_END
515
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
519
520 #else
521
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)
527
528 #endif /* if DEVELOPMENT || DEBUG */
529
530 /*
531 * Insert a new page into a free queue and clump pages within the same 16K boundary together
532 */
533 static inline void
534 vm_page_queue_enter_clump(
535 vm_page_queue_t head,
536 vm_page_t elt)
537 {
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;
542 uint_t n_free = 1;
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;
546
547 /*
548 * If elt is part of the vm_pages[] array, find its neighboring buddies in the array.
549 */
550 if (vm_page_array_beginning_addr <= elt && elt < &vm_pages[vm_pages_count]) {
551 vm_page_t p;
552 uint_t i;
553 uint_t n;
554 ppnum_t clump_num;
555
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;
559
560 /*
561 * Check for preceeding vm_pages[] entries in the same chunk
562 */
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)) {
565 if (prev == NULL) {
566 prev = (vm_page_queue_entry_t)p;
567 }
568 first = (vm_page_queue_entry_t)p;
569 n_free++;
570 }
571 }
572
573 /*
574 * Check the following vm_pages[] entries in the same chunk
575 */
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);
580 }
581
582 if (prev == NULL) {
583 prev = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(p->vmp_pageq.prev);
584 }
585 last = (vm_page_queue_entry_t)p;
586 n_free++;
587 }
588 }
589 __DEBUG_STAT_INCREMENT_INRANGE;
590 }
591
592 /* if elt is not part of vm_pages or if 1st page in clump, insert at tail */
593 if (prev == NULL) {
594 prev = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(head->prev);
595 }
596
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;
603
604 /*
605 * Check if clump needs to be promoted to head.
606 */
607 if (n_free >= vm_clump_promote_threshold && n_free > 1) {
608 vm_page_queue_entry_t first_prev;
609
610 first_prev = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(first->prev);
611
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;
616
617 last_next = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(last->next);
618
619 /* verify that the links within the clump are consistent */
620 __DEBUG_VERIFY_LINKS(first, n_free, last_next);
621
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;
627
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);
632 }
633 }
634 }
635 #endif
636
637 /*
638 * Macro: vm_page_queue_enter_first
639 * Function:
640 * Insert a new element at the head of the vm_page queue.
641 * Header:
642 * void queue_enter_first(q, elt, , field)
643 * queue_t q;
644 * vm_page_t elt;
645 * <field> is the linkage field in vm_page
646 *
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.
649 */
650 #define vm_page_queue_enter_first(head, elt, field) \
651 MACRO_BEGIN \
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); \
655 \
656 if (__pck_head == __pck_next) { \
657 (head)->prev = __pck_elt; \
658 } else { \
659 vm_page_t __next; \
660 __next = (vm_page_t)VM_PAGE_UNPACK_PTR(__pck_next); \
661 __next->field.prev = __pck_elt; \
662 } \
663 \
664 (elt)->field.next = __pck_next; \
665 (elt)->field.prev = __pck_head; \
666 (head)->next = __pck_elt; \
667 MACRO_END
668
669
670 /*
671 * Macro: vm_page_queue_remove
672 * Function:
673 * Remove an arbitrary page from a vm_page queue.
674 * Header:
675 * void vm_page_queue_remove(q, qe, field)
676 * arguments as in vm_page_queue_enter
677 *
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.
680 */
681 #define vm_page_queue_remove(head, elt, field) \
682 MACRO_BEGIN \
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); \
687 \
688 if ((void *)(head) == (void *)__next) { \
689 (head)->prev = __pck_prev; \
690 } else { \
691 __next->field.prev = __pck_prev; \
692 } \
693 \
694 if ((void *)(head) == (void *)__prev) { \
695 (head)->next = __pck_next; \
696 } else { \
697 __prev->field.next = __pck_next; \
698 } \
699 \
700 (elt)->field.next = 0; \
701 (elt)->field.prev = 0; \
702 MACRO_END
703
704
705 /*
706 * Macro: vm_page_queue_remove_first
707 *
708 * Function:
709 * Remove and return the entry at the head of a vm_page queue.
710 *
711 * Header:
712 * vm_page_queue_remove_first(head, entry, field)
713 * N.B. entry is returned by reference
714 *
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.
717 */
718 #define vm_page_queue_remove_first(head, entry, field) \
719 MACRO_BEGIN \
720 vm_page_packed_t __pck_head = VM_PAGE_PACK_PTR(head); \
721 vm_page_packed_t __pck_next; \
722 vm_page_t __next; \
723 \
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); \
727 \
728 if (__pck_head == __pck_next) { \
729 (head)->prev = __pck_head; \
730 } else { \
731 __next->field.prev = __pck_head; \
732 } \
733 \
734 (head)->next = __pck_next; \
735 (entry)->field.next = 0; \
736 (entry)->field.prev = 0; \
737 MACRO_END
738
739
740 #if defined(__x86_64__)
741 /*
742 * Macro: vm_page_queue_remove_first_with_clump
743 * Function:
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
746 *
747 * Header:
748 * vm_page_queue_remove_first_with_clump(head, entry, end)
749 * entry is returned by reference
750 * end is returned by reference
751 */
752 #define vm_page_queue_remove_first_with_clump(head, entry, end) \
753 MACRO_BEGIN \
754 vm_page_packed_t __pck_head = VM_PAGE_PACK_PTR(head); \
755 vm_page_packed_t __pck_next; \
756 vm_page_t __next; \
757 \
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); \
761 \
762 (end) = 0; \
763 if (__pck_head == __pck_next) { \
764 (head)->prev = __pck_head; \
765 (end) = 1; \
766 } else { \
767 __next->vmp_pageq.prev = __pck_head; \
768 if (VM_PAGE_GET_CLUMP(entry) != VM_PAGE_GET_CLUMP(__next)) { \
769 (end) = 1; \
770 } \
771 } \
772 \
773 (head)->next = __pck_next; \
774 (entry)->vmp_pageq.next = 0; \
775 (entry)->vmp_pageq.prev = 0; \
776 MACRO_END
777 #endif
778
779 /*
780 * Macro: vm_page_queue_end
781 * Function:
782 * Tests whether a new entry is really the end of
783 * the queue.
784 * Header:
785 * boolean_t vm_page_queue_end(q, qe)
786 * vm_page_queue_t q;
787 * vm_page_queue_entry_t qe;
788 */
789 #define vm_page_queue_end(q, qe) ((q) == (qe))
790
791
792 /*
793 * Macro: vm_page_queue_empty
794 * Function:
795 * Tests whether a queue is empty.
796 * Header:
797 * boolean_t vm_page_queue_empty(q)
798 * vm_page_queue_t q;
799 */
800 #define vm_page_queue_empty(q) vm_page_queue_end((q), ((vm_page_queue_entry_t)vm_page_queue_first(q)))
801
802
803
804 /*
805 * Macro: vm_page_queue_first
806 * Function:
807 * Returns the first entry in the queue,
808 * Header:
809 * uintpr_t vm_page_queue_first(q)
810 * vm_page_queue_t q; \* IN *\
811 */
812 #define vm_page_queue_first(q) (VM_PAGE_UNPACK_PTR((q)->next))
813
814
815
816 /*
817 * Macro: vm_page_queue_last
818 * Function:
819 * Returns the last entry in the queue.
820 * Header:
821 * vm_page_queue_entry_t queue_last(q)
822 * queue_t q; \* IN *\
823 */
824 #define vm_page_queue_last(q) (VM_PAGE_UNPACK_PTR((q)->prev))
825
826
827
828 /*
829 * Macro: vm_page_queue_next
830 * Function:
831 * Returns the entry after an item in the queue.
832 * Header:
833 * uintpr_t vm_page_queue_next(qc)
834 * vm_page_queue_t qc;
835 */
836 #define vm_page_queue_next(qc) (VM_PAGE_UNPACK_PTR((qc)->next))
837
838
839
840 /*
841 * Macro: vm_page_queue_prev
842 * Function:
843 * Returns the entry before an item in the queue.
844 * Header:
845 * uinptr_t vm_page_queue_prev(qc)
846 * vm_page_queue_t qc;
847 */
848 #define vm_page_queue_prev(qc) (VM_PAGE_UNPACK_PTR((qc)->prev))
849
850
851
852 /*
853 * Macro: vm_page_queue_iterate
854 * Function:
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).
858 * Header:
859 * vm_page_queue_iterate(q, elt, field)
860 * queue_t q;
861 * vm_page_t elt;
862 * <field> is the chain field in vm_page_t
863 */
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)) \
868
869 #else
870
871 #define VM_VPLQ_ALIGNMENT 128
872 #define VM_PACKED_POINTER_ALIGNMENT 4
873 #define VM_PACKED_POINTER_SHIFT 0
874
875 #define VM_PACKED_FROM_VM_PAGES_ARRAY 0
876
877 #define VM_PAGE_PACK_PTR(p) (p)
878 #define VM_PAGE_UNPACK_PTR(p) ((uintptr_t)(p))
879
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)))
882
883
884 #define VM_PAGE_ZERO_PAGEQ_ENTRY(p) \
885 MACRO_BEGIN \
886 (p)->vmp_pageq.next = 0; \
887 (p)->vmp_pageq.prev = 0; \
888 MACRO_END
889
890 #define VM_PAGE_CONVERT_TO_QUEUE_ENTRY(p) ((queue_entry_t)(p))
891
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)
906
907 #endif
908
909
910
911 /*
912 * VM_PAGE_MIN_SPECULATIVE_AGE_Q through VM_PAGE_MAX_SPECULATIVE_AGE_Q
913 * represents a set of aging bins that are 'protected'...
914 *
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
926 *
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.
936 *
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
939 */
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
943
944 #define VM_PAGE_SPECULATIVE_Q_AGE_MS 500
945
946 struct vm_speculative_age_q {
947 /*
948 * memory queue for speculative pages via clustered pageins
949 */
950 vm_page_queue_head_t age_q;
951 mach_timespec_t age_ts;
952 } __attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT)));
953
954
955
956 extern
957 struct vm_speculative_age_q vm_page_queue_speculative[];
958
959 extern int speculative_steal_index;
960 extern int speculative_age_index;
961 extern unsigned int vm_page_speculative_q_age_ms;
962
963
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)));
969 } vm_locks_array_t;
970
971
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);
977 #endif
978
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)
982
983 /*
984 * XXX The unusual bit should not be necessary. Most of the bit
985 * XXX fields above really want to be masks.
986 */
987
988 /*
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.
993 */
994
995 #define VM_PAGE_CHECK(mem) \
996 MACRO_BEGIN \
997 MACRO_END
998
999 /* Page coloring:
1000 *
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.
1007 */
1008
1009 #define MAX_COLORS 128
1010 #define DEFAULT_COLORS 32
1011
1012 extern
1013 unsigned int vm_colors; /* must be in range 1..MAX_COLORS */
1014 extern
1015 unsigned int vm_color_mask; /* must be (vm_colors-1) */
1016 extern
1017 unsigned int vm_cache_geometry_colors; /* optimal #colors based on cache geometry */
1018
1019 /*
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:
1022 *
1023 * vm_per_task_user_wire_limit
1024 * how much memory can be user-wired in one user task
1025 *
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
1028 *
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.
1032 *
1033 * Regardless of the amount of memory in the system, we never reserve
1034 * more than VM_NOT_USER_WIREABLE_MAX bytes as unlockable.
1035 */
1036 #if defined(__LP64__)
1037 #define VM_NOT_USER_WIREABLE_MAX (32ULL*1024*1024*1024) /* 32GB */
1038 #else
1039 #define VM_NOT_USER_WIREABLE_MAX (1UL*1024*1024*1024) /* 1GB */
1040 #endif /* __LP64__ */
1041 extern
1042 vm_map_size_t vm_per_task_user_wire_limit;
1043 extern
1044 vm_map_size_t vm_global_user_wire_limit;
1045
1046 /*
1047 * Each pageable resident page falls into one of three lists:
1048 *
1049 * free
1050 * Available for allocation now. The free list is
1051 * actually an array of lists, one per color.
1052 * inactive
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.
1061 * active
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.
1065 */
1066
1067
1068 #define VPL_LOCK_SPIN 1
1069
1070 struct vpl {
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;
1077 #else
1078 lck_mtx_t vpl_lock;
1079 lck_mtx_ext_t vpl_lock_ext;
1080 #endif
1081 };
1082
1083 struct vplq {
1084 union {
1085 char cache_line_pad[VM_VPLQ_ALIGNMENT];
1086 struct vpl vpl;
1087 } vpl_un;
1088 };
1089 extern
1090 unsigned int vm_page_local_q_count;
1091 extern
1092 struct vplq *vm_page_local_q;
1093 extern
1094 unsigned int vm_page_local_q_soft_limit;
1095 extern
1096 unsigned int vm_page_local_q_hard_limit;
1097 extern
1098 vm_locks_array_t vm_page_locks;
1099
1100 extern
1101 vm_page_queue_head_t vm_lopage_queue_free; /* low memory free queue */
1102 extern
1103 vm_page_queue_head_t vm_page_queue_active; /* active memory queue */
1104 extern
1105 vm_page_queue_head_t vm_page_queue_inactive; /* inactive memory queue for normal pages */
1106 #if CONFIG_SECLUDED_MEMORY
1107 extern
1108 vm_page_queue_head_t vm_page_queue_secluded; /* reclaimable pages secluded for Camera */
1109 #endif /* CONFIG_SECLUDED_MEMORY */
1110 extern
1111 vm_page_queue_head_t vm_page_queue_cleaned; /* clean-queue inactive memory */
1112 extern
1113 vm_page_queue_head_t vm_page_queue_anonymous; /* inactive memory queue for anonymous pages */
1114 extern
1115 vm_page_queue_head_t vm_page_queue_throttled; /* memory queue for throttled pageout pages */
1116
1117 extern
1118 queue_head_t vm_objects_wired;
1119 extern
1120 lck_spin_t vm_objects_wired_lock;
1121
1122 #if CONFIG_BACKGROUND_QUEUE
1123
1124 #define VM_PAGE_BACKGROUND_TARGET_MAX 50000
1125
1126 #define VM_PAGE_BG_DISABLED 0
1127 #define VM_PAGE_BG_LEVEL_1 1
1128
1129 extern
1130 vm_page_queue_head_t vm_page_queue_background;
1131 extern
1132 uint64_t vm_page_background_promoted_count;
1133 extern
1134 uint32_t vm_page_background_count;
1135 extern
1136 uint32_t vm_page_background_target;
1137 extern
1138 uint32_t vm_page_background_internal_count;
1139 extern
1140 uint32_t vm_page_background_external_count;
1141 extern
1142 uint32_t vm_page_background_mode;
1143 extern
1144 uint32_t vm_page_background_exclude_external;
1145
1146 #endif
1147
1148 extern
1149 vm_offset_t first_phys_addr; /* physical address for first_page */
1150 extern
1151 vm_offset_t last_phys_addr; /* physical address for last_page */
1152
1153 extern
1154 unsigned int vm_page_free_count; /* How many pages are free? (sum of all colors) */
1155 extern
1156 unsigned int vm_page_active_count; /* How many pages are active? */
1157 extern
1158 unsigned int vm_page_inactive_count; /* How many pages are inactive? */
1159 #if CONFIG_SECLUDED_MEMORY
1160 extern
1161 unsigned int vm_page_secluded_count; /* How many pages are secluded? */
1162 extern
1163 unsigned int vm_page_secluded_count_free; /* how many of them are free? */
1164 extern
1165 unsigned int vm_page_secluded_count_inuse; /* how many of them are in use? */
1166 /*
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.
1175 */
1176 extern
1177 unsigned int vm_page_secluded_count_over_target;
1178 #define VM_PAGE_SECLUDED_COUNT_OVER_TARGET_UPDATE() \
1179 MACRO_BEGIN \
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); \
1183 } else { \
1184 vm_page_secluded_count_over_target = 0; \
1185 } \
1186 MACRO_END
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() \
1190 MACRO_BEGIN \
1191 MACRO_END
1192 #define VM_PAGE_SECLUDED_COUNT_OVER_TARGET() 0
1193 #endif /* CONFIG_SECLUDED_MEMORY */
1194 extern
1195 unsigned int vm_page_cleaned_count; /* How many pages are in the clean queue? */
1196 extern
1197 unsigned int vm_page_throttled_count;/* How many inactives are throttled */
1198 extern
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;
1202 extern
1203 unsigned int vm_page_xpmapped_external_count; /* How many pages are mapped executable? */
1204 extern
1205 unsigned int vm_page_external_count; /* How many pages are file-backed? */
1206 extern
1207 unsigned int vm_page_internal_count; /* How many pages are anonymous? */
1208 extern
1209 unsigned int vm_page_wire_count; /* How many pages are wired? */
1210 extern
1211 unsigned int vm_page_wire_count_initial; /* How many pages wired at startup */
1212 extern
1213 unsigned int vm_page_wire_count_on_boot; /* even earlier than _initial */
1214 extern
1215 unsigned int vm_page_free_target; /* How many do we want free? */
1216 extern
1217 unsigned int vm_page_free_min; /* When to wakeup pageout */
1218 extern
1219 unsigned int vm_page_throttle_limit; /* When to throttle new page creation */
1220 extern
1221 unsigned int vm_page_inactive_target;/* How many do we want inactive? */
1222 #if CONFIG_SECLUDED_MEMORY
1223 extern
1224 unsigned int vm_page_secluded_target;/* How many do we want secluded? */
1225 #endif /* CONFIG_SECLUDED_MEMORY */
1226 extern
1227 unsigned int vm_page_anonymous_min; /* When it's ok to pre-clean */
1228 extern
1229 unsigned int vm_page_free_reserved; /* How many pages reserved to do pageout */
1230 extern
1231 unsigned int vm_page_gobble_count;
1232 extern
1233 unsigned int vm_page_stolen_count; /* Count of stolen pages not acccounted in zones */
1234 extern
1235 unsigned int vm_page_kern_lpage_count; /* Count of large pages used in early boot */
1236
1237
1238 #if DEVELOPMENT || DEBUG
1239 extern
1240 unsigned int vm_page_speculative_used;
1241 #endif
1242
1243 extern
1244 unsigned int vm_page_purgeable_count;/* How many pages are purgeable now ? */
1245 extern
1246 unsigned int vm_page_purgeable_wired_count;/* How many purgeable pages are wired now ? */
1247 extern
1248 uint64_t vm_page_purged_count; /* How many pages got purged so far ? */
1249
1250 extern unsigned int vm_page_free_wanted;
1251 /* how many threads are waiting for memory */
1252
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 */
1259
1260 extern const ppnum_t vm_page_fictitious_addr;
1261 /* (fake) phys_addr of fictitious pages */
1262
1263 extern const ppnum_t vm_page_guard_addr;
1264 /* (fake) phys_addr of guard pages */
1265
1266
1267 extern boolean_t vm_page_deactivate_hint;
1268
1269 extern int vm_compressor_mode;
1270
1271 /*
1272 * Defaults to true, so highest memory is used first.
1273 */
1274 extern boolean_t vm_himemory_mode;
1275
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;
1283
1284 /*
1285 * Prototypes for functions exported by this module.
1286 */
1287 extern void vm_page_bootstrap(
1288 vm_offset_t *startp,
1289 vm_offset_t *endp);
1290
1291 extern void vm_page_module_init(void);
1292
1293 extern void vm_page_init_local_q(void);
1294
1295 extern void vm_page_create(
1296 ppnum_t start,
1297 ppnum_t end);
1298
1299 extern vm_page_t kdp_vm_page_lookup(
1300 vm_object_t object,
1301 vm_object_offset_t offset);
1302
1303 extern vm_page_t vm_page_lookup(
1304 vm_object_t object,
1305 vm_object_offset_t offset);
1306
1307 extern vm_page_t vm_page_grab_fictitious(void);
1308
1309 extern vm_page_t vm_page_grab_guard(void);
1310
1311 extern void vm_page_release_fictitious(
1312 vm_page_t page);
1313
1314 extern void vm_free_delayed_pages(void);
1315
1316 extern void vm_page_more_fictitious(void);
1317
1318 extern int vm_pool_low(void);
1319
1320 extern vm_page_t vm_page_grab(void);
1321 extern vm_page_t vm_page_grab_options(int flags);
1322
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
1328
1329 extern vm_page_t vm_page_grablo(void);
1330
1331 extern void vm_page_release(
1332 vm_page_t page,
1333 boolean_t page_queues_locked);
1334
1335 extern boolean_t vm_page_wait(
1336 int interruptible );
1337
1338 extern vm_page_t vm_page_alloc(
1339 vm_object_t object,
1340 vm_object_offset_t offset);
1341
1342 extern vm_page_t vm_page_alloc_guard(
1343 vm_object_t object,
1344 vm_object_offset_t offset);
1345
1346 extern void vm_page_init(
1347 vm_page_t page,
1348 ppnum_t phys_page,
1349 boolean_t lopage);
1350
1351 extern void vm_page_free(
1352 vm_page_t page);
1353
1354 extern void vm_page_free_unlocked(
1355 vm_page_t page,
1356 boolean_t remove_from_hash);
1357
1358 extern void vm_page_balance_inactive(
1359 int max_to_move);
1360
1361 extern void vm_page_activate(
1362 vm_page_t page);
1363
1364 extern void vm_page_deactivate(
1365 vm_page_t page);
1366
1367 extern void vm_page_deactivate_internal(
1368 vm_page_t page,
1369 boolean_t clear_hw_reference);
1370
1371 extern void vm_page_enqueue_cleaned(vm_page_t page);
1372
1373 extern void vm_page_lru(
1374 vm_page_t page);
1375
1376 extern void vm_page_speculate(
1377 vm_page_t page,
1378 boolean_t new);
1379
1380 extern void vm_page_speculate_ageit(
1381 struct vm_speculative_age_q *aq);
1382
1383 extern void vm_page_reactivate_all_throttled(void);
1384
1385 extern void vm_page_reactivate_local(uint32_t lid, boolean_t force, boolean_t nolocks);
1386
1387 extern void vm_page_rename(
1388 vm_page_t page,
1389 vm_object_t new_object,
1390 vm_object_offset_t new_offset);
1391
1392 extern void vm_page_insert(
1393 vm_page_t page,
1394 vm_object_t object,
1395 vm_object_offset_t offset);
1396
1397 extern void vm_page_insert_wired(
1398 vm_page_t page,
1399 vm_object_t object,
1400 vm_object_offset_t offset,
1401 vm_tag_t tag);
1402
1403 extern void vm_page_insert_internal(
1404 vm_page_t page,
1405 vm_object_t object,
1406 vm_object_offset_t offset,
1407 vm_tag_t tag,
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);
1413
1414 extern void vm_page_replace(
1415 vm_page_t mem,
1416 vm_object_t object,
1417 vm_object_offset_t offset);
1418
1419 extern void vm_page_remove(
1420 vm_page_t page,
1421 boolean_t remove_from_hash);
1422
1423 extern void vm_page_zero_fill(
1424 vm_page_t page);
1425
1426 extern void vm_page_part_zero_fill(
1427 vm_page_t m,
1428 vm_offset_t m_pa,
1429 vm_size_t len);
1430
1431 extern void vm_page_copy(
1432 vm_page_t src_page,
1433 vm_page_t dest_page);
1434
1435 extern void vm_page_part_copy(
1436 vm_page_t src_m,
1437 vm_offset_t src_pa,
1438 vm_page_t dst_m,
1439 vm_offset_t dst_pa,
1440 vm_size_t len);
1441
1442 extern void vm_page_wire(
1443 vm_page_t page,
1444 vm_tag_t tag,
1445 boolean_t check_memorystatus);
1446
1447 extern void vm_page_unwire(
1448 vm_page_t page,
1449 boolean_t queueit);
1450
1451 extern void vm_set_page_size(void);
1452
1453 extern void vm_page_gobble(
1454 vm_page_t page);
1455
1456 extern void vm_page_validate_cs(vm_page_t page);
1457 extern void vm_page_validate_cs_mapped(
1458 vm_page_t page,
1459 const void *kaddr);
1460 extern void vm_page_validate_cs_mapped_slow(
1461 vm_page_t page,
1462 const void *kaddr);
1463 extern void vm_page_validate_cs_mapped_chunk(
1464 vm_page_t page,
1465 const void *kaddr,
1466 vm_offset_t chunk_offset,
1467 vm_size_t chunk_size,
1468 boolean_t *validated,
1469 unsigned *tainted);
1470
1471 extern void vm_page_free_prepare_queues(
1472 vm_page_t page);
1473
1474 extern void vm_page_free_prepare_object(
1475 vm_page_t page,
1476 boolean_t remove_from_hash);
1477
1478 #if CONFIG_IOSCHED
1479 extern wait_result_t vm_page_sleep(
1480 vm_object_t object,
1481 vm_page_t m,
1482 int interruptible);
1483 #endif
1484
1485 extern void vm_pressure_response(void);
1486
1487 #if CONFIG_JETSAM
1488 extern void memorystatus_pages_update(unsigned int pages_avail);
1489
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) \
1496 ); \
1497 } while(0)
1498
1499 #else /* CONFIG_JETSAM */
1500
1501 #if CONFIG_EMBEDDED
1502
1503 #define VM_CHECK_MEMORYSTATUS do {} while(0)
1504
1505 #else /* CONFIG_EMBEDDED */
1506
1507 #define VM_CHECK_MEMORYSTATUS vm_pressure_response()
1508
1509 #endif /* CONFIG_EMBEDDED */
1510
1511 #endif /* CONFIG_JETSAM */
1512
1513 /*
1514 * Functions implemented as macros. m->vmp_wanted and m->vmp_busy are
1515 * protected by the object lock.
1516 */
1517
1518 #if CONFIG_EMBEDDED
1519 #define SET_PAGE_DIRTY(m, set_pmap_modified) \
1520 MACRO_BEGIN \
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__)); \
1527 } \
1528 __page__->vmp_dirty = TRUE; \
1529 MACRO_END
1530 #else /* CONFIG_EMBEDDED */
1531 #define SET_PAGE_DIRTY(m, set_pmap_modified) \
1532 MACRO_BEGIN \
1533 vm_page_t __page__ = (m); \
1534 __page__->vmp_dirty = TRUE; \
1535 MACRO_END
1536 #endif /* CONFIG_EMBEDDED */
1537
1538 #define PAGE_ASSERT_WAIT(m, interruptible) \
1539 (((m)->vmp_wanted = TRUE), \
1540 assert_wait((event_t) (m), (interruptible)))
1541
1542 #if CONFIG_IOSCHED
1543 #define PAGE_SLEEP(o, m, interruptible) \
1544 vm_page_sleep(o, m, interruptible)
1545 #else
1546 #define PAGE_SLEEP(o, m, interruptible) \
1547 (((m)->vmp_wanted = TRUE), \
1548 thread_sleep_vm_object((o), (m), (interruptible)))
1549 #endif
1550
1551 #define PAGE_WAKEUP_DONE(m) \
1552 MACRO_BEGIN \
1553 (m)->vmp_busy = FALSE; \
1554 if ((m)->vmp_wanted) { \
1555 (m)->vmp_wanted = FALSE; \
1556 thread_wakeup((event_t) (m)); \
1557 } \
1558 MACRO_END
1559
1560 #define PAGE_WAKEUP(m) \
1561 MACRO_BEGIN \
1562 if ((m)->vmp_wanted) { \
1563 (m)->vmp_wanted = FALSE; \
1564 thread_wakeup((event_t) (m)); \
1565 } \
1566 MACRO_END
1567
1568 #define VM_PAGE_FREE(p) \
1569 MACRO_BEGIN \
1570 vm_page_free_unlocked(p, TRUE); \
1571 MACRO_END
1572
1573 #define VM_PAGE_GRAB_FICTITIOUS(M) \
1574 MACRO_BEGIN \
1575 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
1576 vm_page_more_fictitious(); \
1577 MACRO_END
1578
1579 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
1580
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)
1583
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)
1587
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)
1591
1592 #ifdef VPL_LOCK_SPIN
1593 extern lck_grp_t vm_page_lck_grp_local;
1594
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)
1598 #else
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)
1602 #endif
1603
1604
1605 #if DEVELOPMENT || DEBUG
1606 #define VM_PAGE_SPECULATIVE_USED_ADD() \
1607 MACRO_BEGIN \
1608 OSAddAtomic(1, &vm_page_speculative_used); \
1609 MACRO_END
1610 #else
1611 #define VM_PAGE_SPECULATIVE_USED_ADD()
1612 #endif
1613
1614
1615 #define VM_PAGE_CONSUME_CLUSTERED(mem) \
1616 MACRO_BEGIN \
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) { \
1621 vm_object_t o; \
1622 o = VM_PAGE_OBJECT(mem); \
1623 assert(o); \
1624 o->pages_used++; \
1625 mem->vmp_clustered = FALSE; \
1626 VM_PAGE_SPECULATIVE_USED_ADD(); \
1627 } \
1628 pmap_unlock_phys_page(__phys_page); \
1629 MACRO_END
1630
1631
1632 #define VM_PAGE_COUNT_AS_PAGEIN(mem) \
1633 MACRO_BEGIN \
1634 { \
1635 vm_object_t o; \
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); \
1641 } else { \
1642 DTRACE_VM2(fspgin, int, 1, (uint64_t *), NULL); \
1643 } \
1644 } \
1645 MACRO_END
1646
1647 /* adjust for stolen pages accounted elsewhere */
1648 #define VM_PAGE_MOVE_STOLEN(page_count) \
1649 MACRO_BEGIN \
1650 vm_page_stolen_count -= (page_count); \
1651 vm_page_wire_count_initial -= (page_count); \
1652 MACRO_END
1653
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
1670
1671 struct vm_page_delayed_work {
1672 vm_page_t dw_m;
1673 int dw_mask;
1674 };
1675
1676 void vm_page_do_delayed_work(vm_object_t object, vm_tag_t tag, struct vm_page_delayed_work *dwp, int dw_count);
1677
1678 extern unsigned int vm_max_delayed_work_limit;
1679
1680 #define DEFAULT_DELAYED_WORK_LIMIT 32
1681
1682 #define DELAYED_WORK_LIMIT(max) ((vm_max_delayed_work_limit >= max ? max : vm_max_delayed_work_limit))
1683
1684 /*
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.
1691 */
1692
1693 #define VM_PAGE_ADD_DELAYED_WORK(dwp, mem, dw_cnt) \
1694 MACRO_BEGIN \
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); \
1699 } \
1700 dwp->dw_m = mem; \
1701 dwp++; \
1702 dw_cnt++; \
1703 MACRO_END
1704
1705 extern vm_page_t vm_object_page_grab(vm_object_t);
1706
1707 #if VM_PAGE_BUCKETS_CHECK
1708 extern void vm_page_buckets_check(void);
1709 #endif /* VM_PAGE_BUCKETS_CHECK */
1710
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);
1716
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 */
1722
1723
1724 #endif /* _VM_VM_PAGE_H_ */