]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_page.h
xnu-1504.15.3.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
71 #include <mach/boolean.h>
72 #include <mach/vm_prot.h>
73 #include <mach/vm_param.h>
74 #include <vm/vm_object.h>
75 #include <kern/queue.h>
76 #include <kern/lock.h>
77
78 #include <kern/macro_help.h>
79 #include <libkern/OSAtomic.h>
80
81
82 /*
83 * VM_PAGE_MIN_SPECULATIVE_AGE_Q through VM_PAGE_MAX_SPECULATIVE_AGE_Q
84 * represents a set of aging bins that are 'protected'...
85 *
86 * VM_PAGE_SPECULATIVE_AGED_Q is a list of the speculative pages that have
87 * not yet been 'claimed' but have been aged out of the protective bins
88 * this occurs in vm_page_speculate when it advances to the next bin
89 * and discovers that it is still occupied... at that point, all of the
90 * pages in that bin are moved to the VM_PAGE_SPECULATIVE_AGED_Q. the pages
91 * in that bin are all guaranteed to have reached at least the maximum age
92 * we allow for a protected page... they can be older if there is no
93 * memory pressure to pull them from the bin, or there are no new speculative pages
94 * being generated to push them out.
95 * this list is the one that vm_pageout_scan will prefer when looking
96 * for pages to move to the underweight free list
97 *
98 * VM_PAGE_MAX_SPECULATIVE_AGE_Q * VM_PAGE_SPECULATIVE_Q_AGE_MS
99 * defines the amount of time a speculative page is normally
100 * allowed to live in the 'protected' state (i.e. not available
101 * to be stolen if vm_pageout_scan is running and looking for
102 * pages)... however, if the total number of speculative pages
103 * in the protected state exceeds our limit (defined in vm_pageout.c)
104 * and there are none available in VM_PAGE_SPECULATIVE_AGED_Q, then
105 * vm_pageout_scan is allowed to steal pages from the protected
106 * bucket even if they are underage.
107 *
108 * vm_pageout_scan is also allowed to pull pages from a protected
109 * bin if the bin has reached the "age of consent" we've set
110 */
111 #define VM_PAGE_MAX_SPECULATIVE_AGE_Q 10
112 #define VM_PAGE_MIN_SPECULATIVE_AGE_Q 1
113 #define VM_PAGE_SPECULATIVE_AGED_Q 0
114
115 #define VM_PAGE_SPECULATIVE_Q_AGE_MS 500
116
117
118 struct vm_speculative_age_q {
119 /*
120 * memory queue for speculative pages via clustered pageins
121 */
122 queue_head_t age_q;
123 mach_timespec_t age_ts;
124 };
125
126
127 extern
128 struct vm_speculative_age_q vm_page_queue_speculative[];
129
130 extern int speculative_steal_index;
131 extern int speculative_age_index;
132
133
134 /*
135 * Management of resident (logical) pages.
136 *
137 * A small structure is kept for each resident
138 * page, indexed by page number. Each structure
139 * is an element of several lists:
140 *
141 * A hash table bucket used to quickly
142 * perform object/offset lookups
143 *
144 * A list of all pages for a given object,
145 * so they can be quickly deactivated at
146 * time of deallocation.
147 *
148 * An ordered list of pages due for pageout.
149 *
150 * In addition, the structure contains the object
151 * and offset to which this page belongs (for pageout),
152 * and sundry status bits.
153 *
154 * Fields in this structure are locked either by the lock on the
155 * object that the page belongs to (O) or by the lock on the page
156 * queues (P). [Some fields require that both locks be held to
157 * change that field; holding either lock is sufficient to read.]
158 */
159
160 struct vm_page {
161 queue_chain_t pageq; /* queue info for FIFO */
162 /* queue or free list (P) */
163
164 queue_chain_t listq; /* all pages in same object (O) */
165 struct vm_page *next; /* VP bucket link (O) */
166
167 vm_object_t object; /* which object am I in (O&P) */
168 vm_object_offset_t offset; /* offset into that object (O,P) */
169
170 /*
171 * The following word of flags is protected
172 * by the "page queues" lock.
173 *
174 * we use the 'wire_count' field to store the local
175 * queue id if local queues are enabled...
176 * see the comments at 'VM_PAGE_QUEUES_REMOVE' as to
177 * why this is safe to do
178 */
179 #define local_id wire_count
180 unsigned int wire_count:16, /* how many wired down maps use me? (O&P) */
181 /* boolean_t */ inactive:1, /* page is in inactive list (P) */
182 active:1, /* page is in active list (P) */
183 pageout_queue:1,/* page is on queue for pageout (P) */
184 speculative:1, /* page is on speculative list (P) */
185 laundry:1, /* page is being cleaned now (P)*/
186 free:1, /* page is on free list (P) */
187 reference:1, /* page has been used (P) */
188 gobbled:1, /* page used internally (P) */
189 private:1, /* Page should not be returned to
190 * the free list (P) */
191 throttled:1, /* pager is not responding (P) */
192 local:1,
193 __unused_pageq_bits:5; /* 5 bits available here */
194
195 ppnum_t phys_page; /* Physical address of page, passed
196 * to pmap_enter (read-only) */
197
198 /*
199 * The following word of flags is protected
200 * by the "VM object" lock.
201 */
202 unsigned int
203 /* boolean_t */ busy:1, /* page is in transit (O) */
204 wanted:1, /* someone is waiting for page (O) */
205 tabled:1, /* page is in VP table (O) */
206 fictitious:1, /* Physical page doesn't exist (O) */
207 /*
208 * IMPORTANT: the "pmapped" bit can be turned on while holding the
209 * VM object "shared" lock. See vm_fault_enter().
210 * This is OK as long as it's the only bit in this bit field that
211 * can be updated without holding the VM object "exclusive" lock.
212 */
213 pmapped:1, /* page has been entered at some
214 * point into a pmap (O **shared**) */
215 wpmapped:1, /* page has been entered at some
216 * point into a pmap for write (O) */
217 pageout:1, /* page wired & busy for pageout (O) */
218 absent:1, /* Data has been requested, but is
219 * not yet available (O) */
220 error:1, /* Data manager was unable to provide
221 * data due to error (O) */
222 dirty:1, /* Page must be cleaned (O) */
223 cleaning:1, /* Page clean has begun (O) */
224 precious:1, /* Page is precious; data must be
225 * returned even if clean (O) */
226 clustered:1, /* page is not the faulted page (O) */
227 overwriting:1, /* Request to unlock has been made
228 * without having data. (O)
229 * [See vm_fault_page_overwrite] */
230 restart:1, /* Page was pushed higher in shadow
231 chain by copy_call-related pagers;
232 start again at top of chain */
233 unusual:1, /* Page is absent, error, restart or
234 page locked */
235 encrypted:1, /* encrypted for secure swap (O) */
236 encrypted_cleaning:1, /* encrypting page */
237 list_req_pending:1, /* pagein/pageout alt mechanism */
238 /* allows creation of list */
239 /* requests on pages that are */
240 /* actively being paged. */
241 dump_cleaning:1, /* set by the pageout daemon when */
242 /* a page being cleaned is */
243 /* encountered and targeted as */
244 /* a pageout candidate */
245 cs_validated:1, /* code-signing: page was checked */
246 cs_tainted:1, /* code-signing: page is tainted */
247 no_cache:1, /* page is not to be cached and */
248 /* should be reused ahead of */
249 /* other pages */
250 zero_fill:1,
251 reusable:1,
252 lopage:1,
253 __unused_object_bits:6; /* 6 bits available here */
254
255 #if __LP64__
256 unsigned int __unused_padding; /* Pad structure explicitly
257 * to 8-byte multiple for LP64 */
258 #endif
259 };
260
261 #define DEBUG_ENCRYPTED_SWAP 1
262 #if DEBUG_ENCRYPTED_SWAP
263 #define ASSERT_PAGE_DECRYPTED(page) \
264 MACRO_BEGIN \
265 if ((page)->encrypted) { \
266 panic("VM page %p should not be encrypted here\n", \
267 (page)); \
268 } \
269 MACRO_END
270 #else /* DEBUG_ENCRYPTED_SWAP */
271 #define ASSERT_PAGE_DECRYPTED(page) assert(!(page)->encrypted)
272 #endif /* DEBUG_ENCRYPTED_SWAP */
273
274 typedef struct vm_page *vm_page_t;
275
276
277 typedef struct vm_locks_array {
278 char pad __attribute__ ((aligned (64)));
279 lck_mtx_t vm_page_queue_lock2 __attribute__ ((aligned (64)));
280 lck_mtx_t vm_page_queue_free_lock2 __attribute__ ((aligned (64)));
281 char pad2 __attribute__ ((aligned (64)));
282 } vm_locks_array_t;
283
284
285 #define VM_PAGE_WIRED(m) ((!(m)->local && (m)->wire_count))
286 #define VM_PAGE_NULL ((vm_page_t) 0)
287 #define NEXT_PAGE(m) ((vm_page_t) (m)->pageq.next)
288 #define NEXT_PAGE_PTR(m) ((vm_page_t *) &(m)->pageq.next)
289
290 /*
291 * XXX The unusual bit should not be necessary. Most of the bit
292 * XXX fields above really want to be masks.
293 */
294
295 /*
296 * For debugging, this macro can be defined to perform
297 * some useful check on a page structure.
298 */
299
300 #define VM_PAGE_CHECK(mem) \
301 MACRO_BEGIN \
302 VM_PAGE_QUEUES_ASSERT(mem, 1); \
303 MACRO_END
304
305 /* Page coloring:
306 *
307 * The free page list is actually n lists, one per color,
308 * where the number of colors is a function of the machine's
309 * cache geometry set at system initialization. To disable
310 * coloring, set vm_colors to 1 and vm_color_mask to 0.
311 * The boot-arg "colors" may be used to override vm_colors.
312 * Note that there is little harm in having more colors than needed.
313 */
314
315 #define MAX_COLORS 128
316 #define DEFAULT_COLORS 32
317
318 extern
319 unsigned int vm_colors; /* must be in range 1..MAX_COLORS */
320 extern
321 unsigned int vm_color_mask; /* must be (vm_colors-1) */
322 extern
323 unsigned int vm_cache_geometry_colors; /* optimal #colors based on cache geometry */
324
325 /*
326 * Wired memory is a very limited resource and we can't let users exhaust it
327 * and deadlock the entire system. We enforce the following limits:
328 *
329 * vm_user_wire_limit (default: all memory minus vm_global_no_user_wire_amount)
330 * how much memory can be user-wired in one user task
331 *
332 * vm_global_user_wire_limit (default: same as vm_user_wire_limit)
333 * how much memory can be user-wired in all user tasks
334 *
335 * vm_global_no_user_wire_amount (default: VM_NOT_USER_WIREABLE)
336 * how much memory must remain user-unwired at any time
337 */
338 #define VM_NOT_USER_WIREABLE (64*1024*1024) /* 64MB */
339 extern
340 vm_map_size_t vm_user_wire_limit;
341 extern
342 vm_map_size_t vm_global_user_wire_limit;
343 extern
344 vm_map_size_t vm_global_no_user_wire_amount;
345
346 /*
347 * Each pageable resident page falls into one of three lists:
348 *
349 * free
350 * Available for allocation now. The free list is
351 * actually an array of lists, one per color.
352 * inactive
353 * Not referenced in any map, but still has an
354 * object/offset-page mapping, and may be dirty.
355 * This is the list of pages that should be
356 * paged out next. There are actually two
357 * inactive lists, one for pages brought in from
358 * disk or other backing store, and another
359 * for "zero-filled" pages. See vm_pageout_scan()
360 * for the distinction and usage.
361 * active
362 * A list of pages which have been placed in
363 * at least one physical map. This list is
364 * ordered, in LRU-like fashion.
365 */
366
367
368 #define VPL_LOCK_SPIN 1
369
370 struct vpl {
371 unsigned int vpl_count;
372 queue_head_t vpl_queue;
373 #ifdef VPL_LOCK_SPIN
374 lck_spin_t vpl_lock;
375 #else
376 lck_mtx_t vpl_lock;
377 lck_mtx_ext_t vpl_lock_ext;
378 #endif
379 };
380
381 struct vplq {
382 union {
383 char cache_line_pad[128];
384 struct vpl vpl;
385 } vpl_un;
386 };
387 extern
388 unsigned int vm_page_local_q_count;
389 extern
390 struct vplq *vm_page_local_q;
391 extern
392 unsigned int vm_page_local_q_soft_limit;
393 extern
394 unsigned int vm_page_local_q_hard_limit;
395 extern
396 vm_locks_array_t vm_page_locks;
397
398 extern
399 queue_head_t vm_page_queue_free[MAX_COLORS]; /* memory free queue */
400 extern
401 queue_head_t vm_lopage_queue_free; /* low memory free queue */
402 extern
403 vm_page_t vm_page_queue_fictitious; /* fictitious free queue */
404 extern
405 queue_head_t vm_page_queue_active; /* active memory queue */
406 extern
407 queue_head_t vm_page_queue_inactive; /* inactive memory queue for normal pages */
408 extern
409 queue_head_t vm_page_queue_zf; /* inactive memory queue for zero fill */
410 extern
411 queue_head_t vm_page_queue_throttled; /* memory queue for throttled pageout pages */
412
413 extern
414 vm_offset_t first_phys_addr; /* physical address for first_page */
415 extern
416 vm_offset_t last_phys_addr; /* physical address for last_page */
417
418 extern
419 unsigned int vm_page_free_count; /* How many pages are free? (sum of all colors) */
420 extern
421 unsigned int vm_page_fictitious_count;/* How many fictitious pages are free? */
422 extern
423 unsigned int vm_page_active_count; /* How many pages are active? */
424 extern
425 unsigned int vm_page_inactive_count; /* How many pages are inactive? */
426 extern
427 unsigned int vm_page_throttled_count;/* How many inactives are throttled */
428 extern
429 unsigned int vm_page_speculative_count; /* How many speculative pages are unclaimed? */
430 extern
431 unsigned int vm_page_wire_count; /* How many pages are wired? */
432 extern
433 unsigned int vm_page_free_target; /* How many do we want free? */
434 extern
435 unsigned int vm_page_free_min; /* When to wakeup pageout */
436 extern
437 unsigned int vm_page_throttle_limit; /* When to throttle new page creation */
438 extern
439 uint32_t vm_page_creation_throttle; /* When to throttle new page creation */
440 extern
441 unsigned int vm_page_inactive_target;/* How many do we want inactive? */
442 extern
443 unsigned int vm_page_inactive_min; /* When do wakeup pageout */
444 extern
445 unsigned int vm_page_free_reserved; /* How many pages reserved to do pageout */
446 extern
447 unsigned int vm_page_throttle_count; /* Count of page allocations throttled */
448 extern
449 unsigned int vm_page_gobble_count;
450
451 #if DEVELOPMENT || DEBUG
452 extern
453 unsigned int vm_page_speculative_used;
454 #endif
455
456 extern
457 unsigned int vm_page_purgeable_count;/* How many pages are purgeable now ? */
458 extern
459 unsigned int vm_page_purgeable_wired_count;/* How many purgeable pages are wired now ? */
460 extern
461 uint64_t vm_page_purged_count; /* How many pages got purged so far ? */
462
463 extern unsigned int vm_page_free_wanted;
464 /* how many threads are waiting for memory */
465
466 extern unsigned int vm_page_free_wanted_privileged;
467 /* how many VM privileged threads are waiting for memory */
468
469 extern ppnum_t vm_page_fictitious_addr;
470 /* (fake) phys_addr of fictitious pages */
471
472 extern ppnum_t vm_page_guard_addr;
473 /* (fake) phys_addr of guard pages */
474
475
476 extern boolean_t vm_page_deactivate_hint;
477
478 /*
479 0 = all pages avail ( default. )
480 1 = disable high mem ( cap max pages to 4G)
481 2 = prefer himem
482 */
483 extern int vm_himemory_mode;
484
485 extern boolean_t vm_lopage_needed;
486 extern uint32_t vm_lopage_free_count;
487 extern uint32_t vm_lopage_free_limit;
488 extern uint32_t vm_lopage_lowater;
489 extern boolean_t vm_lopage_refill;
490 extern uint64_t max_valid_dma_address;
491 extern ppnum_t max_valid_low_ppnum;
492
493 /*
494 * Prototypes for functions exported by this module.
495 */
496 extern void vm_page_bootstrap(
497 vm_offset_t *startp,
498 vm_offset_t *endp) __attribute__((section("__TEXT, initcode")));
499
500 extern void vm_page_module_init(void) __attribute__((section("__TEXT, initcode")));
501
502 extern void vm_page_init_local_q(void);
503
504 extern void vm_page_create(
505 ppnum_t start,
506 ppnum_t end);
507
508 extern vm_page_t vm_page_lookup(
509 vm_object_t object,
510 vm_object_offset_t offset);
511
512 extern vm_page_t vm_page_grab_fictitious(void);
513
514 extern vm_page_t vm_page_grab_guard(void);
515
516 extern void vm_page_release_fictitious(
517 vm_page_t page);
518
519 extern void vm_page_more_fictitious(void);
520
521 extern int vm_pool_low(void);
522
523 extern vm_page_t vm_page_grab(void);
524
525 extern vm_page_t vm_page_grablo(void);
526
527 extern void vm_page_release(
528 vm_page_t page);
529
530 extern boolean_t vm_page_wait(
531 int interruptible );
532
533 extern vm_page_t vm_page_alloc(
534 vm_object_t object,
535 vm_object_offset_t offset);
536
537 extern vm_page_t vm_page_alloclo(
538 vm_object_t object,
539 vm_object_offset_t offset);
540
541 extern vm_page_t vm_page_alloc_guard(
542 vm_object_t object,
543 vm_object_offset_t offset);
544
545 extern void vm_page_init(
546 vm_page_t page,
547 ppnum_t phys_page,
548 boolean_t lopage);
549
550 extern void vm_page_free(
551 vm_page_t page);
552
553 extern void vm_page_free_unlocked(
554 vm_page_t page,
555 boolean_t remove_from_hash);
556
557 extern void vm_page_activate(
558 vm_page_t page);
559
560 extern void vm_page_deactivate(
561 vm_page_t page);
562
563 extern void vm_page_deactivate_internal(
564 vm_page_t page,
565 boolean_t clear_hw_reference);
566
567 extern void vm_page_lru(
568 vm_page_t page);
569
570 extern void vm_page_speculate(
571 vm_page_t page,
572 boolean_t new);
573
574 extern void vm_page_speculate_ageit(
575 struct vm_speculative_age_q *aq);
576
577 extern void vm_page_reactivate_all_throttled(void);
578
579 extern void vm_page_reactivate_local(uint32_t lid, boolean_t force, boolean_t nolocks);
580
581 extern void vm_page_rename(
582 vm_page_t page,
583 vm_object_t new_object,
584 vm_object_offset_t new_offset,
585 boolean_t encrypted_ok);
586
587 extern void vm_page_insert(
588 vm_page_t page,
589 vm_object_t object,
590 vm_object_offset_t offset);
591
592 extern void vm_page_insert_internal(
593 vm_page_t page,
594 vm_object_t object,
595 vm_object_offset_t offset,
596 boolean_t queues_lock_held,
597 boolean_t insert_in_hash);
598
599 extern void vm_page_replace(
600 vm_page_t mem,
601 vm_object_t object,
602 vm_object_offset_t offset);
603
604 extern void vm_page_remove(
605 vm_page_t page,
606 boolean_t remove_from_hash);
607
608 extern void vm_page_zero_fill(
609 vm_page_t page);
610
611 extern void vm_page_part_zero_fill(
612 vm_page_t m,
613 vm_offset_t m_pa,
614 vm_size_t len);
615
616 extern void vm_page_copy(
617 vm_page_t src_page,
618 vm_page_t dest_page);
619
620 extern void vm_page_part_copy(
621 vm_page_t src_m,
622 vm_offset_t src_pa,
623 vm_page_t dst_m,
624 vm_offset_t dst_pa,
625 vm_size_t len);
626
627 extern void vm_page_wire(
628 vm_page_t page);
629
630 extern void vm_page_unwire(
631 vm_page_t page,
632 boolean_t queueit);
633
634 extern void vm_set_page_size(void);
635
636 extern void vm_page_gobble(
637 vm_page_t page);
638
639 extern void vm_page_validate_cs(vm_page_t page);
640 extern void vm_page_validate_cs_mapped(
641 vm_page_t page,
642 const void *kaddr);
643
644 extern void vm_page_free_prepare_queues(
645 vm_page_t page);
646
647 extern void vm_page_free_prepare_object(
648 vm_page_t page,
649 boolean_t remove_from_hash);
650
651 /*
652 * Functions implemented as macros. m->wanted and m->busy are
653 * protected by the object lock.
654 */
655
656 #define PAGE_ASSERT_WAIT(m, interruptible) \
657 (((m)->wanted = TRUE), \
658 assert_wait((event_t) (m), (interruptible)))
659
660 #define PAGE_SLEEP(o, m, interruptible) \
661 (((m)->wanted = TRUE), \
662 thread_sleep_vm_object((o), (m), (interruptible)))
663
664 #define PAGE_WAKEUP_DONE(m) \
665 MACRO_BEGIN \
666 (m)->busy = FALSE; \
667 if ((m)->wanted) { \
668 (m)->wanted = FALSE; \
669 thread_wakeup((event_t) (m)); \
670 } \
671 MACRO_END
672
673 #define PAGE_WAKEUP(m) \
674 MACRO_BEGIN \
675 if ((m)->wanted) { \
676 (m)->wanted = FALSE; \
677 thread_wakeup((event_t) (m)); \
678 } \
679 MACRO_END
680
681 #define VM_PAGE_FREE(p) \
682 MACRO_BEGIN \
683 vm_page_free_unlocked(p, TRUE); \
684 MACRO_END
685
686 #define VM_PAGE_GRAB_FICTITIOUS(M) \
687 MACRO_BEGIN \
688 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
689 vm_page_more_fictitious(); \
690 MACRO_END
691
692 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
693
694 #define vm_page_queue_lock (vm_page_locks.vm_page_queue_lock2)
695 #define vm_page_queue_free_lock (vm_page_locks.vm_page_queue_free_lock2)
696
697 #define vm_page_lock_queues() lck_mtx_lock(&vm_page_queue_lock)
698 #define vm_page_unlock_queues() lck_mtx_unlock(&vm_page_queue_lock)
699
700 #define vm_page_lockspin_queues() lck_mtx_lock_spin(&vm_page_queue_lock)
701 #define vm_page_trylockspin_queues() lck_mtx_try_lock_spin(&vm_page_queue_lock)
702 #define vm_page_lockconvert_queues() lck_mtx_convert_spin(&vm_page_queue_lock)
703
704 #ifdef VPL_LOCK_SPIN
705 #define VPL_LOCK_INIT(vlq, vpl_grp, vpl_attr) lck_spin_init(&vlq->vpl_lock, vpl_grp, vpl_attr)
706 #define VPL_LOCK(vpl) lck_spin_lock(vpl)
707 #define VPL_UNLOCK(vpl) lck_spin_unlock(vpl)
708 #else
709 #define VPL_LOCK_INIT(vlq, vpl_grp, vpl_attr) lck_mtx_init_ext(&vlq->vpl_lock, &vlq->vpl_lock_ext, vpl_grp, vpl_attr)
710 #define VPL_LOCK(vpl) lck_mtx_lock_spin(vpl)
711 #define VPL_UNLOCK(vpl) lck_mtx_unlock(vpl)
712 #endif
713
714 #if MACH_ASSERT
715 extern void vm_page_queues_assert(vm_page_t mem, int val);
716 #define VM_PAGE_QUEUES_ASSERT(mem, val) vm_page_queues_assert((mem), (val))
717 #else
718 #define VM_PAGE_QUEUES_ASSERT(mem, val)
719 #endif
720
721
722 /*
723 * 'vm_fault_enter' will place newly created pages (zero-fill and COW) onto the
724 * local queues if they exist... its the only spot in the system where we add pages
725 * to those queues... once on those queues, those pages can only move to one of the
726 * global page queues or the free queues... they NEVER move from local q to local q.
727 * the 'local' state is stable when VM_PAGE_QUEUES_REMOVE is called since we're behind
728 * the global vm_page_queue_lock at this point... we still need to take the local lock
729 * in case this operation is being run on a different CPU then the local queue's identity,
730 * but we don't have to worry about the page moving to a global queue or becoming wired
731 * while we're grabbing the local lock since those operations would require the global
732 * vm_page_queue_lock to be held, and we already own it.
733 *
734 * this is why its safe to utilze the wire_count field in the vm_page_t as the local_id...
735 * 'wired' and local are ALWAYS mutually exclusive conditions.
736 */
737 #define VM_PAGE_QUEUES_REMOVE(mem) \
738 MACRO_BEGIN \
739 VM_PAGE_QUEUES_ASSERT(mem, 1); \
740 assert(!mem->laundry); \
741 assert(!mem->pageout_queue); \
742 if (mem->local) { \
743 struct vpl *lq; \
744 assert(mem->object != kernel_object); \
745 assert(!mem->inactive && !mem->speculative); \
746 assert(!mem->active && !mem->throttled); \
747 lq = &vm_page_local_q[mem->local_id].vpl_un.vpl; \
748 VPL_LOCK(&lq->vpl_lock); \
749 queue_remove(&lq->vpl_queue, \
750 mem, vm_page_t, pageq); \
751 mem->local = FALSE; \
752 mem->local_id = 0; \
753 lq->vpl_count--; \
754 VPL_UNLOCK(&lq->vpl_lock); \
755 } \
756 if (mem->active) { \
757 assert(mem->object != kernel_object); \
758 assert(!mem->inactive && !mem->speculative); \
759 assert(!mem->throttled); \
760 queue_remove(&vm_page_queue_active, \
761 mem, vm_page_t, pageq); \
762 mem->active = FALSE; \
763 if (!mem->fictitious) { \
764 vm_page_active_count--; \
765 } else { \
766 assert(mem->phys_page == \
767 vm_page_fictitious_addr); \
768 } \
769 } \
770 \
771 else if (mem->inactive) { \
772 assert(mem->object != kernel_object); \
773 assert(!mem->active && !mem->speculative); \
774 assert(!mem->throttled); \
775 if (mem->zero_fill) { \
776 queue_remove(&vm_page_queue_zf, \
777 mem, vm_page_t, pageq); \
778 vm_zf_queue_count--; \
779 } else { \
780 queue_remove(&vm_page_queue_inactive, \
781 mem, vm_page_t, pageq); \
782 } \
783 mem->inactive = FALSE; \
784 if (!mem->fictitious) { \
785 vm_page_inactive_count--; \
786 vm_purgeable_q_advance_all(); \
787 } else { \
788 assert(mem->phys_page == \
789 vm_page_fictitious_addr); \
790 } \
791 } \
792 \
793 else if (mem->throttled) { \
794 assert(!mem->active && !mem->inactive); \
795 assert(!mem->speculative); \
796 queue_remove(&vm_page_queue_throttled, \
797 mem, vm_page_t, pageq); \
798 mem->throttled = FALSE; \
799 if (!mem->fictitious) \
800 vm_page_throttled_count--; \
801 } \
802 \
803 else if (mem->speculative) { \
804 assert(!mem->active && !mem->inactive); \
805 assert(!mem->throttled); \
806 assert(!mem->fictitious); \
807 remque(&mem->pageq); \
808 mem->speculative = FALSE; \
809 vm_page_speculative_count--; \
810 } \
811 mem->pageq.next = NULL; \
812 mem->pageq.prev = NULL; \
813 VM_PAGE_QUEUES_ASSERT(mem, 0); \
814 MACRO_END
815
816
817 #if DEVELOPMENT || DEBUG
818 #define VM_PAGE_SPECULATIVE_USED_ADD() \
819 MACRO_BEGIN \
820 OSAddAtomic(1, &vm_page_speculative_used); \
821 MACRO_END
822 #else
823 #define VM_PAGE_SPECULATIVE_USED_ADD()
824 #endif
825
826
827 #define VM_PAGE_CONSUME_CLUSTERED(mem) \
828 MACRO_BEGIN \
829 if (mem->clustered) { \
830 assert(mem->object); \
831 mem->object->pages_used++; \
832 mem->clustered = FALSE; \
833 VM_PAGE_SPECULATIVE_USED_ADD(); \
834 } \
835 MACRO_END
836
837 #endif /* _VM_VM_PAGE_H_ */