]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_init.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_init.c
1 /*
2 * Copyright (c) 2000-2012 Apple 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 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 * Copyright (c) 2005 SPARTA, Inc.
62 */
63 /*
64 */
65 /*
66 * File: ipc/ipc_init.c
67 * Author: Rich Draves
68 * Date: 1989
69 *
70 * Functions to initialize the IPC system.
71 */
72
73 #include <mach_debug.h>
74
75 #include <mach/port.h>
76 #include <mach/message.h>
77 #include <mach/kern_return.h>
78
79 #include <kern/kern_types.h>
80 #include <kern/arcade.h>
81 #include <kern/kalloc.h>
82 #include <kern/simple_lock.h>
83 #include <kern/mach_param.h>
84 #include <kern/ipc_host.h>
85 #include <kern/ipc_mig.h>
86 #include <kern/host_notify.h>
87 #include <kern/mk_timer.h>
88 #include <kern/misc_protos.h>
89 #include <kern/sync_lock.h>
90 #include <kern/sync_sema.h>
91 #include <kern/ux_handler.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_kern.h>
94
95 #include <ipc/ipc_entry.h>
96 #include <ipc/ipc_space.h>
97 #include <ipc/ipc_object.h>
98 #include <ipc/ipc_port.h>
99 #include <ipc/ipc_pset.h>
100 #include <ipc/ipc_notify.h>
101 #include <ipc/ipc_kmsg.h>
102 #include <ipc/ipc_hash.h>
103 #include <ipc/ipc_init.h>
104 #include <ipc/ipc_table.h>
105 #include <ipc/ipc_voucher.h>
106 #include <ipc/ipc_importance.h>
107
108 #include <mach/machine/ndr_def.h> /* NDR_record */
109
110 vm_map_t ipc_kernel_map;
111 vm_size_t ipc_kernel_map_size = 1024 * 1024;
112
113 /* values to limit physical copy out-of-line memory descriptors */
114 vm_map_t ipc_kernel_copy_map;
115 #define IPC_KERNEL_COPY_MAP_SIZE (8 * 1024 * 1024)
116 vm_size_t ipc_kernel_copy_map_size = IPC_KERNEL_COPY_MAP_SIZE;
117 vm_size_t ipc_kmsg_max_vm_space = ((IPC_KERNEL_COPY_MAP_SIZE * 7) / 8);
118
119 /*
120 * values to limit inline message body handling
121 * avoid copyin/out limits - even after accounting for maximum descriptor expansion.
122 */
123 #define IPC_KMSG_MAX_SPACE (64 * 1024 * 1024) /* keep in sync with COPYSIZELIMIT_PANIC */
124 vm_size_t ipc_kmsg_max_body_space = ((IPC_KMSG_MAX_SPACE * 3) / 4 - MAX_TRAILER_SIZE);
125
126 int ipc_space_max;
127 int ipc_port_max;
128 int ipc_pset_max;
129 int prioritize_launch = 1;
130 int enforce_strict_reply = 0;
131
132
133 lck_grp_t ipc_lck_grp;
134 lck_attr_t ipc_lck_attr;
135
136 static lck_grp_attr_t ipc_lck_grp_attr;
137
138 /*
139 * Routine: ipc_bootstrap
140 * Purpose:
141 * Initialization needed before the kernel task
142 * can be created.
143 */
144
145 void
146 ipc_bootstrap(void)
147 {
148 kern_return_t kr;
149 int prioritize_launch_bootarg;
150 int strict_reply_bootarg;
151
152 lck_grp_attr_setdefault(&ipc_lck_grp_attr);
153 lck_grp_init(&ipc_lck_grp, "ipc", &ipc_lck_grp_attr);
154 lck_attr_setdefault(&ipc_lck_attr);
155
156 ipc_port_multiple_lock_init();
157
158 ipc_port_timestamp_data = 0;
159
160 /* all IPC zones should be exhaustible */
161
162 ipc_space_zone = zinit(sizeof(struct ipc_space),
163 ipc_space_max * sizeof(struct ipc_space),
164 sizeof(struct ipc_space),
165 "ipc spaces");
166 zone_change(ipc_space_zone, Z_NOENCRYPT, TRUE);
167
168 /*
169 * populate all port(set) zones
170 */
171 ipc_object_zones[IOT_PORT] =
172 zinit(sizeof(struct ipc_port),
173 ipc_port_max * sizeof(struct ipc_port),
174 sizeof(struct ipc_port),
175 "ipc ports");
176 /* cant charge callers for port allocations (references passed) */
177 zone_change(ipc_object_zones[IOT_PORT], Z_CALLERACCT, FALSE);
178 zone_change(ipc_object_zones[IOT_PORT], Z_NOENCRYPT, TRUE);
179 zone_change(ipc_object_zones[IOT_PORT], Z_CLEARMEMORY, TRUE);
180
181 ipc_object_zones[IOT_PORT_SET] =
182 zinit(sizeof(struct ipc_pset),
183 ipc_pset_max * sizeof(struct ipc_pset),
184 sizeof(struct ipc_pset),
185 "ipc port sets");
186 zone_change(ipc_object_zones[IOT_PORT_SET], Z_NOENCRYPT, TRUE);
187 zone_change(ipc_object_zones[IOT_PORT_SET], Z_CLEARMEMORY, TRUE);
188
189 /*
190 * Create the basic ipc_kmsg_t zone (the one we also cache)
191 * elements at the processor-level to avoid the locking.
192 */
193 ipc_kmsg_zone = zinit(IKM_SAVED_KMSG_SIZE,
194 ipc_port_max * MACH_PORT_QLIMIT_DEFAULT *
195 IKM_SAVED_KMSG_SIZE,
196 IKM_SAVED_KMSG_SIZE,
197 "ipc kmsgs");
198 zone_change(ipc_kmsg_zone, Z_CALLERACCT, FALSE);
199 zone_change(ipc_kmsg_zone, Z_CACHING_ENABLED, TRUE);
200
201 /* create special spaces */
202
203 kr = ipc_space_create_special(&ipc_space_kernel);
204 assert(kr == KERN_SUCCESS);
205
206
207 kr = ipc_space_create_special(&ipc_space_reply);
208 assert(kr == KERN_SUCCESS);
209
210 /* initialize modules with hidden data structures */
211
212 #if MACH_ASSERT
213 ipc_port_debug_init();
214 #endif
215 mig_init();
216 ipc_table_init();
217 ipc_voucher_init();
218
219 #if IMPORTANCE_INHERITANCE
220 ipc_importance_init();
221 #endif
222
223 semaphore_init();
224 mk_timer_init();
225 host_notify_init();
226
227 #if CONFIG_ARCADE
228 arcade_init();
229 #endif
230
231 if (PE_parse_boot_argn("prioritize_launch", &prioritize_launch_bootarg, sizeof(prioritize_launch_bootarg))) {
232 prioritize_launch = !!prioritize_launch_bootarg;
233 }
234 if (PE_parse_boot_argn("ipc_strict_reply", &strict_reply_bootarg, sizeof(strict_reply_bootarg))) {
235 enforce_strict_reply = !!strict_reply_bootarg;
236 }
237 }
238
239 /*
240 * XXX tunable, belongs in mach.message.h
241 */
242 #define MSG_OOL_SIZE_SMALL_MAX (2*PAGE_SIZE)
243 vm_size_t msg_ool_size_small;
244
245 /*
246 * Routine: ipc_init
247 * Purpose:
248 * Final initialization of the IPC system.
249 */
250
251 void
252 ipc_init(void)
253 {
254 kern_return_t retval;
255 vm_offset_t min;
256
257 retval = kmem_suballoc(kernel_map, &min, ipc_kernel_map_size,
258 TRUE,
259 (VM_FLAGS_ANYWHERE),
260 VM_MAP_KERNEL_FLAGS_NONE,
261 VM_KERN_MEMORY_IPC,
262 &ipc_kernel_map);
263
264 if (retval != KERN_SUCCESS) {
265 panic("ipc_init: kmem_suballoc of ipc_kernel_map failed");
266 }
267
268 retval = kmem_suballoc(kernel_map, &min, ipc_kernel_copy_map_size,
269 TRUE,
270 (VM_FLAGS_ANYWHERE),
271 VM_MAP_KERNEL_FLAGS_NONE,
272 VM_KERN_MEMORY_IPC,
273 &ipc_kernel_copy_map);
274
275 if (retval != KERN_SUCCESS) {
276 panic("ipc_init: kmem_suballoc of ipc_kernel_copy_map failed");
277 }
278
279 ipc_kernel_copy_map->no_zero_fill = TRUE;
280 ipc_kernel_copy_map->wait_for_space = TRUE;
281
282 /*
283 * As an optimization, 'small' out of line data regions using a
284 * physical copy strategy are copied into kalloc'ed buffers.
285 * The value of 'small' is determined here. Requests kalloc()
286 * with sizes greater or equal to kalloc_max_prerounded may fail.
287 */
288 if (kalloc_max_prerounded <= MSG_OOL_SIZE_SMALL_MAX) {
289 msg_ool_size_small = kalloc_max_prerounded;
290 } else {
291 msg_ool_size_small = MSG_OOL_SIZE_SMALL_MAX;
292 }
293 /* account for overhead to avoid spilling over a page */
294 msg_ool_size_small -= cpy_kdata_hdr_sz;
295
296 ipc_host_init();
297 ux_handler_init();
298 }
299
300
301 /*
302 * Routine: ipc_thread_call_init
303 * Purpose:
304 * Initialize IPC logic that needs thread call support
305 */
306
307 void
308 ipc_thread_call_init(void)
309 {
310 #if IMPORTANCE_INHERITANCE
311 ipc_importance_thread_call_init();
312 #endif
313 }