]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_space.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_space.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, 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 */
1c79356b
A
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
91zone_t ipc_space_zone;
92ipc_space_t ipc_space_kernel;
93ipc_space_t ipc_space_reply;
94#if MACH_KDB
95ipc_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
107void
108ipc_space_reference(
109 ipc_space_t space)
110{
111 ipc_space_reference_macro(space);
112}
113
114void
115ipc_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
135kern_return_t
136ipc_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;
2d21ac55 186 space->is_task = NULL;
1c79356b
A
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
207kern_return_t
208ipc_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 */
234void
235ipc_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);
9bccf70c
A
249 while (space->is_growing)
250 is_write_sleep(space);
1c79356b
A
251
252 /*
253 * Now we can futz with it since we have the write lock.
254 */
255#if MACH_KDB
256 if (space == default_pager_space)
257 default_pager_space = IS_NULL;
258#endif /* MACH_KDB */
259
260 table = space->is_table;
261 size = space->is_table_size;
262
263 for (index = 0; index < size; index++) {
264 ipc_entry_t entry = &table[index];
265 mach_port_type_t type;
266
267 type = IE_BITS_TYPE(entry->ie_bits);
268 if (type != MACH_PORT_TYPE_NONE) {
269 mach_port_name_t name = MACH_PORT_MAKE(index,
270 IE_BITS_GEN(entry->ie_bits));
271 ipc_right_destroy(space, name, entry);
272 }
273 }
274
275 /*
276 * JMM - Now the table is cleaned out. We don't bother shrinking the
277 * size of the table at this point, but we probably should if it is
278 * really large. Lets just clean up the splay tree.
279 */
280 start_splay:
281 for (tentry = ipc_splay_traverse_start(&space->is_tree);
282 tentry != ITE_NULL;
283 tentry = ipc_splay_traverse_next(&space->is_tree, TRUE)) {
1c79356b
A
284 mach_port_type_t type;
285 mach_port_name_t name = tentry->ite_name;
286
287 type = IE_BITS_TYPE(tentry->ite_bits);
288 /*
289 * If it is a real right, then destroy it. This will have the
290 * side effect of removing it from the splay, so start over.
291 */
292 if(type != MACH_PORT_TYPE_NONE) {
293 ipc_splay_traverse_finish(&space->is_tree);
294 ipc_right_destroy(space, name, &tentry->ite_entry);
295 goto start_splay;
296 }
297 }
298 ipc_splay_traverse_finish(&space->is_tree);
299 is_write_unlock(space);
300}
301
302
303/*
304 * Routine: ipc_space_destroy
305 * Purpose:
306 * Marks the space as dead and cleans up the entries.
307 * Does nothing if the space is already dead.
308 * Conditions:
309 * Nothing locked.
310 */
311
312void
313ipc_space_destroy(
314 ipc_space_t space)
315{
316 boolean_t active;
317 ipc_tree_entry_t tentry;
318 ipc_entry_t table;
319 ipc_entry_num_t size;
320 mach_port_index_t index;
321
322 assert(space != IS_NULL);
323
324 is_write_lock(space);
325 active = space->is_active;
326 space->is_active = FALSE;
327 is_write_unlock(space);
328
329 if (!active)
330 return;
331
332
333 /*
334 * If somebody is trying to grow the table,
335 * we must wait until they finish and figure
336 * out the space died.
337 */
338 is_read_lock(space);
9bccf70c
A
339 while (space->is_growing)
340 is_read_sleep(space);
1c79356b
A
341
342 is_read_unlock(space);
343 /*
344 * Now we can futz with it unlocked.
345 */
346#if MACH_KDB
347 if (space == default_pager_space)
348 default_pager_space = IS_NULL;
349#endif /* MACH_KDB */
350
351 table = space->is_table;
352 size = space->is_table_size;
353
354 for (index = 0; index < size; index++) {
355 ipc_entry_t entry = &table[index];
356 mach_port_type_t type;
357
358 type = IE_BITS_TYPE(entry->ie_bits);
359 if (type != MACH_PORT_TYPE_NONE) {
360 mach_port_name_t name;
361
362 name = MACH_PORT_MAKE(index,
363 IE_BITS_GEN(entry->ie_bits));
364 ipc_right_clean(space, name, entry);
365 }
366 }
367
368 it_entries_free(space->is_table_next-1, table);
369 space->is_table_size = 0;
370
371 for (tentry = ipc_splay_traverse_start(&space->is_tree);
372 tentry != ITE_NULL;
373 tentry = ipc_splay_traverse_next(&space->is_tree, TRUE)) {
374 mach_port_type_t type;
375 mach_port_name_t name = tentry->ite_name;
376
377 type = IE_BITS_TYPE(tentry->ite_bits);
378 assert(type != MACH_PORT_TYPE_NONE);
379
380 ipc_right_clean(space, name, &tentry->ite_entry);
381
382 if(type == MACH_PORT_TYPE_SEND)
383 ipc_hash_global_delete(space, tentry->ite_object,
384 name, tentry);
385 }
386 ipc_splay_traverse_finish(&space->is_tree);
387
388 /*
389 * Because the space is now dead,
390 * we must release the "active" reference for it.
391 * Our caller still has his reference.
392 */
393 is_release(space);
394}
91447636
A
395
396