]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_page.h
xnu-517.tar.gz
[apple/xnu.git] / osfmk / vm / vm_page.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * @OSF_COPYRIGHT@
27 */
28/*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53/*
54 */
55/*
56 * File: vm/vm_page.h
57 * Author: Avadis Tevanian, Jr., Michael Wayne Young
58 * Date: 1985
59 *
60 * Resident memory system definitions.
61 */
62
63#ifndef _VM_VM_PAGE_H_
64#define _VM_VM_PAGE_H_
65
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>
72
73#include <kern/macro_help.h>
74
0b4e3aa0
A
75/*
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.
84 */
85
86
87extern int vm_page_ticket_roll;
88extern int vm_page_ticket;
89
9bccf70c 90
0b4e3aa0
A
91#define VM_PAGE_TICKETS_IN_ROLL 512
92#define VM_PAGE_TICKET_ROLL_IDS 16
93
1c79356b
A
94/*
95 * Management of resident (logical) pages.
96 *
97 * A small structure is kept for each resident
98 * page, indexed by page number. Each structure
99 * is an element of several lists:
100 *
101 * A hash table bucket used to quickly
102 * perform object/offset lookups
103 *
104 * A list of all pages for a given object,
105 * so they can be quickly deactivated at
106 * time of deallocation.
107 *
108 * An ordered list of pages due for pageout.
109 *
110 * In addition, the structure contains the object
111 * and offset to which this page belongs (for pageout),
112 * and sundry status bits.
113 *
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.]
118 */
119
120struct vm_page {
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) */
125
126 vm_object_t object; /* which object am I in (O&P) */
127 vm_object_offset_t offset; /* offset into that object (O,P) */
128
129 unsigned int wire_count:16, /* how many wired down maps use me? (O&P) */
0b4e3aa0
A
130 page_ticket:4, /* age of the page on the */
131 /* inactive queue. */
1c79356b
A
132 /* boolean_t */ inactive:1, /* page is in inactive list (P) */
133 active:1, /* page is in active list (P) */
134 laundry:1, /* page is being cleaned now (P)*/
135 free:1, /* page is on free list (P) */
136 reference:1, /* page has been used (P) */
1c79356b 137 pageout:1, /* page wired & busy for pageout (P) */
0b4e3aa0
A
138 gobbled:1, /* page used internally (P) */
139 private:1, /* Page should not be returned to
140 * the free list (O) */
9bccf70c 141 zero_fill:1,
0b4e3aa0 142 :0;
1c79356b
A
143
144 unsigned int
0b4e3aa0 145 page_error:8, /* error from I/O operations */
1c79356b
A
146 /* boolean_t */ busy:1, /* page is in transit (O) */
147 wanted:1, /* someone is waiting for page (O) */
148 tabled:1, /* page is in VP table (O) */
149 fictitious:1, /* Physical page doesn't exist (O) */
0b4e3aa0 150 no_isync:1, /* page has not been instruction synced */
1c79356b
A
151 absent:1, /* Data has been requested, but is
152 * not yet available (O) */
153 error:1, /* Data manager was unable to provide
154 * data due to error (O) */
155 dirty:1, /* Page must be cleaned (O) */
156 cleaning:1, /* Page clean has begun (O) */
157 precious:1, /* Page is precious; data must be
158 * returned even if clean (O) */
159 clustered:1, /* page is not the faulted page (O) */
160 overwriting:1, /* Request to unlock has been made
161 * without having data. (O)
162 * [See vm_fault_page_overwrite] */
163 restart:1, /* Page was pushed higher in shadow
164 chain by copy_call-related pagers;
165 start again at top of chain */
166 lock_supplied:1,/* protection supplied by pager (O) */
167 /* vm_prot_t */ page_lock:3, /* Uses prohibited by pager (O) */
168 /* vm_prot_t */ unlock_request:3,/* Outstanding unlock request (O) */
169 unusual:1, /* Page is absent, error, restart or
170 page locked */
171 discard_request:1,/* a memory_object_discard_request()
172 * has been sent */
173 list_req_pending:1, /* pagein/pageout alt mechanism */
174 /* allows creation of list */
175 /* requests on pages that are */
176 /* actively being paged. */
0b4e3aa0
A
177 dump_cleaning:1; /* set by the pageout daemon when */
178 /* a page being cleaned is */
179 /* encountered and targeted as */
180 /* a pageout candidate */
181 /* we've used up all 32 bits */
1c79356b 182
55e303ae 183 vm_offset_t phys_page; /* Physical address of page, passed
1c79356b 184 * to pmap_enter (read-only) */
1c79356b
A
185};
186
187typedef struct vm_page *vm_page_t;
188
1c79356b
A
189#define VM_PAGE_NULL ((vm_page_t) 0)
190#define NEXT_PAGE(m) ((vm_page_t) (m)->pageq.next)
191
192/*
193 * XXX The unusual bit should not be necessary. Most of the bit
194 * XXX fields above really want to be masks.
195 */
196
197/*
198 * For debugging, this macro can be defined to perform
199 * some useful check on a page structure.
200 */
201
202#define VM_PAGE_CHECK(mem)
203
204/*
205 * Each pageable resident page falls into one of three lists:
206 *
207 * free
208 * Available for allocation now.
209 * inactive
210 * Not referenced in any map, but still has an
211 * object/offset-page mapping, and may be dirty.
212 * This is the list of pages that should be
213 * paged out next.
214 * active
215 * A list of pages which have been placed in
216 * at least one physical map. This list is
217 * ordered, in LRU-like fashion.
218 */
219
220extern
221vm_page_t vm_page_queue_free; /* memory free queue */
222extern
223vm_page_t vm_page_queue_fictitious; /* fictitious free queue */
224extern
225queue_head_t vm_page_queue_active; /* active memory queue */
226extern
227queue_head_t vm_page_queue_inactive; /* inactive memory queue */
9bccf70c 228queue_head_t vm_page_queue_zf; /* inactive memory queue for zero fill */
1c79356b
A
229
230extern
231vm_offset_t first_phys_addr; /* physical address for first_page */
232extern
233vm_offset_t last_phys_addr; /* physical address for last_page */
234
235extern
236int vm_page_free_count; /* How many pages are free? */
237extern
238int vm_page_fictitious_count;/* How many fictitious pages are free? */
239extern
240int vm_page_active_count; /* How many pages are active? */
241extern
242int vm_page_inactive_count; /* How many pages are inactive? */
243extern
244int vm_page_wire_count; /* How many pages are wired? */
245extern
246int vm_page_free_target; /* How many do we want free? */
247extern
248int vm_page_free_min; /* When to wakeup pageout */
249extern
250int vm_page_inactive_target;/* How many do we want inactive? */
251extern
252int vm_page_free_reserved; /* How many pages reserved to do pageout */
253extern
254int vm_page_laundry_count; /* How many pages being laundered? */
55e303ae
A
255extern
256int vm_page_burst_count; /* How many pages being laundered to EMM? */
257extern
258int vm_page_throttled_count;/* Count of zero-fill allocations throttled */
1c79356b
A
259
260decl_mutex_data(,vm_page_queue_lock)
261 /* lock on active and inactive page queues */
262decl_mutex_data(,vm_page_queue_free_lock)
263 /* lock on free page queue */
264decl_simple_lock_data(extern,vm_page_preppin_lock) /* lock for prep/pin */
9bccf70c 265decl_mutex_data(,vm_page_zero_fill_lock)
1c79356b
A
266
267extern unsigned int vm_page_free_wanted;
268 /* how many threads are waiting for memory */
269
270extern vm_offset_t vm_page_fictitious_addr;
271 /* (fake) phys_addr of fictitious pages */
272
273/*
274 * Prototypes for functions exported by this module.
275 */
276extern void vm_page_bootstrap(
277 vm_offset_t *startp,
278 vm_offset_t *endp);
279
280extern void vm_page_module_init(void);
281
282extern void vm_page_create(
55e303ae
A
283 ppnum_t start,
284 ppnum_t end);
1c79356b
A
285
286extern vm_page_t vm_page_lookup(
287 vm_object_t object,
288 vm_object_offset_t offset);
289
290extern vm_page_t vm_page_grab_fictitious(void);
291
292extern void vm_page_release_fictitious(
293 vm_page_t page);
294
295extern boolean_t vm_page_convert(
296 vm_page_t page);
297
298extern void vm_page_more_fictitious(void);
299
300extern int vm_pool_low(void);
301
302extern vm_page_t vm_page_grab(void);
303
304extern void vm_page_release(
305 vm_page_t page);
306
307extern void vm_page_release_limbo(
308 vm_page_t page);
309
310extern void vm_page_limbo_exchange(
311 vm_page_t limbo_m,
312 vm_page_t new_m);
313
314extern boolean_t vm_page_wait(
315 int interruptible );
316
317extern vm_page_t vm_page_alloc(
318 vm_object_t object,
319 vm_object_offset_t offset);
320
321extern void vm_page_init(
322 vm_page_t page,
55e303ae 323 ppnum_t phys_page);
1c79356b
A
324
325extern void vm_page_free(
326 vm_page_t page);
327
328extern void vm_page_activate(
329 vm_page_t page);
330
331extern void vm_page_deactivate(
332 vm_page_t page);
333
334extern void vm_page_rename(
335 vm_page_t page,
336 vm_object_t new_object,
337 vm_object_offset_t new_offset);
338
339extern void vm_page_insert(
340 vm_page_t page,
341 vm_object_t object,
342 vm_object_offset_t offset);
343
344extern void vm_page_replace(
345 vm_page_t mem,
346 vm_object_t object,
347 vm_object_offset_t offset);
348
349extern void vm_page_remove(
350 vm_page_t page);
351
352extern void vm_page_zero_fill(
353 vm_page_t page);
354
355extern void vm_page_part_zero_fill(
356 vm_page_t m,
357 vm_offset_t m_pa,
358 vm_size_t len);
359
360extern void vm_page_copy(
361 vm_page_t src_page,
362 vm_page_t dest_page);
363
364extern void vm_page_part_copy(
365 vm_page_t src_m,
366 vm_offset_t src_pa,
367 vm_page_t dst_m,
368 vm_offset_t dst_pa,
369 vm_size_t len);
370
371extern void vm_page_wire(
372 vm_page_t page);
373
374extern void vm_page_unwire(
375 vm_page_t page);
376
377extern void vm_set_page_size(void);
378
379extern void vm_page_gobble(
380 vm_page_t page);
381
1c79356b
A
382/*
383 * Functions implemented as macros. m->wanted and m->busy are
384 * protected by the object lock.
385 */
386
387#define PAGE_ASSERT_WAIT(m, interruptible) \
9bccf70c
A
388 (((m)->wanted = TRUE), \
389 assert_wait((event_t) (m), (interruptible)))
390
391#define PAGE_SLEEP(o, m, interruptible) \
392 (((m)->wanted = TRUE), \
393 thread_sleep_vm_object((o), (m), (interruptible)))
1c79356b
A
394
395#define PAGE_WAKEUP_DONE(m) \
396 MACRO_BEGIN \
397 (m)->busy = FALSE; \
398 if ((m)->wanted) { \
399 (m)->wanted = FALSE; \
400 thread_wakeup((event_t) (m)); \
401 } \
402 MACRO_END
403
404#define PAGE_WAKEUP(m) \
405 MACRO_BEGIN \
406 if ((m)->wanted) { \
407 (m)->wanted = FALSE; \
408 thread_wakeup((event_t) (m)); \
409 } \
410 MACRO_END
411
412#define VM_PAGE_FREE(p) \
413 MACRO_BEGIN \
414 vm_page_lock_queues(); \
415 vm_page_free(p); \
416 vm_page_unlock_queues(); \
417 MACRO_END
418
419#define VM_PAGE_GRAB_FICTITIOUS(M) \
420 MACRO_BEGIN \
421 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
422 vm_page_more_fictitious(); \
423 MACRO_END
424
425#define VM_PAGE_THROTTLED() \
55e303ae
A
426 (vm_page_free_count < vm_page_free_min && \
427 !current_thread()->vm_privilege && \
428 ++vm_page_throttled_count)
1c79356b
A
429
430#define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
431
432#define vm_page_lock_queues() mutex_lock(&vm_page_queue_lock)
433#define vm_page_unlock_queues() mutex_unlock(&vm_page_queue_lock)
434#define vm_page_pin_lock() simple_lock(&vm_page_preppin_lock)
435#define vm_page_pin_unlock() simple_unlock(&vm_page_preppin_lock)
436
437#define VM_PAGE_QUEUES_REMOVE(mem) \
438 MACRO_BEGIN \
439 if (mem->active) { \
440 assert(!mem->inactive); \
441 queue_remove(&vm_page_queue_active, \
442 mem, vm_page_t, pageq); \
443 mem->active = FALSE; \
444 if (!mem->fictitious) \
445 vm_page_active_count--; \
446 } \
447 \
448 if (mem->inactive) { \
449 assert(!mem->active); \
9bccf70c
A
450 if (mem->zero_fill) { \
451 queue_remove(&vm_page_queue_zf, \
452 mem, vm_page_t, pageq); \
453 } else { \
454 queue_remove(&vm_page_queue_inactive, \
1c79356b 455 mem, vm_page_t, pageq); \
9bccf70c 456 } \
1c79356b
A
457 mem->inactive = FALSE; \
458 if (!mem->fictitious) \
459 vm_page_inactive_count--; \
460 } \
461 MACRO_END
462
463#endif /* _VM_VM_PAGE_H_ */