2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
57 #include "default_pager_internal.h"
58 #include <default_pager/default_pager_object_server.h>
59 #include <kern/host.h>
60 #include <kern/ledger.h>
61 #include <mach/host_info.h>
62 #include <mach/host_priv.h>
63 #include <mach/vm_map.h>
64 #include <ipc/ipc_space.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_protos.h>
69 char my_name
[] = "(default pager): ";
71 #if DEFAULT_PAGER_DEBUG
73 #endif /* DEFAULT_PAGER_DEBUG */
76 * Use 16 Kbyte stacks instead of the default 64K.
77 * Use 4 Kbyte waiting stacks instead of the default 8K.
80 vm_size_t cthread_stack_size
= 16 *1024;
81 extern vm_size_t cthread_wait_stack_size
;
83 unsigned long long vm_page_mask
;
90 /* task_t default_pager_self; */ /* Our task port. */
91 mutex_t dpt_lock
; /* lock for the dpt array struct */
92 default_pager_thread_t
**dpt_array
;
94 memory_object_default_t default_pager_object
; /* for memory_object_create. */
96 MACH_PORT_FACE default_pager_default_set
; /* Port set for "default" thread. */
97 MACH_PORT_FACE default_pager_internal_set
; /* Port set for internal objects. */
98 MACH_PORT_FACE default_pager_external_set
; /* Port set for external objects. */
100 #define DEFAULT_PAGER_INTERNAL_COUNT (4)
103 /* Memory created by default_pager_object_create should mostly be resident. */
104 #define DEFAULT_PAGER_EXTERNAL_COUNT (2)
106 int default_pager_internal_count
= DEFAULT_PAGER_INTERNAL_COUNT
;
107 /* Number of "internal" threads. */
108 int default_pager_external_count
= DEFAULT_PAGER_EXTERNAL_COUNT
;
109 /* Number of "external" threads. */
112 * Forward declarations.
114 boolean_t
default_pager_notify_server(mach_msg_header_t
*,
115 mach_msg_header_t
*);
116 boolean_t
default_pager_demux_object(mach_msg_header_t
*,
117 mach_msg_header_t
*);
118 boolean_t
default_pager_demux_default(mach_msg_header_t
*,
119 mach_msg_header_t
*);
120 default_pager_thread_t
*start_default_pager_thread(int, boolean_t
);
121 void default_pager(void);
122 void default_pager_thread(void *);
123 void default_pager_initialize(void);
124 boolean_t
dp_parse_argument(char *); /* forward; */
125 unsigned int d_to_i(char *); /* forward; */
126 boolean_t
strprefix(register const char *s1
, register const char *s2
);
129 extern int vstruct_def_clshift
;
133 * Initialize and Run the default pager
139 __unused
static char here
[] = "default_pager";
140 default_pager_thread_t dpt
;
146 * Give me space for the thread array and zero it.
148 i
= default_pager_internal_count
+ default_pager_external_count
+ 1;
149 dpt_array
= (default_pager_thread_t
**)
150 kalloc(i
* sizeof(default_pager_thread_t
*));
151 memset(dpt_array
, 0, i
* sizeof(default_pager_thread_t
*));
153 /* Setup my thread structure. */
156 dpt
.dpt_internal
= FALSE
;
157 dpt
.dpt_initialized_p
= TRUE
;
161 * Now we create the threads that will actually
165 for (i
= 0; i
< default_pager_internal_count
; i
++) {
166 dpt_array
[id
] = (default_pager_thread_t
*)
167 kalloc(sizeof (default_pager_thread_t
));
168 if (dpt_array
[id
] == NULL
)
169 Panic("alloc pager thread");
170 kr
= vm_allocate(kernel_map
, &((dpt_array
[id
])->dpt_buffer
),
171 vm_page_size
<< vstruct_def_clshift
, VM_FLAGS_ANYWHERE
);
172 if (kr
!= KERN_SUCCESS
)
173 Panic("alloc thread buffer");
174 kr
= vm_map_wire(kernel_map
, (dpt_array
[id
])->dpt_buffer
,
175 ((dpt_array
[id
])->dpt_buffer
)
176 +(vm_page_size
<< vstruct_def_clshift
),
179 if (kr
!= KERN_SUCCESS
)
180 Panic("wire thread buffer");
181 (dpt_array
[id
])->dpt_internal
= TRUE
;
182 (dpt_array
[id
])->dpt_initialized_p
= TRUE
;
183 (dpt_array
[id
])->checked_out
= FALSE
;
186 DPT_LOCK_INIT(dpt_lock
);
194 /* simple utility: only works for 2^n */
203 while ((n
& 1) == 0) {
213 /* another simple utility, d_to_i(char*) supporting only decimal
214 * and devoid of range checking; obscure name chosen deliberately
215 * to avoid confusion with semantic-rich POSIX routines */
219 unsigned int rval
= 0;
222 while ((ch
= *arg
++) && ch
>= '0' && ch
<= '9') {
233 * Check for non-disk-partition arguments of the form
235 * returning TRUE if one if found
237 boolean_t
dp_parse_argument(char *av
)
240 __unused
static char here
[] = "dp_parse_argument";
242 /* Check for '-v' flag */
244 if (av
[0] == '-' && av
[1] == 'v' && av
[2] == 0) {
250 * If we find a '=' followed by an argument in the string,
251 * check for known arguments
253 while (*rhs
&& *rhs
!= '=')
255 if (*rhs
&& *++rhs
) {
257 if (strprefix(av
,"cl")) {
258 if (!bs_set_default_clsize(d_to_i(rhs
)))
259 dprintf(("Bad argument (%s) - ignored\n", av
));
262 /* else if strprefix(av,"another_argument")) {
263 handle_another_argument(av);
271 start_def_pager( __unused
char *bs_device
)
274 MACH_PORT_FACE master_device_port;
277 MACH_PORT_FACE security_port;
278 MACH_PORT_FACE root_ledger_wired;
279 MACH_PORT_FACE root_ledger_paged;
281 __unused
static char here
[] = "main";
286 default_pager_host_port = ipc_port_make_send(realhost.host_priv_self);
287 master_device_port = ipc_port_make_send(master_device_port);
288 root_ledger_wired = ipc_port_make_send(root_wired_ledger_port);
289 root_ledger_paged = ipc_port_make_send(root_paged_ledger_port);
290 security_port = ipc_port_make_send(realhost.host_security_self);
301 /* setup read buffers, etc */
302 default_pager_initialize();
305 /* start the backing store monitor, it runs on a callout thread */
306 default_pager_backing_store_monitor_callout
=
307 thread_call_allocate(default_pager_backing_store_monitor
, NULL
);
308 if (!default_pager_backing_store_monitor_callout
)
309 panic("can't start backing store monitor thread");
310 thread_call_enter(default_pager_backing_store_monitor_callout
);
314 * Return TRUE if string 2 is a prefix of string 1.
317 strprefix(register const char *s1
, register const char *s2
)
321 while ((c
= *s2
++) != '\0') {
331 memory_object_default_t pager
,
332 default_pager_info_t
*infop
)
334 vm_size_t pages_total
, pages_free
;
336 if (pager
!= default_pager_object
)
337 return KERN_INVALID_ARGUMENT
;
339 bs_global_info(&pages_total
, &pages_free
);
341 infop
->dpi_total_space
= ptoa_32(pages_total
);
342 infop
->dpi_free_space
= ptoa_32(pages_free
);
343 infop
->dpi_page_size
= vm_page_size
;
350 default_pager_info_64(
351 memory_object_default_t pager
,
352 default_pager_info_64_t
*infop
)
354 vm_size_t pages_total
, pages_free
;
356 if (pager
!= default_pager_object
)
357 return KERN_INVALID_ARGUMENT
;
359 bs_global_info(&pages_total
, &pages_free
);
361 infop
->dpi_total_space
= ptoa_64(pages_total
);
362 infop
->dpi_free_space
= ptoa_64(pages_free
);
363 infop
->dpi_page_size
= vm_page_size
;
364 infop
->dpi_flags
= 0;
365 if (dp_encryption_inited
&& dp_encryption
== TRUE
) {
366 infop
->dpi_flags
|= DPI_ENCRYPTED
;
374 default_pager_initialize()
377 __unused
static char here
[] = "default_pager_initialize";
383 vm_page_mask
= vm_page_size
- 1;
384 vm_page_shift
= local_log2(vm_page_size
);
387 * List of all vstructs.
389 vstruct_zone
= zinit(sizeof(struct vstruct
),
390 10000 * sizeof(struct vstruct
),
391 8192, "vstruct zone");
393 queue_init(&vstruct_list
.vsl_queue
);
394 vstruct_list
.vsl_count
= 0;
396 VSTATS_LOCK_INIT(&global_stats
.gs_lock
);
403 default_pager_object
= ipc_port_alloc_kernel();
407 * Export pager interfaces.
410 if ((kr
= netname_check_in(name_server_port
, "UserPager",
412 default_pager_object
))
414 dprintf(("netname_check_in returned 0x%x\n", kr
));
417 #else /* USER_PAGER */
420 memory_object_default_t dmm
;
422 dmm
= default_pager_object
;
423 clsize
= (vm_page_size
<< vstruct_def_clshift
);
424 kr
= host_default_memory_manager(host_priv_self(), &dmm
, clsize
);
425 if ((kr
!= KERN_SUCCESS
) ||
426 (dmm
!= MEMORY_OBJECT_DEFAULT_NULL
))
427 Panic("default memory manager");
430 #endif /* USER_PAGER */