]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_page.h
xnu-792.22.5.tar.gz
[apple/xnu.git] / osfmk / vm / vm_page.h
1 /*
2 * Copyright (c) 2000-2005 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
80 /*
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.
89 */
90
91
92 extern unsigned int vm_page_ticket_roll;
93 extern unsigned int vm_page_ticket;
94
95
96 #define VM_PAGE_TICKETS_IN_ROLL 512
97 #define VM_PAGE_TICKET_ROLL_IDS 16
98
99 /*
100 * Management of resident (logical) pages.
101 *
102 * A small structure is kept for each resident
103 * page, indexed by page number. Each structure
104 * is an element of several lists:
105 *
106 * A hash table bucket used to quickly
107 * perform object/offset lookups
108 *
109 * A list of all pages for a given object,
110 * so they can be quickly deactivated at
111 * time of deallocation.
112 *
113 * An ordered list of pages due for pageout.
114 *
115 * In addition, the structure contains the object
116 * and offset to which this page belongs (for pageout),
117 * and sundry status bits.
118 *
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.]
123 */
124
125 struct vm_page {
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) */
130
131 vm_object_t object; /* which object am I in (O&P) */
132 vm_object_offset_t offset; /* offset into that object (O,P) */
133
134 /*
135 * The following word of flags is protected
136 * by the "page queues" lock.
137 */
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) */
151 zero_fill:1,
152 :0;
153
154 /*
155 * The following word of flags is protected
156 * by the "VM object" lock.
157 */
158 unsigned int
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
184 page locked */
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 */
195
196 ppnum_t phys_page; /* Physical address of page, passed
197 * to pmap_enter (read-only) */
198 };
199
200 #define DEBUG_ENCRYPTED_SWAP 1
201 #if DEBUG_ENCRYPTED_SWAP
202 #define ASSERT_PAGE_DECRYPTED(page) \
203 MACRO_BEGIN \
204 if ((page)->encrypted) { \
205 panic("VM page %p should not be encrypted here\n", \
206 (page)); \
207 } \
208 MACRO_END
209 #else /* DEBUG_ENCRYPTED_SWAP */
210 #define ASSERT_PAGE_DECRYPTED(page) assert(!(page)->encrypted)
211 #endif /* DEBUG_ENCRYPTED_SWAP */
212
213 typedef struct vm_page *vm_page_t;
214
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)
218
219 /*
220 * XXX The unusual bit should not be necessary. Most of the bit
221 * XXX fields above really want to be masks.
222 */
223
224 /*
225 * For debugging, this macro can be defined to perform
226 * some useful check on a page structure.
227 */
228
229 #define VM_PAGE_CHECK(mem)
230
231 /*
232 * Each pageable resident page falls into one of three lists:
233 *
234 * free
235 * Available for allocation now.
236 * inactive
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
240 * paged out next.
241 * active
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.
245 */
246
247 extern
248 vm_page_t vm_page_queue_free; /* memory free queue */
249 extern
250 vm_page_t vm_page_queue_fictitious; /* fictitious free queue */
251 extern
252 queue_head_t vm_page_queue_active; /* active memory queue */
253 extern
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 */
256
257 extern
258 vm_offset_t first_phys_addr; /* physical address for first_page */
259 extern
260 vm_offset_t last_phys_addr; /* physical address for last_page */
261
262 extern
263 unsigned int vm_page_free_count; /* How many pages are free? */
264 extern
265 unsigned int vm_page_fictitious_count;/* How many fictitious pages are free? */
266 extern
267 unsigned int vm_page_active_count; /* How many pages are active? */
268 extern
269 unsigned int vm_page_inactive_count; /* How many pages are inactive? */
270 extern
271 unsigned int vm_page_wire_count; /* How many pages are wired? */
272 extern
273 unsigned int vm_page_free_target; /* How many do we want free? */
274 extern
275 unsigned int vm_page_free_min; /* When to wakeup pageout */
276 extern
277 unsigned int vm_page_inactive_target;/* How many do we want inactive? */
278 extern
279 unsigned int vm_page_free_reserved; /* How many pages reserved to do pageout */
280 extern
281 unsigned int vm_page_throttled_count;/* Count of zero-fill allocations throttled */
282 extern
283 unsigned int vm_page_gobble_count;
284
285 extern
286 unsigned int vm_page_purgeable_count;/* How many pages are purgeable now ? */
287 extern
288 uint64_t vm_page_purged_count; /* How many pages got purged so far ? */
289
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 */
294
295 extern unsigned int vm_page_free_wanted;
296 /* how many threads are waiting for memory */
297
298 extern vm_offset_t vm_page_fictitious_addr;
299 /* (fake) phys_addr of fictitious pages */
300
301 extern boolean_t vm_page_deactivate_hint;
302
303 // 0 = all pages avail, 1 = disable high mem, 2 = prefer himem
304 extern int vm_himemory_mode;
305
306 extern ppnum_t vm_lopage_poolend;
307 extern int vm_lopage_poolsize;
308 extern uint64_t max_valid_dma_address;
309
310
311 /*
312 * Prototypes for functions exported by this module.
313 */
314 extern void vm_page_bootstrap(
315 vm_offset_t *startp,
316 vm_offset_t *endp);
317
318 extern void vm_page_module_init(void);
319
320 extern void vm_page_create(
321 ppnum_t start,
322 ppnum_t end);
323
324 extern vm_page_t vm_page_lookup(
325 vm_object_t object,
326 vm_object_offset_t offset);
327
328 extern vm_page_t vm_page_grab_fictitious(void);
329
330 extern void vm_page_release_fictitious(
331 vm_page_t page);
332
333 extern boolean_t vm_page_convert(
334 vm_page_t page);
335
336 extern void vm_page_more_fictitious(void);
337
338 extern int vm_pool_low(void);
339
340 extern vm_page_t vm_page_grab(void);
341
342 extern vm_page_t vm_page_grablo(void);
343
344 extern void vm_page_release(
345 vm_page_t page);
346
347 extern boolean_t vm_page_wait(
348 int interruptible );
349
350 extern vm_page_t vm_page_alloc(
351 vm_object_t object,
352 vm_object_offset_t offset);
353
354 extern vm_page_t vm_page_alloclo(
355 vm_object_t object,
356 vm_object_offset_t offset);
357
358 extern void vm_page_init(
359 vm_page_t page,
360 ppnum_t phys_page);
361
362 extern void vm_page_free(
363 vm_page_t page);
364
365 extern void vm_page_activate(
366 vm_page_t page);
367
368 extern void vm_page_deactivate(
369 vm_page_t page);
370
371 extern void vm_page_rename(
372 vm_page_t page,
373 vm_object_t new_object,
374 vm_object_offset_t new_offset);
375
376 extern void vm_page_insert(
377 vm_page_t page,
378 vm_object_t object,
379 vm_object_offset_t offset);
380
381 extern void vm_page_replace(
382 vm_page_t mem,
383 vm_object_t object,
384 vm_object_offset_t offset);
385
386 extern void vm_page_remove(
387 vm_page_t page);
388
389 extern void vm_page_zero_fill(
390 vm_page_t page);
391
392 extern void vm_page_part_zero_fill(
393 vm_page_t m,
394 vm_offset_t m_pa,
395 vm_size_t len);
396
397 extern void vm_page_copy(
398 vm_page_t src_page,
399 vm_page_t dest_page);
400
401 extern void vm_page_part_copy(
402 vm_page_t src_m,
403 vm_offset_t src_pa,
404 vm_page_t dst_m,
405 vm_offset_t dst_pa,
406 vm_size_t len);
407
408 extern void vm_page_wire(
409 vm_page_t page);
410
411 extern void vm_page_unwire(
412 vm_page_t page);
413
414 extern void vm_set_page_size(void);
415
416 extern void vm_page_gobble(
417 vm_page_t page);
418
419 /*
420 * Functions implemented as macros. m->wanted and m->busy are
421 * protected by the object lock.
422 */
423
424 #define PAGE_ASSERT_WAIT(m, interruptible) \
425 (((m)->wanted = TRUE), \
426 assert_wait((event_t) (m), (interruptible)))
427
428 #define PAGE_SLEEP(o, m, interruptible) \
429 (((m)->wanted = TRUE), \
430 thread_sleep_vm_object((o), (m), (interruptible)))
431
432 #define PAGE_WAKEUP_DONE(m) \
433 MACRO_BEGIN \
434 (m)->busy = FALSE; \
435 if ((m)->wanted) { \
436 (m)->wanted = FALSE; \
437 thread_wakeup((event_t) (m)); \
438 } \
439 MACRO_END
440
441 #define PAGE_WAKEUP(m) \
442 MACRO_BEGIN \
443 if ((m)->wanted) { \
444 (m)->wanted = FALSE; \
445 thread_wakeup((event_t) (m)); \
446 } \
447 MACRO_END
448
449 #define VM_PAGE_FREE(p) \
450 MACRO_BEGIN \
451 vm_page_lock_queues(); \
452 vm_page_free(p); \
453 vm_page_unlock_queues(); \
454 MACRO_END
455
456 #define VM_PAGE_GRAB_FICTITIOUS(M) \
457 MACRO_BEGIN \
458 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
459 vm_page_more_fictitious(); \
460 MACRO_END
461
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)
466
467 #define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
468
469 #define vm_page_lock_queues() mutex_lock(&vm_page_queue_lock)
470 #define vm_page_unlock_queues() mutex_unlock(&vm_page_queue_lock)
471
472 #define VM_PAGE_QUEUES_REMOVE(mem) \
473 MACRO_BEGIN \
474 assert(!mem->laundry); \
475 if (mem->active) { \
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--; \
485 } \
486 \
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); \
493 } else { \
494 queue_remove(&vm_page_queue_inactive, \
495 mem, vm_page_t, pageq); \
496 } \
497 mem->pageq.next = NULL; \
498 mem->pageq.prev = NULL; \
499 mem->inactive = FALSE; \
500 if (!mem->fictitious) \
501 vm_page_inactive_count--; \
502 } \
503 MACRO_END
504
505 #endif /* _VM_VM_PAGE_H_ */