2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
55 * Author: Avadis Tevanian, Jr., Michael Wayne Young
58 * Resident memory system definitions.
61 #ifndef _VM_VM_PAGE_H_
62 #define _VM_VM_PAGE_H_
66 #include <mach/boolean.h>
67 #include <mach/vm_prot.h>
68 #include <mach/vm_param.h>
69 #include <vm/vm_object.h>
70 #include <kern/queue.h>
71 #include <kern/lock.h>
73 #include <kern/macro_help.h>
76 * Each page entered on the inactive queue obtains a ticket from a
77 * particular ticket roll. Pages granted tickets from a particular
78 * roll generally flow through the queue as a group. In this way when a
79 * page with a ticket from a particular roll is pulled from the top of the
80 * queue it is extremely likely that the pages near the top will have tickets
81 * from the same or adjacent rolls. In this way the proximity to the top
82 * of the queue can be loosely ascertained by determining the identity of
83 * the roll the pages ticket came from.
87 extern unsigned int vm_page_ticket_roll
;
88 extern unsigned int vm_page_ticket
;
91 #define VM_PAGE_TICKETS_IN_ROLL 512
92 #define VM_PAGE_TICKET_ROLL_IDS 16
95 * Management of resident (logical) pages.
97 * A small structure is kept for each resident
98 * page, indexed by page number. Each structure
99 * is an element of several lists:
101 * A hash table bucket used to quickly
102 * perform object/offset lookups
104 * A list of all pages for a given object,
105 * so they can be quickly deactivated at
106 * time of deallocation.
108 * An ordered list of pages due for pageout.
110 * In addition, the structure contains the object
111 * and offset to which this page belongs (for pageout),
112 * and sundry status bits.
114 * Fields in this structure are locked either by the lock on the
115 * object that the page belongs to (O) or by the lock on the page
116 * queues (P). [Some fields require that both locks be held to
117 * change that field; holding either lock is sufficient to read.]
121 queue_chain_t pageq
; /* queue info for FIFO
122 * queue or free list (P) */
123 queue_chain_t listq
; /* all pages in same object (O) */
124 struct vm_page
*next
; /* VP bucket link (O) */
126 vm_object_t object
; /* which object am I in (O&P) */
127 vm_object_offset_t offset
; /* offset into that object (O,P) */
130 * The following word of flags is protected
131 * by the "page queues" lock.
133 unsigned int wire_count
:16, /* how many wired down maps use me? (O&P) */
134 page_ticket
:4, /* age of the page on the */
135 /* inactive queue. */
136 /* boolean_t */ inactive
:1, /* page is in inactive list (P) */
137 active
:1, /* page is in active list (P) */
138 pageout_queue
:1,/* page is on queue for pageout (P) */
139 laundry
:1, /* page is being cleaned now (P)*/
140 free
:1, /* page is on free list (P) */
141 reference
:1, /* page has been used (P) */
142 pageout
:1, /* page wired & busy for pageout (P) */
143 gobbled
:1, /* page used internally (P) */
144 private:1, /* Page should not be returned to
145 * the free list (P) */
150 * The following word of flags is protected
151 * by the "VM object" lock.
154 page_error
:8, /* error from I/O operations */
155 /* boolean_t */ busy
:1, /* page is in transit (O) */
156 wanted
:1, /* someone is waiting for page (O) */
157 tabled
:1, /* page is in VP table (O) */
158 fictitious
:1, /* Physical page doesn't exist (O) */
159 no_isync
:1, /* page has not been instruction synced */
160 absent
:1, /* Data has been requested, but is
161 * not yet available (O) */
162 error
:1, /* Data manager was unable to provide
163 * data due to error (O) */
164 dirty
:1, /* Page must be cleaned (O) */
165 cleaning
:1, /* Page clean has begun (O) */
166 precious
:1, /* Page is precious; data must be
167 * returned even if clean (O) */
168 clustered
:1, /* page is not the faulted page (O) */
169 overwriting
:1, /* Request to unlock has been made
170 * without having data. (O)
171 * [See vm_fault_page_overwrite] */
172 restart
:1, /* Page was pushed higher in shadow
173 chain by copy_call-related pagers;
174 start again at top of chain */
175 lock_supplied
:1,/* protection supplied by pager (O) */
176 /* vm_prot_t */ page_lock
:3, /* Uses prohibited by pager (O) */
177 /* vm_prot_t */ unlock_request
:3,/* Outstanding unlock request (O) */
178 unusual
:1, /* Page is absent, error, restart or
180 encrypted
:1, /* encrypted for secure swap (O) */
181 list_req_pending
:1, /* pagein/pageout alt mechanism */
182 /* allows creation of list */
183 /* requests on pages that are */
184 /* actively being paged. */
185 dump_cleaning
:1; /* set by the pageout daemon when */
186 /* a page being cleaned is */
187 /* encountered and targeted as */
188 /* a pageout candidate */
189 /* we've used up all 32 bits */
191 ppnum_t phys_page
; /* Physical address of page, passed
192 * to pmap_enter (read-only) */
195 #define DEBUG_ENCRYPTED_SWAP 1
196 #if DEBUG_ENCRYPTED_SWAP
197 #define ASSERT_PAGE_DECRYPTED(page) \
199 if ((page)->encrypted) { \
200 panic("VM page %p should not be encrypted here\n", \
204 #else /* DEBUG_ENCRYPTED_SWAP */
205 #define ASSERT_PAGE_DECRYPTED(page) assert(!(page)->encrypted)
206 #endif /* DEBUG_ENCRYPTED_SWAP */
208 typedef struct vm_page
*vm_page_t
;
210 #define VM_PAGE_NULL ((vm_page_t) 0)
211 #define NEXT_PAGE(m) ((vm_page_t) (m)->pageq.next)
212 #define NEXT_PAGE_PTR(m) ((vm_page_t *) &(m)->pageq.next)
215 * XXX The unusual bit should not be necessary. Most of the bit
216 * XXX fields above really want to be masks.
220 * For debugging, this macro can be defined to perform
221 * some useful check on a page structure.
224 #define VM_PAGE_CHECK(mem)
227 * Each pageable resident page falls into one of three lists:
230 * Available for allocation now.
232 * Not referenced in any map, but still has an
233 * object/offset-page mapping, and may be dirty.
234 * This is the list of pages that should be
237 * A list of pages which have been placed in
238 * at least one physical map. This list is
239 * ordered, in LRU-like fashion.
243 vm_page_t vm_page_queue_free
; /* memory free queue */
245 vm_page_t vm_page_queue_fictitious
; /* fictitious free queue */
247 queue_head_t vm_page_queue_active
; /* active memory queue */
249 queue_head_t vm_page_queue_inactive
; /* inactive memory queue */
250 queue_head_t vm_page_queue_zf
; /* inactive memory queue for zero fill */
253 vm_offset_t first_phys_addr
; /* physical address for first_page */
255 vm_offset_t last_phys_addr
; /* physical address for last_page */
258 unsigned int vm_page_free_count
; /* How many pages are free? */
260 unsigned int vm_page_fictitious_count
;/* How many fictitious pages are free? */
262 unsigned int vm_page_active_count
; /* How many pages are active? */
264 unsigned int vm_page_inactive_count
; /* How many pages are inactive? */
266 unsigned int vm_page_wire_count
; /* How many pages are wired? */
268 unsigned int vm_page_free_target
; /* How many do we want free? */
270 unsigned int vm_page_free_min
; /* When to wakeup pageout */
272 unsigned int vm_page_inactive_target
;/* How many do we want inactive? */
274 unsigned int vm_page_free_reserved
; /* How many pages reserved to do pageout */
276 unsigned int vm_page_throttled_count
;/* Count of zero-fill allocations throttled */
278 unsigned int vm_page_gobble_count
;
281 unsigned int vm_page_purgeable_count
;/* How many pages are purgeable now ? */
283 uint64_t vm_page_purged_count
; /* How many pages got purged so far ? */
285 decl_mutex_data(,vm_page_queue_lock
)
286 /* lock on active and inactive page queues */
287 decl_mutex_data(,vm_page_queue_free_lock
)
288 /* lock on free page queue */
290 extern unsigned int vm_page_free_wanted
;
291 /* how many threads are waiting for memory */
293 extern vm_offset_t vm_page_fictitious_addr
;
294 /* (fake) phys_addr of fictitious pages */
296 extern boolean_t vm_page_deactivate_hint
;
299 * Prototypes for functions exported by this module.
301 extern void vm_page_bootstrap(
305 extern void vm_page_module_init(void);
307 extern void vm_page_create(
311 extern vm_page_t
vm_page_lookup(
313 vm_object_offset_t offset
);
315 extern vm_page_t
vm_page_grab_fictitious(void);
317 extern void vm_page_release_fictitious(
320 extern boolean_t
vm_page_convert(
323 extern void vm_page_more_fictitious(void);
325 extern int vm_pool_low(void);
327 extern vm_page_t
vm_page_grab(void);
329 extern void vm_page_release(
332 extern boolean_t
vm_page_wait(
335 extern vm_page_t
vm_page_alloc(
337 vm_object_offset_t offset
);
339 extern void vm_page_init(
343 extern void vm_page_free(
346 extern void vm_page_activate(
349 extern void vm_page_deactivate(
352 extern void vm_page_rename(
354 vm_object_t new_object
,
355 vm_object_offset_t new_offset
);
357 extern void vm_page_insert(
360 vm_object_offset_t offset
);
362 extern void vm_page_replace(
365 vm_object_offset_t offset
);
367 extern void vm_page_remove(
370 extern void vm_page_zero_fill(
373 extern void vm_page_part_zero_fill(
378 extern void vm_page_copy(
380 vm_page_t dest_page
);
382 extern void vm_page_part_copy(
389 extern void vm_page_wire(
392 extern void vm_page_unwire(
395 extern void vm_set_page_size(void);
397 extern void vm_page_gobble(
401 * Functions implemented as macros. m->wanted and m->busy are
402 * protected by the object lock.
405 #define PAGE_ASSERT_WAIT(m, interruptible) \
406 (((m)->wanted = TRUE), \
407 assert_wait((event_t) (m), (interruptible)))
409 #define PAGE_SLEEP(o, m, interruptible) \
410 (((m)->wanted = TRUE), \
411 thread_sleep_vm_object((o), (m), (interruptible)))
413 #define PAGE_WAKEUP_DONE(m) \
417 (m)->wanted = FALSE; \
418 thread_wakeup((event_t) (m)); \
422 #define PAGE_WAKEUP(m) \
425 (m)->wanted = FALSE; \
426 thread_wakeup((event_t) (m)); \
430 #define VM_PAGE_FREE(p) \
432 vm_page_lock_queues(); \
434 vm_page_unlock_queues(); \
437 #define VM_PAGE_GRAB_FICTITIOUS(M) \
439 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
440 vm_page_more_fictitious(); \
443 #define VM_PAGE_THROTTLED() \
444 (vm_page_free_count < vm_page_free_min && \
445 !(current_thread()->options & TH_OPT_VMPRIV) && \
446 ++vm_page_throttled_count)
448 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
450 #define vm_page_lock_queues() mutex_lock(&vm_page_queue_lock)
451 #define vm_page_unlock_queues() mutex_unlock(&vm_page_queue_lock)
453 #define VM_PAGE_QUEUES_REMOVE(mem) \
455 assert(!mem->laundry); \
457 assert(mem->object != kernel_object); \
458 assert(!mem->inactive); \
459 queue_remove(&vm_page_queue_active, \
460 mem, vm_page_t, pageq); \
461 mem->pageq.next = NULL; \
462 mem->pageq.prev = NULL; \
463 mem->active = FALSE; \
464 if (!mem->fictitious) \
465 vm_page_active_count--; \
468 if (mem->inactive) { \
469 assert(mem->object != kernel_object); \
470 assert(!mem->active); \
471 if (mem->zero_fill) { \
472 queue_remove(&vm_page_queue_zf, \
473 mem, vm_page_t, pageq); \
475 queue_remove(&vm_page_queue_inactive, \
476 mem, vm_page_t, pageq); \
478 mem->pageq.next = NULL; \
479 mem->pageq.prev = NULL; \
480 mem->inactive = FALSE; \
481 if (!mem->fictitious) \
482 vm_page_inactive_count--; \
486 #endif /* _VM_VM_PAGE_H_ */