1/*
2 * Copyright (c) 2000-2007 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#include <mach_rt.h>
75
76#include <mach/port.h>
77#include <mach/kern_return.h>
78
79#include <kern/kern_types.h>
80#include <kern/kalloc.h>
81#include <kern/mach_param.h>
82#include <kern/ipc_host.h>
83#include <kern/ipc_mig.h>
84#include <kern/host_notify.h>
85#include <kern/mk_timer.h>
86#include <kern/misc_protos.h>
87#include <kern/sync_lock.h>
88#include <kern/sync_sema.h>
89#include <vm/vm_map.h>
90#include <vm/vm_kern.h>
91
92#include <ipc/ipc_entry.h>
93#include <ipc/ipc_space.h>
94#include <ipc/ipc_object.h>
95#include <ipc/ipc_port.h>
96#include <ipc/ipc_pset.h>
97#include <ipc/ipc_notify.h>
98#include <ipc/ipc_kmsg.h>
99#include <ipc/ipc_hash.h>
100#include <ipc/ipc_init.h>
101#include <ipc/ipc_table.h>
102
103#include <mach/machine/ndr_def.h>   /* NDR_record */
104#include <ipc/ipc_labelh.h>
105
106vm_map_t ipc_kernel_map;
107vm_size_t ipc_kernel_map_size = 1024 * 1024;
108
109vm_map_t ipc_kernel_copy_map;
110#define IPC_KERNEL_COPY_MAP_SIZE (8 * 1024 * 1024)
111vm_size_t ipc_kernel_copy_map_size = IPC_KERNEL_COPY_MAP_SIZE;
112vm_size_t ipc_kmsg_max_vm_space = (IPC_KERNEL_COPY_MAP_SIZE * 7)/8;
113
114int ipc_space_max = SPACE_MAX;
115int ipc_tree_entry_max = ITE_MAX;
116int ipc_port_max = PORT_MAX;
117int ipc_pset_max = SET_MAX;
118
119extern void ikm_cache_init(void);
120
121/*
122 *	Routine:	ipc_bootstrap
123 *	Purpose:
124 *		Initialization needed before the kernel task
125 *		can be created.
126 */
127
128void
129ipc_bootstrap(void)
130{
131	kern_return_t kr;
132
133	ipc_port_multiple_lock_init();
134
135	ipc_port_timestamp_lock_init();
136	ipc_port_timestamp_data = 0;
137
138	/* all IPC zones should be exhaustible */
139
140	ipc_space_zone = zinit(sizeof(struct ipc_space),
141			       ipc_space_max * sizeof(struct ipc_space),
142			       sizeof(struct ipc_space),
143			       "ipc spaces");
144#if 0
145	/* make it exhaustible */
146	zone_change(ipc_space_zone, Z_EXHAUST, TRUE);
147#endif
148
149	ipc_tree_entry_zone =
150		zinit(sizeof(struct ipc_tree_entry),
151			ipc_tree_entry_max * sizeof(struct ipc_tree_entry),
152			sizeof(struct ipc_tree_entry),
153			"ipc tree entries");
154#if 0
155	/* make it exhaustible */
156	zone_change(ipc_tree_entry_zone, Z_EXHAUST, TRUE);
157#endif
158
159	/*
160	 * populate all port(set) zones
161	 */
162	ipc_object_zones[IOT_PORT] =
163		zinit(sizeof(struct ipc_port),
164		      ipc_port_max * sizeof(struct ipc_port),
165		      sizeof(struct ipc_port),
166		      "ipc ports");
167	/*
168	 * XXX  Can't make the port zone exhaustible because the kernel
169	 * XXX	panics when port allocation for an internal object fails.
170	 *zone_change(ipc_object_zones[IOT_PORT], Z_EXHAUST, TRUE);
171	 */
172
173	ipc_object_zones[IOT_PORT_SET] =
174		zinit(sizeof(struct ipc_pset),
175		      ipc_pset_max * sizeof(struct ipc_pset),
176		      sizeof(struct ipc_pset),
177		      "ipc port sets");
178	/* make it exhaustible */
179	zone_change(ipc_object_zones[IOT_PORT_SET], Z_EXHAUST, TRUE);
180
181	/*
182	 * Create the basic ipc_kmsg_t zone (the one we also cache)
183	 * elements at the processor-level to avoid the locking.
184	 */
185	ipc_kmsg_zone = zinit(IKM_SAVED_KMSG_SIZE,
186			      ipc_port_max * MACH_PORT_QLIMIT_DEFAULT *
187			      IKM_SAVED_KMSG_SIZE,
188			      IKM_SAVED_KMSG_SIZE,
189			      "ipc kmsgs");
190
191#if CONFIG_MACF_MACH
192	ipc_labelh_zone =
193		zinit(sizeof(struct ipc_labelh),
194		      ipc_port_max * sizeof(struct ipc_labelh),
195		      sizeof(struct ipc_labelh),
196		      "label handles");
197#endif
198
199	/* create special spaces */
200
201	kr = ipc_space_create_special(&ipc_space_kernel);
202	assert(kr == KERN_SUCCESS);
203
204
205	kr = ipc_space_create_special(&ipc_space_reply);
206	assert(kr == KERN_SUCCESS);
207
208	/* initialize modules with hidden data structures */
209
210#if	MACH_ASSERT
211	ipc_port_debug_init();
212#endif
213	mig_init();
214	ipc_table_init();
215	ipc_hash_init();
216	semaphore_init();
217	lock_set_init();
218	mk_timer_init();
219	host_notify_init();
220}
221
222/*
223 * XXX tunable, belongs in mach.message.h
224 */
225#define MSG_OOL_SIZE_SMALL_MAX 4096
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;
238	vm_offset_t min;
239
240	retval = kmem_suballoc(kernel_map, &min, ipc_kernel_map_size,
241			       TRUE, VM_FLAGS_ANYWHERE, &ipc_kernel_map);
242
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,
247			       TRUE, VM_FLAGS_ANYWHERE, &ipc_kernel_copy_map);
248
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	}
267
268	ipc_host_init();
269}
270