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