2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
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.
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.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 * File: ipc/ipc_space.c
58 * Functions to manipulate IPC capability spaces.
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>
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>
80 zone_t ipc_space_zone
;
81 ipc_space_t ipc_space_kernel
;
82 ipc_space_t ipc_space_reply
;
84 ipc_space_t default_pager_space
;
88 * Routine: ipc_space_reference
89 * Routine: ipc_space_release
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.
100 ipc_space_reference_macro(space
);
107 ipc_space_release_macro(space
);
111 * Routine: ipc_space_create
113 * Creates a new IPC space.
115 * The new space has two references, one for the caller
116 * and one because it is active.
118 * Nothing locked. Allocates memory.
120 * KERN_SUCCESS Created a space.
121 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
126 ipc_table_size_t initial
,
131 ipc_entry_num_t new_size
;
132 mach_port_index_t index
;
135 if (space
== IS_NULL
)
136 return KERN_RESOURCE_SHORTAGE
;
138 table
= it_entries_alloc(initial
);
139 if (table
== IE_NULL
) {
141 return KERN_RESOURCE_SHORTAGE
;
144 new_size
= initial
->its_size
;
145 memset((void *) table
, 0, new_size
* sizeof(struct ipc_entry
));
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.
153 for (index
= 0; index
< new_size
; index
++) {
154 ipc_entry_t entry
= &table
[index
];
156 entry
->ie_bits
= IE_BITS_GEN_MASK
;
157 entry
->ie_next
= index
+1;
159 table
[new_size
-1].ie_next
= 0;
161 is_ref_lock_init(space
);
162 space
->is_references
= 2;
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;
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;
181 * Routine: ipc_space_create_special
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.
191 * KERN_SUCCESS Created a space.
192 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
196 ipc_space_create_special(
202 if (space
== IS_NULL
)
203 return KERN_RESOURCE_SHORTAGE
;
205 is_ref_lock_init(space
);
206 space
->is_references
= 1;
209 space
->is_active
= FALSE
;
216 * ipc_space_clean - remove all port references from an ipc space.
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.
226 ipc_tree_entry_t tentry
;
228 ipc_entry_num_t size
;
229 mach_port_index_t index
;
232 * If somebody is trying to grow the table,
233 * we must wait until they finish and figure
234 * out the space died.
236 is_write_lock(space
);
237 while (space
->is_growing
)
238 is_write_sleep(space
);
241 * Now we can futz with it since we have the write lock.
244 if (space
== default_pager_space
)
245 default_pager_space
= IS_NULL
;
246 #endif /* MACH_KDB */
248 table
= space
->is_table
;
249 size
= space
->is_table_size
;
251 for (index
= 0; index
< size
; index
++) {
252 ipc_entry_t entry
= &table
[index
];
253 mach_port_type_t type
;
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
);
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.
269 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
271 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
272 mach_port_type_t type
;
273 mach_port_name_t name
= tentry
->ite_name
;
275 type
= IE_BITS_TYPE(tentry
->ite_bits
);
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.
280 if(type
!= MACH_PORT_TYPE_NONE
) {
281 ipc_splay_traverse_finish(&space
->is_tree
);
282 ipc_right_destroy(space
, name
, &tentry
->ite_entry
);
286 ipc_splay_traverse_finish(&space
->is_tree
);
287 is_write_unlock(space
);
292 * Routine: ipc_space_destroy
294 * Marks the space as dead and cleans up the entries.
295 * Does nothing if the space is already dead.
305 ipc_tree_entry_t tentry
;
307 ipc_entry_num_t size
;
308 mach_port_index_t index
;
310 assert(space
!= IS_NULL
);
312 is_write_lock(space
);
313 active
= space
->is_active
;
314 space
->is_active
= FALSE
;
315 is_write_unlock(space
);
322 * If somebody is trying to grow the table,
323 * we must wait until they finish and figure
324 * out the space died.
327 while (space
->is_growing
)
328 is_read_sleep(space
);
330 is_read_unlock(space
);
332 * Now we can futz with it unlocked.
335 if (space
== default_pager_space
)
336 default_pager_space
= IS_NULL
;
337 #endif /* MACH_KDB */
339 table
= space
->is_table
;
340 size
= space
->is_table_size
;
342 for (index
= 0; index
< size
; index
++) {
343 ipc_entry_t entry
= &table
[index
];
344 mach_port_type_t type
;
346 type
= IE_BITS_TYPE(entry
->ie_bits
);
347 if (type
!= MACH_PORT_TYPE_NONE
) {
348 mach_port_name_t name
;
350 name
= MACH_PORT_MAKE(index
,
351 IE_BITS_GEN(entry
->ie_bits
));
352 ipc_right_clean(space
, name
, entry
);
356 it_entries_free(space
->is_table_next
-1, table
);
357 space
->is_table_size
= 0;
359 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
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
;
365 type
= IE_BITS_TYPE(tentry
->ite_bits
);
366 assert(type
!= MACH_PORT_TYPE_NONE
);
368 ipc_right_clean(space
, name
, &tentry
->ite_entry
);
370 if(type
== MACH_PORT_TYPE_SEND
)
371 ipc_hash_global_delete(space
, tentry
->ite_object
,
374 ipc_splay_traverse_finish(&space
->is_tree
);
377 * Because the space is now dead,
378 * we must release the "active" reference for it.
379 * Our caller still has his reference.