2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr., Michael Wayne Young
57 * Resident memory system definitions.
60 #ifndef _VM_VM_PAGE_H_
61 #define _VM_VM_PAGE_H_
65 #include <mach/boolean.h>
66 #include <mach/vm_prot.h>
67 #include <mach/vm_param.h>
68 #include <vm/vm_object.h>
69 #include <kern/queue.h>
70 #include <kern/lock.h>
72 #include <kern/macro_help.h>
75 * Each page entered on the inactive queue obtains a ticket from a
76 * particular ticket roll. Pages granted tickets from a particular
77 * roll generally flow through the queue as a group. In this way when a
78 * page with a ticket from a particular roll is pulled from the top of the
79 * queue it is extremely likely that the pages near the top will have tickets
80 * from the same or adjacent rolls. In this way the proximity to the top
81 * of the queue can be loosely ascertained by determining the identity of
82 * the roll the pages ticket came from.
86 extern unsigned int vm_page_ticket_roll
;
87 extern unsigned int vm_page_ticket
;
90 #define VM_PAGE_TICKETS_IN_ROLL 512
91 #define VM_PAGE_TICKET_ROLL_IDS 16
94 * Management of resident (logical) pages.
96 * A small structure is kept for each resident
97 * page, indexed by page number. Each structure
98 * is an element of several lists:
100 * A hash table bucket used to quickly
101 * perform object/offset lookups
103 * A list of all pages for a given object,
104 * so they can be quickly deactivated at
105 * time of deallocation.
107 * An ordered list of pages due for pageout.
109 * In addition, the structure contains the object
110 * and offset to which this page belongs (for pageout),
111 * and sundry status bits.
113 * Fields in this structure are locked either by the lock on the
114 * object that the page belongs to (O) or by the lock on the page
115 * queues (P). [Some fields require that both locks be held to
116 * change that field; holding either lock is sufficient to read.]
120 queue_chain_t pageq
; /* queue info for FIFO
121 * queue or free list (P) */
122 queue_chain_t listq
; /* all pages in same object (O) */
123 struct vm_page
*next
; /* VP bucket link (O) */
125 vm_object_t object
; /* which object am I in (O&P) */
126 vm_object_offset_t offset
; /* offset into that object (O,P) */
129 * The following word of flags is protected
130 * by the "page queues" lock.
132 unsigned int wire_count
:16, /* how many wired down maps use me? (O&P) */
133 page_ticket
:4, /* age of the page on the */
134 /* inactive queue. */
135 /* boolean_t */ inactive
:1, /* page is in inactive list (P) */
136 active
:1, /* page is in active list (P) */
137 pageout_queue
:1,/* page is on queue for pageout (P) */
138 laundry
:1, /* page is being cleaned now (P)*/
139 free
:1, /* page is on free list (P) */
140 reference
:1, /* page has been used (P) */
141 pageout
:1, /* page wired & busy for pageout (P) */
142 gobbled
:1, /* page used internally (P) */
143 private:1, /* Page should not be returned to
144 * the free list (P) */
149 * The following word of flags is protected
150 * by the "VM object" lock.
153 page_error
:8, /* error from I/O operations */
154 /* boolean_t */ busy
:1, /* page is in transit (O) */
155 wanted
:1, /* someone is waiting for page (O) */
156 tabled
:1, /* page is in VP table (O) */
157 fictitious
:1, /* Physical page doesn't exist (O) */
158 no_isync
:1, /* page has not been instruction synced */
159 absent
:1, /* Data has been requested, but is
160 * not yet available (O) */
161 error
:1, /* Data manager was unable to provide
162 * data due to error (O) */
163 dirty
:1, /* Page must be cleaned (O) */
164 cleaning
:1, /* Page clean has begun (O) */
165 precious
:1, /* Page is precious; data must be
166 * returned even if clean (O) */
167 clustered
:1, /* page is not the faulted page (O) */
168 overwriting
:1, /* Request to unlock has been made
169 * without having data. (O)
170 * [See vm_fault_page_overwrite] */
171 restart
:1, /* Page was pushed higher in shadow
172 chain by copy_call-related pagers;
173 start again at top of chain */
174 lock_supplied
:1,/* protection supplied by pager (O) */
175 /* vm_prot_t */ page_lock
:3, /* Uses prohibited by pager (O) */
176 /* vm_prot_t */ unlock_request
:3,/* Outstanding unlock request (O) */
177 unusual
:1, /* Page is absent, error, restart or
179 encrypted
:1, /* encrypted for secure swap (O) */
180 list_req_pending
:1, /* pagein/pageout alt mechanism */
181 /* allows creation of list */
182 /* requests on pages that are */
183 /* actively being paged. */
184 dump_cleaning
:1; /* set by the pageout daemon when */
185 /* a page being cleaned is */
186 /* encountered and targeted as */
187 /* a pageout candidate */
188 /* we've used up all 32 bits */
190 ppnum_t phys_page
; /* Physical address of page, passed
191 * to pmap_enter (read-only) */
194 #define DEBUG_ENCRYPTED_SWAP 1
195 #if DEBUG_ENCRYPTED_SWAP
196 #define ASSERT_PAGE_DECRYPTED(page) \
198 if ((page)->encrypted) { \
199 panic("VM page %p should not be encrypted here\n", \
203 #else /* DEBUG_ENCRYPTED_SWAP */
204 #define ASSERT_PAGE_DECRYPTED(page) assert(!(page)->encrypted)
205 #endif /* DEBUG_ENCRYPTED_SWAP */
207 typedef struct vm_page
*vm_page_t
;
209 #define VM_PAGE_NULL ((vm_page_t) 0)
210 #define NEXT_PAGE(m) ((vm_page_t) (m)->pageq.next)
211 #define NEXT_PAGE_PTR(m) ((vm_page_t *) &(m)->pageq.next)
214 * XXX The unusual bit should not be necessary. Most of the bit
215 * XXX fields above really want to be masks.
219 * For debugging, this macro can be defined to perform
220 * some useful check on a page structure.
223 #define VM_PAGE_CHECK(mem)
226 * Each pageable resident page falls into one of three lists:
229 * Available for allocation now.
231 * Not referenced in any map, but still has an
232 * object/offset-page mapping, and may be dirty.
233 * This is the list of pages that should be
236 * A list of pages which have been placed in
237 * at least one physical map. This list is
238 * ordered, in LRU-like fashion.
242 vm_page_t vm_page_queue_free
; /* memory free queue */
244 vm_page_t vm_page_queue_fictitious
; /* fictitious free queue */
246 queue_head_t vm_page_queue_active
; /* active memory queue */
248 queue_head_t vm_page_queue_inactive
; /* inactive memory queue */
249 queue_head_t vm_page_queue_zf
; /* inactive memory queue for zero fill */
252 vm_offset_t first_phys_addr
; /* physical address for first_page */
254 vm_offset_t last_phys_addr
; /* physical address for last_page */
257 unsigned int vm_page_free_count
; /* How many pages are free? */
259 unsigned int vm_page_fictitious_count
;/* How many fictitious pages are free? */
261 unsigned int vm_page_active_count
; /* How many pages are active? */
263 unsigned int vm_page_inactive_count
; /* How many pages are inactive? */
265 unsigned int vm_page_wire_count
; /* How many pages are wired? */
267 unsigned int vm_page_free_target
; /* How many do we want free? */
269 unsigned int vm_page_free_min
; /* When to wakeup pageout */
271 unsigned int vm_page_inactive_target
;/* How many do we want inactive? */
273 unsigned int vm_page_free_reserved
; /* How many pages reserved to do pageout */
275 unsigned int vm_page_throttled_count
;/* Count of zero-fill allocations throttled */
277 unsigned int vm_page_gobble_count
;
280 unsigned int vm_page_purgeable_count
;/* How many pages are purgeable now ? */
282 uint64_t vm_page_purged_count
; /* How many pages got purged so far ? */
284 decl_mutex_data(,vm_page_queue_lock
)
285 /* lock on active and inactive page queues */
286 decl_mutex_data(,vm_page_queue_free_lock
)
287 /* lock on free page queue */
289 extern unsigned int vm_page_free_wanted
;
290 /* how many threads are waiting for memory */
292 extern vm_offset_t vm_page_fictitious_addr
;
293 /* (fake) phys_addr of fictitious pages */
295 extern boolean_t vm_page_deactivate_hint
;
298 * Prototypes for functions exported by this module.
300 extern void vm_page_bootstrap(
304 extern void vm_page_module_init(void);
306 extern void vm_page_create(
310 extern vm_page_t
vm_page_lookup(
312 vm_object_offset_t offset
);
314 extern vm_page_t
vm_page_grab_fictitious(void);
316 extern void vm_page_release_fictitious(
319 extern boolean_t
vm_page_convert(
322 extern void vm_page_more_fictitious(void);
324 extern int vm_pool_low(void);
326 extern vm_page_t
vm_page_grab(void);
328 extern void vm_page_release(
331 extern boolean_t
vm_page_wait(
334 extern vm_page_t
vm_page_alloc(
336 vm_object_offset_t offset
);
338 extern void vm_page_init(
342 extern void vm_page_free(
345 extern void vm_page_activate(
348 extern void vm_page_deactivate(
351 extern void vm_page_rename(
353 vm_object_t new_object
,
354 vm_object_offset_t new_offset
);
356 extern void vm_page_insert(
359 vm_object_offset_t offset
);
361 extern void vm_page_replace(
364 vm_object_offset_t offset
);
366 extern void vm_page_remove(
369 extern void vm_page_zero_fill(
372 extern void vm_page_part_zero_fill(
377 extern void vm_page_copy(
379 vm_page_t dest_page
);
381 extern void vm_page_part_copy(
388 extern void vm_page_wire(
391 extern void vm_page_unwire(
394 extern void vm_set_page_size(void);
396 extern void vm_page_gobble(
400 * Functions implemented as macros. m->wanted and m->busy are
401 * protected by the object lock.
404 #define PAGE_ASSERT_WAIT(m, interruptible) \
405 (((m)->wanted = TRUE), \
406 assert_wait((event_t) (m), (interruptible)))
408 #define PAGE_SLEEP(o, m, interruptible) \
409 (((m)->wanted = TRUE), \
410 thread_sleep_vm_object((o), (m), (interruptible)))
412 #define PAGE_WAKEUP_DONE(m) \
416 (m)->wanted = FALSE; \
417 thread_wakeup((event_t) (m)); \
421 #define PAGE_WAKEUP(m) \
424 (m)->wanted = FALSE; \
425 thread_wakeup((event_t) (m)); \
429 #define VM_PAGE_FREE(p) \
431 vm_page_lock_queues(); \
433 vm_page_unlock_queues(); \
436 #define VM_PAGE_GRAB_FICTITIOUS(M) \
438 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
439 vm_page_more_fictitious(); \
442 #define VM_PAGE_THROTTLED() \
443 (vm_page_free_count < vm_page_free_min && \
444 !(current_thread()->options & TH_OPT_VMPRIV) && \
445 ++vm_page_throttled_count)
447 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
449 #define vm_page_lock_queues() mutex_lock(&vm_page_queue_lock)
450 #define vm_page_unlock_queues() mutex_unlock(&vm_page_queue_lock)
452 #define VM_PAGE_QUEUES_REMOVE(mem) \
454 assert(!mem->laundry); \
456 assert(mem->object != kernel_object); \
457 assert(!mem->inactive); \
458 queue_remove(&vm_page_queue_active, \
459 mem, vm_page_t, pageq); \
460 mem->pageq.next = NULL; \
461 mem->pageq.prev = NULL; \
462 mem->active = FALSE; \
463 if (!mem->fictitious) \
464 vm_page_active_count--; \
467 if (mem->inactive) { \
468 assert(mem->object != kernel_object); \
469 assert(!mem->active); \
470 if (mem->zero_fill) { \
471 queue_remove(&vm_page_queue_zf, \
472 mem, vm_page_t, pageq); \
474 queue_remove(&vm_page_queue_inactive, \
475 mem, vm_page_t, pageq); \
477 mem->pageq.next = NULL; \
478 mem->pageq.prev = NULL; \
479 mem->inactive = FALSE; \
480 if (!mem->fictitious) \
481 vm_page_inactive_count--; \
485 #endif /* _VM_VM_PAGE_H_ */