]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_object.h
xnu-792.17.14.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_OSREFERENCE_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. 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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 */
56 /*
57 */
58 /*
59 * File: ipc/ipc_object.h
60 * Author: Rich Draves
61 * Date: 1989
62 *
63 * Definitions for IPC objects, for which tasks have capabilities.
64 */
65
66 #ifndef _IPC_IPC_OBJECT_H_
67 #define _IPC_IPC_OBJECT_H_
68
69 #include <mach_rt.h>
70 #include <mach_kdb.h>
71
72 #include <mach/kern_return.h>
73 #include <mach/message.h>
74 #include <kern/lock.h>
75 #include <kern/macro_help.h>
76 #include <kern/zalloc.h>
77 #include <ipc/ipc_types.h>
78
79 typedef natural_t ipc_object_refs_t; /* for ipc/ipc_object.h */
80 typedef natural_t ipc_object_bits_t;
81 typedef natural_t ipc_object_type_t;
82
83 /*
84 * There is no lock in the ipc_object; it is in the enclosing kernel
85 * data structure (rpc_common_data) used by both ipc_port and ipc_pset.
86 * The ipc_object is used to both tag and reference count these two data
87 * structures, and (Noto Bene!) pointers to either of these or the
88 * ipc_object at the head of these are freely cast back and forth; hence
89 * the ipc_object MUST BE FIRST in the ipc_common_data.
90 *
91 * If the RPC implementation enabled user-mode code to use kernel-level
92 * data structures (as ours used to), this peculiar structuring would
93 * avoid having anything in user code depend on the kernel configuration
94 * (with which lock size varies).
95 */
96 struct ipc_object {
97 ipc_object_refs_t io_references;
98 ipc_object_bits_t io_bits;
99 mach_port_name_t io_receiver_name;
100 decl_mutex_data(, io_lock_data)
101 };
102
103 /*
104 * Legacy defines. Should use IPC_OBJECT_NULL, etc...
105 */
106 #define IO_NULL ((ipc_object_t) 0)
107 #define IO_DEAD ((ipc_object_t) -1)
108 #define IO_VALID(io) (((io) != IO_NULL) && ((io) != IO_DEAD))
109
110 /*
111 * IPC steals the high-order bits from the kotype to use
112 * for its own purposes. This allows IPC to record facts
113 * about ports that aren't otherwise obvious from the
114 * existing port fields. In particular, IPC can optionally
115 * mark a port for no more senders detection. Any change
116 * to IO_BITS_PORT_INFO must be coordinated with bitfield
117 * definitions in ipc_port.h.
118 */
119 #define IO_BITS_PORT_INFO 0x0000f000 /* stupid port tricks */
120 #define IO_BITS_KOTYPE 0x00000fff /* used by the object */
121 #define IO_BITS_OTYPE 0x7fff0000 /* determines a zone */
122 #define IO_BITS_ACTIVE 0x80000000 /* is object alive? */
123
124 #define io_active(io) ((io)->io_bits & IO_BITS_ACTIVE)
125
126 #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16)
127 #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE)
128
129 #define io_makebits(active, otype, kotype) \
130 (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype))
131
132 /*
133 * Object types: ports, port sets, kernel-loaded ports
134 */
135 #define IOT_PORT 0
136 #define IOT_PORT_SET 1
137 #define IOT_NUMBER 2 /* number of types used */
138
139 extern zone_t ipc_object_zones[IOT_NUMBER];
140
141 #define io_alloc(otype) \
142 ((ipc_object_t) zalloc(ipc_object_zones[(otype)]))
143
144 #if MACH_ASSERT
145 /*
146 * Call the routine for io_free so that checking can be performed.
147 */
148 extern void io_free(
149 unsigned int otype,
150 ipc_object_t object);
151
152 #else /* MACH_ASSERT */
153 #define io_free(otype, io) \
154 zfree(ipc_object_zones[(otype)], (io))
155 #endif /* MACH_ASSERT */
156
157 /*
158 * Here we depend on the ipc_object being first within the ipc_common_data,
159 * which is first within the rpc_common_data, which in turn must be first
160 * within any kernel data structure needing to lock an ipc_object
161 * (ipc_port and ipc_pset).
162 */
163 #define io_lock_init(io) \
164 mutex_init(&(io)->io_lock_data, 0)
165 #define io_lock(io) \
166 mutex_lock(&(io)->io_lock_data)
167 #define io_lock_try(io) \
168 mutex_try(&(io)->io_lock_data)
169 #define io_unlock(io) \
170 mutex_unlock(&(io)->io_lock_data)
171
172 #define _VOLATILE_ volatile
173
174 #define io_check_unlock(io) \
175 MACRO_BEGIN \
176 _VOLATILE_ ipc_object_refs_t _refs = (io)->io_references; \
177 \
178 io_unlock(io); \
179 if (_refs == 0) \
180 io_free(io_otype(io), io); \
181 MACRO_END
182
183 /* Sanity check the ref count. If it is 0, we may be doubly zfreeing.
184 * If it is larger than max int, it has been corrupted, probably by being
185 * modified into an address (this is architecture dependent, but it's
186 * safe to assume there cannot really be max int references).
187 *
188 * NOTE: The 0 test alone will not catch double zfreeing of ipc_port
189 * structs, because the io_references field is the first word of the struct,
190 * and zfree modifies that to point to the next free zone element.
191 */
192 #define IO_MAX_REFERENCES \
193 (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1)))
194
195 #define io_reference(io) \
196 MACRO_BEGIN \
197 assert((io)->io_references < IO_MAX_REFERENCES); \
198 (io)->io_references++; \
199 MACRO_END
200
201 #define io_release(io) \
202 MACRO_BEGIN \
203 assert((io)->io_references > 0 && \
204 (io)->io_references <= IO_MAX_REFERENCES); \
205 (io)->io_references--; \
206 MACRO_END
207
208 /*
209 * Exported interfaces
210 */
211
212 /* Take a reference to an object */
213 extern void ipc_object_reference(
214 ipc_object_t object);
215
216 /* Release a reference to an object */
217 extern void ipc_object_release(
218 ipc_object_t object);
219
220 /* Look up an object in a space */
221 extern kern_return_t ipc_object_translate(
222 ipc_space_t space,
223 mach_port_name_t name,
224 mach_port_right_t right,
225 ipc_object_t *objectp);
226
227 /* Look up two objects in a space, locking them in the order described */
228 extern kern_return_t ipc_object_translate_two(
229 ipc_space_t space,
230 mach_port_name_t name1,
231 mach_port_right_t right1,
232 ipc_object_t *objectp1,
233 mach_port_name_t name2,
234 mach_port_right_t right2,
235 ipc_object_t *objectp2);
236
237 /* Allocate a dead-name entry */
238 extern kern_return_t
239 ipc_object_alloc_dead(
240 ipc_space_t space,
241 mach_port_name_t *namep);
242
243 /* Allocate a dead-name entry, with a specific name */
244 extern kern_return_t ipc_object_alloc_dead_name(
245 ipc_space_t space,
246 mach_port_name_t name);
247
248 /* Allocate an object */
249 extern kern_return_t ipc_object_alloc(
250 ipc_space_t space,
251 ipc_object_type_t otype,
252 mach_port_type_t type,
253 mach_port_urefs_t urefs,
254 mach_port_name_t *namep,
255 ipc_object_t *objectp);
256
257 /* Allocate an object, with a specific name */
258 extern kern_return_t ipc_object_alloc_name(
259 ipc_space_t space,
260 ipc_object_type_t otype,
261 mach_port_type_t type,
262 mach_port_urefs_t urefs,
263 mach_port_name_t name,
264 ipc_object_t *objectp);
265
266 /* Convert a send type name to a received type name */
267 extern mach_msg_type_name_t ipc_object_copyin_type(
268 mach_msg_type_name_t msgt_name);
269
270 /* Copyin a capability from a space */
271 extern kern_return_t ipc_object_copyin(
272 ipc_space_t space,
273 mach_port_name_t name,
274 mach_msg_type_name_t msgt_name,
275 ipc_object_t *objectp);
276
277 /* Copyin a naked capability from the kernel */
278 extern void ipc_object_copyin_from_kernel(
279 ipc_object_t object,
280 mach_msg_type_name_t msgt_name);
281
282 /* Destroy a naked capability */
283 extern void ipc_object_destroy(
284 ipc_object_t object,
285 mach_msg_type_name_t msgt_name);
286
287 /* Copyout a capability, placing it into a space */
288 extern kern_return_t ipc_object_copyout(
289 ipc_space_t space,
290 ipc_object_t object,
291 mach_msg_type_name_t msgt_name,
292 boolean_t overflow,
293 mach_port_name_t *namep);
294
295 /* Copyout a capability with a name, placing it into a space */
296 extern kern_return_t ipc_object_copyout_name(
297 ipc_space_t space,
298 ipc_object_t object,
299 mach_msg_type_name_t msgt_name,
300 boolean_t overflow,
301 mach_port_name_t name);
302
303 /* Translate/consume the destination right of a message */
304 extern void ipc_object_copyout_dest(
305 ipc_space_t space,
306 ipc_object_t object,
307 mach_msg_type_name_t msgt_name,
308 mach_port_name_t *namep);
309
310 /* Rename an entry in a space */
311 extern kern_return_t ipc_object_rename(
312 ipc_space_t space,
313 mach_port_name_t oname,
314 mach_port_name_t nname);
315
316 #if MACH_KDB
317 /* Pretty-print an ipc object */
318
319 extern void ipc_object_print(
320 ipc_object_t object);
321
322 #endif /* MACH_KDB */
323
324 #endif /* _IPC_IPC_OBJECT_H_ */