]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_space.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_space.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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 */
62 /*
63 */
64 /*
65 * File: ipc/ipc_space.c
66 * Author: Rich Draves
67 * Date: 1989
68 *
69 * Functions to manipulate IPC capability spaces.
70 */
71
72 #include <mach_kdb.h>
73
74 #include <mach/boolean.h>
75 #include <mach/kern_return.h>
76 #include <mach/port.h>
77 #include <kern/assert.h>
78 #include <kern/sched_prim.h>
79 #include <kern/zalloc.h>
80 #include <ipc/port.h>
81 #include <ipc/ipc_entry.h>
82 #include <ipc/ipc_splay.h>
83 #include <ipc/ipc_object.h>
84 #include <ipc/ipc_hash.h>
85 #include <ipc/ipc_table.h>
86 #include <ipc/ipc_port.h>
87 #include <ipc/ipc_space.h>
88 #include <ipc/ipc_right.h>
89 #include <string.h>
90
91 zone_t ipc_space_zone;
92 ipc_space_t ipc_space_kernel;
93 ipc_space_t ipc_space_reply;
94 #if MACH_KDB
95 ipc_space_t default_pager_space;
96 #endif /* MACH_KDB */
97
98 /*
99 * Routine: ipc_space_reference
100 * Routine: ipc_space_release
101 * Purpose:
102 * Function versions of the IPC space macros.
103 * The "is_" cover macros can be defined to use the
104 * macros or the functions, as desired.
105 */
106
107 void
108 ipc_space_reference(
109 ipc_space_t space)
110 {
111 ipc_space_reference_macro(space);
112 }
113
114 void
115 ipc_space_release(
116 ipc_space_t space)
117 {
118 ipc_space_release_macro(space);
119 }
120
121 /*
122 * Routine: ipc_space_create
123 * Purpose:
124 * Creates a new IPC space.
125 *
126 * The new space has two references, one for the caller
127 * and one because it is active.
128 * Conditions:
129 * Nothing locked. Allocates memory.
130 * Returns:
131 * KERN_SUCCESS Created a space.
132 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
133 */
134
135 kern_return_t
136 ipc_space_create(
137 ipc_table_size_t initial,
138 ipc_space_t *spacep)
139 {
140 ipc_space_t space;
141 ipc_entry_t table;
142 ipc_entry_num_t new_size;
143 mach_port_index_t index;
144
145 space = is_alloc();
146 if (space == IS_NULL)
147 return KERN_RESOURCE_SHORTAGE;
148
149 table = it_entries_alloc(initial);
150 if (table == IE_NULL) {
151 is_free(space);
152 return KERN_RESOURCE_SHORTAGE;
153 }
154
155 new_size = initial->its_size;
156 memset((void *) table, 0, new_size * sizeof(struct ipc_entry));
157
158 /*
159 * Initialize the free list in the table.
160 * Add the entries in reverse order, and
161 * set the generation number to -1, so that
162 * initial allocations produce "natural" names.
163 */
164 for (index = 0; index < new_size; index++) {
165 ipc_entry_t entry = &table[index];
166
167 entry->ie_bits = IE_BITS_GEN_MASK;
168 entry->ie_next = index+1;
169 }
170 table[new_size-1].ie_next = 0;
171
172 is_ref_lock_init(space);
173 space->is_references = 2;
174
175 is_lock_init(space);
176 space->is_active = TRUE;
177 space->is_growing = FALSE;
178 space->is_table = table;
179 space->is_table_size = new_size;
180 space->is_table_next = initial+1;
181
182 ipc_splay_tree_init(&space->is_tree);
183 space->is_tree_total = 0;
184 space->is_tree_small = 0;
185 space->is_tree_hash = 0;
186 space->is_task = NULL;
187
188 *spacep = space;
189 return KERN_SUCCESS;
190 }
191
192 /*
193 * Routine: ipc_space_create_special
194 * Purpose:
195 * Create a special space. A special space
196 * doesn't hold rights in the normal way.
197 * Instead it is place-holder for holding
198 * disembodied (naked) receive rights.
199 * See ipc_port_alloc_special/ipc_port_dealloc_special.
200 * Conditions:
201 * Nothing locked.
202 * Returns:
203 * KERN_SUCCESS Created a space.
204 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
205 */
206
207 kern_return_t
208 ipc_space_create_special(
209 ipc_space_t *spacep)
210 {
211 ipc_space_t space;
212
213 space = is_alloc();
214 if (space == IS_NULL)
215 return KERN_RESOURCE_SHORTAGE;
216
217 is_ref_lock_init(space);
218 space->is_references = 1;
219
220 is_lock_init(space);
221 space->is_active = FALSE;
222
223 *spacep = space;
224 return KERN_SUCCESS;
225 }
226
227 /*
228 * ipc_space_clean - remove all port references from an ipc space.
229 *
230 * In order to follow the traditional semantic, ipc_space_destroy
231 * will not destroy the entire port table of a shared space. Instead
232 * it will simply clear its own sub-space.
233 */
234 void
235 ipc_space_clean(
236 ipc_space_t space)
237 {
238 ipc_tree_entry_t tentry;
239 ipc_entry_t table;
240 ipc_entry_num_t size;
241 mach_port_index_t index;
242
243 /*
244 * If somebody is trying to grow the table,
245 * we must wait until they finish and figure
246 * out the space died.
247 */
248 is_write_lock(space);
249 while (space->is_growing)
250 is_write_sleep(space);
251
252 if (!space->is_active) {
253 is_write_unlock(space);
254 return;
255 }
256
257 /*
258 * Now we can futz with it since we have the write lock.
259 */
260 #if MACH_KDB
261 if (space == default_pager_space)
262 default_pager_space = IS_NULL;
263 #endif /* MACH_KDB */
264
265 table = space->is_table;
266 size = space->is_table_size;
267
268 for (index = 0; index < size; index++) {
269 ipc_entry_t entry = &table[index];
270 mach_port_type_t type;
271
272 type = IE_BITS_TYPE(entry->ie_bits);
273 if (type != MACH_PORT_TYPE_NONE) {
274 mach_port_name_t name = MACH_PORT_MAKE(index,
275 IE_BITS_GEN(entry->ie_bits));
276 ipc_right_destroy(space, name, entry);
277 }
278 }
279
280 /*
281 * JMM - Now the table is cleaned out. We don't bother shrinking the
282 * size of the table at this point, but we probably should if it is
283 * really large. Lets just clean up the splay tree.
284 */
285 start_splay:
286 for (tentry = ipc_splay_traverse_start(&space->is_tree);
287 tentry != ITE_NULL;
288 tentry = ipc_splay_traverse_next(&space->is_tree, TRUE)) {
289 mach_port_type_t type;
290 mach_port_name_t name = tentry->ite_name;
291
292 type = IE_BITS_TYPE(tentry->ite_bits);
293 /*
294 * If it is a real right, then destroy it. This will have the
295 * side effect of removing it from the splay, so start over.
296 */
297 if(type != MACH_PORT_TYPE_NONE) {
298 ipc_splay_traverse_finish(&space->is_tree);
299 ipc_right_destroy(space, name, &tentry->ite_entry);
300 goto start_splay;
301 }
302 }
303 ipc_splay_traverse_finish(&space->is_tree);
304 is_write_unlock(space);
305 }
306
307
308 /*
309 * Routine: ipc_space_destroy
310 * Purpose:
311 * Marks the space as dead and cleans up the entries.
312 * Does nothing if the space is already dead.
313 * Conditions:
314 * Nothing locked.
315 */
316
317 void
318 ipc_space_destroy(
319 ipc_space_t space)
320 {
321 boolean_t active;
322 ipc_tree_entry_t tentry;
323 ipc_entry_t table;
324 ipc_entry_num_t size;
325 mach_port_index_t index;
326
327 assert(space != IS_NULL);
328
329 is_write_lock(space);
330 active = space->is_active;
331 space->is_active = FALSE;
332 is_write_unlock(space);
333
334 if (!active)
335 return;
336
337
338 /*
339 * If somebody is trying to grow the table,
340 * we must wait until they finish and figure
341 * out the space died.
342 */
343 is_read_lock(space);
344 while (space->is_growing)
345 is_read_sleep(space);
346
347 is_read_unlock(space);
348 /*
349 * Now we can futz with it unlocked.
350 */
351 #if MACH_KDB
352 if (space == default_pager_space)
353 default_pager_space = IS_NULL;
354 #endif /* MACH_KDB */
355
356 table = space->is_table;
357 size = space->is_table_size;
358
359 for (index = 0; index < size; index++) {
360 ipc_entry_t entry = &table[index];
361 mach_port_type_t type;
362
363 type = IE_BITS_TYPE(entry->ie_bits);
364 if (type != MACH_PORT_TYPE_NONE) {
365 mach_port_name_t name;
366
367 name = MACH_PORT_MAKE(index,
368 IE_BITS_GEN(entry->ie_bits));
369 ipc_right_clean(space, name, entry);
370 }
371 }
372
373 it_entries_free(space->is_table_next-1, table);
374 space->is_table_size = 0;
375
376 for (tentry = ipc_splay_traverse_start(&space->is_tree);
377 tentry != ITE_NULL;
378 tentry = ipc_splay_traverse_next(&space->is_tree, TRUE)) {
379 mach_port_type_t type;
380 mach_port_name_t name = tentry->ite_name;
381
382 type = IE_BITS_TYPE(tentry->ite_bits);
383 assert(type != MACH_PORT_TYPE_NONE);
384
385 ipc_right_clean(space, name, &tentry->ite_entry);
386
387 if(type == MACH_PORT_TYPE_SEND)
388 ipc_hash_global_delete(space, tentry->ite_object,
389 name, tentry);
390 }
391 ipc_splay_traverse_finish(&space->is_tree);
392
393 /*
394 * Because the space is now dead,
395 * we must release the "active" reference for it.
396 * Our caller still has his reference.
397 */
398 is_release(space);
399 }
400
401