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
);
253 * Now we can futz with it since we have the write lock.
256 if (space
== default_pager_space
)
257 default_pager_space
= IS_NULL
;
258 #endif /* MACH_KDB */
260 table
= space
->is_table
;
261 size
= space
->is_table_size
;
263 for (index
= 0; index
< size
; index
++) {
264 ipc_entry_t entry
= &table
[index
];
265 mach_port_type_t type
;
267 type
= IE_BITS_TYPE(entry
->ie_bits
);
268 if (type
!= MACH_PORT_TYPE_NONE
) {
269 mach_port_name_t name
= MACH_PORT_MAKE(index
,
270 IE_BITS_GEN(entry
->ie_bits
));
271 ipc_right_destroy(space
, name
, entry
);
276 * JMM - Now the table is cleaned out. We don't bother shrinking the
277 * size of the table at this point, but we probably should if it is
278 * really large. Lets just clean up the splay tree.
281 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
283 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
284 mach_port_type_t type
;
285 mach_port_name_t name
= tentry
->ite_name
;
287 type
= IE_BITS_TYPE(tentry
->ite_bits
);
289 * If it is a real right, then destroy it. This will have the
290 * side effect of removing it from the splay, so start over.
292 if(type
!= MACH_PORT_TYPE_NONE
) {
293 ipc_splay_traverse_finish(&space
->is_tree
);
294 ipc_right_destroy(space
, name
, &tentry
->ite_entry
);
298 ipc_splay_traverse_finish(&space
->is_tree
);
299 is_write_unlock(space
);
304 * Routine: ipc_space_destroy
306 * Marks the space as dead and cleans up the entries.
307 * Does nothing if the space is already dead.
317 ipc_tree_entry_t tentry
;
319 ipc_entry_num_t size
;
320 mach_port_index_t index
;
322 assert(space
!= IS_NULL
);
324 is_write_lock(space
);
325 active
= space
->is_active
;
326 space
->is_active
= FALSE
;
327 is_write_unlock(space
);
334 * If somebody is trying to grow the table,
335 * we must wait until they finish and figure
336 * out the space died.
339 while (space
->is_growing
)
340 is_read_sleep(space
);
342 is_read_unlock(space
);
344 * Now we can futz with it unlocked.
347 if (space
== default_pager_space
)
348 default_pager_space
= IS_NULL
;
349 #endif /* MACH_KDB */
351 table
= space
->is_table
;
352 size
= space
->is_table_size
;
354 for (index
= 0; index
< size
; index
++) {
355 ipc_entry_t entry
= &table
[index
];
356 mach_port_type_t type
;
358 type
= IE_BITS_TYPE(entry
->ie_bits
);
359 if (type
!= MACH_PORT_TYPE_NONE
) {
360 mach_port_name_t name
;
362 name
= MACH_PORT_MAKE(index
,
363 IE_BITS_GEN(entry
->ie_bits
));
364 ipc_right_clean(space
, name
, entry
);
368 it_entries_free(space
->is_table_next
-1, table
);
369 space
->is_table_size
= 0;
371 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
);
373 tentry
= ipc_splay_traverse_next(&space
->is_tree
, TRUE
)) {
374 mach_port_type_t type
;
375 mach_port_name_t name
= tentry
->ite_name
;
377 type
= IE_BITS_TYPE(tentry
->ite_bits
);
378 assert(type
!= MACH_PORT_TYPE_NONE
);
380 ipc_right_clean(space
, name
, &tentry
->ite_entry
);
382 if(type
== MACH_PORT_TYPE_SEND
)
383 ipc_hash_global_delete(space
, tentry
->ite_object
,
386 ipc_splay_traverse_finish(&space
->is_tree
);
389 * Because the space is now dead,
390 * we must release the "active" reference for it.
391 * Our caller still has his reference.