]> git.saurik.com Git - apple/xnu.git/blob - osfmk/default_pager/default_pager_internal.h
xnu-792.6.56.tar.gz
[apple/xnu.git] / osfmk / default_pager / default_pager_internal.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * @OSF_COPYRIGHT@
25 */
26 /*
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
30 *
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
36 *
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 *
41 * Carnegie Mellon requests users of this software to return to
42 *
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
47 *
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
50 */
51
52 /*
53 * Default pager.
54 * General definitions.
55 */
56
57 #ifndef _DEFAULT_PAGER_INTERNAL_H_
58 #define _DEFAULT_PAGER_INTERNAL_H_
59
60 #include <default_pager/diag.h>
61 #include <default_pager/default_pager_types.h>
62 #include <mach/mach_types.h>
63 #include <ipc/ipc_port.h>
64 #include <ipc/ipc_types.h>
65 #include <ipc/ipc_space.h>
66 #include <kern/lock.h>
67 #include <kern/kalloc.h>
68 #include <kern/thread.h>
69 #include <vm/vm_kern.h>
70 #include <device/device_types.h>
71
72 /*
73 * Default option settings.
74 */
75 #ifndef PARALLEL
76 #define PARALLEL 1
77 #endif
78
79 #ifndef CHECKSUM
80 #define CHECKSUM 0
81 #endif
82
83 #define MACH_PORT_FACE mach_port_t
84
85 #if 0
86 #ifndef USE_PRECIOUS
87 #define USE_PRECIOUS TRUE
88 #endif
89 #endif
90
91 #ifdef USER_PAGER
92 #define UP(stuff) stuff
93 #else /* USER_PAGER */
94 #define UP(stuff)
95 #endif /* USER_PAGER */
96
97 #ifndef MACH_KERNEL
98 extern struct mutex dprintf_lock;
99 #define PRINTF_LOCK_INIT() mutex_init(&dprintf_lock)
100 #define PRINTF_LOCK() mutex_lock(&dprintf_lock)
101 #define PRINTF_UNLOCK() mutex_unlock(&dprintf_lock)
102 #endif
103
104 #ifndef MACH_KERNEL
105 #define dprintf(args) \
106 do { \
107 PRINTF_LOCK(); \
108 printf("%s[%d]: ", my_name, dp_thread_id()); \
109 printf args; \
110 PRINTF_UNLOCK(); \
111 } while (0)
112 #else
113 #define dprintf(args) \
114 do { \
115 printf("%s[KERNEL]: ", my_name); \
116 printf args; \
117 } while (0)
118 #endif
119
120 /*
121 * Debug.
122 */
123 __private_extern__ char my_name[];
124
125 #define DEFAULT_PAGER_DEBUG 0
126
127 #if DEFAULT_PAGER_DEBUG
128
129 extern int debug_mask;
130 #define DEBUG_MSG_EXTERNAL 0x00000001
131 #define DEBUG_MSG_INTERNAL 0x00000002
132 #define DEBUG_MO_EXTERNAL 0x00000100
133 #define DEBUG_MO_INTERNAL 0x00000200
134 #define DEBUG_VS_EXTERNAL 0x00010000
135 #define DEBUG_VS_INTERNAL 0x00020000
136 #define DEBUG_BS_EXTERNAL 0x01000000
137 #define DEBUG_BS_INTERNAL 0x02000000
138
139 #define DP_DEBUG(level, args) \
140 do { \
141 if (debug_mask & (level)) \
142 dprintf(args); \
143 } while (0)
144
145 #define ASSERT(expr) \
146 do { \
147 if (!(expr)) \
148 #ifndef MACH_KERNEL
149 panic("%s[%d]%s: assertion failed in %s line %d: %s",\
150 my_name, dp_thread_id(), here, \
151 __FILE__, __LINE__, # expr); \
152 #else
153 panic("%s[KERNEL]: assertion failed in %s line %d: %s",\
154 my_name, __FILE__, __LINE__, # expr); \
155 #endif
156 } while (0)
157
158 #else /* DEFAULT_PAGER_DEBUG */
159
160 #define DP_DEBUG(level, args)
161 #define ASSERT(clause)
162
163 #endif /* DEFAULT_PAGER_DEBUG */
164
165 #ifndef MACH_KERNEL
166 extern char *mach_error_string(kern_return_t);
167 #endif
168
169 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
170
171 #define PAGER_SUCCESS 0
172 #define PAGER_FULL 1
173 #define PAGER_ERROR 2
174
175 /*
176 * VM and IPC globals.
177 */
178 #ifdef MACH_KERNEL
179 #define vm_page_size page_size
180 #else
181 extern vm_object_size_t vm_page_size;
182 #endif
183 extern unsigned long long vm_page_mask;
184 extern int vm_page_shift;
185
186 #ifndef MACH_KERNEL
187 #define ptoa(p) ((p)*vm_page_size)
188 #define atop(a) ((a)/vm_page_size)
189 #endif
190 #define howmany(a,b) (((a) + (b) - 1)/(b))
191
192 extern memory_object_default_t default_pager_object;
193
194 #ifdef MACH_KERNEL
195 extern mutex_t dpt_lock; /* Lock for the dpt array */
196 extern int default_pager_internal_count;
197 extern MACH_PORT_FACE default_pager_host_port;
198 /* extern task_t default_pager_self; */ /* dont need or want */
199 extern MACH_PORT_FACE default_pager_internal_set;
200 extern MACH_PORT_FACE default_pager_external_set;
201 extern MACH_PORT_FACE default_pager_default_set;
202 #else
203 extern mach_port_t default_pager_host_port;
204 extern task_port_t default_pager_self;
205 extern mach_port_t default_pager_internal_set;
206 extern mach_port_t default_pager_external_set;
207 extern mach_port_t default_pager_default_set;
208 #endif
209
210 typedef struct default_pager_thread {
211 #ifndef MACH_KERNEL
212 cthread_t dpt_thread; /* Server thread. */
213 #endif
214 vm_offset_t dpt_buffer; /* Read buffer. */
215 boolean_t dpt_internal; /* Do we handle internal objects? */
216 #ifndef MACH_KERNEL
217 int dpt_id; /* thread id for printf */
218 #else
219 int checked_out;
220 #endif
221 boolean_t dpt_initialized_p; /* Thread is ready for requests. */
222 } default_pager_thread_t;
223
224 #ifdef MACH_KERNEL
225 extern default_pager_thread_t **dpt_array;
226 #endif
227
228 /*
229 * Global statistics.
230 */
231 struct {
232 unsigned int gs_pageout_calls; /* # pageout calls */
233 unsigned int gs_pagein_calls; /* # pagein calls */
234 unsigned int gs_pages_in; /* # pages paged in (total) */
235 unsigned int gs_pages_out; /* # pages paged out (total) */
236 unsigned int gs_pages_unavail; /* # zero-fill pages */
237 unsigned int gs_pages_init; /* # page init requests */
238 unsigned int gs_pages_init_writes; /* # page init writes */
239 VSTATS_LOCK_DECL(gs_lock)
240 } global_stats;
241 #define GSTAT(clause) VSTATS_ACTION(&global_stats.gs_lock, (clause))
242
243 /*
244 * Cluster related definitions.
245 * Clusters are sized in number of pages per cluster.
246 * Cluster sizes must be powers of two.
247 *
248 * These numbers are related to the struct vs_map,
249 * defined below.
250 */
251 #define MAX_CLUSTER_SIZE 8
252 #define MAX_CLUSTER_SHIFT 3
253 #define NO_CLSIZE 0
254
255 /*
256 * bit map related macros
257 */
258 #define NBBY 8 /* bits per byte XXX */
259 #define BYTEMASK 0xff
260 #define setbit(a,i) (*(((char *)(a)) + ((i)/NBBY)) |= 1<<((i)%NBBY))
261 #define clrbit(a,i) (*(((char *)(a)) + ((i)/NBBY)) &= ~(1<<((i)%NBBY)))
262 #define isset(a,i) (*(((char *)(a)) + ((i)/NBBY)) & (1<<((i)%NBBY)))
263 #define isclr(a,i) ((*(((char *)(a)) + ((i)/NBBY)) & (1<<((i)%NBBY))) == 0)
264
265 /*
266 * Default Pager.
267 * Backing Store Management.
268 */
269
270 #define BS_MAXPRI 4
271 #define BS_MINPRI 0
272 #define BS_NOPRI -1
273 #define BS_FULLPRI -2
274
275 /*
276 * Mapping between backing store port and backing store object.
277 */
278 struct backing_store {
279 queue_chain_t bs_links; /* link in backing_store_list */
280 #ifdef MACH_KERNEL
281 mutex_t bs_lock; /* lock for the structure */
282 #else
283 struct mutex bs_lock; /* lock for the structure */
284 #endif
285 MACH_PORT_FACE bs_port; /* backing store port */
286 int bs_priority;
287 int bs_clsize; /* cluster size in pages */
288
289 /* statistics */
290 unsigned int bs_pages_free; /* # unallocated pages */
291 unsigned int bs_pages_total; /* # pages (total) */
292 unsigned int bs_pages_in; /* # page read requests */
293 unsigned int bs_pages_in_fail; /* # page read errors */
294 unsigned int bs_pages_out; /* # page write requests */
295 unsigned int bs_pages_out_fail; /* # page write errors */
296 };
297 typedef struct backing_store *backing_store_t;
298 #define BACKING_STORE_NULL ((backing_store_t) 0)
299 #define BS_STAT(bs, clause) VSTATS_ACTION(&(bs)->bs_lock, (clause))
300
301 #ifdef MACH_KERNEL
302 #define BS_LOCK_INIT(bs) mutex_init(&(bs)->bs_lock, 0)
303 #else
304 #define BS_LOCK_INIT(bs) mutex_init(&(bs)->bs_lock)
305 #endif
306 #define BS_LOCK(bs) mutex_lock(&(bs)->bs_lock)
307 #define BS_UNLOCK(bs) mutex_unlock(&(bs)->bs_lock)
308
309 struct backing_store_list_head {
310 queue_head_t bsl_queue;
311 #ifdef MACH_KERNEL
312 mutex_t bsl_lock;
313 #else
314 struct mutex bsl_lock;
315 #endif
316 };
317 extern struct backing_store_list_head backing_store_list;
318 extern int backing_store_release_trigger_disable;
319
320 #ifdef MACH_KERNEL
321 #define BSL_LOCK_INIT() mutex_init(&backing_store_list.bsl_lock, 0)
322 #else
323 #define BSL_LOCK_INIT() mutex_init(&backing_store_list.bsl_lock)
324 #endif
325 #define BSL_LOCK() mutex_lock(&backing_store_list.bsl_lock)
326 #define BSL_UNLOCK() mutex_unlock(&backing_store_list.bsl_lock)
327
328 /*
329 * Paging segment management.
330 * Controls allocation of blocks within paging area.
331 */
332 struct paging_segment {
333 /* device management */
334 union {
335 MACH_PORT_FACE dev; /* Port to device */
336 struct vnode *vnode; /* vnode for bs file */
337 } storage_type;
338 unsigned int ps_segtype; /* file type or partition */
339 MACH_PORT_FACE ps_device; /* Port to device */
340 vm_offset_t ps_offset; /* Offset of segment within device */
341 vm_offset_t ps_recnum; /* Number of device records in segment*/
342 unsigned int ps_pgnum; /* Number of pages in segment */
343 unsigned int ps_record_shift;/* Bit shift: pages to device records */
344
345 /* clusters and pages */
346 unsigned int ps_clshift; /* Bit shift: clusters to pages */
347 unsigned int ps_ncls; /* Number of clusters in segment */
348 unsigned int ps_clcount; /* Number of free clusters */
349 unsigned int ps_pgcount; /* Number of free pages */
350 unsigned long ps_hint; /* Hint of where to look next. */
351
352 /* bitmap */
353 #ifdef MACH_KERNEL
354 mutex_t ps_lock; /* Lock for contents of struct */
355 #else
356 struct mutex ps_lock; /* Lock for contents of struct */
357 #endif
358 unsigned char *ps_bmap; /* Map of used clusters */
359
360 /* backing store */
361 backing_store_t ps_bs; /* Backing store segment belongs to */
362
363 boolean_t ps_going_away; /* Destroy attempt in progress */
364 };
365
366 #define ps_vnode storage_type.vnode
367 #define ps_device storage_type.dev
368 #define PS_PARTITION 1
369 #define PS_FILE 2
370
371 typedef struct paging_segment *paging_segment_t;
372
373 #define PAGING_SEGMENT_NULL ((paging_segment_t) 0)
374
375 #ifdef MACH_KERNEL
376 #define PS_LOCK_INIT(ps) mutex_init(&(ps)->ps_lock, 0)
377 #else
378 #define PS_LOCK_INIT(ps) mutex_init(&(ps)->ps_lock)
379 #endif
380 #define PS_LOCK(ps) mutex_lock(&(ps)->ps_lock)
381 #define PS_UNLOCK(ps) mutex_unlock(&(ps)->ps_lock)
382
383 typedef unsigned int pseg_index_t;
384
385 #define INVALID_PSEG_INDEX ((pseg_index_t)-1)
386 #define NULL_PSEG_INDEX ((pseg_index_t) 0)
387 /*
388 * MAX_PSEG_INDEX value is related to struct vs_map below.
389 * "0" is reserved for empty map entries (no segment).
390 */
391 #define MAX_PSEG_INDEX 63 /* 0 is reserved for empty map */
392 #define MAX_NUM_PAGING_SEGMENTS MAX_PSEG_INDEX
393
394 /* paging segments array */
395 extern paging_segment_t paging_segments[MAX_NUM_PAGING_SEGMENTS];
396 #ifdef MACH_KERNEL
397 extern mutex_t paging_segments_lock;
398 #else
399 extern struct mutex paging_segments_lock;
400 #endif
401 extern int paging_segment_count; /* number of active paging segments */
402 extern int paging_segment_max; /* highest used paging segment index */
403 extern int ps_select_array[DEFAULT_PAGER_BACKING_STORE_MAXPRI+1];
404
405 #ifdef MACH_KERNEL
406 #define PSL_LOCK_INIT() mutex_init(&paging_segments_lock, 0)
407 #else
408 #define PSL_LOCK_INIT() mutex_init(&paging_segments_lock)
409 #endif
410 #define PSL_LOCK() mutex_lock(&paging_segments_lock)
411 #define PSL_UNLOCK() mutex_unlock(&paging_segments_lock)
412
413 /*
414 * Vstruct manipulation. The vstruct is the pager's internal
415 * representation of vm objects it manages. There is one vstruct allocated
416 * per vm object.
417 *
418 * The following data structures are defined for vstruct and vm object
419 * management.
420 */
421
422 /*
423 * vs_map
424 * A structure used only for temporary objects. It is the element
425 * contained in the vs_clmap structure, which contains information
426 * about which clusters and pages in an object are present on backing
427 * store (a paging file).
428 * Note that this structure and its associated constants may change
429 * with minimal impact on code. The only function which knows the
430 * internals of this structure is ps_clmap().
431 *
432 * If it is necessary to change the maximum number of paging segments
433 * or pages in a cluster, then this structure is the one most
434 * affected. The constants and structures which *may* change are:
435 * MAX_CLUSTER_SIZE
436 * MAX_CLUSTER_SHIFT
437 * MAX_NUM_PAGING_SEGMENTS
438 * VSTRUCT_DEF_CLSHIFT
439 * struct vs_map and associated macros and constants (VSM_*)
440 * (only the macro definitions need change, the exported (inside the
441 * pager only) interfaces remain the same; the constants are for
442 * internal vs_map manipulation only).
443 * struct clbmap (below).
444 */
445 struct vs_map {
446 unsigned int vsmap_entry:23, /* offset in paging segment */
447 vsmap_psindex:8, /* paging segment */
448 vsmap_error:1,
449 vsmap_bmap:16,
450 vsmap_alloc:16;
451 };
452
453 typedef struct vs_map *vs_map_t;
454
455
456 #define VSM_ENTRY_NULL 0x7fffff
457
458 /*
459 * Exported macros for manipulating the vs_map structure --
460 * checking status, getting and setting bits.
461 */
462 #define VSCLSIZE(vs) (1UL << (vs)->vs_clshift)
463 #define VSM_ISCLR(vsm) (((vsm).vsmap_entry == VSM_ENTRY_NULL) && \
464 ((vsm).vsmap_error == 0))
465 #define VSM_ISERR(vsm) ((vsm).vsmap_error)
466 #define VSM_SETCLOFF(vsm, val) ((vsm).vsmap_entry = (val))
467 #define VSM_SETERR(vsm, err) ((vsm).vsmap_error = 1, \
468 (vsm).vsmap_entry = (err))
469 #define VSM_GETERR(vsm) ((vsm).vsmap_entry)
470 #define VSM_SETPG(vsm, page) ((vsm).vsmap_bmap |= (1 << (page)))
471 #define VSM_CLRPG(vsm, page) ((vsm).vsmap_bmap &= ~(1 << (page)))
472 #define VSM_SETPS(vsm, psindx) ((vsm).vsmap_psindex = (psindx))
473 #define VSM_PSINDEX(vsm) ((vsm).vsmap_psindex)
474 #define VSM_PS(vsm) paging_segments[(vsm).vsmap_psindex]
475 #define VSM_BMAP(vsm) ((vsm).vsmap_bmap)
476 #define VSM_CLOFF(vsm) ((vsm).vsmap_entry)
477 #define VSM_CLR(vsm) ((vsm).vsmap_entry = VSM_ENTRY_NULL, \
478 (vsm).vsmap_psindex = 0, \
479 (vsm).vsmap_error = 0, \
480 (vsm).vsmap_bmap = 0, \
481 (vsm).vsmap_alloc = 0)
482 #define VSM_ALLOC(vsm) ((vsm).vsmap_alloc)
483 #define VSM_SETALLOC(vsm, page) ((vsm).vsmap_alloc |= (1 << (page)))
484 #define VSM_CLRALLOC(vsm, page) ((vsm).vsmap_alloc &= ~(1 << (page)))
485
486 /*
487 * Constants and macros for dealing with vstruct maps,
488 * which comprise vs_map structures, which
489 * map vm objects to backing storage (paging files and clusters).
490 */
491 #define CLMAP_THRESHOLD 512 /* bytes */
492 #define CLMAP_ENTRIES (CLMAP_THRESHOLD/sizeof(struct vs_map))
493 #define CLMAP_SIZE(ncls) (ncls*sizeof(struct vs_map))
494
495 #define INDIRECT_CLMAP_ENTRIES(ncls) (((ncls-1)/CLMAP_ENTRIES) + 1)
496 #define INDIRECT_CLMAP_SIZE(ncls) (INDIRECT_CLMAP_ENTRIES(ncls) * sizeof(struct vs_map *))
497 #define INDIRECT_CLMAP(size) (CLMAP_SIZE(size) > CLMAP_THRESHOLD)
498
499 #define RMAPSIZE(blocks) (howmany(blocks,NBBY))
500
501 #define CL_FIND 1
502 #define CL_ALLOC 2
503
504 /*
505 * clmap
506 *
507 * A cluster map returned by ps_clmap. It is an abstracted cluster of
508 * pages. It gives the caller information about the cluster
509 * desired. On read it tells the caller if a cluster is mapped, and if so,
510 * which of its pages are valid. It should not be referenced directly,
511 * except by ps_clmap; macros should be used. If the number of pages
512 * in a cluster needs to be more than 32, then the struct clbmap must
513 * become larger.
514 */
515 struct clbmap {
516 unsigned int clb_map;
517 };
518
519 struct clmap {
520 paging_segment_t cl_ps; /* paging segment backing cluster */
521 int cl_numpages; /* number of valid pages */
522 struct clbmap cl_bmap; /* map of pages in cluster */
523 int cl_error; /* cluster error value */
524 struct clbmap cl_alloc; /* map of allocated pages in cluster */
525 };
526
527 #define CLMAP_ERROR(clm) (clm).cl_error
528 #define CLMAP_PS(clm) (clm).cl_ps
529 #define CLMAP_NPGS(clm) (clm).cl_numpages
530 #define CLMAP_ISSET(clm,i) ((1<<(i))&((clm).cl_bmap.clb_map))
531 #define CLMAP_ALLOC(clm) (clm).cl_alloc.clb_map
532 /*
533 * Shift off unused bits in a partial cluster
534 */
535 #define CLMAP_SHIFT(clm,vs) \
536 (clm)->cl_bmap.clb_map >>= (VSCLSIZE(vs) - (clm)->cl_numpages)
537 #define CLMAP_SHIFTALLOC(clm,vs) \
538 (clm)->cl_alloc.clb_map >>= (VSCLSIZE(vs) - (clm)->cl_numpages)
539
540 typedef struct vstruct_alias {
541 int *name;
542 struct vstruct *vs;
543 } vstruct_alias_t;
544
545 #ifdef MACH_KERNEL
546 #define DPT_LOCK_INIT(lock) mutex_init(&(lock), 0)
547 #define DPT_LOCK(lock) mutex_lock(&(lock))
548 #define DPT_UNLOCK(lock) mutex_unlock(&(lock))
549 #define DPT_SLEEP(lock, e, i) thread_sleep_mutex(&(lock), (event_t)(e), i)
550 #define VS_LOCK_TYPE hw_lock_data_t
551 #define VS_LOCK_INIT(vs) hw_lock_init(&(vs)->vs_lock)
552 #define VS_TRY_LOCK(vs) (VS_LOCK(vs),TRUE)
553 #define VS_LOCK(vs) hw_lock_lock(&(vs)->vs_lock)
554 #define VS_UNLOCK(vs) hw_lock_unlock(&(vs)->vs_lock)
555 #define VS_MAP_LOCK_TYPE mutex_t
556 #define VS_MAP_LOCK_INIT(vs) mutex_init(&(vs)->vs_map_lock, 0)
557 #define VS_MAP_LOCK(vs) mutex_lock(&(vs)->vs_map_lock)
558 #define VS_MAP_TRY_LOCK(vs) mutex_try(&(vs)->vs_map_lock)
559 #define VS_MAP_UNLOCK(vs) mutex_unlock(&(vs)->vs_map_lock)
560 #else
561 #define VS_LOCK_TYPE struct mutex
562 #define VS_LOCK_INIT(vs) mutex_init(&(vs)->vs_lock, 0)
563 #define VS_TRY_LOCK(vs) mutex_try(&(vs)->vs_lock)
564 #define VS_LOCK(vs) mutex_lock(&(vs)->vs_lock)
565 #define VS_UNLOCK(vs) mutex_unlock(&(vs)->vs_lock)
566 #define VS_MAP_LOCK_TYPE struct mutex
567 #define VS_MAP_LOCK_INIT(vs) mutex_init(&(vs)->vs_map_lock)
568 #define VS_MAP_LOCK(vs) mutex_lock(&(vs)->vs_map_lock)
569 #define VS_MAP_TRY_LOCK(vs) mutex_try(&(vs)->vs_map_lock)
570 #define VS_MAP_UNLOCK(vs) mutex_unlock(&(vs)->vs_map_lock)
571 #endif
572
573
574 /*
575 * VM Object Structure: This is the structure used to manage
576 * default pager object associations with their control counter-
577 * parts (VM objects).
578 */
579 typedef struct vstruct {
580 int *vs_mem_obj; /* our memory obj - temp */
581 int vs_mem_obj_ikot;/* JMM:fake ip_kotype() */
582 memory_object_control_t vs_control; /* our mem obj control ref */
583 VS_LOCK_TYPE vs_lock; /* data for the lock */
584
585 /* JMM - Could combine these first two in a single pending count now */
586 unsigned int vs_next_seqno; /* next sequence num to issue */
587 unsigned int vs_seqno; /* Pager port sequence number */
588 unsigned int vs_readers; /* Reads in progress */
589 unsigned int vs_writers; /* Writes in progress */
590
591 #ifdef MACH_KERNEL
592 int
593 /* boolean_t */ vs_waiting_seqno:1, /* to wait on seqno */
594 /* boolean_t */ vs_waiting_read:1, /* waiting on reader? */
595 /* boolean_t */ vs_waiting_write:1, /* waiting on writer? */
596 /* boolean_t */ vs_waiting_async:1, /* waiting on async? */
597 /* boolean_t */ vs_indirect:1, /* map indirect? */
598 /* boolean_t */ vs_xfer_pending:1; /* xfer out of seg? */
599 #else
600 event_t vs_waiting_seqno;/* to wait on seqno */
601 event_t vs_waiting_read; /* to wait on readers */
602 event_t vs_waiting_write;/* to wait on writers */
603 event_t vs_waiting_async;/* to wait on async_pending */
604 int vs_indirect:1, /* Is the map indirect ? */
605 vs_xfer_pending:1; /* xfering out of a seg ? */
606 #endif
607
608 unsigned int vs_async_pending;/* pending async write count */
609 unsigned int vs_errors; /* Pageout error count */
610 unsigned int vs_references; /* references */
611
612 queue_chain_t vs_links; /* Link in pager-wide list */
613
614 unsigned int vs_clshift; /* Bit shift: clusters->pages */
615 unsigned int vs_size; /* Object size in clusters */
616 #ifdef MACH_KERNEL
617 mutex_t vs_map_lock; /* to protect map below */
618 #else
619 struct mutex vs_map_lock; /* to protect map below */
620 #endif
621 union {
622 struct vs_map *vsu_dmap; /* Direct map of clusters */
623 struct vs_map **vsu_imap; /* Indirect map of clusters */
624 } vs_un;
625 } *vstruct_t;
626
627 #define vs_dmap vs_un.vsu_dmap
628 #define vs_imap vs_un.vsu_imap
629
630 #define VSTRUCT_NULL ((vstruct_t) 0)
631
632 __private_extern__ void vs_async_wait(vstruct_t);
633
634 #if PARALLEL
635 __private_extern__ void vs_lock(vstruct_t);
636 __private_extern__ void vs_unlock(vstruct_t);
637 __private_extern__ void vs_start_read(vstruct_t);
638 __private_extern__ void vs_finish_read(vstruct_t);
639 __private_extern__ void vs_wait_for_readers(vstruct_t);
640 __private_extern__ void vs_start_write(vstruct_t);
641 __private_extern__ void vs_finish_write(vstruct_t);
642 __private_extern__ void vs_wait_for_writers(vstruct_t);
643 __private_extern__ void vs_wait_for_sync_writers(vstruct_t);
644 #else /* PARALLEL */
645 #define vs_lock(vs)
646 #define vs_unlock(vs)
647 #define vs_start_read(vs)
648 #define vs_wait_for_readers(vs)
649 #define vs_finish_read(vs)
650 #define vs_start_write(vs)
651 #define vs_wait_for_writers(vs)
652 #define vs_wait_for_sync_writers(vs)
653 #define vs_finish_write(vs)
654 #endif /* PARALLEL */
655
656 /*
657 * Data structures and variables dealing with asynchronous
658 * completion of paging operations.
659 */
660 /*
661 * vs_async
662 * A structure passed to ps_write_device for asynchronous completions.
663 * It contains enough information to complete the write and
664 * inform the VM of its completion.
665 */
666 struct vs_async {
667 struct vs_async *vsa_next; /* pointer to next structure */
668 vstruct_t vsa_vs; /* the vstruct for the object */
669 vm_offset_t vsa_addr; /* the vaddr of the data moved */
670 vm_offset_t vsa_offset; /* the object offset of the data */
671 vm_size_t vsa_size; /* the number of bytes moved */
672 paging_segment_t vsa_ps; /* the paging segment used */
673 int vsa_flags; /* flags */
674 int vsa_error; /* error, if there is one */
675 mutex_t vsa_lock;
676 MACH_PORT_FACE reply_port; /* associated reply port */
677 };
678
679 /*
680 * flags values.
681 */
682 #define VSA_READ 0x0001
683 #define VSA_WRITE 0x0002
684 #define VSA_TRANSFER 0x0004
685
686 /*
687 * List of all vstructs. A specific vstruct is
688 * found directly via its port, this list is
689 * only used for monitoring purposes by the
690 * default_pager_object* calls
691 */
692 struct vstruct_list_head {
693 queue_head_t vsl_queue;
694 #ifdef MACH_KERNEL
695 mutex_t vsl_lock;
696 #else
697 struct mutex vsl_lock;
698 #endif
699 int vsl_count; /* saves code */
700 };
701
702 __private_extern__ struct vstruct_list_head vstruct_list;
703
704 __private_extern__ void vstruct_list_insert(vstruct_t vs);
705 __private_extern__ void vstruct_list_delete(vstruct_t vs);
706
707
708 #ifdef MACH_KERNEL
709 #define VSL_LOCK_INIT() mutex_init(&vstruct_list.vsl_lock, 0)
710 #else
711 #define VSL_LOCK_INIT() mutex_init(&vstruct_list.vsl_lock)
712 #endif
713 #define VSL_LOCK() mutex_lock(&vstruct_list.vsl_lock)
714 #define VSL_LOCK_TRY() mutex_try(&vstruct_list.vsl_lock)
715 #define VSL_UNLOCK() mutex_unlock(&vstruct_list.vsl_lock)
716 #define VSL_SLEEP(e,i) thread_sleep_mutex((e), &vstruct_list.vsl_lock, (i))
717
718 #ifdef MACH_KERNEL
719 __private_extern__ zone_t vstruct_zone;
720 #endif
721
722 /*
723 * Create port alias for vstruct address.
724 *
725 * We assume that the last two bits of a vstruct address will be zero due to
726 * memory allocation restrictions, hence are available for use as a sanity
727 * check.
728 */
729 #ifdef MACH_KERNEL
730
731 #define ISVS ((int *)123456)
732 #define mem_obj_is_vs(_mem_obj_) \
733 (((_mem_obj_) != NULL) && ((_mem_obj_)->pager == ISVS))
734 #define mem_obj_to_vs(_mem_obj_) \
735 ((vstruct_t)(_mem_obj_))
736 #define vs_to_mem_obj(_vs_) ((memory_object_t)(_vs_))
737 #define vs_lookup(_mem_obj_, _vs_) \
738 do { \
739 if (!mem_obj_is_vs(_mem_obj_)) \
740 panic("bad dp memory object"); \
741 _vs_ = mem_obj_to_vs(_mem_obj_); \
742 } while (0)
743 #define vs_lookup_safe(_mem_obj_, _vs_) \
744 do { \
745 if (!mem_obj_is_vs(_mem_obj_)) \
746 _vs_ = VSTRUCT_NULL; \
747 else \
748 _vs_ = mem_obj_to_vs(_mem_obj_); \
749 } while (0)
750 #else
751
752 #define vs_to_port(_vs_) (((vm_offset_t)(_vs_))+1)
753 #define port_to_vs(_port_) ((vstruct_t)(((vm_offset_t)(_port_))&~3))
754 #define port_is_vs(_port_) ((((vm_offset_t)(_port_))&3) == 1)
755
756 #define vs_lookup(_port_, _vs_) \
757 do { \
758 if (!MACH_PORT_VALID(_port_) || !port_is_vs(_port_) \
759 || port_to_vs(_port_)->vs_mem_obj != (_port_)) \
760 Panic("bad pager port"); \
761 _vs_ = port_to_vs(_port_); \
762 } while (0)
763 #endif
764
765 /*
766 * Cross-module routines declaration.
767 */
768 #ifndef MACH_KERNEL
769 extern int dp_thread_id(void);
770 #endif
771 extern boolean_t device_reply_server(mach_msg_header_t *,
772 mach_msg_header_t *);
773 #ifdef MACH_KERNEL
774 extern boolean_t default_pager_no_senders(memory_object_t,
775 mach_port_mscount_t);
776 #else
777 extern void default_pager_no_senders(memory_object_t,
778 mach_port_seqno_t,
779 mach_port_mscount_t);
780 #endif
781
782 extern int local_log2(unsigned int);
783 extern void bs_initialize(void);
784 extern void bs_global_info(vm_size_t *,
785 vm_size_t *);
786 extern boolean_t bs_add_device(char *,
787 MACH_PORT_FACE);
788 extern vstruct_t ps_vstruct_create(vm_size_t);
789 extern void ps_vstruct_dealloc(vstruct_t);
790 extern kern_return_t pvs_cluster_read(vstruct_t,
791 vm_offset_t,
792 vm_size_t);
793 extern kern_return_t vs_cluster_write(vstruct_t,
794 upl_t,
795 upl_offset_t,
796 upl_size_t,
797 boolean_t,
798 int);
799 extern vm_offset_t ps_clmap(vstruct_t,
800 vm_offset_t,
801 struct clmap *,
802 int,
803 vm_size_t,
804 int);
805 extern vm_size_t ps_vstruct_allocated_size(vstruct_t);
806 extern size_t ps_vstruct_allocated_pages(vstruct_t,
807 default_pager_page_t *,
808 size_t);
809 extern boolean_t bs_set_default_clsize(unsigned int);
810
811 extern boolean_t verbose;
812
813 extern thread_call_t default_pager_backing_store_monitor_callout;
814 extern void default_pager_backing_store_monitor(thread_call_param_t, thread_call_param_t);
815
816 extern ipc_port_t max_pages_trigger_port;
817 extern unsigned int dp_pages_free;
818 extern unsigned int maximum_pages_free;
819
820 /* Do we know yet if swap files need to be encrypted ? */
821 extern boolean_t dp_encryption_inited;
822 /* Should we encrypt data before writing to swap ? */
823 extern boolean_t dp_encryption;
824
825 #endif /* _DEFAULT_PAGER_INTERNAL_H_ */