2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
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.
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.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 * File: ipc/ipc_object.h
58 * Definitions for IPC objects, for which tasks have capabilities.
61 #ifndef _IPC_IPC_OBJECT_H_
62 #define _IPC_IPC_OBJECT_H_
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>
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
;
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.
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).
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
)
99 * Legacy defines. Should use IPC_OBJECT_NULL, etc...
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))
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.
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? */
119 #define io_active(io) ((io)->io_bits & IO_BITS_ACTIVE)
121 #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16)
122 #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE)
124 #define io_makebits(active, otype, kotype) \
125 (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype))
128 * Object types: ports, port sets, kernel-loaded ports
131 #define IOT_PORT_SET 1
132 #define IOT_NUMBER 2 /* number of types used */
134 extern zone_t ipc_object_zones
[IOT_NUMBER
];
136 #define io_alloc(otype) \
137 ((ipc_object_t) zalloc(ipc_object_zones[(otype)]))
141 * Call the routine for io_free so that checking can be performed.
145 ipc_object_t object
);
147 #else /* MACH_ASSERT */
148 #define io_free(otype, io) \
149 zfree(ipc_object_zones[(otype)], (io))
150 #endif /* MACH_ASSERT */
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).
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)
167 #define _VOLATILE_ volatile
169 #define io_check_unlock(io) \
171 _VOLATILE_ ipc_object_refs_t _refs = (io)->io_references; \
175 io_free(io_otype(io), io); \
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).
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.
187 #define IO_MAX_REFERENCES \
188 (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1)))
190 #define io_reference(io) \
192 assert((io)->io_references < IO_MAX_REFERENCES); \
193 (io)->io_references++; \
196 #define io_release(io) \
198 assert((io)->io_references > 0 && \
199 (io)->io_references <= IO_MAX_REFERENCES); \
200 (io)->io_references--; \
204 * Exported interfaces
207 /* Take a reference to an object */
208 extern void ipc_object_reference(
209 ipc_object_t object
);
211 /* Release a reference to an object */
212 extern void ipc_object_release(
213 ipc_object_t object
);
215 /* Look up an object in a space */
216 extern kern_return_t
ipc_object_translate(
218 mach_port_name_t name
,
219 mach_port_right_t right
,
220 ipc_object_t
*objectp
);
222 /* Look up two objects in a space, locking them in the order described */
223 extern kern_return_t
ipc_object_translate_two(
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
);
232 /* Allocate a dead-name entry */
234 ipc_object_alloc_dead(
236 mach_port_name_t
*namep
);
238 /* Allocate a dead-name entry, with a specific name */
239 extern kern_return_t
ipc_object_alloc_dead_name(
241 mach_port_name_t name
);
243 /* Allocate an object */
244 extern kern_return_t
ipc_object_alloc(
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
);
252 /* Allocate an object, with a specific name */
253 extern kern_return_t
ipc_object_alloc_name(
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
);
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
);
265 /* Copyin a capability from a space */
266 extern kern_return_t
ipc_object_copyin(
268 mach_port_name_t name
,
269 mach_msg_type_name_t msgt_name
,
270 ipc_object_t
*objectp
);
272 /* Copyin a naked capability from the kernel */
273 extern void ipc_object_copyin_from_kernel(
275 mach_msg_type_name_t msgt_name
);
277 /* Destroy a naked capability */
278 extern void ipc_object_destroy(
280 mach_msg_type_name_t msgt_name
);
282 /* Copyout a capability, placing it into a space */
283 extern kern_return_t
ipc_object_copyout(
286 mach_msg_type_name_t msgt_name
,
288 mach_port_name_t
*namep
);
290 /* Copyout a capability with a name, placing it into a space */
291 extern kern_return_t
ipc_object_copyout_name(
294 mach_msg_type_name_t msgt_name
,
296 mach_port_name_t name
);
298 /* Translate/consume the destination right of a message */
299 extern void ipc_object_copyout_dest(
302 mach_msg_type_name_t msgt_name
,
303 mach_port_name_t
*namep
);
305 /* Rename an entry in a space */
306 extern kern_return_t
ipc_object_rename(
308 mach_port_name_t oname
,
309 mach_port_name_t nname
);
312 /* Pretty-print an ipc object */
314 extern void ipc_object_print(
315 ipc_object_t object
);
317 #endif /* MACH_KDB */
319 #endif /* _IPC_IPC_OBJECT_H_ */