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