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