2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * File: ipc/ipc_space.c
65 * Functions to manipulate IPC capability spaces.
70 #include <mach/boolean.h>
71 #include <mach/kern_return.h>
72 #include <mach/port.h>
73 #include <kern/assert.h>
74 #include <kern/sched_prim.h>
75 #include <kern/zalloc.h>
77 #include <ipc/ipc_entry.h>
78 #include <ipc/ipc_splay.h>
79 #include <ipc/ipc_object.h>
80 #include <ipc/ipc_hash.h>
81 #include <ipc/ipc_table.h>
82 #include <ipc/ipc_port.h>
83 #include <ipc/ipc_space.h>
84 #include <ipc/ipc_right.h>
87 zone_t ipc_space_zone
;
88 ipc_space_t ipc_space_kernel
;
89 ipc_space_t ipc_space_reply
;
91 ipc_space_t default_pager_space
;
95 * Routine: ipc_space_reference
96 * Routine: ipc_space_release
98 * Function versions of the IPC space macros.
99 * The "is_" cover macros can be defined to use the
100 * macros or the functions, as desired.
107 ipc_space_reference_macro(space
);
114 ipc_space_release_macro(space
);
118 * Routine: ipc_space_create
120 * Creates a new IPC space.
122 * The new space has two references, one for the caller
123 * and one because it is active.
125 * Nothing locked. Allocates memory.
127 * KERN_SUCCESS Created a space.
128 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
133 ipc_table_size_t initial
,
138 ipc_entry_num_t new_size
;
139 mach_port_index_t index
;
142 if (space
== IS_NULL
)
143 return KERN_RESOURCE_SHORTAGE
;
145 table
= it_entries_alloc(initial
);
146 if (table
== IE_NULL
) {
148 return KERN_RESOURCE_SHORTAGE
;
151 new_size
= initial
->its_size
;
152 memset((void *) table
, 0, new_size
* sizeof(struct ipc_entry
));
155 * Initialize the free list in the table.
156 * Add the entries in reverse order, and
157 * set the generation number to -1, so that
158 * initial allocations produce "natural" names.
160 for (index
= 0; index
< new_size
; index
++) {
161 ipc_entry_t entry
= &table
[index
];
163 entry
->ie_bits
= IE_BITS_GEN_MASK
;
164 entry
->ie_next
= index
+1;
166 table
[new_size
-1].ie_next
= 0;
168 is_ref_lock_init(space
);
169 space
->is_references
= 2;
172 space
->is_active
= TRUE
;
173 space
->is_growing
= FALSE
;
174 space
->is_table
= table
;
175 space
->is_table_size
= new_size
;
176 space
->is_table_next
= initial
+1;
178 ipc_splay_tree_init(&space
->is_tree
);
179 space
->is_tree_total
= 0;
180 space
->is_tree_small
= 0;
181 space
->is_tree_hash
= 0;
188 * Routine: ipc_space_create_special
190 * Create a special space. A special space
191 * doesn't hold rights in the normal way.
192 * Instead it is place-holder for holding
193 * disembodied (naked) receive rights.
194 * See ipc_port_alloc_special/ipc_port_dealloc_special.
198 * KERN_SUCCESS Created a space.
199 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
203 ipc_space_create_special(
209 if (space
== IS_NULL
)
210 return KERN_RESOURCE_SHORTAGE
;
212 is_ref_lock_init(space
);
213 space
->is_references
= 1;
216 space
->is_active
= FALSE
;
223 * ipc_space_clean - remove all port references from an ipc space.
225 * In order to follow the traditional semantic, ipc_space_destroy
226 * will not destroy the entire port table of a shared space. Instead
227 * it will simply clear its own sub-space.
233 ipc_tree_entry_t tentry
;
235 ipc_entry_num_t size
;
236 mach_port_index_t index
;
239 * If somebody is trying to grow the table,
240 * we must wait until they finish and figure
241 * out the space died.
243 is_write_lock(space
);
244 while (space
->is_growing
)
245 is_write_sleep(space
);
248 * Now we can futz with it since we have the write lock.
251 if (space
== default_pager_space
)
252 default_pager_space
= IS_NULL
;
253 #endif /* MACH_KDB */
255 table
= space
->is_table
;
256 size
= space
->is_table_size
;
258 for (index
= 0; index
< size
; index
++) {
259 ipc_entry_t entry
= &table
[index
];
260 mach_port_type_t type
;
262 type
= IE_BITS_TYPE(entry
->ie_bits
);
263 if (type
!= MACH_PORT_TYPE_NONE
) {
264 mach_port_name_t name
= MACH_PORT_MAKE(index
,
265 IE_BITS_GEN(entry
->ie_bits
));
266 ipc_right_destroy(space
, name
, entry
);
271 * JMM - Now the table is cleaned out. We don't bother shrinking the
272 * size of the table at this point, but we probably should if it is
273 * really large. Lets just clean up the splay tree.
276 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
278 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
279 mach_port_type_t type
;
280 mach_port_name_t name
= tentry
->ite_name
;
282 type
= IE_BITS_TYPE(tentry
->ite_bits
);
284 * If it is a real right, then destroy it. This will have the
285 * side effect of removing it from the splay, so start over.
287 if(type
!= MACH_PORT_TYPE_NONE
) {
288 ipc_splay_traverse_finish(&space
->is_tree
);
289 ipc_right_destroy(space
, name
, &tentry
->ite_entry
);
293 ipc_splay_traverse_finish(&space
->is_tree
);
294 is_write_unlock(space
);
299 * Routine: ipc_space_destroy
301 * Marks the space as dead and cleans up the entries.
302 * Does nothing if the space is already dead.
312 ipc_tree_entry_t tentry
;
314 ipc_entry_num_t size
;
315 mach_port_index_t index
;
317 assert(space
!= IS_NULL
);
319 is_write_lock(space
);
320 active
= space
->is_active
;
321 space
->is_active
= FALSE
;
322 is_write_unlock(space
);
329 * If somebody is trying to grow the table,
330 * we must wait until they finish and figure
331 * out the space died.
334 while (space
->is_growing
)
335 is_read_sleep(space
);
337 is_read_unlock(space
);
339 * Now we can futz with it unlocked.
342 if (space
== default_pager_space
)
343 default_pager_space
= IS_NULL
;
344 #endif /* MACH_KDB */
346 table
= space
->is_table
;
347 size
= space
->is_table_size
;
349 for (index
= 0; index
< size
; index
++) {
350 ipc_entry_t entry
= &table
[index
];
351 mach_port_type_t type
;
353 type
= IE_BITS_TYPE(entry
->ie_bits
);
354 if (type
!= MACH_PORT_TYPE_NONE
) {
355 mach_port_name_t name
;
357 name
= MACH_PORT_MAKE(index
,
358 IE_BITS_GEN(entry
->ie_bits
));
359 ipc_right_clean(space
, name
, entry
);
363 it_entries_free(space
->is_table_next
-1, table
);
364 space
->is_table_size
= 0;
366 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
368 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
369 mach_port_type_t type
;
370 mach_port_name_t name
= tentry
->ite_name
;
372 type
= IE_BITS_TYPE(tentry
->ite_bits
);
373 assert(type
!= MACH_PORT_TYPE_NONE
);
375 ipc_right_clean(space
, name
, &tentry
->ite_entry
);
377 if(type
== MACH_PORT_TYPE_SEND
)
378 ipc_hash_global_delete(space
, tentry
->ite_object
,
381 ipc_splay_traverse_finish(&space
->is_tree
);
384 * Because the space is now dead,
385 * we must release the "active" reference for it.
386 * Our caller still has his reference.