]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_page.h
xnu-4570.71.2.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->vm_page_q_state])
162 #define VM_PAGE_PAGEABLE(m) (vm_page_pageable_states[m->vm_page_q_state])
163 #define VM_PAGE_NON_SPECULATIVE_PAGEABLE(m) (vm_page_non_speculative_pageable_states[m->vm_page_q_state])
164 #define VM_PAGE_ACTIVE_OR_INACTIVE(m) (vm_page_active_or_inactive_states[m->vm_page_q_state])
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 #define pageq pageq_un.vm_page_pageq
188 #define snext pageq_un.vm_page_snext
189
190 struct vm_page {
191 union {
192 vm_page_queue_chain_t vm_page_pageq; /* queue info for FIFO queue or free list (P) */
193 struct vm_page *vm_page_snext;
194 } pageq_un;
195
196 vm_page_queue_chain_t listq; /* all pages in same object (O) */
197
198 #if CONFIG_BACKGROUND_QUEUE
199 vm_page_queue_chain_t vm_page_backgroundq; /* anonymous pages in the background pool (P) */
200 #endif
201
202 vm_object_offset_t offset; /* offset into that object (O,P) */
203 vm_page_object_t vm_page_object; /* which object am I in (O&P) */
204
205 /*
206 * The following word of flags is protected
207 * by the "page queues" lock.
208 *
209 * we use the 'wire_count' field to store the local
210 * queue id if local queues are enabled...
211 * see the comments at 'vm_page_queues_remove' as to
212 * why this is safe to do
213 */
214 #define local_id wire_count
215 unsigned int wire_count:16, /* how many wired down maps use me? (O&P) */
216 vm_page_q_state:4, /* which q is the page on (P) */
217
218 vm_page_in_background:1,
219 vm_page_on_backgroundq:1,
220 /* boolean_t */
221 gobbled:1, /* page used internally (P) */
222 laundry:1, /* page is being cleaned now (P)*/
223 no_cache:1, /* page is not to be cached and should
224 * be reused ahead of other pages (P) */
225 private:1, /* Page should not be returned to
226 * the free list (P) */
227 reference:1, /* page has been used (P) */
228
229 __unused_pageq_bits:5; /* 5 bits available here */
230
231 /*
232 * MUST keep the 2 32 bit words used as bit fields
233 * separated since the compiler has a nasty habit
234 * of using 64 bit loads and stores on them as
235 * if they were a single 64 bit field... since
236 * they are protected by 2 different locks, this
237 * is a real problem
238 */
239 vm_page_packed_t next_m; /* VP bucket link (O) */
240
241 /*
242 * The following word of flags is protected
243 * by the "VM object" lock.
244 */
245 unsigned int
246 /* boolean_t */ busy:1, /* page is in transit (O) */
247 wanted:1, /* someone is waiting for page (O) */
248 tabled:1, /* page is in VP table (O) */
249 hashed:1, /* page is in vm_page_buckets[]
250 (O) + the bucket lock */
251 fictitious:1, /* Physical page doesn't exist (O) */
252 /*
253 * IMPORTANT: the "pmapped", "xpmapped" and "clustered" bits can be modified while holding the
254 * VM object "shared" lock + the page lock provided through the pmap_lock_phys_page function.
255 * This is done in vm_fault_enter and the CONSUME_CLUSTERED macro.
256 * It's also ok to modify them behind just the VM object "exclusive" lock.
257 */
258 clustered:1, /* page is not the faulted page (O) or (O-shared AND pmap_page) */
259 pmapped:1, /* page has been entered at some
260 * point into a pmap (O) or (O-shared AND pmap_page) */
261 xpmapped:1, /* page has been entered with execute permission (O)
262 or (O-shared AND pmap_page) */
263
264 wpmapped:1, /* page has been entered at some
265 * point into a pmap for write (O) */
266 free_when_done:1, /* page is to be freed once cleaning is completed (O) */
267 absent:1, /* Data has been requested, but is
268 * not yet available (O) */
269 error:1, /* Data manager was unable to provide
270 * data due to error (O) */
271 dirty:1, /* Page must be cleaned (O) */
272 cleaning:1, /* Page clean has begun (O) */
273 precious:1, /* Page is precious; data must be
274 * returned even if clean (O) */
275 overwriting:1, /* Request to unlock has been made
276 * without having data. (O)
277 * [See vm_fault_page_overwrite] */
278 restart:1, /* Page was pushed higher in shadow
279 chain by copy_call-related pagers;
280 start again at top of chain */
281 unusual:1, /* Page is absent, error, restart or
282 page locked */
283 cs_validated:1, /* code-signing: page was checked */
284 cs_tainted:1, /* code-signing: page is tainted */
285 cs_nx:1, /* code-signing: page is nx */
286 reusable:1,
287 lopage:1,
288 slid:1,
289 written_by_kernel:1, /* page was written by kernel (i.e. decompressed) */
290 __unused_object_bits:7; /* 7 bits available here */
291
292 #if !defined(__arm__) && !defined(__arm64__)
293 ppnum_t phys_page; /* Physical address of page, passed
294 * to pmap_enter (read-only) */
295 #endif
296 };
297
298
299 typedef struct vm_page *vm_page_t;
300 extern vm_page_t vm_pages;
301 extern vm_page_t vm_page_array_beginning_addr;
302 extern vm_page_t vm_page_array_ending_addr;
303
304
305 #if defined(__arm__) || defined(__arm64__)
306
307 extern unsigned int vm_first_phys_ppnum;
308
309 struct vm_page_with_ppnum {
310 struct vm_page vm_page_wo_ppnum;
311
312 ppnum_t phys_page;
313 };
314 typedef struct vm_page_with_ppnum *vm_page_with_ppnum_t;
315
316
317 static inline ppnum_t VM_PAGE_GET_PHYS_PAGE(vm_page_t m)
318 {
319 if (m >= vm_page_array_beginning_addr && m < vm_page_array_ending_addr)
320 return ((ppnum_t)((uintptr_t)(m - vm_page_array_beginning_addr) + vm_first_phys_ppnum));
321 else
322 return (((vm_page_with_ppnum_t)m)->phys_page);
323 }
324
325 #define VM_PAGE_SET_PHYS_PAGE(m, ppnum) \
326 MACRO_BEGIN \
327 if ((m) < vm_page_array_beginning_addr || (m) >= vm_page_array_ending_addr) \
328 ((vm_page_with_ppnum_t)(m))->phys_page = ppnum; \
329 assert(ppnum == VM_PAGE_GET_PHYS_PAGE(m)); \
330 MACRO_END
331
332 #define VM_PAGE_GET_COLOR(m) (VM_PAGE_GET_PHYS_PAGE(m) & vm_color_mask)
333
334 #else /* defined(__arm__) || defined(__arm64__) */
335
336
337 struct vm_page_with_ppnum {
338 struct vm_page vm_page_with_ppnum;
339 };
340 typedef struct vm_page_with_ppnum *vm_page_with_ppnum_t;
341
342
343 #define VM_PAGE_GET_PHYS_PAGE(page) (page)->phys_page
344 #define VM_PAGE_SET_PHYS_PAGE(page, ppnum) \
345 MACRO_BEGIN \
346 (page)->phys_page = ppnum; \
347 MACRO_END
348
349 #define VM_PAGE_GET_CLUMP(m) ((VM_PAGE_GET_PHYS_PAGE(m)) >> vm_clump_shift)
350 #define VM_PAGE_GET_COLOR(m) ((VM_PAGE_GET_CLUMP(m)) & vm_color_mask)
351
352 #endif /* defined(__arm__) || defined(__arm64__) */
353
354
355
356 #if defined(__LP64__)
357
358 #define VM_VPLQ_ALIGNMENT 128
359 #define VM_PACKED_POINTER_ALIGNMENT 64 /* must be a power of 2 */
360 #define VM_PACKED_POINTER_SHIFT 6
361
362 #define VM_PACKED_FROM_VM_PAGES_ARRAY 0x80000000
363
364 static inline vm_page_packed_t vm_page_pack_ptr(uintptr_t p)
365 {
366 vm_page_packed_t packed_ptr;
367
368 if (!p)
369 return ((vm_page_packed_t)0);
370
371 if (p >= (uintptr_t)(vm_page_array_beginning_addr) && p < (uintptr_t)(vm_page_array_ending_addr)) {
372 packed_ptr = ((vm_page_packed_t)(((vm_page_t)p - vm_page_array_beginning_addr)));
373 assert(! (packed_ptr & VM_PACKED_FROM_VM_PAGES_ARRAY));
374 packed_ptr |= VM_PACKED_FROM_VM_PAGES_ARRAY;
375 return packed_ptr;
376 }
377
378 assert((p & (VM_PACKED_POINTER_ALIGNMENT - 1)) == 0);
379
380 packed_ptr = ((vm_page_packed_t)(((uintptr_t)(p - (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS)) >> VM_PACKED_POINTER_SHIFT));
381 assert(packed_ptr != 0);
382 assert(! (packed_ptr & VM_PACKED_FROM_VM_PAGES_ARRAY));
383 return packed_ptr;
384 }
385
386
387 static inline uintptr_t vm_page_unpack_ptr(uintptr_t p)
388 {
389 if (!p)
390 return ((uintptr_t)0);
391
392 if (p & VM_PACKED_FROM_VM_PAGES_ARRAY)
393 return ((uintptr_t)(&vm_pages[(uint32_t)(p & ~VM_PACKED_FROM_VM_PAGES_ARRAY)]));
394 return (((p << VM_PACKED_POINTER_SHIFT) + (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS));
395 }
396
397
398 #define VM_PAGE_PACK_PTR(p) vm_page_pack_ptr((uintptr_t)(p))
399 #define VM_PAGE_UNPACK_PTR(p) vm_page_unpack_ptr((uintptr_t)(p))
400
401 #define VM_PAGE_OBJECT(p) ((vm_object_t)(VM_PAGE_UNPACK_PTR(p->vm_page_object)))
402 #define VM_PAGE_PACK_OBJECT(o) ((vm_page_object_t)(VM_PAGE_PACK_PTR(o)))
403
404
405 #define VM_PAGE_ZERO_PAGEQ_ENTRY(p) \
406 MACRO_BEGIN \
407 (p)->snext = 0; \
408 MACRO_END
409
410
411 #define VM_PAGE_CONVERT_TO_QUEUE_ENTRY(p) VM_PAGE_PACK_PTR(p)
412
413
414 static __inline__ void
415 vm_page_enqueue_tail(
416 vm_page_queue_t que,
417 vm_page_queue_entry_t elt)
418 {
419 vm_page_queue_entry_t old_tail;
420
421 old_tail = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(que->prev);
422 elt->next = VM_PAGE_PACK_PTR(que);
423 elt->prev = que->prev;
424 old_tail->next = VM_PAGE_PACK_PTR(elt);
425 que->prev = VM_PAGE_PACK_PTR(elt);
426 }
427
428
429 static __inline__ void
430 vm_page_remque(
431 vm_page_queue_entry_t elt)
432 {
433 vm_page_queue_entry_t next_elt, prev_elt;
434
435 next_elt = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(elt->next);
436
437 /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
438 prev_elt = (vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR(elt->prev);
439
440 next_elt->prev = VM_PAGE_PACK_PTR(prev_elt);
441 prev_elt->next = VM_PAGE_PACK_PTR(next_elt);
442
443 elt->next = 0;
444 elt->prev = 0;
445 }
446
447
448 /*
449 * Macro: vm_page_queue_init
450 * Function:
451 * Initialize the given queue.
452 * Header:
453 * void vm_page_queue_init(q)
454 * vm_page_queue_t q; \* MODIFIED *\
455 */
456 #define vm_page_queue_init(q) \
457 MACRO_BEGIN \
458 assert((((uintptr_t)q) & (VM_PACKED_POINTER_ALIGNMENT-1)) == 0); \
459 assert((VM_PAGE_UNPACK_PTR(VM_PAGE_PACK_PTR((uintptr_t)q))) == (uintptr_t)q); \
460 (q)->next = VM_PAGE_PACK_PTR(q); \
461 (q)->prev = VM_PAGE_PACK_PTR(q); \
462 MACRO_END
463
464
465 /*
466 * Macro: vm_page_queue_enter
467 * Function:
468 * Insert a new element at the tail of the queue.
469 * Header:
470 * void vm_page_queue_enter(q, elt, type, field)
471 * queue_t q;
472 * <type> elt;
473 * <type> is what's in our queue
474 * <field> is the chain field in (*<type>)
475 * Note:
476 * This should only be used with Method 2 queue iteration (element chains)
477 */
478 #define vm_page_queue_enter(head, elt, type, field) \
479 MACRO_BEGIN \
480 vm_page_queue_entry_t __prev; \
481 \
482 __prev = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((head)->prev)); \
483 if ((head) == __prev) { \
484 (head)->next = VM_PAGE_PACK_PTR(elt); \
485 } \
486 else { \
487 ((type)(void *)__prev)->field.next = VM_PAGE_PACK_PTR(elt); \
488 } \
489 (elt)->field.prev = VM_PAGE_PACK_PTR(__prev); \
490 (elt)->field.next = VM_PAGE_PACK_PTR(head); \
491 (head)->prev = VM_PAGE_PACK_PTR(elt); \
492 MACRO_END
493
494
495 /*
496 * These are helper macros for vm_page_queue_enter_clump to assist
497 * with conditional compilation (release / debug / development)
498 */
499 #if DEVELOPMENT || DEBUG
500
501 #define __DEBUG_CHECK_BUDDIES(__check, __prev, __p, field) \
502 MACRO_BEGIN \
503 if(__check) { /* if first forward buddy.. */ \
504 if(__prev) { /* ..and if a backward buddy was found, verify link consistency */ \
505 assert(__p == (vm_page_t) VM_PAGE_UNPACK_PTR(__prev->next)); \
506 assert(__prev == (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__p->field.prev)); \
507 } \
508 __check=0; \
509 } \
510 MACRO_END
511
512 #define __DEBUG_VERIFY_LINKS(__i, __first, __n_free, __last_next) \
513 MACRO_BEGIN \
514 vm_page_queue_entry_t __tmp; \
515 for(__i=0, __tmp=__first; __i<__n_free; __i++) \
516 __tmp=(vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__tmp->next); \
517 assert(__tmp == __last_next); \
518 MACRO_END
519
520 #define __DEBUG_STAT_INCREMENT_INRANGE vm_clump_inrange++
521 #define __DEBUG_STAT_INCREMENT_INSERTS vm_clump_inserts++
522 #define __DEBUG_STAT_INCREMENT_PROMOTES(__n_free) vm_clump_promotes+=__n_free
523
524 #else
525
526 #define __DEBUG_CHECK_BUDDIES(__check, __prev, __p, field) __check=1
527 #define __DEBUG_VERIFY_LINKS(__i, __first, __n_free, __last_next)
528 #define __DEBUG_STAT_INCREMENT_INRANGE
529 #define __DEBUG_STAT_INCREMENT_INSERTS
530 #define __DEBUG_STAT_INCREMENT_PROMOTES(__n_free)
531
532 #endif /* if DEVELOPMENT || DEBUG */
533
534 /*
535 * Macro: vm_page_queue_enter_clump
536 * Function:
537 * Insert a new element into the free queue and clump pages within the same 16K boundary together
538 *
539 * Header:
540 * void vm_page_queue_enter_clump(q, elt, type, field)
541 * queue_t q;
542 * <type> elt;
543 * <type> is what's in our queue
544 * <field> is the chain field in (*<type>)
545 * Note:
546 * This should only be used with Method 2 queue iteration (element chains)
547 */
548 #if defined(__x86_64__)
549 #define vm_page_queue_enter_clump(head, elt, type, field) \
550 MACRO_BEGIN \
551 ppnum_t __clump_num; \
552 unsigned int __i, __n, __n_free=1, __check=1; \
553 vm_page_queue_entry_t __prev=0, __next, __last, __last_next, __first, __first_prev, __head_next; \
554 vm_page_t __p; \
555 \
556 /* if elt is part of vm_pages[] */ \
557 if((elt) >= vm_page_array_beginning_addr && (elt) < vm_page_array_boundary) { \
558 __first = __last = (vm_page_queue_entry_t) (elt); \
559 __clump_num = VM_PAGE_GET_CLUMP(elt); \
560 __n = VM_PAGE_GET_PHYS_PAGE(elt) & vm_clump_mask; \
561 /* scan backward looking for a buddy page */ \
562 for(__i=0, __p=(elt)-1; __i<__n && __p>=vm_page_array_beginning_addr; __i++, __p--) { \
563 if(__p->vm_page_q_state == VM_PAGE_ON_FREE_Q && __clump_num == VM_PAGE_GET_CLUMP(__p)) { \
564 if(__prev == 0) __prev = (vm_page_queue_entry_t) __p; \
565 __first = (vm_page_queue_entry_t) __p; \
566 __n_free++; \
567 } \
568 } \
569 /* scan forward looking for a buddy page */ \
570 for(__i=__n+1, __p=(elt)+1; __i<vm_clump_size && __p<vm_page_array_boundary; __i++, __p++) { \
571 if(__p->vm_page_q_state == VM_PAGE_ON_FREE_Q && __clump_num == VM_PAGE_GET_CLUMP(__p)) { \
572 __DEBUG_CHECK_BUDDIES(__check, __prev, __p, field); \
573 if(__prev == 0) __prev = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__p->field.prev); \
574 __last = (vm_page_queue_entry_t) __p; \
575 __n_free++; \
576 } \
577 } \
578 __DEBUG_STAT_INCREMENT_INRANGE; \
579 } \
580 /* if elt is not part of vm_pages or if 1st page in clump, insert at tail */ \
581 if(__prev == 0) __prev = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR((head)->prev); \
582 \
583 /* insert the element */ \
584 __next = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__prev->next); \
585 (elt)->field.next = __prev->next; \
586 (elt)->field.prev = __next->prev; \
587 __prev->next = __next->prev = VM_PAGE_PACK_PTR(elt); \
588 __DEBUG_STAT_INCREMENT_INSERTS; \
589 \
590 /* check if clump needs to be promoted to head */ \
591 if(__n_free >= vm_clump_promote_threshold && __n_free > 1) { \
592 __first_prev = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__first->prev); \
593 if(__first_prev != (head)) { /* if not at head already */ \
594 __last_next = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR(__last->next); \
595 /* verify that the links within the clump are consistent */ \
596 __DEBUG_VERIFY_LINKS(__i, __first, __n_free, __last_next); \
597 /* promote clump to head */ \
598 __first_prev->next = __last->next; \
599 __last_next->prev = __first->prev; \
600 __first->prev = VM_PAGE_PACK_PTR(head); \
601 __last->next = (head)->next; \
602 __head_next = (vm_page_queue_entry_t) VM_PAGE_UNPACK_PTR((head)->next); \
603 __head_next->prev = VM_PAGE_PACK_PTR(__last); \
604 (head)->next = VM_PAGE_PACK_PTR(__first); \
605 __DEBUG_STAT_INCREMENT_PROMOTES(__n_free); \
606 } \
607 } \
608 MACRO_END
609 #endif
610
611 /*
612 * Macro: vm_page_queue_enter_first
613 * Function:
614 * Insert a new element at the head of the queue.
615 * Header:
616 * void queue_enter_first(q, elt, type, field)
617 * queue_t q;
618 * <type> elt;
619 * <type> is what's in our queue
620 * <field> is the chain field in (*<type>)
621 * Note:
622 * This should only be used with Method 2 queue iteration (element chains)
623 */
624 #define vm_page_queue_enter_first(head, elt, type, field) \
625 MACRO_BEGIN \
626 vm_page_queue_entry_t __next; \
627 \
628 __next = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((head)->next)); \
629 if ((head) == __next) { \
630 (head)->prev = VM_PAGE_PACK_PTR(elt); \
631 } \
632 else { \
633 ((type)(void *)__next)->field.prev = VM_PAGE_PACK_PTR(elt); \
634 } \
635 (elt)->field.next = VM_PAGE_PACK_PTR(__next); \
636 (elt)->field.prev = VM_PAGE_PACK_PTR(head); \
637 (head)->next = VM_PAGE_PACK_PTR(elt); \
638 MACRO_END
639
640
641 /*
642 * Macro: vm_page_queue_remove
643 * Function:
644 * Remove an arbitrary item from the queue.
645 * Header:
646 * void vm_page_queue_remove(q, qe, type, field)
647 * arguments as in vm_page_queue_enter
648 * Note:
649 * This should only be used with Method 2 queue iteration (element chains)
650 */
651 #define vm_page_queue_remove(head, elt, type, field) \
652 MACRO_BEGIN \
653 vm_page_queue_entry_t __next, __prev; \
654 \
655 __next = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((elt)->field.next)); \
656 __prev = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((elt)->field.prev)); \
657 \
658 if ((head) == __next) \
659 (head)->prev = VM_PAGE_PACK_PTR(__prev); \
660 else \
661 ((type)(void *)__next)->field.prev = VM_PAGE_PACK_PTR(__prev); \
662 \
663 if ((head) == __prev) \
664 (head)->next = VM_PAGE_PACK_PTR(__next); \
665 else \
666 ((type)(void *)__prev)->field.next = VM_PAGE_PACK_PTR(__next); \
667 \
668 (elt)->field.next = 0; \
669 (elt)->field.prev = 0; \
670 MACRO_END
671
672
673 /*
674 * Macro: vm_page_queue_remove_first
675 * Function:
676 * Remove and return the entry at the head of
677 * the queue.
678 * Header:
679 * vm_page_queue_remove_first(head, entry, type, field)
680 * entry is returned by reference
681 * Note:
682 * This should only be used with Method 2 queue iteration (element chains)
683 */
684 #define vm_page_queue_remove_first(head, entry, type, field) \
685 MACRO_BEGIN \
686 vm_page_queue_entry_t __next; \
687 \
688 (entry) = (type)(void *) VM_PAGE_UNPACK_PTR(((head)->next)); \
689 __next = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((entry)->field.next)); \
690 \
691 if ((head) == __next) \
692 (head)->prev = VM_PAGE_PACK_PTR(head); \
693 else \
694 ((type)(void *)(__next))->field.prev = VM_PAGE_PACK_PTR(head); \
695 (head)->next = VM_PAGE_PACK_PTR(__next); \
696 \
697 (entry)->field.next = 0; \
698 (entry)->field.prev = 0; \
699 MACRO_END
700
701
702 /*
703 * Macro: vm_page_queue_remove_first_with_clump
704 * Function:
705 * Remove and return the entry at the head of the free queue
706 * end is set to 1 to indicate that we just returned the last page in a clump
707 *
708 * Header:
709 * vm_page_queue_remove_first_with_clump(head, entry, type, field, end)
710 * entry is returned by reference
711 * end is returned by reference
712 * Note:
713 * This should only be used with Method 2 queue iteration (element chains)
714 */
715 #if defined(__x86_64__)
716 #define vm_page_queue_remove_first_with_clump(head, entry, type, field, end) \
717 MACRO_BEGIN \
718 vm_page_queue_entry_t __next; \
719 \
720 (entry) = (type)(void *) VM_PAGE_UNPACK_PTR(((head)->next)); \
721 __next = ((vm_page_queue_entry_t)VM_PAGE_UNPACK_PTR((entry)->field.next)); \
722 \
723 (end)=0; \
724 if ((head) == __next) { \
725 (head)->prev = VM_PAGE_PACK_PTR(head); \
726 (end)=1; \
727 } \
728 else { \
729 ((type)(void *)(__next))->field.prev = VM_PAGE_PACK_PTR(head); \
730 if(VM_PAGE_GET_CLUMP(entry) != VM_PAGE_GET_CLUMP(((type)(void *)(__next)))) (end)=1; \
731 } \
732 (head)->next = VM_PAGE_PACK_PTR(__next); \
733 \
734 (entry)->field.next = 0; \
735 (entry)->field.prev = 0; \
736 \
737 MACRO_END
738 #endif
739
740 /*
741 * Macro: vm_page_queue_end
742 * Function:
743 * Tests whether a new entry is really the end of
744 * the queue.
745 * Header:
746 * boolean_t vm_page_queue_end(q, qe)
747 * vm_page_queue_t q;
748 * vm_page_queue_entry_t qe;
749 */
750 #define vm_page_queue_end(q, qe) ((q) == (qe))
751
752
753 /*
754 * Macro: vm_page_queue_empty
755 * Function:
756 * Tests whether a queue is empty.
757 * Header:
758 * boolean_t vm_page_queue_empty(q)
759 * vm_page_queue_t q;
760 */
761 #define vm_page_queue_empty(q) vm_page_queue_end((q), ((vm_page_queue_entry_t)vm_page_queue_first(q)))
762
763
764
765 /*
766 * Macro: vm_page_queue_first
767 * Function:
768 * Returns the first entry in the queue,
769 * Header:
770 * uintpr_t vm_page_queue_first(q)
771 * vm_page_queue_t q; \* IN *\
772 */
773 #define vm_page_queue_first(q) (VM_PAGE_UNPACK_PTR((q)->next))
774
775
776
777 /*
778 * Macro: vm_page_queue_last
779 * Function:
780 * Returns the last entry in the queue.
781 * Header:
782 * vm_page_queue_entry_t queue_last(q)
783 * queue_t q; \* IN *\
784 */
785 #define vm_page_queue_last(q) (VM_PAGE_UNPACK_PTR((q)->prev))
786
787
788
789 /*
790 * Macro: vm_page_queue_next
791 * Function:
792 * Returns the entry after an item in the queue.
793 * Header:
794 * uintpr_t vm_page_queue_next(qc)
795 * vm_page_queue_t qc;
796 */
797 #define vm_page_queue_next(qc) (VM_PAGE_UNPACK_PTR((qc)->next))
798
799
800
801 /*
802 * Macro: vm_page_queue_prev
803 * Function:
804 * Returns the entry before an item in the queue.
805 * Header:
806 * uinptr_t vm_page_queue_prev(qc)
807 * vm_page_queue_t qc;
808 */
809 #define vm_page_queue_prev(qc) (VM_PAGE_UNPACK_PTR((qc)->prev))
810
811
812
813 /*
814 * Macro: vm_page_queue_iterate
815 * Function:
816 * iterate over each item in the queue.
817 * Generates a 'for' loop, setting elt to
818 * each item in turn (by reference).
819 * Header:
820 * vm_page_queue_iterate(q, elt, type, field)
821 * queue_t q;
822 * <type> elt;
823 * <type> is what's in our queue
824 * <field> is the chain field in (*<type>)
825 * Note:
826 * This should only be used with Method 2 queue iteration (element chains)
827 */
828 #define vm_page_queue_iterate(head, elt, type, field) \
829 for ((elt) = (type)(void *) vm_page_queue_first(head); \
830 !vm_page_queue_end((head), (vm_page_queue_entry_t)(elt)); \
831 (elt) = (type)(void *) vm_page_queue_next(&(elt)->field))
832
833 #else
834
835 #define VM_VPLQ_ALIGNMENT 128
836 #define VM_PACKED_POINTER_ALIGNMENT 4
837 #define VM_PACKED_POINTER_SHIFT 0
838
839 #define VM_PACKED_FROM_VM_PAGES_ARRAY 0
840
841 #define VM_PAGE_PACK_PTR(p) (p)
842 #define VM_PAGE_UNPACK_PTR(p) ((uintptr_t)(p))
843
844 #define VM_PAGE_OBJECT(p) (vm_object_t)(p->vm_page_object)
845 #define VM_PAGE_PACK_OBJECT(o) ((vm_page_object_t)(VM_PAGE_PACK_PTR(o)))
846
847
848 #define VM_PAGE_ZERO_PAGEQ_ENTRY(p) \
849 MACRO_BEGIN \
850 (p)->pageq.next = 0; \
851 (p)->pageq.prev = 0; \
852 MACRO_END
853
854 #define VM_PAGE_CONVERT_TO_QUEUE_ENTRY(p) ((queue_entry_t)(p))
855
856 #define vm_page_remque remque
857 #define vm_page_enqueue_tail enqueue_tail
858 #define vm_page_queue_init queue_init
859 #define vm_page_queue_enter queue_enter
860 #define vm_page_queue_enter_first queue_enter_first
861 #define vm_page_queue_remove queue_remove
862 #define vm_page_queue_remove_first queue_remove_first
863 #define vm_page_queue_end queue_end
864 #define vm_page_queue_empty queue_empty
865 #define vm_page_queue_first queue_first
866 #define vm_page_queue_last queue_last
867 #define vm_page_queue_next queue_next
868 #define vm_page_queue_prev queue_prev
869 #define vm_page_queue_iterate queue_iterate
870
871 #endif
872
873
874
875 /*
876 * VM_PAGE_MIN_SPECULATIVE_AGE_Q through VM_PAGE_MAX_SPECULATIVE_AGE_Q
877 * represents a set of aging bins that are 'protected'...
878 *
879 * VM_PAGE_SPECULATIVE_AGED_Q is a list of the speculative pages that have
880 * not yet been 'claimed' but have been aged out of the protective bins
881 * this occurs in vm_page_speculate when it advances to the next bin
882 * and discovers that it is still occupied... at that point, all of the
883 * pages in that bin are moved to the VM_PAGE_SPECULATIVE_AGED_Q. the pages
884 * in that bin are all guaranteed to have reached at least the maximum age
885 * we allow for a protected page... they can be older if there is no
886 * memory pressure to pull them from the bin, or there are no new speculative pages
887 * being generated to push them out.
888 * this list is the one that vm_pageout_scan will prefer when looking
889 * for pages to move to the underweight free list
890 *
891 * VM_PAGE_MAX_SPECULATIVE_AGE_Q * VM_PAGE_SPECULATIVE_Q_AGE_MS
892 * defines the amount of time a speculative page is normally
893 * allowed to live in the 'protected' state (i.e. not available
894 * to be stolen if vm_pageout_scan is running and looking for
895 * pages)... however, if the total number of speculative pages
896 * in the protected state exceeds our limit (defined in vm_pageout.c)
897 * and there are none available in VM_PAGE_SPECULATIVE_AGED_Q, then
898 * vm_pageout_scan is allowed to steal pages from the protected
899 * bucket even if they are underage.
900 *
901 * vm_pageout_scan is also allowed to pull pages from a protected
902 * bin if the bin has reached the "age of consent" we've set
903 */
904 #define VM_PAGE_MAX_SPECULATIVE_AGE_Q 10
905 #define VM_PAGE_MIN_SPECULATIVE_AGE_Q 1
906 #define VM_PAGE_SPECULATIVE_AGED_Q 0
907
908 #define VM_PAGE_SPECULATIVE_Q_AGE_MS 500
909
910 struct vm_speculative_age_q {
911 /*
912 * memory queue for speculative pages via clustered pageins
913 */
914 vm_page_queue_head_t age_q;
915 mach_timespec_t age_ts;
916 } __attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT)));
917
918
919
920 extern
921 struct vm_speculative_age_q vm_page_queue_speculative[];
922
923 extern int speculative_steal_index;
924 extern int speculative_age_index;
925 extern unsigned int vm_page_speculative_q_age_ms;
926
927
928 typedef struct vm_locks_array {
929 char pad __attribute__ ((aligned (64)));
930 lck_mtx_t vm_page_queue_lock2 __attribute__ ((aligned (64)));
931 lck_mtx_t vm_page_queue_free_lock2 __attribute__ ((aligned (64)));
932 char pad2 __attribute__ ((aligned (64)));
933 } vm_locks_array_t;
934
935
936 #if CONFIG_BACKGROUND_QUEUE
937 extern void vm_page_assign_background_state(vm_page_t mem);
938 extern void vm_page_update_background_state(vm_page_t mem);
939 extern void vm_page_add_to_backgroundq(vm_page_t mem, boolean_t first);
940 extern void vm_page_remove_from_backgroundq(vm_page_t mem);
941 #endif
942
943 #define VM_PAGE_WIRED(m) ((m)->vm_page_q_state == VM_PAGE_IS_WIRED)
944 #define NEXT_PAGE(m) ((m)->snext)
945 #define NEXT_PAGE_PTR(m) (&(m)->snext)
946
947 /*
948 * XXX The unusual bit should not be necessary. Most of the bit
949 * XXX fields above really want to be masks.
950 */
951
952 /*
953 * For debugging, this macro can be defined to perform
954 * some useful check on a page structure.
955 * INTENTIONALLY left as a no-op so that the
956 * current call-sites can be left intact for future uses.
957 */
958
959 #define VM_PAGE_CHECK(mem) \
960 MACRO_BEGIN \
961 MACRO_END
962
963 /* Page coloring:
964 *
965 * The free page list is actually n lists, one per color,
966 * where the number of colors is a function of the machine's
967 * cache geometry set at system initialization. To disable
968 * coloring, set vm_colors to 1 and vm_color_mask to 0.
969 * The boot-arg "colors" may be used to override vm_colors.
970 * Note that there is little harm in having more colors than needed.
971 */
972
973 #define MAX_COLORS 128
974 #define DEFAULT_COLORS 32
975
976 extern
977 unsigned int vm_colors; /* must be in range 1..MAX_COLORS */
978 extern
979 unsigned int vm_color_mask; /* must be (vm_colors-1) */
980 extern
981 unsigned int vm_cache_geometry_colors; /* optimal #colors based on cache geometry */
982
983 /*
984 * Wired memory is a very limited resource and we can't let users exhaust it
985 * and deadlock the entire system. We enforce the following limits:
986 *
987 * vm_user_wire_limit (default: all memory minus vm_global_no_user_wire_amount)
988 * how much memory can be user-wired in one user task
989 *
990 * vm_global_user_wire_limit (default: same as vm_user_wire_limit)
991 * how much memory can be user-wired in all user tasks
992 *
993 * vm_global_no_user_wire_amount (default: VM_NOT_USER_WIREABLE)
994 * how much memory must remain user-unwired at any time
995 */
996 #define VM_NOT_USER_WIREABLE (64*1024*1024) /* 64MB */
997 extern
998 vm_map_size_t vm_user_wire_limit;
999 extern
1000 vm_map_size_t vm_global_user_wire_limit;
1001 extern
1002 vm_map_size_t vm_global_no_user_wire_amount;
1003
1004 /*
1005 * Each pageable resident page falls into one of three lists:
1006 *
1007 * free
1008 * Available for allocation now. The free list is
1009 * actually an array of lists, one per color.
1010 * inactive
1011 * Not referenced in any map, but still has an
1012 * object/offset-page mapping, and may be dirty.
1013 * This is the list of pages that should be
1014 * paged out next. There are actually two
1015 * inactive lists, one for pages brought in from
1016 * disk or other backing store, and another
1017 * for "zero-filled" pages. See vm_pageout_scan()
1018 * for the distinction and usage.
1019 * active
1020 * A list of pages which have been placed in
1021 * at least one physical map. This list is
1022 * ordered, in LRU-like fashion.
1023 */
1024
1025
1026 #define VPL_LOCK_SPIN 1
1027
1028 struct vpl {
1029 vm_page_queue_head_t vpl_queue;
1030 unsigned int vpl_count;
1031 unsigned int vpl_internal_count;
1032 unsigned int vpl_external_count;
1033 #ifdef VPL_LOCK_SPIN
1034 lck_spin_t vpl_lock;
1035 #else
1036 lck_mtx_t vpl_lock;
1037 lck_mtx_ext_t vpl_lock_ext;
1038 #endif
1039 };
1040
1041 struct vplq {
1042 union {
1043 char cache_line_pad[VM_VPLQ_ALIGNMENT];
1044 struct vpl vpl;
1045 } vpl_un;
1046 };
1047 extern
1048 unsigned int vm_page_local_q_count;
1049 extern
1050 struct vplq *vm_page_local_q;
1051 extern
1052 unsigned int vm_page_local_q_soft_limit;
1053 extern
1054 unsigned int vm_page_local_q_hard_limit;
1055 extern
1056 vm_locks_array_t vm_page_locks;
1057
1058 extern
1059 vm_page_queue_head_t vm_lopage_queue_free; /* low memory free queue */
1060 extern
1061 vm_page_queue_head_t vm_page_queue_active; /* active memory queue */
1062 extern
1063 vm_page_queue_head_t vm_page_queue_inactive; /* inactive memory queue for normal pages */
1064 #if CONFIG_SECLUDED_MEMORY
1065 extern
1066 vm_page_queue_head_t vm_page_queue_secluded; /* reclaimable pages secluded for Camera */
1067 #endif /* CONFIG_SECLUDED_MEMORY */
1068 extern
1069 vm_page_queue_head_t vm_page_queue_cleaned; /* clean-queue inactive memory */
1070 extern
1071 vm_page_queue_head_t vm_page_queue_anonymous; /* inactive memory queue for anonymous pages */
1072 extern
1073 vm_page_queue_head_t vm_page_queue_throttled; /* memory queue for throttled pageout pages */
1074
1075 extern
1076 queue_head_t vm_objects_wired;
1077 extern
1078 lck_spin_t vm_objects_wired_lock;
1079
1080 #if CONFIG_BACKGROUND_QUEUE
1081
1082 #define VM_PAGE_BACKGROUND_TARGET_MAX 50000
1083
1084 #define VM_PAGE_BG_DISABLED 0
1085 #define VM_PAGE_BG_LEVEL_1 1
1086
1087 extern
1088 vm_page_queue_head_t vm_page_queue_background;
1089 extern
1090 uint64_t vm_page_background_promoted_count;
1091 extern
1092 uint32_t vm_page_background_count;
1093 extern
1094 uint32_t vm_page_background_target;
1095 extern
1096 uint32_t vm_page_background_internal_count;
1097 extern
1098 uint32_t vm_page_background_external_count;
1099 extern
1100 uint32_t vm_page_background_mode;
1101 extern
1102 uint32_t vm_page_background_exclude_external;
1103
1104 #endif
1105
1106 extern
1107 vm_offset_t first_phys_addr; /* physical address for first_page */
1108 extern
1109 vm_offset_t last_phys_addr; /* physical address for last_page */
1110
1111 extern
1112 unsigned int vm_page_free_count; /* How many pages are free? (sum of all colors) */
1113 extern
1114 unsigned int vm_page_active_count; /* How many pages are active? */
1115 extern
1116 unsigned int vm_page_inactive_count; /* How many pages are inactive? */
1117 #if CONFIG_SECLUDED_MEMORY
1118 extern
1119 unsigned int vm_page_secluded_count; /* How many pages are secluded? */
1120 extern
1121 unsigned int vm_page_secluded_count_free;
1122 extern
1123 unsigned int vm_page_secluded_count_inuse;
1124 #endif /* CONFIG_SECLUDED_MEMORY */
1125 extern
1126 unsigned int vm_page_cleaned_count; /* How many pages are in the clean queue? */
1127 extern
1128 unsigned int vm_page_throttled_count;/* How many inactives are throttled */
1129 extern
1130 unsigned int vm_page_speculative_count; /* How many speculative pages are unclaimed? */
1131 extern unsigned int vm_page_pageable_internal_count;
1132 extern unsigned int vm_page_pageable_external_count;
1133 extern
1134 unsigned int vm_page_xpmapped_external_count; /* How many pages are mapped executable? */
1135 extern
1136 unsigned int vm_page_external_count; /* How many pages are file-backed? */
1137 extern
1138 unsigned int vm_page_internal_count; /* How many pages are anonymous? */
1139 extern
1140 unsigned int vm_page_wire_count; /* How many pages are wired? */
1141 extern
1142 unsigned int vm_page_wire_count_initial; /* How many pages wired at startup */
1143 extern
1144 unsigned int vm_page_free_target; /* How many do we want free? */
1145 extern
1146 unsigned int vm_page_free_min; /* When to wakeup pageout */
1147 extern
1148 unsigned int vm_page_throttle_limit; /* When to throttle new page creation */
1149 extern
1150 uint32_t vm_page_creation_throttle; /* When to throttle new page creation */
1151 extern
1152 unsigned int vm_page_inactive_target;/* How many do we want inactive? */
1153 #if CONFIG_SECLUDED_MEMORY
1154 extern
1155 unsigned int vm_page_secluded_target;/* How many do we want secluded? */
1156 #endif /* CONFIG_SECLUDED_MEMORY */
1157 extern
1158 unsigned int vm_page_anonymous_min; /* When it's ok to pre-clean */
1159 extern
1160 unsigned int vm_page_inactive_min; /* When to wakeup pageout */
1161 extern
1162 unsigned int vm_page_free_reserved; /* How many pages reserved to do pageout */
1163 extern
1164 unsigned int vm_page_throttle_count; /* Count of page allocations throttled */
1165 extern
1166 unsigned int vm_page_gobble_count;
1167 extern
1168 unsigned int vm_page_stolen_count; /* Count of stolen pages not acccounted in zones */
1169
1170
1171 #if DEVELOPMENT || DEBUG
1172 extern
1173 unsigned int vm_page_speculative_used;
1174 #endif
1175
1176 extern
1177 unsigned int vm_page_purgeable_count;/* How many pages are purgeable now ? */
1178 extern
1179 unsigned int vm_page_purgeable_wired_count;/* How many purgeable pages are wired now ? */
1180 extern
1181 uint64_t vm_page_purged_count; /* How many pages got purged so far ? */
1182
1183 extern unsigned int vm_page_free_wanted;
1184 /* how many threads are waiting for memory */
1185
1186 extern unsigned int vm_page_free_wanted_privileged;
1187 /* how many VM privileged threads are waiting for memory */
1188 #if CONFIG_SECLUDED_MEMORY
1189 extern unsigned int vm_page_free_wanted_secluded;
1190 /* how many threads are waiting for secluded memory */
1191 #endif /* CONFIG_SECLUDED_MEMORY */
1192
1193 extern const ppnum_t vm_page_fictitious_addr;
1194 /* (fake) phys_addr of fictitious pages */
1195
1196 extern const ppnum_t vm_page_guard_addr;
1197 /* (fake) phys_addr of guard pages */
1198
1199
1200 extern boolean_t vm_page_deactivate_hint;
1201
1202 extern int vm_compressor_mode;
1203
1204 /*
1205 0 = all pages avail ( default. )
1206 1 = disable high mem ( cap max pages to 4G)
1207 2 = prefer himem
1208 */
1209 extern int vm_himemory_mode;
1210
1211 extern boolean_t vm_lopage_needed;
1212 extern uint32_t vm_lopage_free_count;
1213 extern uint32_t vm_lopage_free_limit;
1214 extern uint32_t vm_lopage_lowater;
1215 extern boolean_t vm_lopage_refill;
1216 extern uint64_t max_valid_dma_address;
1217 extern ppnum_t max_valid_low_ppnum;
1218
1219 /*
1220 * Prototypes for functions exported by this module.
1221 */
1222 extern void vm_page_bootstrap(
1223 vm_offset_t *startp,
1224 vm_offset_t *endp);
1225
1226 extern void vm_page_module_init(void);
1227
1228 extern void vm_page_init_local_q(void);
1229
1230 extern void vm_page_create(
1231 ppnum_t start,
1232 ppnum_t end);
1233
1234 extern vm_page_t kdp_vm_page_lookup(
1235 vm_object_t object,
1236 vm_object_offset_t offset);
1237
1238 extern vm_page_t vm_page_lookup(
1239 vm_object_t object,
1240 vm_object_offset_t offset);
1241
1242 extern vm_page_t vm_page_grab_fictitious(void);
1243
1244 extern vm_page_t vm_page_grab_guard(void);
1245
1246 extern void vm_page_release_fictitious(
1247 vm_page_t page);
1248
1249 extern void vm_page_more_fictitious(void);
1250
1251 extern int vm_pool_low(void);
1252
1253 extern vm_page_t vm_page_grab(void);
1254 extern vm_page_t vm_page_grab_options(int flags);
1255 #if CONFIG_SECLUDED_MEMORY
1256 #define VM_PAGE_GRAB_SECLUDED 0x00000001
1257 #endif /* CONFIG_SECLUDED_MEMORY */
1258
1259 extern vm_page_t vm_page_grablo(void);
1260
1261 extern void vm_page_release(
1262 vm_page_t page,
1263 boolean_t page_queues_locked);
1264
1265 extern boolean_t vm_page_wait(
1266 int interruptible );
1267
1268 extern vm_page_t vm_page_alloc(
1269 vm_object_t object,
1270 vm_object_offset_t offset);
1271
1272 extern vm_page_t vm_page_alloc_guard(
1273 vm_object_t object,
1274 vm_object_offset_t offset);
1275
1276 extern void vm_page_init(
1277 vm_page_t page,
1278 ppnum_t phys_page,
1279 boolean_t lopage);
1280
1281 extern void vm_page_free(
1282 vm_page_t page);
1283
1284 extern void vm_page_free_unlocked(
1285 vm_page_t page,
1286 boolean_t remove_from_hash);
1287
1288 extern void vm_page_activate(
1289 vm_page_t page);
1290
1291 extern void vm_page_deactivate(
1292 vm_page_t page);
1293
1294 extern void vm_page_deactivate_internal(
1295 vm_page_t page,
1296 boolean_t clear_hw_reference);
1297
1298 extern void vm_page_enqueue_cleaned(vm_page_t page);
1299
1300 extern void vm_page_lru(
1301 vm_page_t page);
1302
1303 extern void vm_page_speculate(
1304 vm_page_t page,
1305 boolean_t new);
1306
1307 extern void vm_page_speculate_ageit(
1308 struct vm_speculative_age_q *aq);
1309
1310 extern void vm_page_reactivate_all_throttled(void);
1311
1312 extern void vm_page_reactivate_local(uint32_t lid, boolean_t force, boolean_t nolocks);
1313
1314 extern void vm_page_rename(
1315 vm_page_t page,
1316 vm_object_t new_object,
1317 vm_object_offset_t new_offset);
1318
1319 extern void vm_page_insert(
1320 vm_page_t page,
1321 vm_object_t object,
1322 vm_object_offset_t offset);
1323
1324 extern void vm_page_insert_wired(
1325 vm_page_t page,
1326 vm_object_t object,
1327 vm_object_offset_t offset,
1328 vm_tag_t tag);
1329
1330 extern void vm_page_insert_internal(
1331 vm_page_t page,
1332 vm_object_t object,
1333 vm_object_offset_t offset,
1334 vm_tag_t tag,
1335 boolean_t queues_lock_held,
1336 boolean_t insert_in_hash,
1337 boolean_t batch_pmap_op,
1338 boolean_t delayed_accounting,
1339 uint64_t *delayed_ledger_update);
1340
1341 extern void vm_page_replace(
1342 vm_page_t mem,
1343 vm_object_t object,
1344 vm_object_offset_t offset);
1345
1346 extern void vm_page_remove(
1347 vm_page_t page,
1348 boolean_t remove_from_hash);
1349
1350 extern void vm_page_zero_fill(
1351 vm_page_t page);
1352
1353 extern void vm_page_part_zero_fill(
1354 vm_page_t m,
1355 vm_offset_t m_pa,
1356 vm_size_t len);
1357
1358 extern void vm_page_copy(
1359 vm_page_t src_page,
1360 vm_page_t dest_page);
1361
1362 extern void vm_page_part_copy(
1363 vm_page_t src_m,
1364 vm_offset_t src_pa,
1365 vm_page_t dst_m,
1366 vm_offset_t dst_pa,
1367 vm_size_t len);
1368
1369 extern void vm_page_wire(
1370 vm_page_t page,
1371 vm_tag_t tag,
1372 boolean_t check_memorystatus);
1373
1374 extern void vm_page_unwire(
1375 vm_page_t page,
1376 boolean_t queueit);
1377
1378 extern void vm_set_page_size(void);
1379
1380 extern void vm_page_gobble(
1381 vm_page_t page);
1382
1383 extern void vm_page_validate_cs(vm_page_t page);
1384 extern void vm_page_validate_cs_mapped(
1385 vm_page_t page,
1386 const void *kaddr);
1387 extern void vm_page_validate_cs_mapped_chunk(
1388 vm_page_t page,
1389 const void *kaddr,
1390 vm_offset_t chunk_offset,
1391 vm_size_t chunk_size,
1392 boolean_t *validated,
1393 unsigned *tainted);
1394
1395 extern void vm_page_free_prepare_queues(
1396 vm_page_t page);
1397
1398 extern void vm_page_free_prepare_object(
1399 vm_page_t page,
1400 boolean_t remove_from_hash);
1401
1402 #if CONFIG_IOSCHED
1403 extern wait_result_t vm_page_sleep(
1404 vm_object_t object,
1405 vm_page_t m,
1406 int interruptible);
1407 #endif
1408
1409 extern void vm_pressure_response(void);
1410
1411 #if CONFIG_JETSAM
1412 extern void memorystatus_pages_update(unsigned int pages_avail);
1413
1414 #define VM_CHECK_MEMORYSTATUS do { \
1415 memorystatus_pages_update( \
1416 vm_page_pageable_external_count + \
1417 vm_page_free_count + \
1418 (VM_DYNAMIC_PAGING_ENABLED() ? 0 : vm_page_purgeable_count) \
1419 ); \
1420 } while(0)
1421
1422 #else /* CONFIG_JETSAM */
1423
1424 #if CONFIG_EMBEDDED
1425
1426 #define VM_CHECK_MEMORYSTATUS do {} while(0)
1427
1428 #else /* CONFIG_EMBEDDED */
1429
1430 #define VM_CHECK_MEMORYSTATUS vm_pressure_response()
1431
1432 #endif /* CONFIG_EMBEDDED */
1433
1434 #endif /* CONFIG_JETSAM */
1435
1436 /*
1437 * Functions implemented as macros. m->wanted and m->busy are
1438 * protected by the object lock.
1439 */
1440
1441 #if CONFIG_EMBEDDED
1442 #define SET_PAGE_DIRTY(m, set_pmap_modified) \
1443 MACRO_BEGIN \
1444 vm_page_t __page__ = (m); \
1445 if (__page__->dirty == FALSE && (set_pmap_modified)) { \
1446 pmap_set_modify(VM_PAGE_GET_PHYS_PAGE(__page__)); \
1447 } \
1448 __page__->dirty = TRUE; \
1449 MACRO_END
1450 #else /* CONFIG_EMBEDDED */
1451 #define SET_PAGE_DIRTY(m, set_pmap_modified) \
1452 MACRO_BEGIN \
1453 vm_page_t __page__ = (m); \
1454 __page__->dirty = TRUE; \
1455 MACRO_END
1456 #endif /* CONFIG_EMBEDDED */
1457
1458 #define PAGE_ASSERT_WAIT(m, interruptible) \
1459 (((m)->wanted = TRUE), \
1460 assert_wait((event_t) (m), (interruptible)))
1461
1462 #if CONFIG_IOSCHED
1463 #define PAGE_SLEEP(o, m, interruptible) \
1464 vm_page_sleep(o, m, interruptible)
1465 #else
1466 #define PAGE_SLEEP(o, m, interruptible) \
1467 (((m)->wanted = TRUE), \
1468 thread_sleep_vm_object((o), (m), (interruptible)))
1469 #endif
1470
1471 #define PAGE_WAKEUP_DONE(m) \
1472 MACRO_BEGIN \
1473 (m)->busy = FALSE; \
1474 if ((m)->wanted) { \
1475 (m)->wanted = FALSE; \
1476 thread_wakeup((event_t) (m)); \
1477 } \
1478 MACRO_END
1479
1480 #define PAGE_WAKEUP(m) \
1481 MACRO_BEGIN \
1482 if ((m)->wanted) { \
1483 (m)->wanted = FALSE; \
1484 thread_wakeup((event_t) (m)); \
1485 } \
1486 MACRO_END
1487
1488 #define VM_PAGE_FREE(p) \
1489 MACRO_BEGIN \
1490 vm_page_free_unlocked(p, TRUE); \
1491 MACRO_END
1492
1493 #define VM_PAGE_GRAB_FICTITIOUS(M) \
1494 MACRO_BEGIN \
1495 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
1496 vm_page_more_fictitious(); \
1497 MACRO_END
1498
1499 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
1500
1501 #define vm_page_queue_lock (vm_page_locks.vm_page_queue_lock2)
1502 #define vm_page_queue_free_lock (vm_page_locks.vm_page_queue_free_lock2)
1503
1504 #define vm_page_lock_queues() lck_mtx_lock(&vm_page_queue_lock)
1505 #define vm_page_trylock_queues() lck_mtx_try_lock(&vm_page_queue_lock)
1506 #define vm_page_unlock_queues() lck_mtx_unlock(&vm_page_queue_lock)
1507
1508 #define vm_page_lockspin_queues() lck_mtx_lock_spin(&vm_page_queue_lock)
1509 #define vm_page_trylockspin_queues() lck_mtx_try_lock_spin(&vm_page_queue_lock)
1510 #define vm_page_lockconvert_queues() lck_mtx_convert_spin(&vm_page_queue_lock)
1511
1512 #ifdef VPL_LOCK_SPIN
1513 #define VPL_LOCK_INIT(vlq, vpl_grp, vpl_attr) lck_spin_init(&vlq->vpl_lock, vpl_grp, vpl_attr)
1514 #define VPL_LOCK(vpl) lck_spin_lock(vpl)
1515 #define VPL_UNLOCK(vpl) lck_spin_unlock(vpl)
1516 #else
1517 #define VPL_LOCK_INIT(vlq, vpl_grp, vpl_attr) lck_mtx_init_ext(&vlq->vpl_lock, &vlq->vpl_lock_ext, vpl_grp, vpl_attr)
1518 #define VPL_LOCK(vpl) lck_mtx_lock_spin(vpl)
1519 #define VPL_UNLOCK(vpl) lck_mtx_unlock(vpl)
1520 #endif
1521
1522
1523 #if DEVELOPMENT || DEBUG
1524 #define VM_PAGE_SPECULATIVE_USED_ADD() \
1525 MACRO_BEGIN \
1526 OSAddAtomic(1, &vm_page_speculative_used); \
1527 MACRO_END
1528 #else
1529 #define VM_PAGE_SPECULATIVE_USED_ADD()
1530 #endif
1531
1532
1533 #define VM_PAGE_CONSUME_CLUSTERED(mem) \
1534 MACRO_BEGIN \
1535 ppnum_t __phys_page; \
1536 __phys_page = VM_PAGE_GET_PHYS_PAGE(mem); \
1537 pmap_lock_phys_page(__phys_page); \
1538 if (mem->clustered) { \
1539 vm_object_t o; \
1540 o = VM_PAGE_OBJECT(mem); \
1541 assert(o); \
1542 o->pages_used++; \
1543 mem->clustered = FALSE; \
1544 VM_PAGE_SPECULATIVE_USED_ADD(); \
1545 } \
1546 pmap_unlock_phys_page(__phys_page); \
1547 MACRO_END
1548
1549
1550 #define VM_PAGE_COUNT_AS_PAGEIN(mem) \
1551 MACRO_BEGIN \
1552 { \
1553 vm_object_t o; \
1554 o = VM_PAGE_OBJECT(mem); \
1555 DTRACE_VM2(pgin, int, 1, (uint64_t *), NULL); \
1556 current_task()->pageins++; \
1557 if (o->internal) { \
1558 DTRACE_VM2(anonpgin, int, 1, (uint64_t *), NULL); \
1559 } else { \
1560 DTRACE_VM2(fspgin, int, 1, (uint64_t *), NULL); \
1561 } \
1562 } \
1563 MACRO_END
1564
1565 /* adjust for stolen pages accounted elsewhere */
1566 #define VM_PAGE_MOVE_STOLEN(page_count) \
1567 MACRO_BEGIN \
1568 vm_page_stolen_count -= (page_count); \
1569 vm_page_wire_count_initial -= (page_count); \
1570 MACRO_END
1571
1572 #define DW_vm_page_unwire 0x01
1573 #define DW_vm_page_wire 0x02
1574 #define DW_vm_page_free 0x04
1575 #define DW_vm_page_activate 0x08
1576 #define DW_vm_page_deactivate_internal 0x10
1577 #define DW_vm_page_speculate 0x20
1578 #define DW_vm_page_lru 0x40
1579 #define DW_vm_pageout_throttle_up 0x80
1580 #define DW_PAGE_WAKEUP 0x100
1581 #define DW_clear_busy 0x200
1582 #define DW_clear_reference 0x400
1583 #define DW_set_reference 0x800
1584 #define DW_move_page 0x1000
1585 #define DW_VM_PAGE_QUEUES_REMOVE 0x2000
1586 #define DW_enqueue_cleaned 0x4000
1587 #define DW_vm_phantom_cache_update 0x8000
1588
1589 struct vm_page_delayed_work {
1590 vm_page_t dw_m;
1591 int dw_mask;
1592 };
1593
1594 void vm_page_do_delayed_work(vm_object_t object, vm_tag_t tag, struct vm_page_delayed_work *dwp, int dw_count);
1595
1596 extern unsigned int vm_max_delayed_work_limit;
1597
1598 #define DEFAULT_DELAYED_WORK_LIMIT 32
1599
1600 #define DELAYED_WORK_LIMIT(max) ((vm_max_delayed_work_limit >= max ? max : vm_max_delayed_work_limit))
1601
1602 /*
1603 * vm_page_do_delayed_work may need to drop the object lock...
1604 * if it does, we need the pages it's looking at to
1605 * be held stable via the busy bit, so if busy isn't already
1606 * set, we need to set it and ask vm_page_do_delayed_work
1607 * to clear it and wakeup anyone that might have blocked on
1608 * it once we're done processing the page.
1609 */
1610
1611 #define VM_PAGE_ADD_DELAYED_WORK(dwp, mem, dw_cnt) \
1612 MACRO_BEGIN \
1613 if (mem->busy == FALSE) { \
1614 mem->busy = TRUE; \
1615 if ( !(dwp->dw_mask & DW_vm_page_free)) \
1616 dwp->dw_mask |= (DW_clear_busy | DW_PAGE_WAKEUP); \
1617 } \
1618 dwp->dw_m = mem; \
1619 dwp++; \
1620 dw_cnt++; \
1621 MACRO_END
1622
1623 extern vm_page_t vm_object_page_grab(vm_object_t);
1624
1625 #if VM_PAGE_BUCKETS_CHECK
1626 extern void vm_page_buckets_check(void);
1627 #endif /* VM_PAGE_BUCKETS_CHECK */
1628
1629 extern void vm_page_queues_remove(vm_page_t mem, boolean_t remove_from_backgroundq);
1630 extern void vm_page_remove_internal(vm_page_t page);
1631 extern void vm_page_enqueue_inactive(vm_page_t mem, boolean_t first);
1632 extern void vm_page_enqueue_active(vm_page_t mem, boolean_t first);
1633 extern void vm_page_check_pageable_safe(vm_page_t page);
1634
1635
1636 #endif /* _VM_VM_PAGE_H_ */