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