]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_page.h
xnu-201.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 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_COPYRIGHT@
24 */
25/*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50/*
51 */
52/*
53 * File: vm/vm_page.h
54 * Author: Avadis Tevanian, Jr., Michael Wayne Young
55 * Date: 1985
56 *
57 * Resident memory system definitions.
58 */
59
60#ifndef _VM_VM_PAGE_H_
61#define _VM_VM_PAGE_H_
62
63#include <mach/boolean.h>
64#include <mach/vm_prot.h>
65#include <mach/vm_param.h>
66#include <vm/vm_object.h>
67#include <kern/queue.h>
68#include <kern/lock.h>
69
70#include <kern/macro_help.h>
71
0b4e3aa0
A
72/*
73 * Each page entered on the inactive queue obtains a ticket from a
74 * particular ticket roll. Pages granted tickets from a particular
75 * roll generally flow through the queue as a group. In this way when a
76 * page with a ticket from a particular roll is pulled from the top of the
77 * queue it is extremely likely that the pages near the top will have tickets
78 * from the same or adjacent rolls. In this way the proximity to the top
79 * of the queue can be loosely ascertained by determining the identity of
80 * the roll the pages ticket came from.
81 */
82
83
84extern int vm_page_ticket_roll;
85extern int vm_page_ticket;
86
87#define VM_PAGE_TICKETS_IN_ROLL 512
88#define VM_PAGE_TICKET_ROLL_IDS 16
89
1c79356b
A
90/*
91 * Management of resident (logical) pages.
92 *
93 * A small structure is kept for each resident
94 * page, indexed by page number. Each structure
95 * is an element of several lists:
96 *
97 * A hash table bucket used to quickly
98 * perform object/offset lookups
99 *
100 * A list of all pages for a given object,
101 * so they can be quickly deactivated at
102 * time of deallocation.
103 *
104 * An ordered list of pages due for pageout.
105 *
106 * In addition, the structure contains the object
107 * and offset to which this page belongs (for pageout),
108 * and sundry status bits.
109 *
110 * Fields in this structure are locked either by the lock on the
111 * object that the page belongs to (O) or by the lock on the page
112 * queues (P). [Some fields require that both locks be held to
113 * change that field; holding either lock is sufficient to read.]
114 */
115
116struct vm_page {
117 queue_chain_t pageq; /* queue info for FIFO
118 * queue or free list (P) */
119 queue_chain_t listq; /* all pages in same object (O) */
120 struct vm_page *next; /* VP bucket link (O) */
121
122 vm_object_t object; /* which object am I in (O&P) */
123 vm_object_offset_t offset; /* offset into that object (O,P) */
124
125 unsigned int wire_count:16, /* how many wired down maps use me? (O&P) */
0b4e3aa0
A
126 page_ticket:4, /* age of the page on the */
127 /* inactive queue. */
1c79356b
A
128 /* boolean_t */ inactive:1, /* page is in inactive list (P) */
129 active:1, /* page is in active list (P) */
130 laundry:1, /* page is being cleaned now (P)*/
131 free:1, /* page is on free list (P) */
132 reference:1, /* page has been used (P) */
1c79356b 133 pageout:1, /* page wired & busy for pageout (P) */
0b4e3aa0
A
134 gobbled:1, /* page used internally (P) */
135 private:1, /* Page should not be returned to
136 * the free list (O) */
137 :0;
1c79356b
A
138
139 unsigned int
0b4e3aa0 140 page_error:8, /* error from I/O operations */
1c79356b
A
141 /* boolean_t */ busy:1, /* page is in transit (O) */
142 wanted:1, /* someone is waiting for page (O) */
143 tabled:1, /* page is in VP table (O) */
144 fictitious:1, /* Physical page doesn't exist (O) */
0b4e3aa0 145 no_isync:1, /* page has not been instruction synced */
1c79356b
A
146 absent:1, /* Data has been requested, but is
147 * not yet available (O) */
148 error:1, /* Data manager was unable to provide
149 * data due to error (O) */
150 dirty:1, /* Page must be cleaned (O) */
151 cleaning:1, /* Page clean has begun (O) */
152 precious:1, /* Page is precious; data must be
153 * returned even if clean (O) */
154 clustered:1, /* page is not the faulted page (O) */
155 overwriting:1, /* Request to unlock has been made
156 * without having data. (O)
157 * [See vm_fault_page_overwrite] */
158 restart:1, /* Page was pushed higher in shadow
159 chain by copy_call-related pagers;
160 start again at top of chain */
161 lock_supplied:1,/* protection supplied by pager (O) */
162 /* vm_prot_t */ page_lock:3, /* Uses prohibited by pager (O) */
163 /* vm_prot_t */ unlock_request:3,/* Outstanding unlock request (O) */
164 unusual:1, /* Page is absent, error, restart or
165 page locked */
166 discard_request:1,/* a memory_object_discard_request()
167 * has been sent */
168 list_req_pending:1, /* pagein/pageout alt mechanism */
169 /* allows creation of list */
170 /* requests on pages that are */
171 /* actively being paged. */
0b4e3aa0
A
172 dump_cleaning:1; /* set by the pageout daemon when */
173 /* a page being cleaned is */
174 /* encountered and targeted as */
175 /* a pageout candidate */
176 /* we've used up all 32 bits */
1c79356b
A
177
178 vm_offset_t phys_addr; /* Physical address of page, passed
179 * to pmap_enter (read-only) */
1c79356b
A
180};
181
182typedef struct vm_page *vm_page_t;
183
1c79356b
A
184#define VM_PAGE_NULL ((vm_page_t) 0)
185#define NEXT_PAGE(m) ((vm_page_t) (m)->pageq.next)
186
187/*
188 * XXX The unusual bit should not be necessary. Most of the bit
189 * XXX fields above really want to be masks.
190 */
191
192/*
193 * For debugging, this macro can be defined to perform
194 * some useful check on a page structure.
195 */
196
197#define VM_PAGE_CHECK(mem)
198
199/*
200 * Each pageable resident page falls into one of three lists:
201 *
202 * free
203 * Available for allocation now.
204 * inactive
205 * Not referenced in any map, but still has an
206 * object/offset-page mapping, and may be dirty.
207 * This is the list of pages that should be
208 * paged out next.
209 * active
210 * A list of pages which have been placed in
211 * at least one physical map. This list is
212 * ordered, in LRU-like fashion.
213 */
214
215extern
216vm_page_t vm_page_queue_free; /* memory free queue */
217extern
218vm_page_t vm_page_queue_fictitious; /* fictitious free queue */
219extern
220queue_head_t vm_page_queue_active; /* active memory queue */
221extern
222queue_head_t vm_page_queue_inactive; /* inactive memory queue */
223
224extern
225vm_offset_t first_phys_addr; /* physical address for first_page */
226extern
227vm_offset_t last_phys_addr; /* physical address for last_page */
228
229extern
230int vm_page_free_count; /* How many pages are free? */
231extern
232int vm_page_fictitious_count;/* How many fictitious pages are free? */
233extern
234int vm_page_active_count; /* How many pages are active? */
235extern
236int vm_page_inactive_count; /* How many pages are inactive? */
237extern
238int vm_page_wire_count; /* How many pages are wired? */
239extern
240int vm_page_free_target; /* How many do we want free? */
241extern
242int vm_page_free_min; /* When to wakeup pageout */
243extern
244int vm_page_inactive_target;/* How many do we want inactive? */
245extern
246int vm_page_free_reserved; /* How many pages reserved to do pageout */
247extern
248int vm_page_laundry_count; /* How many pages being laundered? */
249
250decl_mutex_data(,vm_page_queue_lock)
251 /* lock on active and inactive page queues */
252decl_mutex_data(,vm_page_queue_free_lock)
253 /* lock on free page queue */
254decl_simple_lock_data(extern,vm_page_preppin_lock) /* lock for prep/pin */
255
256extern unsigned int vm_page_free_wanted;
257 /* how many threads are waiting for memory */
258
259extern vm_offset_t vm_page_fictitious_addr;
260 /* (fake) phys_addr of fictitious pages */
261
262/*
263 * Prototypes for functions exported by this module.
264 */
265extern void vm_page_bootstrap(
266 vm_offset_t *startp,
267 vm_offset_t *endp);
268
269extern void vm_page_module_init(void);
270
271extern void vm_page_create(
272 vm_offset_t start,
273 vm_offset_t end);
274
275extern vm_page_t vm_page_lookup(
276 vm_object_t object,
277 vm_object_offset_t offset);
278
279extern vm_page_t vm_page_grab_fictitious(void);
280
281extern void vm_page_release_fictitious(
282 vm_page_t page);
283
284extern boolean_t vm_page_convert(
285 vm_page_t page);
286
287extern void vm_page_more_fictitious(void);
288
289extern int vm_pool_low(void);
290
291extern vm_page_t vm_page_grab(void);
292
293extern void vm_page_release(
294 vm_page_t page);
295
296extern void vm_page_release_limbo(
297 vm_page_t page);
298
299extern void vm_page_limbo_exchange(
300 vm_page_t limbo_m,
301 vm_page_t new_m);
302
303extern boolean_t vm_page_wait(
304 int interruptible );
305
306extern vm_page_t vm_page_alloc(
307 vm_object_t object,
308 vm_object_offset_t offset);
309
310extern void vm_page_init(
311 vm_page_t page,
312 vm_offset_t phys_addr);
313
314extern void vm_page_free(
315 vm_page_t page);
316
317extern void vm_page_activate(
318 vm_page_t page);
319
320extern void vm_page_deactivate(
321 vm_page_t page);
322
323extern void vm_page_rename(
324 vm_page_t page,
325 vm_object_t new_object,
326 vm_object_offset_t new_offset);
327
328extern void vm_page_insert(
329 vm_page_t page,
330 vm_object_t object,
331 vm_object_offset_t offset);
332
333extern void vm_page_replace(
334 vm_page_t mem,
335 vm_object_t object,
336 vm_object_offset_t offset);
337
338extern void vm_page_remove(
339 vm_page_t page);
340
341extern void vm_page_zero_fill(
342 vm_page_t page);
343
344extern void vm_page_part_zero_fill(
345 vm_page_t m,
346 vm_offset_t m_pa,
347 vm_size_t len);
348
349extern void vm_page_copy(
350 vm_page_t src_page,
351 vm_page_t dest_page);
352
353extern void vm_page_part_copy(
354 vm_page_t src_m,
355 vm_offset_t src_pa,
356 vm_page_t dst_m,
357 vm_offset_t dst_pa,
358 vm_size_t len);
359
360extern void vm_page_wire(
361 vm_page_t page);
362
363extern void vm_page_unwire(
364 vm_page_t page);
365
366extern void vm_set_page_size(void);
367
368extern void vm_page_gobble(
369 vm_page_t page);
370
1c79356b
A
371/*
372 * Functions implemented as macros. m->wanted and m->busy are
373 * protected by the object lock.
374 */
375
376#define PAGE_ASSERT_WAIT(m, interruptible) \
377 MACRO_BEGIN \
378 (m)->wanted = TRUE; \
379 assert_wait((event_t) (m), (interruptible)); \
380 MACRO_END
381
382#define PAGE_WAKEUP_DONE(m) \
383 MACRO_BEGIN \
384 (m)->busy = FALSE; \
385 if ((m)->wanted) { \
386 (m)->wanted = FALSE; \
387 thread_wakeup((event_t) (m)); \
388 } \
389 MACRO_END
390
391#define PAGE_WAKEUP(m) \
392 MACRO_BEGIN \
393 if ((m)->wanted) { \
394 (m)->wanted = FALSE; \
395 thread_wakeup((event_t) (m)); \
396 } \
397 MACRO_END
398
399#define VM_PAGE_FREE(p) \
400 MACRO_BEGIN \
401 vm_page_lock_queues(); \
402 vm_page_free(p); \
403 vm_page_unlock_queues(); \
404 MACRO_END
405
406#define VM_PAGE_GRAB_FICTITIOUS(M) \
407 MACRO_BEGIN \
408 while ((M = vm_page_grab_fictitious()) == VM_PAGE_NULL) \
409 vm_page_more_fictitious(); \
410 MACRO_END
411
412#define VM_PAGE_THROTTLED() \
413 (vm_page_free_count < (vm_page_free_target - \
414 ((vm_page_free_target-vm_page_free_min)>>2)))
415
416#define VM_PAGE_WAIT() ((void)vm_page_wait(THREAD_UNINT))
417
418#define vm_page_lock_queues() mutex_lock(&vm_page_queue_lock)
419#define vm_page_unlock_queues() mutex_unlock(&vm_page_queue_lock)
420#define vm_page_pin_lock() simple_lock(&vm_page_preppin_lock)
421#define vm_page_pin_unlock() simple_unlock(&vm_page_preppin_lock)
422
423#define VM_PAGE_QUEUES_REMOVE(mem) \
424 MACRO_BEGIN \
425 if (mem->active) { \
426 assert(!mem->inactive); \
427 queue_remove(&vm_page_queue_active, \
428 mem, vm_page_t, pageq); \
429 mem->active = FALSE; \
430 if (!mem->fictitious) \
431 vm_page_active_count--; \
432 } \
433 \
434 if (mem->inactive) { \
435 assert(!mem->active); \
436 queue_remove(&vm_page_queue_inactive, \
437 mem, vm_page_t, pageq); \
438 mem->inactive = FALSE; \
439 if (!mem->fictitious) \
440 vm_page_inactive_count--; \
441 } \
442 MACRO_END
443
444#endif /* _VM_VM_PAGE_H_ */