]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
6601e61a | 4 | * @APPLE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
6601e61a A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
8f6c56a5 | 11 | * |
6601e61a A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
6601e61a A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
8f6c56a5 | 19 | * |
6601e61a | 20 | * @APPLE_LICENSE_HEADER_END@ |
1c79356b A |
21 | */ |
22 | /* | |
23 | * @OSF_COPYRIGHT@ | |
24 | */ | |
25 | /* | |
26 | * Mach Operating System | |
27 | * Copyright (c) 1991,1990,1989 Carnegie Mellon University | |
28 | * All Rights Reserved. | |
29 | * | |
30 | * Permission to use, copy, modify and distribute this software and its | |
31 | * documentation is hereby granted, provided that both the copyright | |
32 | * notice and this permission notice appear in all copies of the | |
33 | * software, derivative works or modified versions, and any portions | |
34 | * thereof, and that both notices appear in supporting documentation. | |
35 | * | |
36 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
37 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
38 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
39 | * | |
40 | * Carnegie Mellon requests users of this software to return to | |
41 | * | |
42 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
43 | * School of Computer Science | |
44 | * Carnegie Mellon University | |
45 | * Pittsburgh PA 15213-3890 | |
46 | * | |
47 | * any improvements or extensions that they make and grant Carnegie Mellon | |
48 | * the rights to redistribute these changes. | |
49 | */ | |
50 | /* | |
51 | */ | |
52 | /* | |
53 | * File: ipc/ipc_object.h | |
54 | * Author: Rich Draves | |
55 | * Date: 1989 | |
56 | * | |
57 | * Definitions for IPC objects, for which tasks have capabilities. | |
58 | */ | |
59 | ||
60 | #ifndef _IPC_IPC_OBJECT_H_ | |
61 | #define _IPC_IPC_OBJECT_H_ | |
62 | ||
63 | #include <mach_rt.h> | |
1c79356b A |
64 | #include <mach_kdb.h> |
65 | ||
66 | #include <mach/kern_return.h> | |
67 | #include <mach/message.h> | |
68 | #include <kern/lock.h> | |
69 | #include <kern/macro_help.h> | |
1c79356b A |
70 | #include <kern/zalloc.h> |
71 | #include <ipc/ipc_types.h> | |
72 | ||
73 | typedef natural_t ipc_object_refs_t; /* for ipc/ipc_object.h */ | |
74 | typedef natural_t ipc_object_bits_t; | |
75 | typedef natural_t ipc_object_type_t; | |
76 | ||
77 | /* | |
78 | * There is no lock in the ipc_object; it is in the enclosing kernel | |
79 | * data structure (rpc_common_data) used by both ipc_port and ipc_pset. | |
80 | * The ipc_object is used to both tag and reference count these two data | |
81 | * structures, and (Noto Bene!) pointers to either of these or the | |
82 | * ipc_object at the head of these are freely cast back and forth; hence | |
83 | * the ipc_object MUST BE FIRST in the ipc_common_data. | |
84 | * | |
85 | * If the RPC implementation enabled user-mode code to use kernel-level | |
86 | * data structures (as ours used to), this peculiar structuring would | |
87 | * avoid having anything in user code depend on the kernel configuration | |
88 | * (with which lock size varies). | |
89 | */ | |
90 | struct ipc_object { | |
91 | ipc_object_refs_t io_references; | |
92 | ipc_object_bits_t io_bits; | |
91447636 | 93 | mach_port_name_t io_receiver_name; |
1c79356b | 94 | decl_mutex_data(, io_lock_data) |
1c79356b A |
95 | }; |
96 | ||
97 | /* | |
98 | * Legacy defines. Should use IPC_OBJECT_NULL, etc... | |
99 | */ | |
100 | #define IO_NULL ((ipc_object_t) 0) | |
101 | #define IO_DEAD ((ipc_object_t) -1) | |
102 | #define IO_VALID(io) (((io) != IO_NULL) && ((io) != IO_DEAD)) | |
103 | ||
104 | /* | |
105 | * IPC steals the high-order bits from the kotype to use | |
106 | * for its own purposes. This allows IPC to record facts | |
107 | * about ports that aren't otherwise obvious from the | |
108 | * existing port fields. In particular, IPC can optionally | |
109 | * mark a port for no more senders detection. Any change | |
110 | * to IO_BITS_PORT_INFO must be coordinated with bitfield | |
111 | * definitions in ipc_port.h. | |
112 | */ | |
113 | #define IO_BITS_PORT_INFO 0x0000f000 /* stupid port tricks */ | |
114 | #define IO_BITS_KOTYPE 0x00000fff /* used by the object */ | |
115 | #define IO_BITS_OTYPE 0x7fff0000 /* determines a zone */ | |
116 | #define IO_BITS_ACTIVE 0x80000000 /* is object alive? */ | |
117 | ||
118 | #define io_active(io) ((io)->io_bits & IO_BITS_ACTIVE) | |
119 | ||
120 | #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16) | |
121 | #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE) | |
122 | ||
123 | #define io_makebits(active, otype, kotype) \ | |
124 | (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype)) | |
125 | ||
126 | /* | |
127 | * Object types: ports, port sets, kernel-loaded ports | |
128 | */ | |
129 | #define IOT_PORT 0 | |
130 | #define IOT_PORT_SET 1 | |
131 | #define IOT_NUMBER 2 /* number of types used */ | |
132 | ||
133 | extern zone_t ipc_object_zones[IOT_NUMBER]; | |
134 | ||
135 | #define io_alloc(otype) \ | |
136 | ((ipc_object_t) zalloc(ipc_object_zones[(otype)])) | |
137 | ||
138 | #if MACH_ASSERT | |
139 | /* | |
140 | * Call the routine for io_free so that checking can be performed. | |
141 | */ | |
142 | extern void io_free( | |
143 | unsigned int otype, | |
144 | ipc_object_t object); | |
145 | ||
146 | #else /* MACH_ASSERT */ | |
147 | #define io_free(otype, io) \ | |
91447636 | 148 | zfree(ipc_object_zones[(otype)], (io)) |
1c79356b A |
149 | #endif /* MACH_ASSERT */ |
150 | ||
151 | /* | |
152 | * Here we depend on the ipc_object being first within the ipc_common_data, | |
153 | * which is first within the rpc_common_data, which in turn must be first | |
154 | * within any kernel data structure needing to lock an ipc_object | |
155 | * (ipc_port and ipc_pset). | |
156 | */ | |
1c79356b | 157 | #define io_lock_init(io) \ |
91447636 | 158 | mutex_init(&(io)->io_lock_data, 0) |
1c79356b A |
159 | #define io_lock(io) \ |
160 | mutex_lock(&(io)->io_lock_data) | |
161 | #define io_lock_try(io) \ | |
162 | mutex_try(&(io)->io_lock_data) | |
163 | #define io_unlock(io) \ | |
164 | mutex_unlock(&(io)->io_lock_data) | |
165 | ||
1c79356b | 166 | #define _VOLATILE_ volatile |
1c79356b A |
167 | |
168 | #define io_check_unlock(io) \ | |
169 | MACRO_BEGIN \ | |
170 | _VOLATILE_ ipc_object_refs_t _refs = (io)->io_references; \ | |
171 | \ | |
172 | io_unlock(io); \ | |
173 | if (_refs == 0) \ | |
174 | io_free(io_otype(io), io); \ | |
175 | MACRO_END | |
176 | ||
177 | /* Sanity check the ref count. If it is 0, we may be doubly zfreeing. | |
178 | * If it is larger than max int, it has been corrupted, probably by being | |
179 | * modified into an address (this is architecture dependent, but it's | |
180 | * safe to assume there cannot really be max int references). | |
181 | * | |
182 | * NOTE: The 0 test alone will not catch double zfreeing of ipc_port | |
183 | * structs, because the io_references field is the first word of the struct, | |
184 | * and zfree modifies that to point to the next free zone element. | |
185 | */ | |
186 | #define IO_MAX_REFERENCES \ | |
187 | (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1))) | |
188 | ||
189 | #define io_reference(io) \ | |
190 | MACRO_BEGIN \ | |
191 | assert((io)->io_references < IO_MAX_REFERENCES); \ | |
192 | (io)->io_references++; \ | |
193 | MACRO_END | |
194 | ||
195 | #define io_release(io) \ | |
196 | MACRO_BEGIN \ | |
197 | assert((io)->io_references > 0 && \ | |
198 | (io)->io_references <= IO_MAX_REFERENCES); \ | |
199 | (io)->io_references--; \ | |
200 | MACRO_END | |
201 | ||
202 | /* | |
203 | * Exported interfaces | |
204 | */ | |
205 | ||
206 | /* Take a reference to an object */ | |
207 | extern void ipc_object_reference( | |
208 | ipc_object_t object); | |
209 | ||
210 | /* Release a reference to an object */ | |
211 | extern void ipc_object_release( | |
212 | ipc_object_t object); | |
213 | ||
214 | /* Look up an object in a space */ | |
215 | extern kern_return_t ipc_object_translate( | |
216 | ipc_space_t space, | |
217 | mach_port_name_t name, | |
218 | mach_port_right_t right, | |
219 | ipc_object_t *objectp); | |
220 | ||
221 | /* Look up two objects in a space, locking them in the order described */ | |
222 | extern kern_return_t ipc_object_translate_two( | |
223 | ipc_space_t space, | |
224 | mach_port_name_t name1, | |
225 | mach_port_right_t right1, | |
226 | ipc_object_t *objectp1, | |
227 | mach_port_name_t name2, | |
228 | mach_port_right_t right2, | |
229 | ipc_object_t *objectp2); | |
230 | ||
231 | /* Allocate a dead-name entry */ | |
232 | extern kern_return_t | |
233 | ipc_object_alloc_dead( | |
234 | ipc_space_t space, | |
235 | mach_port_name_t *namep); | |
236 | ||
237 | /* Allocate a dead-name entry, with a specific name */ | |
238 | extern kern_return_t ipc_object_alloc_dead_name( | |
239 | ipc_space_t space, | |
240 | mach_port_name_t name); | |
241 | ||
242 | /* Allocate an object */ | |
243 | extern kern_return_t ipc_object_alloc( | |
244 | ipc_space_t space, | |
245 | ipc_object_type_t otype, | |
246 | mach_port_type_t type, | |
247 | mach_port_urefs_t urefs, | |
248 | mach_port_name_t *namep, | |
249 | ipc_object_t *objectp); | |
250 | ||
251 | /* Allocate an object, with a specific name */ | |
252 | extern kern_return_t ipc_object_alloc_name( | |
253 | ipc_space_t space, | |
254 | ipc_object_type_t otype, | |
255 | mach_port_type_t type, | |
256 | mach_port_urefs_t urefs, | |
257 | mach_port_name_t name, | |
258 | ipc_object_t *objectp); | |
259 | ||
260 | /* Convert a send type name to a received type name */ | |
261 | extern mach_msg_type_name_t ipc_object_copyin_type( | |
262 | mach_msg_type_name_t msgt_name); | |
263 | ||
264 | /* Copyin a capability from a space */ | |
265 | extern kern_return_t ipc_object_copyin( | |
266 | ipc_space_t space, | |
267 | mach_port_name_t name, | |
268 | mach_msg_type_name_t msgt_name, | |
269 | ipc_object_t *objectp); | |
270 | ||
271 | /* Copyin a naked capability from the kernel */ | |
272 | extern void ipc_object_copyin_from_kernel( | |
273 | ipc_object_t object, | |
274 | mach_msg_type_name_t msgt_name); | |
275 | ||
276 | /* Destroy a naked capability */ | |
277 | extern void ipc_object_destroy( | |
278 | ipc_object_t object, | |
279 | mach_msg_type_name_t msgt_name); | |
280 | ||
281 | /* Copyout a capability, placing it into a space */ | |
282 | extern kern_return_t ipc_object_copyout( | |
283 | ipc_space_t space, | |
284 | ipc_object_t object, | |
285 | mach_msg_type_name_t msgt_name, | |
286 | boolean_t overflow, | |
287 | mach_port_name_t *namep); | |
288 | ||
289 | /* Copyout a capability with a name, placing it into a space */ | |
290 | extern kern_return_t ipc_object_copyout_name( | |
291 | ipc_space_t space, | |
292 | ipc_object_t object, | |
293 | mach_msg_type_name_t msgt_name, | |
294 | boolean_t overflow, | |
295 | mach_port_name_t name); | |
296 | ||
297 | /* Translate/consume the destination right of a message */ | |
298 | extern void ipc_object_copyout_dest( | |
299 | ipc_space_t space, | |
300 | ipc_object_t object, | |
301 | mach_msg_type_name_t msgt_name, | |
302 | mach_port_name_t *namep); | |
303 | ||
304 | /* Rename an entry in a space */ | |
305 | extern kern_return_t ipc_object_rename( | |
306 | ipc_space_t space, | |
307 | mach_port_name_t oname, | |
308 | mach_port_name_t nname); | |
309 | ||
310 | #if MACH_KDB | |
311 | /* Pretty-print an ipc object */ | |
312 | ||
313 | extern void ipc_object_print( | |
314 | ipc_object_t object); | |
315 | ||
316 | #endif /* MACH_KDB */ | |
317 | ||
318 | #endif /* _IPC_IPC_OBJECT_H_ */ |