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