]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
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. | |
41 | * | |
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. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
2d21ac55 A |
56 | /* |
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, | |
60 | * Version 2.0. | |
61 | */ | |
1c79356b A |
62 | /* |
63 | */ | |
64 | /* | |
65 | * File: ipc/ipc_space.c | |
66 | * Author: Rich Draves | |
67 | * Date: 1989 | |
68 | * | |
69 | * Functions to manipulate IPC capability spaces. | |
70 | */ | |
71 | ||
1c79356b A |
72 | #include <mach/boolean.h> |
73 | #include <mach/kern_return.h> | |
74 | #include <mach/port.h> | |
75 | #include <kern/assert.h> | |
76 | #include <kern/sched_prim.h> | |
77 | #include <kern/zalloc.h> | |
78 | #include <ipc/port.h> | |
79 | #include <ipc/ipc_entry.h> | |
1c79356b A |
80 | #include <ipc/ipc_object.h> |
81 | #include <ipc/ipc_hash.h> | |
82 | #include <ipc/ipc_table.h> | |
83 | #include <ipc/ipc_port.h> | |
84 | #include <ipc/ipc_space.h> | |
85 | #include <ipc/ipc_right.h> | |
86 | #include <string.h> | |
87 | ||
88 | zone_t ipc_space_zone; | |
89 | ipc_space_t ipc_space_kernel; | |
90 | ipc_space_t ipc_space_reply; | |
1c79356b A |
91 | |
92 | /* | |
93 | * Routine: ipc_space_reference | |
94 | * Routine: ipc_space_release | |
95 | * Purpose: | |
316670eb | 96 | * Function versions of the IPC space inline reference. |
1c79356b A |
97 | */ |
98 | ||
99 | void | |
100 | ipc_space_reference( | |
101 | ipc_space_t space) | |
102 | { | |
316670eb | 103 | is_reference(space); |
1c79356b A |
104 | } |
105 | ||
106 | void | |
107 | ipc_space_release( | |
108 | ipc_space_t space) | |
109 | { | |
316670eb | 110 | is_release(space); |
1c79356b A |
111 | } |
112 | ||
113 | /* | |
114 | * Routine: ipc_space_create | |
115 | * Purpose: | |
116 | * Creates a new IPC space. | |
117 | * | |
118 | * The new space has two references, one for the caller | |
119 | * and one because it is active. | |
120 | * Conditions: | |
121 | * Nothing locked. Allocates memory. | |
122 | * Returns: | |
123 | * KERN_SUCCESS Created a space. | |
124 | * KERN_RESOURCE_SHORTAGE Couldn't allocate memory. | |
125 | */ | |
126 | ||
127 | kern_return_t | |
128 | ipc_space_create( | |
129 | ipc_table_size_t initial, | |
130 | ipc_space_t *spacep) | |
131 | { | |
132 | ipc_space_t space; | |
133 | ipc_entry_t table; | |
134 | ipc_entry_num_t new_size; | |
135 | mach_port_index_t index; | |
136 | ||
137 | space = is_alloc(); | |
138 | if (space == IS_NULL) | |
139 | return KERN_RESOURCE_SHORTAGE; | |
140 | ||
141 | table = it_entries_alloc(initial); | |
142 | if (table == IE_NULL) { | |
143 | is_free(space); | |
144 | return KERN_RESOURCE_SHORTAGE; | |
145 | } | |
146 | ||
147 | new_size = initial->its_size; | |
148 | memset((void *) table, 0, new_size * sizeof(struct ipc_entry)); | |
149 | ||
150 | /* | |
151 | * Initialize the free list in the table. | |
152 | * Add the entries in reverse order, and | |
153 | * set the generation number to -1, so that | |
154 | * initial allocations produce "natural" names. | |
155 | */ | |
156 | for (index = 0; index < new_size; index++) { | |
157 | ipc_entry_t entry = &table[index]; | |
158 | ||
159 | entry->ie_bits = IE_BITS_GEN_MASK; | |
160 | entry->ie_next = index+1; | |
161 | } | |
162 | table[new_size-1].ie_next = 0; | |
163 | ||
1c79356b | 164 | is_lock_init(space); |
316670eb | 165 | space->is_bits = 2; /* 2 refs, active, not growing */ |
1c79356b | 166 | space->is_table_size = new_size; |
fe8ab488 | 167 | space->is_table_free = new_size - 1; |
316670eb | 168 | space->is_table = table; |
1c79356b | 169 | space->is_table_next = initial+1; |
2d21ac55 | 170 | space->is_task = NULL; |
316670eb A |
171 | space->is_low_mod = new_size; |
172 | space->is_high_mod = 0; | |
1c79356b A |
173 | |
174 | *spacep = space; | |
175 | return KERN_SUCCESS; | |
176 | } | |
177 | ||
178 | /* | |
179 | * Routine: ipc_space_create_special | |
180 | * Purpose: | |
181 | * Create a special space. A special space | |
182 | * doesn't hold rights in the normal way. | |
183 | * Instead it is place-holder for holding | |
184 | * disembodied (naked) receive rights. | |
185 | * See ipc_port_alloc_special/ipc_port_dealloc_special. | |
186 | * Conditions: | |
187 | * Nothing locked. | |
188 | * Returns: | |
189 | * KERN_SUCCESS Created a space. | |
190 | * KERN_RESOURCE_SHORTAGE Couldn't allocate memory. | |
191 | */ | |
192 | ||
193 | kern_return_t | |
194 | ipc_space_create_special( | |
195 | ipc_space_t *spacep) | |
196 | { | |
197 | ipc_space_t space; | |
198 | ||
199 | space = is_alloc(); | |
200 | if (space == IS_NULL) | |
201 | return KERN_RESOURCE_SHORTAGE; | |
202 | ||
1c79356b | 203 | is_lock_init(space); |
39236c6e A |
204 | |
205 | space->is_bits = IS_INACTIVE | 1; /* 1 ref, not active, not growing */ | |
206 | space->is_table = IE_NULL; | |
207 | space->is_task = TASK_NULL; | |
208 | space->is_table_next = 0; | |
209 | space->is_low_mod = 0; | |
210 | space->is_high_mod = 0; | |
211 | ||
1c79356b A |
212 | *spacep = space; |
213 | return KERN_SUCCESS; | |
214 | } | |
215 | ||
216 | /* | |
217 | * ipc_space_clean - remove all port references from an ipc space. | |
218 | * | |
219 | * In order to follow the traditional semantic, ipc_space_destroy | |
220 | * will not destroy the entire port table of a shared space. Instead | |
221 | * it will simply clear its own sub-space. | |
222 | */ | |
223 | void | |
224 | ipc_space_clean( | |
225 | ipc_space_t space) | |
226 | { | |
1c79356b A |
227 | ipc_entry_t table; |
228 | ipc_entry_num_t size; | |
229 | mach_port_index_t index; | |
230 | ||
231 | /* | |
232 | * If somebody is trying to grow the table, | |
233 | * we must wait until they finish and figure | |
234 | * out the space died. | |
235 | */ | |
316670eb | 236 | retry: |
1c79356b | 237 | is_write_lock(space); |
316670eb | 238 | while (is_growing(space)) |
9bccf70c | 239 | is_write_sleep(space); |
1c79356b | 240 | |
316670eb | 241 | if (!is_active(space)) { |
6d2010ae A |
242 | is_write_unlock(space); |
243 | return; | |
244 | } | |
245 | ||
1c79356b A |
246 | /* |
247 | * Now we can futz with it since we have the write lock. | |
248 | */ | |
1c79356b A |
249 | |
250 | table = space->is_table; | |
251 | size = space->is_table_size; | |
252 | ||
253 | for (index = 0; index < size; index++) { | |
254 | ipc_entry_t entry = &table[index]; | |
255 | mach_port_type_t type; | |
256 | ||
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)); | |
39236c6e | 261 | ipc_right_destroy(space, name, entry, FALSE, 0); /* unlocks space */ |
316670eb | 262 | goto retry; |
1c79356b A |
263 | } |
264 | } | |
265 | ||
316670eb | 266 | /* |
1c79356b A |
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 | |
316670eb | 269 | * really large. |
1c79356b | 270 | */ |
316670eb | 271 | |
1c79356b A |
272 | is_write_unlock(space); |
273 | } | |
274 | ||
275 | ||
276 | /* | |
316670eb | 277 | * Routine: ipc_space_terminate |
1c79356b A |
278 | * Purpose: |
279 | * Marks the space as dead and cleans up the entries. | |
280 | * Does nothing if the space is already dead. | |
281 | * Conditions: | |
282 | * Nothing locked. | |
283 | */ | |
284 | ||
285 | void | |
316670eb | 286 | ipc_space_terminate( |
1c79356b A |
287 | ipc_space_t space) |
288 | { | |
1c79356b A |
289 | ipc_entry_t table; |
290 | ipc_entry_num_t size; | |
291 | mach_port_index_t index; | |
292 | ||
293 | assert(space != IS_NULL); | |
294 | ||
295 | is_write_lock(space); | |
316670eb A |
296 | if (!is_active(space)) { |
297 | is_write_unlock(space); | |
1c79356b | 298 | return; |
316670eb A |
299 | } |
300 | is_mark_inactive(space); | |
1c79356b A |
301 | |
302 | /* | |
303 | * If somebody is trying to grow the table, | |
304 | * we must wait until they finish and figure | |
305 | * out the space died. | |
306 | */ | |
316670eb A |
307 | while (is_growing(space)) |
308 | is_write_sleep(space); | |
309 | ||
310 | is_write_unlock(space); | |
311 | ||
1c79356b | 312 | |
1c79356b A |
313 | /* |
314 | * Now we can futz with it unlocked. | |
315 | */ | |
1c79356b A |
316 | |
317 | table = space->is_table; | |
318 | size = space->is_table_size; | |
319 | ||
320 | for (index = 0; index < size; index++) { | |
321 | ipc_entry_t entry = &table[index]; | |
322 | mach_port_type_t type; | |
323 | ||
324 | type = IE_BITS_TYPE(entry->ie_bits); | |
325 | if (type != MACH_PORT_TYPE_NONE) { | |
326 | mach_port_name_t name; | |
327 | ||
328 | name = MACH_PORT_MAKE(index, | |
329 | IE_BITS_GEN(entry->ie_bits)); | |
316670eb | 330 | ipc_right_terminate(space, name, entry); |
1c79356b A |
331 | } |
332 | } | |
333 | ||
334 | it_entries_free(space->is_table_next-1, table); | |
335 | space->is_table_size = 0; | |
fe8ab488 | 336 | space->is_table_free = 0; |
1c79356b | 337 | |
1c79356b A |
338 | /* |
339 | * Because the space is now dead, | |
340 | * we must release the "active" reference for it. | |
341 | * Our caller still has his reference. | |
342 | */ | |
343 | is_release(space); | |
344 | } | |
91447636 A |
345 | |
346 |