2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
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.
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.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * File: ipc/ipc_object.h
60 * Definitions for IPC objects, for which tasks have capabilities.
63 #ifndef _IPC_IPC_OBJECT_H_
64 #define _IPC_IPC_OBJECT_H_
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>
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
;
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.
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).
95 ipc_object_refs_t io_references
;
96 ipc_object_bits_t io_bits
;
97 port_name_t io_receiver_name
;
99 usimple_lock_data_t io_lock_data
;
101 decl_mutex_data(, io_lock_data
)
106 * Legacy defines. Should use IPC_OBJECT_NULL, etc...
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))
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.
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? */
126 #define io_active(io) ((io)->io_bits & IO_BITS_ACTIVE)
128 #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16)
129 #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE)
131 #define io_makebits(active, otype, kotype) \
132 (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype))
135 * Object types: ports, port sets, kernel-loaded ports
138 #define IOT_PORT_SET 1
139 #define IOT_NUMBER 2 /* number of types used */
141 extern zone_t ipc_object_zones
[IOT_NUMBER
];
143 #define io_alloc(otype) \
144 ((ipc_object_t) zalloc(ipc_object_zones[(otype)]))
148 * Call the routine for io_free so that checking can be performed.
152 ipc_object_t object
);
154 #else /* MACH_ASSERT */
155 #define io_free(otype, io) \
156 zfree(ipc_object_zones[(otype)], (vm_offset_t) (io))
157 #endif /* MACH_ASSERT */
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).
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)
176 #else /* NCPUS == 1 */
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)
187 #endif /* NCPUS == 1 */
190 #define _VOLATILE_ volatile
191 #else /* NCPUS > 1 */
193 #endif /* NCPUS > 1 */
195 #define io_check_unlock(io) \
197 _VOLATILE_ ipc_object_refs_t _refs = (io)->io_references; \
201 io_free(io_otype(io), io); \
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).
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.
213 #define IO_MAX_REFERENCES \
214 (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1)))
216 #define io_reference(io) \
218 assert((io)->io_references < IO_MAX_REFERENCES); \
219 (io)->io_references++; \
222 #define io_release(io) \
224 assert((io)->io_references > 0 && \
225 (io)->io_references <= IO_MAX_REFERENCES); \
226 (io)->io_references--; \
230 * Exported interfaces
233 /* Take a reference to an object */
234 extern void ipc_object_reference(
235 ipc_object_t object
);
237 /* Release a reference to an object */
238 extern void ipc_object_release(
239 ipc_object_t object
);
241 /* Look up an object in a space */
242 extern kern_return_t
ipc_object_translate(
244 mach_port_name_t name
,
245 mach_port_right_t right
,
246 ipc_object_t
*objectp
);
248 /* Look up two objects in a space, locking them in the order described */
249 extern kern_return_t
ipc_object_translate_two(
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
);
258 /* Allocate a dead-name entry */
260 ipc_object_alloc_dead(
262 mach_port_name_t
*namep
);
264 /* Allocate a dead-name entry, with a specific name */
265 extern kern_return_t
ipc_object_alloc_dead_name(
267 mach_port_name_t name
);
269 /* Allocate an object */
270 extern kern_return_t
ipc_object_alloc(
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
);
278 /* Allocate an object, with a specific name */
279 extern kern_return_t
ipc_object_alloc_name(
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
);
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
);
291 /* Copyin a capability from a space */
292 extern kern_return_t
ipc_object_copyin(
294 mach_port_name_t name
,
295 mach_msg_type_name_t msgt_name
,
296 ipc_object_t
*objectp
);
298 /* Copyin a naked capability from the kernel */
299 extern void ipc_object_copyin_from_kernel(
301 mach_msg_type_name_t msgt_name
);
303 /* Destroy a naked capability */
304 extern void ipc_object_destroy(
306 mach_msg_type_name_t msgt_name
);
308 /* Copyout a capability, placing it into a space */
309 extern kern_return_t
ipc_object_copyout(
312 mach_msg_type_name_t msgt_name
,
314 mach_port_name_t
*namep
);
316 /* Copyout a capability with a name, placing it into a space */
317 extern kern_return_t
ipc_object_copyout_name(
320 mach_msg_type_name_t msgt_name
,
322 mach_port_name_t name
);
324 /* Translate/consume the destination right of a message */
325 extern void ipc_object_copyout_dest(
328 mach_msg_type_name_t msgt_name
,
329 mach_port_name_t
*namep
);
331 /* Rename an entry in a space */
332 extern kern_return_t
ipc_object_rename(
334 mach_port_name_t oname
,
335 mach_port_name_t nname
);
338 /* Pretty-print an ipc object */
340 extern void ipc_object_print(
341 ipc_object_t object
);
343 #endif /* MACH_KDB */
345 #endif /* _IPC_IPC_OBJECT_H_ */