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