]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_space.c
xnu-792.25.20.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_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: ipc/ipc_space.c
54 * Author: Rich Draves
55 * Date: 1989
56 *
57 * Functions to manipulate IPC capability spaces.
58 */
59
60 #include <mach_kdb.h>
61
62 #include <mach/boolean.h>
63 #include <mach/kern_return.h>
64 #include <mach/port.h>
65 #include <kern/assert.h>
66 #include <kern/sched_prim.h>
67 #include <kern/zalloc.h>
68 #include <ipc/port.h>
69 #include <ipc/ipc_entry.h>
70 #include <ipc/ipc_splay.h>
71 #include <ipc/ipc_object.h>
72 #include <ipc/ipc_hash.h>
73 #include <ipc/ipc_table.h>
74 #include <ipc/ipc_port.h>
75 #include <ipc/ipc_space.h>
76 #include <ipc/ipc_right.h>
77 #include <string.h>
78
79 zone_t ipc_space_zone;
80 ipc_space_t ipc_space_kernel;
81 ipc_space_t ipc_space_reply;
82 #if MACH_KDB
83 ipc_space_t default_pager_space;
84 #endif /* MACH_KDB */
85
86 /*
87 * Routine: ipc_space_reference
88 * Routine: ipc_space_release
89 * Purpose:
90 * Function versions of the IPC space macros.
91 * The "is_" cover macros can be defined to use the
92 * macros or the functions, as desired.
93 */
94
95 void
96 ipc_space_reference(
97 ipc_space_t space)
98 {
99 ipc_space_reference_macro(space);
100 }
101
102 void
103 ipc_space_release(
104 ipc_space_t space)
105 {
106 ipc_space_release_macro(space);
107 }
108
109 /*
110 * Routine: ipc_space_create
111 * Purpose:
112 * Creates a new IPC space.
113 *
114 * The new space has two references, one for the caller
115 * and one because it is active.
116 * Conditions:
117 * Nothing locked. Allocates memory.
118 * Returns:
119 * KERN_SUCCESS Created a space.
120 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
121 */
122
123 kern_return_t
124 ipc_space_create(
125 ipc_table_size_t initial,
126 ipc_space_t *spacep)
127 {
128 ipc_space_t space;
129 ipc_entry_t table;
130 ipc_entry_num_t new_size;
131 mach_port_index_t index;
132
133 space = is_alloc();
134 if (space == IS_NULL)
135 return KERN_RESOURCE_SHORTAGE;
136
137 table = it_entries_alloc(initial);
138 if (table == IE_NULL) {
139 is_free(space);
140 return KERN_RESOURCE_SHORTAGE;
141 }
142
143 new_size = initial->its_size;
144 memset((void *) table, 0, new_size * sizeof(struct ipc_entry));
145
146 /*
147 * Initialize the free list in the table.
148 * Add the entries in reverse order, and
149 * set the generation number to -1, so that
150 * initial allocations produce "natural" names.
151 */
152 for (index = 0; index < new_size; index++) {
153 ipc_entry_t entry = &table[index];
154
155 entry->ie_bits = IE_BITS_GEN_MASK;
156 entry->ie_next = index+1;
157 }
158 table[new_size-1].ie_next = 0;
159
160 is_ref_lock_init(space);
161 space->is_references = 2;
162
163 is_lock_init(space);
164 space->is_active = TRUE;
165 space->is_growing = FALSE;
166 space->is_table = table;
167 space->is_table_size = new_size;
168 space->is_table_next = initial+1;
169
170 ipc_splay_tree_init(&space->is_tree);
171 space->is_tree_total = 0;
172 space->is_tree_small = 0;
173 space->is_tree_hash = 0;
174
175 *spacep = space;
176 return KERN_SUCCESS;
177 }
178
179 /*
180 * Routine: ipc_space_create_special
181 * Purpose:
182 * Create a special space. A special space
183 * doesn't hold rights in the normal way.
184 * Instead it is place-holder for holding
185 * disembodied (naked) receive rights.
186 * See ipc_port_alloc_special/ipc_port_dealloc_special.
187 * Conditions:
188 * Nothing locked.
189 * Returns:
190 * KERN_SUCCESS Created a space.
191 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
192 */
193
194 kern_return_t
195 ipc_space_create_special(
196 ipc_space_t *spacep)
197 {
198 ipc_space_t space;
199
200 space = is_alloc();
201 if (space == IS_NULL)
202 return KERN_RESOURCE_SHORTAGE;
203
204 is_ref_lock_init(space);
205 space->is_references = 1;
206
207 is_lock_init(space);
208 space->is_active = FALSE;
209
210 *spacep = space;
211 return KERN_SUCCESS;
212 }
213
214 /*
215 * ipc_space_clean - remove all port references from an ipc space.
216 *
217 * In order to follow the traditional semantic, ipc_space_destroy
218 * will not destroy the entire port table of a shared space. Instead
219 * it will simply clear its own sub-space.
220 */
221 void
222 ipc_space_clean(
223 ipc_space_t space)
224 {
225 ipc_tree_entry_t tentry;
226 ipc_entry_t table;
227 ipc_entry_num_t size;
228 mach_port_index_t index;
229
230 /*
231 * If somebody is trying to grow the table,
232 * we must wait until they finish and figure
233 * out the space died.
234 */
235 is_write_lock(space);
236 while (space->is_growing)
237 is_write_sleep(space);
238
239 /*
240 * Now we can futz with it since we have the write lock.
241 */
242 #if MACH_KDB
243 if (space == default_pager_space)
244 default_pager_space = IS_NULL;
245 #endif /* MACH_KDB */
246
247 table = space->is_table;
248 size = space->is_table_size;
249
250 for (index = 0; index < size; index++) {
251 ipc_entry_t entry = &table[index];
252 mach_port_type_t type;
253
254 type = IE_BITS_TYPE(entry->ie_bits);
255 if (type != MACH_PORT_TYPE_NONE) {
256 mach_port_name_t name = MACH_PORT_MAKE(index,
257 IE_BITS_GEN(entry->ie_bits));
258 ipc_right_destroy(space, name, entry);
259 }
260 }
261
262 /*
263 * JMM - Now the table is cleaned out. We don't bother shrinking the
264 * size of the table at this point, but we probably should if it is
265 * really large. Lets just clean up the splay tree.
266 */
267 start_splay:
268 for (tentry = ipc_splay_traverse_start(&space->is_tree);
269 tentry != ITE_NULL;
270 tentry = ipc_splay_traverse_next(&space->is_tree, TRUE)) {
271 mach_port_type_t type;
272 mach_port_name_t name = tentry->ite_name;
273
274 type = IE_BITS_TYPE(tentry->ite_bits);
275 /*
276 * If it is a real right, then destroy it. This will have the
277 * side effect of removing it from the splay, so start over.
278 */
279 if(type != MACH_PORT_TYPE_NONE) {
280 ipc_splay_traverse_finish(&space->is_tree);
281 ipc_right_destroy(space, name, &tentry->ite_entry);
282 goto start_splay;
283 }
284 }
285 ipc_splay_traverse_finish(&space->is_tree);
286 is_write_unlock(space);
287 }
288
289
290 /*
291 * Routine: ipc_space_destroy
292 * Purpose:
293 * Marks the space as dead and cleans up the entries.
294 * Does nothing if the space is already dead.
295 * Conditions:
296 * Nothing locked.
297 */
298
299 void
300 ipc_space_destroy(
301 ipc_space_t space)
302 {
303 boolean_t active;
304 ipc_tree_entry_t tentry;
305 ipc_entry_t table;
306 ipc_entry_num_t size;
307 mach_port_index_t index;
308
309 assert(space != IS_NULL);
310
311 is_write_lock(space);
312 active = space->is_active;
313 space->is_active = FALSE;
314 is_write_unlock(space);
315
316 if (!active)
317 return;
318
319
320 /*
321 * If somebody is trying to grow the table,
322 * we must wait until they finish and figure
323 * out the space died.
324 */
325 is_read_lock(space);
326 while (space->is_growing)
327 is_read_sleep(space);
328
329 is_read_unlock(space);
330 /*
331 * Now we can futz with it unlocked.
332 */
333 #if MACH_KDB
334 if (space == default_pager_space)
335 default_pager_space = IS_NULL;
336 #endif /* MACH_KDB */
337
338 table = space->is_table;
339 size = space->is_table_size;
340
341 for (index = 0; index < size; index++) {
342 ipc_entry_t entry = &table[index];
343 mach_port_type_t type;
344
345 type = IE_BITS_TYPE(entry->ie_bits);
346 if (type != MACH_PORT_TYPE_NONE) {
347 mach_port_name_t name;
348
349 name = MACH_PORT_MAKE(index,
350 IE_BITS_GEN(entry->ie_bits));
351 ipc_right_clean(space, name, entry);
352 }
353 }
354
355 it_entries_free(space->is_table_next-1, table);
356 space->is_table_size = 0;
357
358 for (tentry = ipc_splay_traverse_start(&space->is_tree);
359 tentry != ITE_NULL;
360 tentry = ipc_splay_traverse_next(&space->is_tree, TRUE)) {
361 mach_port_type_t type;
362 mach_port_name_t name = tentry->ite_name;
363
364 type = IE_BITS_TYPE(tentry->ite_bits);
365 assert(type != MACH_PORT_TYPE_NONE);
366
367 ipc_right_clean(space, name, &tentry->ite_entry);
368
369 if(type == MACH_PORT_TYPE_SEND)
370 ipc_hash_global_delete(space, tentry->ite_object,
371 name, tentry);
372 }
373 ipc_splay_traverse_finish(&space->is_tree);
374
375 /*
376 * Because the space is now dead,
377 * we must release the "active" reference for it.
378 * Our caller still has his reference.
379 */
380 is_release(space);
381 }
382
383