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