2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * Resident memory system definitions.
66 #ifndef _VM_VM_PAGE_H_
67 #define _VM_VM_PAGE_H_
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>
78 #include <kern/macro_help.h>
81 * Each page entered on the inactive queue obtains a ticket from a
82 * particular ticket roll. Pages granted tickets from a particular
83 * roll generally flow through the queue as a group. In this way when a
84 * page with a ticket from a particular roll is pulled from the top of the
85 * queue it is extremely likely that the pages near the top will have tickets
86 * from the same or adjacent rolls. In this way the proximity to the top
87 * of the queue can be loosely ascertained by determining the identity of
88 * the roll the pages ticket came from.
92 extern unsigned int vm_page_ticket_roll
;
93 extern unsigned int vm_page_ticket
;
96 #define VM_PAGE_TICKETS_IN_ROLL 512
97 #define VM_PAGE_TICKET_ROLL_IDS 16
100 * Management of resident (logical) pages.
102 * A small structure is kept for each resident
103 * page, indexed by page number. Each structure
104 * is an element of several lists:
106 * A hash table bucket used to quickly
107 * perform object/offset lookups
109 * A list of all pages for a given object,
110 * so they can be quickly deactivated at
111 * time of deallocation.
113 * An ordered list of pages due for pageout.
115 * In addition, the structure contains the object
116 * and offset to which this page belongs (for pageout),
117 * and sundry status bits.
119 * Fields in this structure are locked either by the lock on the
120 * object that the page belongs to (O) or by the lock on the page
121 * queues (P). [Some fields require that both locks be held to
122 * change that field; holding either lock is sufficient to read.]
126 queue_chain_t pageq
; /* queue info for FIFO
127 * queue or free list (P) */
128 queue_chain_t listq
; /* all pages in same object (O) */
129 struct vm_page
*next
; /* VP bucket link (O) */
131 vm_object_t object
; /* which object am I in (O&P) */
132 vm_object_offset_t offset
; /* offset into that object (O,P) */
135 * The following word of flags is protected
136 * by the "page queues" lock.
138 unsigned int wire_count
:16, /* how many wired down maps use me? (O&P) */
139 page_ticket
:4, /* age of the page on the */
140 /* inactive queue. */
141 /* boolean_t */ inactive
:1, /* page is in inactive list (P) */
142 active
:1, /* page is in active list (P) */
143 pageout_queue
:1,/* page is on queue for pageout (P) */
144 laundry
:1, /* page is being cleaned now (P)*/
145 free
:1, /* page is on free list (P) */
146 reference
:1, /* page has been used (P) */
147 pageout
:1, /* page wired & busy for pageout (P) */
148 gobbled
:1, /* page used internally (P) */
149 private:1, /* Page should not be returned to
150 * the free list (P) */
155 * The following word of flags is protected
156 * by the "VM object" lock.
159 page_error
:8, /* error from I/O operations */
160 /* boolean_t */ busy
:1, /* page is in transit (O) */
161 wanted
:1, /* someone is waiting for page (O) */
162 tabled
:1, /* page is in VP table (O) */
163 fictitious
:1, /* Physical page doesn't exist (O) */
164 no_isync
:1, /* page has not been instruction synced */
165 absent
:1, /* Data has been requested, but is
166 * not yet available (O) */
167 error
:1, /* Data manager was unable to provide
168 * data due to error (O) */
169 dirty
:1, /* Page must be cleaned (O) */
170 cleaning
:1, /* Page clean has begun (O) */
171 precious
:1, /* Page is precious; data must be
172 * returned even if clean (O) */
173 clustered
:1, /* page is not the faulted page (O) */
174 overwriting
:1, /* Request to unlock has been made
175 * without having data. (O)
176 * [See vm_fault_page_overwrite] */
177 restart
:1, /* Page was pushed higher in shadow
178 chain by copy_call-related pagers;
179 start again at top of chain */
180 lock_supplied
:1,/* protection supplied by pager (O) */
181 /* vm_prot_t */ page_lock
:3, /* Uses prohibited by pager (O) */
182 /* vm_prot_t */ unlock_request
:3,/* Outstanding unlock request (O) */
183 unusual
:1, /* Page is absent, error, restart or
185 encrypted
:1, /* encrypted for secure swap (O) */
186 list_req_pending
:1, /* pagein/pageout alt mechanism */
187 /* allows creation of list */
188 /* requests on pages that are */
189 /* actively being paged. */
190 dump_cleaning
:1; /* set by the pageout daemon when */
191 /* a page being cleaned is */
192 /* encountered and targeted as */
193 /* a pageout candidate */
194 /* we've used up all 32 bits */
196 ppnum_t phys_page
; /* Physical address of page, passed
197 * to pmap_enter (read-only) */
200 #define DEBUG_ENCRYPTED_SWAP 1
201 #if DEBUG_ENCRYPTED_SWAP
202 #define ASSERT_PAGE_DECRYPTED(page) \
204 if ((page)->encrypted) { \
205 panic("VM page %p should not be encrypted here\n", \
209 #else /* DEBUG_ENCRYPTED_SWAP */
210 #define ASSERT_PAGE_DECRYPTED(page) assert(!(page)->encrypted)
211 #endif /* DEBUG_ENCRYPTED_SWAP */
213 typedef struct vm_page
*vm_page_t
;
215 #define VM_PAGE_NULL ((vm_page_t) 0)
216 #define NEXT_PAGE(m) ((vm_page_t) (m)->pageq.next)
217 #define NEXT_PAGE_PTR(m) ((vm_page_t *) &(m)->pageq.next)
220 * XXX The unusual bit should not be necessary. Most of the bit
221 * XXX fields above really want to be masks.
225 * For debugging, this macro can be defined to perform
226 * some useful check on a page structure.
229 #define VM_PAGE_CHECK(mem)
232 * Each pageable resident page falls into one of three lists:
235 * Available for allocation now.
237 * Not referenced in any map, but still has an
238 * object/offset-page mapping, and may be dirty.
239 * This is the list of pages that should be
242 * A list of pages which have been placed in
243 * at least one physical map. This list is
244 * ordered, in LRU-like fashion.
248 vm_page_t vm_page_queue_free
; /* memory free queue */
250 vm_page_t vm_page_queue_fictitious
; /* fictitious free queue */
252 queue_head_t vm_page_queue_active
; /* active memory queue */
254 queue_head_t vm_page_queue_inactive
; /* inactive memory queue */
255 queue_head_t vm_page_queue_zf
; /* inactive memory queue for zero fill */
258 vm_offset_t first_phys_addr
; /* physical address for first_page */
260 vm_offset_t last_phys_addr
; /* physical address for last_page */
263 unsigned int vm_page_free_count
; /* How many pages are free? */
265 unsigned int vm_page_fictitious_count
;/* How many fictitious pages are free? */
267 unsigned int vm_page_active_count
; /* How many pages are active? */
269 unsigned int vm_page_inactive_count
; /* How many pages are inactive? */
271 unsigned int vm_page_wire_count
; /* How many pages are wired? */
273 unsigned int vm_page_free_target
; /* How many do we want free? */
275 unsigned int vm_page_free_min
; /* When to wakeup pageout */
277 unsigned int vm_page_inactive_target
;/* How many do we want inactive? */
279 unsigned int vm_page_free_reserved
; /* How many pages reserved to do pageout */
281 unsigned int vm_page_throttled_count
;/* Count of zero-fill allocations throttled */
283 unsigned int vm_page_gobble_count
;
286 unsigned int vm_page_purgeable_count
;/* How many pages are purgeable now ? */
288 uint64_t vm_page_purged_count
; /* How many pages got purged so far ? */
290 decl_mutex_data(,vm_page_queue_lock
)
291 /* lock on active and inactive page queues */
292 decl_mutex_data(,vm_page_queue_free_lock
)
293 /* lock on free page queue */
295 extern unsigned int vm_page_free_wanted
;
296 /* how many threads are waiting for memory */
298 extern vm_offset_t vm_page_fictitious_addr
;
299 /* (fake) phys_addr of fictitious pages */
301 extern boolean_t vm_page_deactivate_hint
;
303 // 0 = all pages avail, 1 = disable high mem, 2 = prefer himem
304 extern int vm_himemory_mode
;
306 extern ppnum_t vm_lopage_poolend
;
307 extern int vm_lopage_poolsize
;
308 extern uint64_t max_valid_dma_address
;
312 * Prototypes for functions exported by this module.
314 extern void vm_page_bootstrap(
318 extern void vm_page_module_init(void);
320 extern void vm_page_create(
324 extern vm_page_t
vm_page_lookup(
326 vm_object_offset_t offset
);
328 extern vm_page_t
vm_page_grab_fictitious(void);
330 extern void vm_page_release_fictitious(
333 extern boolean_t
vm_page_convert(
336 extern void vm_page_more_fictitious(void);
338 extern int vm_pool_low(void);
340 extern vm_page_t
vm_page_grab(void);
342 extern vm_page_t
vm_page_grablo(void);
344 extern void vm_page_release(
347 extern boolean_t
vm_page_wait(
350 extern vm_page_t
vm_page_alloc(
352 vm_object_offset_t offset
);
354 extern vm_page_t
vm_page_alloclo(
356 vm_object_offset_t offset
);
358 extern void vm_page_init(
362 extern void vm_page_free(
365 extern void vm_page_activate(
368 extern void vm_page_deactivate(
371 extern void vm_page_rename(
373 vm_object_t new_object
,
374 vm_object_offset_t new_offset
);
376 extern void vm_page_insert(
379 vm_object_offset_t offset
);
381 extern void vm_page_replace(
384 vm_object_offset_t offset
);
386 extern void vm_page_remove(
389 extern void vm_page_zero_fill(
392 extern void vm_page_part_zero_fill(
397 extern void vm_page_copy(
399 vm_page_t dest_page
);
401 extern void vm_page_part_copy(
408 extern void vm_page_wire(
411 extern void vm_page_unwire(
414 extern void vm_set_page_size(void);
416 extern void vm_page_gobble(
420 * Functions implemented as macros. m->wanted and m->busy are
421 * protected by the object lock.
424 #define PAGE_ASSERT_WAIT(m, interruptible) \
425 (((m)->wanted = TRUE), \
426 assert_wait((event_t) (m), (interruptible)))
428 #define PAGE_SLEEP(o, m, interruptible) \
429 (((m)->wanted = TRUE), \
430 thread_sleep_vm_object((o), (m), (interruptible)))
432 #define PAGE_WAKEUP_DONE(m) \
436 (m)->wanted = FALSE; \
437 thread_wakeup((event_t) (m)); \
441 #define PAGE_WAKEUP(m) \
444 (m)->wanted = FALSE; \
445 thread_wakeup((event_t) (m)); \
449 #define VM_PAGE_FREE(p) \
451 vm_page_lock_queues(); \
453 vm_page_unlock_queues(); \
456 #define VM_PAGE_GRAB_FICTITIOUS(M) \
458 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
459 vm_page_more_fictitious(); \
462 #define VM_PAGE_THROTTLED() \
463 (vm_page_free_count < vm_page_free_min && \
464 !(current_thread()->options & TH_OPT_VMPRIV) && \
465 ++vm_page_throttled_count)
467 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
469 #define vm_page_lock_queues() mutex_lock(&vm_page_queue_lock)
470 #define vm_page_unlock_queues() mutex_unlock(&vm_page_queue_lock)
472 #define VM_PAGE_QUEUES_REMOVE(mem) \
474 assert(!mem->laundry); \
476 assert(mem->object != kernel_object); \
477 assert(!mem->inactive); \
478 queue_remove(&vm_page_queue_active, \
479 mem, vm_page_t, pageq); \
480 mem->pageq.next = NULL; \
481 mem->pageq.prev = NULL; \
482 mem->active = FALSE; \
483 if (!mem->fictitious) \
484 vm_page_active_count--; \
487 if (mem->inactive) { \
488 assert(mem->object != kernel_object); \
489 assert(!mem->active); \
490 if (mem->zero_fill) { \
491 queue_remove(&vm_page_queue_zf, \
492 mem, vm_page_t, pageq); \
494 queue_remove(&vm_page_queue_inactive, \
495 mem, vm_page_t, pageq); \
497 mem->pageq.next = NULL; \
498 mem->pageq.prev = NULL; \
499 mem->inactive = FALSE; \
500 if (!mem->fictitious) \
501 vm_page_inactive_count--; \
505 #endif /* _VM_VM_PAGE_H_ */