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