]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_init.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_init.c
CommitLineData
1c79356b 1/*
316670eb 2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
2d21ac55
A
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 */
1c79356b
A
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#include <mach_rt.h>
75
91447636 76#include <mach/port.h>
316670eb 77#include <mach/message.h>
1c79356b 78#include <mach/kern_return.h>
91447636
A
79
80#include <kern/kern_types.h>
81#include <kern/kalloc.h>
fe8ab488 82#include <kern/simple_lock.h>
1c79356b
A
83#include <kern/mach_param.h>
84#include <kern/ipc_host.h>
91447636 85#include <kern/ipc_mig.h>
55e303ae
A
86#include <kern/host_notify.h>
87#include <kern/mk_timer.h>
1c79356b 88#include <kern/misc_protos.h>
91447636
A
89#include <kern/sync_lock.h>
90#include <kern/sync_sema.h>
1c79356b
A
91#include <vm/vm_map.h>
92#include <vm/vm_kern.h>
91447636 93
1c79356b
A
94#include <ipc/ipc_entry.h>
95#include <ipc/ipc_space.h>
96#include <ipc/ipc_object.h>
97#include <ipc/ipc_port.h>
98#include <ipc/ipc_pset.h>
99#include <ipc/ipc_notify.h>
100#include <ipc/ipc_kmsg.h>
101#include <ipc/ipc_hash.h>
102#include <ipc/ipc_init.h>
91447636 103#include <ipc/ipc_table.h>
fe8ab488
A
104#include <ipc/ipc_voucher.h>
105#include <ipc/ipc_importance.h>
91447636 106
1c79356b
A
107#include <mach/machine/ndr_def.h> /* NDR_record */
108
109vm_map_t ipc_kernel_map;
110vm_size_t ipc_kernel_map_size = 1024 * 1024;
111
316670eb 112/* values to limit physical copy out-of-line memory descriptors */
1c79356b
A
113vm_map_t ipc_kernel_copy_map;
114#define IPC_KERNEL_COPY_MAP_SIZE (8 * 1024 * 1024)
115vm_size_t ipc_kernel_copy_map_size = IPC_KERNEL_COPY_MAP_SIZE;
316670eb
A
116vm_size_t ipc_kmsg_max_vm_space = ((IPC_KERNEL_COPY_MAP_SIZE * 7) / 8);
117
118/*
119 * values to limit inline message body handling
120 * avoid copyin/out limits - even after accounting for maximum descriptor expansion.
121 */
122#define IPC_KMSG_MAX_SPACE (64 * 1024 * 1024) /* keep in sync with COPYSIZELIMIT_PANIC */
123vm_size_t ipc_kmsg_max_body_space = ((IPC_KMSG_MAX_SPACE * 3)/4 - MAX_TRAILER_SIZE);
1c79356b 124
b0d623f7 125int ipc_space_max;
b0d623f7
A
126int ipc_port_max;
127int ipc_pset_max;
128
129
130lck_grp_t ipc_lck_grp;
131lck_attr_t ipc_lck_attr;
132
133static lck_grp_attr_t ipc_lck_grp_attr;
1c79356b 134
1c79356b
A
135extern void ikm_cache_init(void);
136
137/*
138 * Routine: ipc_bootstrap
139 * Purpose:
140 * Initialization needed before the kernel task
141 * can be created.
142 */
143
144void
145ipc_bootstrap(void)
146{
147 kern_return_t kr;
b0d623f7
A
148
149 lck_grp_attr_setdefault(&ipc_lck_grp_attr);
150 lck_grp_init(&ipc_lck_grp, "ipc", &ipc_lck_grp_attr);
151 lck_attr_setdefault(&ipc_lck_attr);
152
1c79356b
A
153 ipc_port_multiple_lock_init();
154
1c79356b
A
155 ipc_port_timestamp_data = 0;
156
157 /* all IPC zones should be exhaustible */
158
159 ipc_space_zone = zinit(sizeof(struct ipc_space),
160 ipc_space_max * sizeof(struct ipc_space),
161 sizeof(struct ipc_space),
162 "ipc spaces");
0b4c1975 163 zone_change(ipc_space_zone, Z_NOENCRYPT, TRUE);
1c79356b 164
1c79356b
A
165 /*
166 * populate all port(set) zones
167 */
168 ipc_object_zones[IOT_PORT] =
169 zinit(sizeof(struct ipc_port),
170 ipc_port_max * sizeof(struct ipc_port),
171 sizeof(struct ipc_port),
172 "ipc ports");
6d2010ae
A
173 /* cant charge callers for port allocations (references passed) */
174 zone_change(ipc_object_zones[IOT_PORT], Z_CALLERACCT, FALSE);
0b4c1975 175 zone_change(ipc_object_zones[IOT_PORT], Z_NOENCRYPT, TRUE);
1c79356b
A
176
177 ipc_object_zones[IOT_PORT_SET] =
178 zinit(sizeof(struct ipc_pset),
179 ipc_pset_max * sizeof(struct ipc_pset),
180 sizeof(struct ipc_pset),
181 "ipc port sets");
0b4c1975 182 zone_change(ipc_object_zones[IOT_PORT_SET], Z_NOENCRYPT, TRUE);
1c79356b 183
91447636
A
184 /*
185 * Create the basic ipc_kmsg_t zone (the one we also cache)
186 * elements at the processor-level to avoid the locking.
187 */
188 ipc_kmsg_zone = zinit(IKM_SAVED_KMSG_SIZE,
2d21ac55 189 ipc_port_max * MACH_PORT_QLIMIT_DEFAULT *
91447636
A
190 IKM_SAVED_KMSG_SIZE,
191 IKM_SAVED_KMSG_SIZE,
192 "ipc kmsgs");
6d2010ae 193 zone_change(ipc_kmsg_zone, Z_CALLERACCT, FALSE);
91447636 194
1c79356b
A
195 /* create special spaces */
196
197 kr = ipc_space_create_special(&ipc_space_kernel);
198 assert(kr == KERN_SUCCESS);
199
200
201 kr = ipc_space_create_special(&ipc_space_reply);
202 assert(kr == KERN_SUCCESS);
203
204 /* initialize modules with hidden data structures */
205
206#if MACH_ASSERT
207 ipc_port_debug_init();
208#endif
209 mig_init();
210 ipc_table_init();
fe8ab488
A
211 ipc_voucher_init();
212
213#if IMPORTANCE_INHERITANCE
214 ipc_importance_init();
215#endif
316670eb 216
1c79356b 217 semaphore_init();
55e303ae
A
218 mk_timer_init();
219 host_notify_init();
1c79356b
A
220}
221
222/*
223 * XXX tunable, belongs in mach.message.h
224 */
3e170ce0 225#define MSG_OOL_SIZE_SMALL_MAX (2*PAGE_SIZE)
1c79356b
A
226vm_size_t msg_ool_size_small;
227
228/*
229 * Routine: ipc_init
230 * Purpose:
231 * Final initialization of the IPC system.
232 */
233
234void
235ipc_init(void)
236{
237 kern_return_t retval;
91447636 238 vm_offset_t min;
1c79356b
A
239
240 retval = kmem_suballoc(kernel_map, &min, ipc_kernel_map_size,
3e170ce0 241 TRUE, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_IPC), &ipc_kernel_map);
91447636 242
1c79356b
A
243 if (retval != KERN_SUCCESS)
244 panic("ipc_init: kmem_suballoc of ipc_kernel_map failed");
245
246 retval = kmem_suballoc(kernel_map, &min, ipc_kernel_copy_map_size,
3e170ce0 247 TRUE, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_IPC), &ipc_kernel_copy_map);
91447636 248
1c79356b
A
249 if (retval != KERN_SUCCESS)
250 panic("ipc_init: kmem_suballoc of ipc_kernel_copy_map failed");
251
252 ipc_kernel_copy_map->no_zero_fill = TRUE;
253 ipc_kernel_copy_map->wait_for_space = TRUE;
254
255 /*
256 * As an optimization, 'small' out of line data regions using a
257 * physical copy strategy are copied into kalloc'ed buffers.
258 * The value of 'small' is determined here. Requests kalloc()
259 * with sizes greater or equal to kalloc_max_prerounded may fail.
260 */
261 if (kalloc_max_prerounded <= MSG_OOL_SIZE_SMALL_MAX) {
262 msg_ool_size_small = kalloc_max_prerounded;
263 }
264 else {
265 msg_ool_size_small = MSG_OOL_SIZE_SMALL_MAX;
266 }
3e170ce0
A
267 /* account for overhead to avoid spilling over a page */
268 msg_ool_size_small -= cpy_kdata_hdr_sz;
1c79356b
A
269
270 ipc_host_init();
fe8ab488
A
271
272}
273
274
275/*
276 * Routine: ipc_thread_call_init
277 * Purpose:
278 * Initialize IPC logic that needs thread call support
279 */
280
281void
282ipc_thread_call_init(void)
283{
284#if IMPORTANCE_INHERITANCE
285 ipc_importance_thread_call_init();
286#endif
1c79356b 287}