2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
65 * File: ipc/ipc_space.c
69 * Functions to manipulate IPC capability spaces.
74 #include <mach/boolean.h>
75 #include <mach/kern_return.h>
76 #include <mach/port.h>
77 #include <kern/assert.h>
78 #include <kern/sched_prim.h>
79 #include <kern/zalloc.h>
81 #include <ipc/ipc_entry.h>
82 #include <ipc/ipc_splay.h>
83 #include <ipc/ipc_object.h>
84 #include <ipc/ipc_hash.h>
85 #include <ipc/ipc_table.h>
86 #include <ipc/ipc_port.h>
87 #include <ipc/ipc_space.h>
88 #include <ipc/ipc_right.h>
91 zone_t ipc_space_zone
;
92 ipc_space_t ipc_space_kernel
;
93 ipc_space_t ipc_space_reply
;
95 ipc_space_t default_pager_space
;
99 * Routine: ipc_space_reference
100 * Routine: ipc_space_release
102 * Function versions of the IPC space macros.
103 * The "is_" cover macros can be defined to use the
104 * macros or the functions, as desired.
111 ipc_space_reference_macro(space
);
118 ipc_space_release_macro(space
);
122 * Routine: ipc_space_create
124 * Creates a new IPC space.
126 * The new space has two references, one for the caller
127 * and one because it is active.
129 * Nothing locked. Allocates memory.
131 * KERN_SUCCESS Created a space.
132 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
137 ipc_table_size_t initial
,
142 ipc_entry_num_t new_size
;
143 mach_port_index_t index
;
146 if (space
== IS_NULL
)
147 return KERN_RESOURCE_SHORTAGE
;
149 table
= it_entries_alloc(initial
);
150 if (table
== IE_NULL
) {
152 return KERN_RESOURCE_SHORTAGE
;
155 new_size
= initial
->its_size
;
156 memset((void *) table
, 0, new_size
* sizeof(struct ipc_entry
));
159 * Initialize the free list in the table.
160 * Add the entries in reverse order, and
161 * set the generation number to -1, so that
162 * initial allocations produce "natural" names.
164 for (index
= 0; index
< new_size
; index
++) {
165 ipc_entry_t entry
= &table
[index
];
167 entry
->ie_bits
= IE_BITS_GEN_MASK
;
168 entry
->ie_next
= index
+1;
170 table
[new_size
-1].ie_next
= 0;
172 is_ref_lock_init(space
);
173 space
->is_references
= 2;
176 space
->is_active
= TRUE
;
177 space
->is_growing
= FALSE
;
178 space
->is_table
= table
;
179 space
->is_table_size
= new_size
;
180 space
->is_table_next
= initial
+1;
182 ipc_splay_tree_init(&space
->is_tree
);
183 space
->is_tree_total
= 0;
184 space
->is_tree_small
= 0;
185 space
->is_tree_hash
= 0;
186 space
->is_task
= NULL
;
193 * Routine: ipc_space_create_special
195 * Create a special space. A special space
196 * doesn't hold rights in the normal way.
197 * Instead it is place-holder for holding
198 * disembodied (naked) receive rights.
199 * See ipc_port_alloc_special/ipc_port_dealloc_special.
203 * KERN_SUCCESS Created a space.
204 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
208 ipc_space_create_special(
214 if (space
== IS_NULL
)
215 return KERN_RESOURCE_SHORTAGE
;
217 is_ref_lock_init(space
);
218 space
->is_references
= 1;
221 space
->is_active
= FALSE
;
228 * ipc_space_clean - remove all port references from an ipc space.
230 * In order to follow the traditional semantic, ipc_space_destroy
231 * will not destroy the entire port table of a shared space. Instead
232 * it will simply clear its own sub-space.
238 ipc_tree_entry_t tentry
;
240 ipc_entry_num_t size
;
241 mach_port_index_t index
;
244 * If somebody is trying to grow the table,
245 * we must wait until they finish and figure
246 * out the space died.
248 is_write_lock(space
);
249 while (space
->is_growing
)
250 is_write_sleep(space
);
252 if (!space
->is_active
) {
253 is_write_unlock(space
);
258 * Now we can futz with it since we have the write lock.
261 if (space
== default_pager_space
)
262 default_pager_space
= IS_NULL
;
263 #endif /* MACH_KDB */
265 table
= space
->is_table
;
266 size
= space
->is_table_size
;
268 for (index
= 0; index
< size
; index
++) {
269 ipc_entry_t entry
= &table
[index
];
270 mach_port_type_t type
;
272 type
= IE_BITS_TYPE(entry
->ie_bits
);
273 if (type
!= MACH_PORT_TYPE_NONE
) {
274 mach_port_name_t name
= MACH_PORT_MAKE(index
,
275 IE_BITS_GEN(entry
->ie_bits
));
276 ipc_right_destroy(space
, name
, entry
);
281 * JMM - Now the table is cleaned out. We don't bother shrinking the
282 * size of the table at this point, but we probably should if it is
283 * really large. Lets just clean up the splay tree.
286 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
288 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
289 mach_port_type_t type
;
290 mach_port_name_t name
= tentry
->ite_name
;
292 type
= IE_BITS_TYPE(tentry
->ite_bits
);
294 * If it is a real right, then destroy it. This will have the
295 * side effect of removing it from the splay, so start over.
297 if(type
!= MACH_PORT_TYPE_NONE
) {
298 ipc_splay_traverse_finish(&space
->is_tree
);
299 ipc_right_destroy(space
, name
, &tentry
->ite_entry
);
303 ipc_splay_traverse_finish(&space
->is_tree
);
304 is_write_unlock(space
);
309 * Routine: ipc_space_destroy
311 * Marks the space as dead and cleans up the entries.
312 * Does nothing if the space is already dead.
322 ipc_tree_entry_t tentry
;
324 ipc_entry_num_t size
;
325 mach_port_index_t index
;
327 assert(space
!= IS_NULL
);
329 is_write_lock(space
);
330 active
= space
->is_active
;
331 space
->is_active
= FALSE
;
332 is_write_unlock(space
);
339 * If somebody is trying to grow the table,
340 * we must wait until they finish and figure
341 * out the space died.
344 while (space
->is_growing
)
345 is_read_sleep(space
);
347 is_read_unlock(space
);
349 * Now we can futz with it unlocked.
352 if (space
== default_pager_space
)
353 default_pager_space
= IS_NULL
;
354 #endif /* MACH_KDB */
356 table
= space
->is_table
;
357 size
= space
->is_table_size
;
359 for (index
= 0; index
< size
; index
++) {
360 ipc_entry_t entry
= &table
[index
];
361 mach_port_type_t type
;
363 type
= IE_BITS_TYPE(entry
->ie_bits
);
364 if (type
!= MACH_PORT_TYPE_NONE
) {
365 mach_port_name_t name
;
367 name
= MACH_PORT_MAKE(index
,
368 IE_BITS_GEN(entry
->ie_bits
));
369 ipc_right_clean(space
, name
, entry
);
373 it_entries_free(space
->is_table_next
-1, table
);
374 space
->is_table_size
= 0;
376 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
378 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
379 mach_port_type_t type
;
380 mach_port_name_t name
= tentry
->ite_name
;
382 type
= IE_BITS_TYPE(tentry
->ite_bits
);
383 assert(type
!= MACH_PORT_TYPE_NONE
);
385 ipc_right_clean(space
, name
, &tentry
->ite_entry
);
387 if(type
== MACH_PORT_TYPE_SEND
)
388 ipc_hash_global_delete(space
, tentry
->ite_object
,
391 ipc_splay_traverse_finish(&space
->is_tree
);
394 * Because the space is now dead,
395 * we must release the "active" reference for it.
396 * Our caller still has his reference.