]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_object.h
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_object.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 /*
56 * File: ipc/ipc_object.h
57 * Author: Rich Draves
58 * Date: 1989
59 *
60 * Definitions for IPC objects, for which tasks have capabilities.
61 */
62
63 #ifndef _IPC_IPC_OBJECT_H_
64 #define _IPC_IPC_OBJECT_H_
65
66 #include <mach_rt.h>
67 #include <cpus.h>
68 #include <mach_kdb.h>
69
70 #include <mach/kern_return.h>
71 #include <mach/message.h>
72 #include <kern/lock.h>
73 #include <kern/macro_help.h>
74 #include <kern/zalloc.h>
75 #include <ipc/ipc_types.h>
76
77 typedef natural_t ipc_object_refs_t; /* for ipc/ipc_object.h */
78 typedef natural_t ipc_object_bits_t;
79 typedef natural_t ipc_object_type_t;
80
81 /*
82 * There is no lock in the ipc_object; it is in the enclosing kernel
83 * data structure (rpc_common_data) used by both ipc_port and ipc_pset.
84 * The ipc_object is used to both tag and reference count these two data
85 * structures, and (Noto Bene!) pointers to either of these or the
86 * ipc_object at the head of these are freely cast back and forth; hence
87 * the ipc_object MUST BE FIRST in the ipc_common_data.
88 *
89 * If the RPC implementation enabled user-mode code to use kernel-level
90 * data structures (as ours used to), this peculiar structuring would
91 * avoid having anything in user code depend on the kernel configuration
92 * (with which lock size varies).
93 */
94 struct ipc_object {
95 ipc_object_refs_t io_references;
96 ipc_object_bits_t io_bits;
97 port_name_t io_receiver_name;
98 #if NCPUS == 1
99 usimple_lock_data_t io_lock_data;
100 #else
101 decl_mutex_data(, io_lock_data)
102 #endif
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)], (vm_offset_t) (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 #if NCPUS == 1
166
167 #define io_lock_init(io) \
168 usimple_lock_init(&(io)-io_lock_data, ETAP_IPC_OBJECT)
169 #define io_lock(io) \
170 usimple_lock(&(io)->io_lock_data)
171 #define io_lock_try(io) \
172 usimple_lock_try(&(io)->io_lock_data)
173 #define io_unlock(io) \
174 usimple_unlock(&(io)->io_lock_data)
175
176 #else /* NCPUS == 1 */
177
178 #define io_lock_init(io) \
179 mutex_init(&(io)->io_lock_data, ETAP_IPC_OBJECT)
180 #define io_lock(io) \
181 mutex_lock(&(io)->io_lock_data)
182 #define io_lock_try(io) \
183 mutex_try(&(io)->io_lock_data)
184 #define io_unlock(io) \
185 mutex_unlock(&(io)->io_lock_data)
186
187 #endif /* NCPUS == 1 */
188
189 #if NCPUS > 1
190 #define _VOLATILE_ volatile
191 #else /* NCPUS > 1 */
192 #define _VOLATILE_
193 #endif /* NCPUS > 1 */
194
195 #define io_check_unlock(io) \
196 MACRO_BEGIN \
197 _VOLATILE_ ipc_object_refs_t _refs = (io)->io_references; \
198 \
199 io_unlock(io); \
200 if (_refs == 0) \
201 io_free(io_otype(io), io); \
202 MACRO_END
203
204 /* Sanity check the ref count. If it is 0, we may be doubly zfreeing.
205 * If it is larger than max int, it has been corrupted, probably by being
206 * modified into an address (this is architecture dependent, but it's
207 * safe to assume there cannot really be max int references).
208 *
209 * NOTE: The 0 test alone will not catch double zfreeing of ipc_port
210 * structs, because the io_references field is the first word of the struct,
211 * and zfree modifies that to point to the next free zone element.
212 */
213 #define IO_MAX_REFERENCES \
214 (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1)))
215
216 #define io_reference(io) \
217 MACRO_BEGIN \
218 assert((io)->io_references < IO_MAX_REFERENCES); \
219 (io)->io_references++; \
220 MACRO_END
221
222 #define io_release(io) \
223 MACRO_BEGIN \
224 assert((io)->io_references > 0 && \
225 (io)->io_references <= IO_MAX_REFERENCES); \
226 (io)->io_references--; \
227 MACRO_END
228
229 /*
230 * Exported interfaces
231 */
232
233 /* Take a reference to an object */
234 extern void ipc_object_reference(
235 ipc_object_t object);
236
237 /* Release a reference to an object */
238 extern void ipc_object_release(
239 ipc_object_t object);
240
241 /* Look up an object in a space */
242 extern kern_return_t ipc_object_translate(
243 ipc_space_t space,
244 mach_port_name_t name,
245 mach_port_right_t right,
246 ipc_object_t *objectp);
247
248 /* Look up two objects in a space, locking them in the order described */
249 extern kern_return_t ipc_object_translate_two(
250 ipc_space_t space,
251 mach_port_name_t name1,
252 mach_port_right_t right1,
253 ipc_object_t *objectp1,
254 mach_port_name_t name2,
255 mach_port_right_t right2,
256 ipc_object_t *objectp2);
257
258 /* Allocate a dead-name entry */
259 extern kern_return_t
260 ipc_object_alloc_dead(
261 ipc_space_t space,
262 mach_port_name_t *namep);
263
264 /* Allocate a dead-name entry, with a specific name */
265 extern kern_return_t ipc_object_alloc_dead_name(
266 ipc_space_t space,
267 mach_port_name_t name);
268
269 /* Allocate an object */
270 extern kern_return_t ipc_object_alloc(
271 ipc_space_t space,
272 ipc_object_type_t otype,
273 mach_port_type_t type,
274 mach_port_urefs_t urefs,
275 mach_port_name_t *namep,
276 ipc_object_t *objectp);
277
278 /* Allocate an object, with a specific name */
279 extern kern_return_t ipc_object_alloc_name(
280 ipc_space_t space,
281 ipc_object_type_t otype,
282 mach_port_type_t type,
283 mach_port_urefs_t urefs,
284 mach_port_name_t name,
285 ipc_object_t *objectp);
286
287 /* Convert a send type name to a received type name */
288 extern mach_msg_type_name_t ipc_object_copyin_type(
289 mach_msg_type_name_t msgt_name);
290
291 /* Copyin a capability from a space */
292 extern kern_return_t ipc_object_copyin(
293 ipc_space_t space,
294 mach_port_name_t name,
295 mach_msg_type_name_t msgt_name,
296 ipc_object_t *objectp);
297
298 /* Copyin a naked capability from the kernel */
299 extern void ipc_object_copyin_from_kernel(
300 ipc_object_t object,
301 mach_msg_type_name_t msgt_name);
302
303 /* Destroy a naked capability */
304 extern void ipc_object_destroy(
305 ipc_object_t object,
306 mach_msg_type_name_t msgt_name);
307
308 /* Copyout a capability, placing it into a space */
309 extern kern_return_t ipc_object_copyout(
310 ipc_space_t space,
311 ipc_object_t object,
312 mach_msg_type_name_t msgt_name,
313 boolean_t overflow,
314 mach_port_name_t *namep);
315
316 /* Copyout a capability with a name, placing it into a space */
317 extern kern_return_t ipc_object_copyout_name(
318 ipc_space_t space,
319 ipc_object_t object,
320 mach_msg_type_name_t msgt_name,
321 boolean_t overflow,
322 mach_port_name_t name);
323
324 /* Translate/consume the destination right of a message */
325 extern void ipc_object_copyout_dest(
326 ipc_space_t space,
327 ipc_object_t object,
328 mach_msg_type_name_t msgt_name,
329 mach_port_name_t *namep);
330
331 /* Rename an entry in a space */
332 extern kern_return_t ipc_object_rename(
333 ipc_space_t space,
334 mach_port_name_t oname,
335 mach_port_name_t nname);
336
337 #if MACH_KDB
338 /* Pretty-print an ipc object */
339
340 extern void ipc_object_print(
341 ipc_object_t object);
342
343 #endif /* MACH_KDB */
344
345 #endif /* _IPC_IPC_OBJECT_H_ */