2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: ipc/ipc_space.c
57 * Functions to manipulate IPC capability spaces.
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>
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>
79 zone_t ipc_space_zone
;
80 ipc_space_t ipc_space_kernel
;
81 ipc_space_t ipc_space_reply
;
83 ipc_space_t default_pager_space
;
87 * Routine: ipc_space_reference
88 * Routine: ipc_space_release
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.
99 ipc_space_reference_macro(space
);
106 ipc_space_release_macro(space
);
110 * Routine: ipc_space_create
112 * Creates a new IPC space.
114 * The new space has two references, one for the caller
115 * and one because it is active.
117 * Nothing locked. Allocates memory.
119 * KERN_SUCCESS Created a space.
120 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
125 ipc_table_size_t initial
,
130 ipc_entry_num_t new_size
;
131 mach_port_index_t index
;
134 if (space
== IS_NULL
)
135 return KERN_RESOURCE_SHORTAGE
;
137 table
= it_entries_alloc(initial
);
138 if (table
== IE_NULL
) {
140 return KERN_RESOURCE_SHORTAGE
;
143 new_size
= initial
->its_size
;
144 memset((void *) table
, 0, new_size
* sizeof(struct ipc_entry
));
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.
152 for (index
= 0; index
< new_size
; index
++) {
153 ipc_entry_t entry
= &table
[index
];
155 entry
->ie_bits
= IE_BITS_GEN_MASK
;
156 entry
->ie_next
= index
+1;
158 table
[new_size
-1].ie_next
= 0;
160 is_ref_lock_init(space
);
161 space
->is_references
= 2;
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;
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;
180 * Routine: ipc_space_create_special
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.
190 * KERN_SUCCESS Created a space.
191 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
195 ipc_space_create_special(
201 if (space
== IS_NULL
)
202 return KERN_RESOURCE_SHORTAGE
;
204 is_ref_lock_init(space
);
205 space
->is_references
= 1;
208 space
->is_active
= FALSE
;
215 * ipc_space_clean - remove all port references from an ipc space.
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.
225 ipc_tree_entry_t tentry
;
227 ipc_entry_num_t size
;
228 mach_port_index_t index
;
231 * If somebody is trying to grow the table,
232 * we must wait until they finish and figure
233 * out the space died.
235 is_write_lock(space
);
236 while (space
->is_growing
)
237 is_write_sleep(space
);
240 * Now we can futz with it since we have the write lock.
243 if (space
== default_pager_space
)
244 default_pager_space
= IS_NULL
;
245 #endif /* MACH_KDB */
247 table
= space
->is_table
;
248 size
= space
->is_table_size
;
250 for (index
= 0; index
< size
; index
++) {
251 ipc_entry_t entry
= &table
[index
];
252 mach_port_type_t type
;
254 type
= IE_BITS_TYPE(entry
->ie_bits
);
255 if (type
!= MACH_PORT_TYPE_NONE
) {
256 mach_port_name_t name
= MACH_PORT_MAKE(index
,
257 IE_BITS_GEN(entry
->ie_bits
));
258 ipc_right_destroy(space
, name
, entry
);
263 * JMM - Now the table is cleaned out. We don't bother shrinking the
264 * size of the table at this point, but we probably should if it is
265 * really large. Lets just clean up the splay tree.
268 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
270 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
271 mach_port_type_t type
;
272 mach_port_name_t name
= tentry
->ite_name
;
274 type
= IE_BITS_TYPE(tentry
->ite_bits
);
276 * If it is a real right, then destroy it. This will have the
277 * side effect of removing it from the splay, so start over.
279 if(type
!= MACH_PORT_TYPE_NONE
) {
280 ipc_splay_traverse_finish(&space
->is_tree
);
281 ipc_right_destroy(space
, name
, &tentry
->ite_entry
);
285 ipc_splay_traverse_finish(&space
->is_tree
);
286 is_write_unlock(space
);
291 * Routine: ipc_space_destroy
293 * Marks the space as dead and cleans up the entries.
294 * Does nothing if the space is already dead.
304 ipc_tree_entry_t tentry
;
306 ipc_entry_num_t size
;
307 mach_port_index_t index
;
309 assert(space
!= IS_NULL
);
311 is_write_lock(space
);
312 active
= space
->is_active
;
313 space
->is_active
= FALSE
;
314 is_write_unlock(space
);
321 * If somebody is trying to grow the table,
322 * we must wait until they finish and figure
323 * out the space died.
326 while (space
->is_growing
)
327 is_read_sleep(space
);
329 is_read_unlock(space
);
331 * Now we can futz with it unlocked.
334 if (space
== default_pager_space
)
335 default_pager_space
= IS_NULL
;
336 #endif /* MACH_KDB */
338 table
= space
->is_table
;
339 size
= space
->is_table_size
;
341 for (index
= 0; index
< size
; index
++) {
342 ipc_entry_t entry
= &table
[index
];
343 mach_port_type_t type
;
345 type
= IE_BITS_TYPE(entry
->ie_bits
);
346 if (type
!= MACH_PORT_TYPE_NONE
) {
347 mach_port_name_t name
;
349 name
= MACH_PORT_MAKE(index
,
350 IE_BITS_GEN(entry
->ie_bits
));
351 ipc_right_clean(space
, name
, entry
);
355 it_entries_free(space
->is_table_next
-1, table
);
356 space
->is_table_size
= 0;
358 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
360 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
361 mach_port_type_t type
;
362 mach_port_name_t name
= tentry
->ite_name
;
364 type
= IE_BITS_TYPE(tentry
->ite_bits
);
365 assert(type
!= MACH_PORT_TYPE_NONE
);
367 ipc_right_clean(space
, name
, &tentry
->ite_entry
);
369 if(type
== MACH_PORT_TYPE_SEND
)
370 ipc_hash_global_delete(space
, tentry
->ite_object
,
373 ipc_splay_traverse_finish(&space
->is_tree
);
376 * Because the space is now dead,
377 * we must release the "active" reference for it.
378 * Our caller still has his reference.