2 * Copyright (c) 2000 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 assert_wait((event_t
) space
, THREAD_UNINT
);
238 is_write_unlock(space
);
239 thread_block((void (*)(void)) 0);
240 is_write_lock(space
);
244 * Now we can futz with it since we have the write lock.
247 if (space
== default_pager_space
)
248 default_pager_space
= IS_NULL
;
249 #endif /* MACH_KDB */
251 table
= space
->is_table
;
252 size
= space
->is_table_size
;
254 for (index
= 0; index
< size
; index
++) {
255 ipc_entry_t entry
= &table
[index
];
256 mach_port_type_t type
;
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
);
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.
272 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
274 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
276 mach_port_type_t type
;
277 mach_port_name_t name
= tentry
->ite_name
;
279 type
= IE_BITS_TYPE(tentry
->ite_bits
);
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.
284 if(type
!= MACH_PORT_TYPE_NONE
) {
285 ipc_splay_traverse_finish(&space
->is_tree
);
286 ipc_right_destroy(space
, name
, &tentry
->ite_entry
);
290 ipc_splay_traverse_finish(&space
->is_tree
);
291 is_write_unlock(space
);
296 * Routine: ipc_space_destroy
298 * Marks the space as dead and cleans up the entries.
299 * Does nothing if the space is already dead.
309 ipc_tree_entry_t tentry
;
311 ipc_entry_num_t size
;
312 mach_port_index_t index
;
314 assert(space
!= IS_NULL
);
316 is_write_lock(space
);
317 active
= space
->is_active
;
318 space
->is_active
= FALSE
;
319 is_write_unlock(space
);
326 * If somebody is trying to grow the table,
327 * we must wait until they finish and figure
328 * out the space died.
331 while (space
->is_growing
) {
332 assert_wait((event_t
) space
, THREAD_UNINT
);
333 is_read_unlock(space
);
334 thread_block((void (*)(void)) 0);
338 is_read_unlock(space
);
340 * Now we can futz with it unlocked.
343 if (space
== default_pager_space
)
344 default_pager_space
= IS_NULL
;
345 #endif /* MACH_KDB */
347 table
= space
->is_table
;
348 size
= space
->is_table_size
;
350 for (index
= 0; index
< size
; index
++) {
351 ipc_entry_t entry
= &table
[index
];
352 mach_port_type_t type
;
354 type
= IE_BITS_TYPE(entry
->ie_bits
);
355 if (type
!= MACH_PORT_TYPE_NONE
) {
356 mach_port_name_t name
;
358 name
= MACH_PORT_MAKE(index
,
359 IE_BITS_GEN(entry
->ie_bits
));
360 ipc_right_clean(space
, name
, entry
);
364 it_entries_free(space
->is_table_next
-1, table
);
365 space
->is_table_size
= 0;
367 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
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
;
373 type
= IE_BITS_TYPE(tentry
->ite_bits
);
374 assert(type
!= MACH_PORT_TYPE_NONE
);
376 ipc_right_clean(space
, name
, &tentry
->ite_entry
);
378 if(type
== MACH_PORT_TYPE_SEND
)
379 ipc_hash_global_delete(space
, tentry
->ite_object
,
382 ipc_splay_traverse_finish(&space
->is_tree
);
385 * Because the space is now dead,
386 * we must release the "active" reference for it.
387 * Our caller still has his reference.