2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * File: ipc/ipc_space.c
60 * Functions to manipulate IPC capability spaces.
65 #include <mach/boolean.h>
66 #include <mach/kern_return.h>
67 #include <mach/port.h>
68 #include <kern/assert.h>
69 #include <kern/sched_prim.h>
70 #include <kern/zalloc.h>
72 #include <ipc/ipc_entry.h>
73 #include <ipc/ipc_splay.h>
74 #include <ipc/ipc_object.h>
75 #include <ipc/ipc_hash.h>
76 #include <ipc/ipc_table.h>
77 #include <ipc/ipc_port.h>
78 #include <ipc/ipc_space.h>
79 #include <ipc/ipc_right.h>
82 zone_t ipc_space_zone
;
83 ipc_space_t ipc_space_kernel
;
84 ipc_space_t ipc_space_reply
;
86 ipc_space_t default_pager_space
;
90 * Routine: ipc_space_reference
91 * Routine: ipc_space_release
93 * Function versions of the IPC space macros.
94 * The "is_" cover macros can be defined to use the
95 * macros or the functions, as desired.
102 ipc_space_reference_macro(space
);
109 ipc_space_release_macro(space
);
113 * Routine: ipc_space_create
115 * Creates a new IPC space.
117 * The new space has two references, one for the caller
118 * and one because it is active.
120 * Nothing locked. Allocates memory.
122 * KERN_SUCCESS Created a space.
123 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
128 ipc_table_size_t initial
,
133 ipc_entry_num_t new_size
;
134 mach_port_index_t index
;
137 if (space
== IS_NULL
)
138 return KERN_RESOURCE_SHORTAGE
;
140 table
= it_entries_alloc(initial
);
141 if (table
== IE_NULL
) {
143 return KERN_RESOURCE_SHORTAGE
;
146 new_size
= initial
->its_size
;
147 memset((void *) table
, 0, new_size
* sizeof(struct ipc_entry
));
150 * Initialize the free list in the table.
151 * Add the entries in reverse order, and
152 * set the generation number to -1, so that
153 * initial allocations produce "natural" names.
155 for (index
= 0; index
< new_size
; index
++) {
156 ipc_entry_t entry
= &table
[index
];
158 entry
->ie_bits
= IE_BITS_GEN_MASK
;
159 entry
->ie_next
= index
+1;
161 table
[new_size
-1].ie_next
= 0;
163 is_ref_lock_init(space
);
164 space
->is_references
= 2;
167 space
->is_active
= TRUE
;
168 space
->is_growing
= FALSE
;
169 space
->is_table
= table
;
170 space
->is_table_size
= new_size
;
171 space
->is_table_next
= initial
+1;
173 ipc_splay_tree_init(&space
->is_tree
);
174 space
->is_tree_total
= 0;
175 space
->is_tree_small
= 0;
176 space
->is_tree_hash
= 0;
183 * Routine: ipc_space_create_special
185 * Create a special space. A special space
186 * doesn't hold rights in the normal way.
187 * Instead it is place-holder for holding
188 * disembodied (naked) receive rights.
189 * See ipc_port_alloc_special/ipc_port_dealloc_special.
193 * KERN_SUCCESS Created a space.
194 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
198 ipc_space_create_special(
204 if (space
== IS_NULL
)
205 return KERN_RESOURCE_SHORTAGE
;
207 is_ref_lock_init(space
);
208 space
->is_references
= 1;
211 space
->is_active
= FALSE
;
218 * ipc_space_clean - remove all port references from an ipc space.
220 * In order to follow the traditional semantic, ipc_space_destroy
221 * will not destroy the entire port table of a shared space. Instead
222 * it will simply clear its own sub-space.
228 ipc_tree_entry_t tentry
;
230 ipc_entry_num_t size
;
231 mach_port_index_t index
;
234 * If somebody is trying to grow the table,
235 * we must wait until they finish and figure
236 * out the space died.
238 is_write_lock(space
);
239 while (space
->is_growing
)
240 is_write_sleep(space
);
243 * Now we can futz with it since we have the write lock.
246 if (space
== default_pager_space
)
247 default_pager_space
= IS_NULL
;
248 #endif /* MACH_KDB */
250 table
= space
->is_table
;
251 size
= space
->is_table_size
;
253 for (index
= 0; index
< size
; index
++) {
254 ipc_entry_t entry
= &table
[index
];
255 mach_port_type_t type
;
257 type
= IE_BITS_TYPE(entry
->ie_bits
);
258 if (type
!= MACH_PORT_TYPE_NONE
) {
259 mach_port_name_t name
= MACH_PORT_MAKE(index
,
260 IE_BITS_GEN(entry
->ie_bits
));
261 ipc_right_destroy(space
, name
, entry
);
266 * JMM - Now the table is cleaned out. We don't bother shrinking the
267 * size of the table at this point, but we probably should if it is
268 * really large. Lets just clean up the splay tree.
271 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
273 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
275 mach_port_type_t type
;
276 mach_port_name_t name
= tentry
->ite_name
;
278 type
= IE_BITS_TYPE(tentry
->ite_bits
);
280 * If it is a real right, then destroy it. This will have the
281 * side effect of removing it from the splay, so start over.
283 if(type
!= MACH_PORT_TYPE_NONE
) {
284 ipc_splay_traverse_finish(&space
->is_tree
);
285 ipc_right_destroy(space
, name
, &tentry
->ite_entry
);
289 ipc_splay_traverse_finish(&space
->is_tree
);
290 is_write_unlock(space
);
295 * Routine: ipc_space_destroy
297 * Marks the space as dead and cleans up the entries.
298 * Does nothing if the space is already dead.
308 ipc_tree_entry_t tentry
;
310 ipc_entry_num_t size
;
311 mach_port_index_t index
;
313 assert(space
!= IS_NULL
);
315 is_write_lock(space
);
316 active
= space
->is_active
;
317 space
->is_active
= FALSE
;
318 is_write_unlock(space
);
325 * If somebody is trying to grow the table,
326 * we must wait until they finish and figure
327 * out the space died.
330 while (space
->is_growing
)
331 is_read_sleep(space
);
333 is_read_unlock(space
);
335 * Now we can futz with it unlocked.
338 if (space
== default_pager_space
)
339 default_pager_space
= IS_NULL
;
340 #endif /* MACH_KDB */
342 table
= space
->is_table
;
343 size
= space
->is_table_size
;
345 for (index
= 0; index
< size
; index
++) {
346 ipc_entry_t entry
= &table
[index
];
347 mach_port_type_t type
;
349 type
= IE_BITS_TYPE(entry
->ie_bits
);
350 if (type
!= MACH_PORT_TYPE_NONE
) {
351 mach_port_name_t name
;
353 name
= MACH_PORT_MAKE(index
,
354 IE_BITS_GEN(entry
->ie_bits
));
355 ipc_right_clean(space
, name
, entry
);
359 it_entries_free(space
->is_table_next
-1, table
);
360 space
->is_table_size
= 0;
362 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
364 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
365 mach_port_type_t type
;
366 mach_port_name_t name
= tentry
->ite_name
;
368 type
= IE_BITS_TYPE(tentry
->ite_bits
);
369 assert(type
!= MACH_PORT_TYPE_NONE
);
371 ipc_right_clean(space
, name
, &tentry
->ite_entry
);
373 if(type
== MACH_PORT_TYPE_SEND
)
374 ipc_hash_global_delete(space
, tentry
->ite_object
,
377 ipc_splay_traverse_finish(&space
->is_tree
);
380 * Because the space is now dead,
381 * we must release the "active" reference for it.
382 * Our caller still has his reference.